Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Fix #14 for getting edit token #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions mediawiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ type outerEdit struct {
}
}

type tokenResponse struct {
Query struct {
Tokens struct {
Csrftoken string
}
}
}

// Response is a struct used for unmarshaling the MediaWiki JSON response.
type Response struct {
Query struct {
Expand Down Expand Up @@ -399,26 +407,26 @@ func (m *MWApi) Login(username, password string) error {
// calls to API().
func (m *MWApi) GetEditToken() error {
query := map[string]string{
"action": "query",
"prop": "info|revisions",
"intoken": "edit",
"titles": "Main Page",
"action": "query",
"meta": "tokens",
"formatversion": "2",
"type": "csrf",
}

body, err := m.API(query)
if err != nil {
return err
}
var response Response
var response tokenResponse
err = json.Unmarshal(body, &response)
if err != nil {
return err
}
pl := response.PageSlice()
if len(pl) < 1 {
pl := response.Query.Tokens.Csrftoken
if pl == "" {
return errors.New("no pages returned for edittoken query")
}
m.edittoken = pl[0].Edittoken
m.edittoken = pl
return nil
}

Expand All @@ -434,12 +442,12 @@ func (m *MWApi) Logout() {
//
// Example:
//
// editConfig := map[string]string{
// "title": "SOME PAGE",
// "summary": "THIS IS WHAT SHOWS UP IN THE LOG",
// "text": "THE ENTIRE TEXT OF THE PAGE",
// }
// err = client.Edit(editConfig)
// editConfig := map[string]string{
// "title": "SOME PAGE",
// "summary": "THIS IS WHAT SHOWS UP IN THE LOG",
// "text": "THE ENTIRE TEXT OF THE PAGE",
// }
// err = client.Edit(editConfig)
func (m *MWApi) Edit(values map[string]string) error {
if m.edittoken == "" {
err := m.GetEditToken()
Expand Down