mediawiki.go 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package main
  2. import (
  3. "cgt.name/pkg/go-mwclient"
  4. "cgt.name/pkg/go-mwclient/params"
  5. "errors"
  6. "fmt"
  7. "strings"
  8. )
  9. func clientLogin(w *mwclient.Client, username, password string) error {
  10. token, err := w.GetToken("login")
  11. if err != nil {
  12. return err
  13. }
  14. v := params.Values{
  15. "action": "login",
  16. "lgname": username,
  17. "lgpassword": password,
  18. "lgtoken": token,
  19. }
  20. resp, err := w.Post(v)
  21. if err != nil {
  22. if strings.Index(err.Error(), "see action=clientlogin.") == -1 {
  23. return err
  24. }
  25. }
  26. lgResult, err := resp.GetString("login", "result")
  27. if err != nil {
  28. return fmt.Errorf("invalid API response: unable to assert login result to string")
  29. }
  30. if lgResult != "Success" {
  31. apierr := mwclient.APIError{Code: lgResult}
  32. if reason, err := resp.GetString("login", "reason"); err == nil {
  33. apierr.Info = reason
  34. }
  35. return apierr
  36. }
  37. return nil
  38. }
  39. type WikiClient struct {
  40. apiUrl string
  41. userName string
  42. password string
  43. }
  44. func CreateWikiClient(apiUrl string, userName string, password string) *WikiClient {
  45. return &WikiClient{apiUrl, userName, password}
  46. }
  47. func (wc *WikiClient) GetWikiPageContent(title string) (content string, err error) {
  48. w, err := mwclient.New(wc.apiUrl, "lamperiWikibot")
  49. if err != nil {
  50. return "", err
  51. }
  52. // Log in.
  53. err = clientLogin(w, wc.userName, wc.password)
  54. if err != nil {
  55. return "", err
  56. }
  57. // Specify parameters to send.
  58. parameters := map[string]string{
  59. "action": "query",
  60. "format": "json",
  61. "titles": title,
  62. "prop": "revisions",
  63. "rvprop": "content",
  64. }
  65. // Make the request.
  66. resp, err := w.Get(parameters)
  67. if err != nil {
  68. return "", err
  69. }
  70. pages, err := resp.GetObjectArray("query", "pages")
  71. if err != nil {
  72. return "", err
  73. }
  74. if len(pages) == 0 {
  75. return "", errors.New("Not pages in page JSON")
  76. }
  77. title, err = pages[0].GetString("title")
  78. if err != nil {
  79. return "", err
  80. }
  81. revisions, err := pages[0].GetObjectArray("revisions")
  82. if err != nil {
  83. return "", err
  84. }
  85. content, err = revisions[0].GetString("content")
  86. if err != nil {
  87. return "", err
  88. }
  89. return content, nil
  90. }
  91. func (wc *WikiClient) GetWikiPageSections(title string) ([]struct {
  92. title string
  93. index string
  94. }, error) {
  95. w, err := mwclient.New(wc.apiUrl, "lamperiWikibot")
  96. if err != nil {
  97. return nil, err
  98. }
  99. // Log in.
  100. err = clientLogin(w, wc.userName, wc.password)
  101. if err != nil {
  102. return nil, err
  103. }
  104. // Specify parameters to send.
  105. parameters := map[string]string{
  106. "action": "parse",
  107. "format": "json",
  108. "page": title,
  109. "prop": "sections",
  110. }
  111. // Make the request.
  112. resp, err := w.Get(parameters)
  113. if err != nil {
  114. return nil, err
  115. }
  116. sections, err := resp.GetObjectArray("parse", "sections")
  117. if err != nil {
  118. return nil, err
  119. }
  120. ret := make([]struct {
  121. title string
  122. index string
  123. }, 0, len(sections))
  124. for _, section := range sections {
  125. title, err := section.GetString("line")
  126. if err != nil {
  127. return nil, err
  128. }
  129. index, err := section.GetString("index")
  130. if err != nil {
  131. return nil, err
  132. }
  133. ret = append(ret, struct {
  134. title string
  135. index string
  136. }{title, index})
  137. }
  138. return ret, nil
  139. }
  140. func (wc *WikiClient) GetWikiPageSectionText(title string, sectionIndex string) (string, error) {
  141. w, err := mwclient.New(wc.apiUrl, "lamperiWikibot")
  142. if err != nil {
  143. return "", err
  144. }
  145. // Log in.
  146. err = clientLogin(w, wc.userName, wc.password)
  147. if err != nil {
  148. return "", err
  149. }
  150. // Specify parameters to send.
  151. parameters := map[string]string{
  152. "action": "parse",
  153. "format": "json",
  154. "page": title,
  155. "section": sectionIndex,
  156. "prop": "wikitext",
  157. }
  158. // Make the request.
  159. resp, err := w.Get(parameters)
  160. if err != nil {
  161. return "", err
  162. }
  163. wikiText, err := resp.GetString("parse", "wikitext")
  164. if err != nil {
  165. return "", err
  166. }
  167. return wikiText, nil
  168. }
  169. func (wc *WikiClient) EditWikiPageSection(title string, sectionIndex string, content string, summary string) (bool, error) {
  170. w, err := mwclient.New(wc.apiUrl, "lamperiWikibot")
  171. if err != nil {
  172. return false, err
  173. }
  174. // Log in.
  175. err = clientLogin(w, wc.userName, wc.password)
  176. if err != nil {
  177. return false, err
  178. }
  179. editToken, err := w.GetToken("csrf")
  180. // Specify parameters to send.
  181. parameters := map[string]string{
  182. "action": "edit",
  183. "title": title,
  184. "section": sectionIndex,
  185. "summary": summary,
  186. "text": content,
  187. "nocreate": "true",
  188. "bot": "true",
  189. "token": editToken,
  190. }
  191. // Make the request.
  192. resp, err := w.Post(parameters)
  193. if err != nil {
  194. return false, err
  195. }
  196. result, err := resp.GetString("edit", "result")
  197. if err != nil {
  198. return false, err
  199. }
  200. return result == "Success", nil
  201. }