mediawiki-bot.scm
(module mediawiki-bot mzscheme
        (require "bot.scm")
        (require "mediawiki-xml.scm")
        (require (lib "string.ss" "srfi" "13"))
        (require (planet "xml.scm" ("oesterholt" "ho-utils.plt" 1 0)))
        (provide mediawiki-bot
                 (all-from "bot.scm"))
        
        (def-class
         (this (mediawiki-bot _base-url))
         (supers (form-data) (mediawiki-xml))
         (private
          (define _user #f)
          (define _pass #f)
          
          
          (define (get-attr key attrs)
            (let ((k (assq key attrs)))
              (if (eq? k #f)
                  #f
                  (cadr k))))
            
          )
         (public
          
          ((define (sp "logs in on the mediawiki with username 'user' and password 'pass'."
                       " It can still be necessary to authenticate to the server itself.")
                   (sp "returns #t, if logged in, #f otherwise."))
           (login user pass)
              (begin
                (-> this form-clear)
                (-> this form-add 'wpName         user)
                (-> this form-add 'wpPassword     pass)
                (-> this form-add 'wpLoginattempt "Aanmelden & Inschrijven")

                (-> this form-add 'title  "Special:Userlogin")
                (-> this form-add 'action "submitlogin")
                (-> this form-add 'type   "login")
		 
                (let ((p (-> this form-post _base-url)))
                  (if (port? p)
                      (close-input-port p))
                  (if (not (eq? (-> this cookie-match 'UserID) #f))
                      #t
                      #f))))
          
          ((define (sp "Reads a page, and returns it's text contents, or #f, if the page does not exist."))
           (page pagename)
             (begin
               (let ((p (-> this get (string-append _base-url "?title=Special:Export" "/" pagename)))
                     (text #f))
                 (if (port? p)
                     (let ((xexpr (read-xexpr p)))
                       (xexpr-sax (lambda (element attributes value level inode xexprs)
                                    (if (eq? element 'text)
                                        (set! text value))
                                    #t)
                                  xexpr))
                     (set! text p))
                 text)))
          
          ((define (sp "Puts a page."))
           (page! pagename text)
           (begin
             (set! text (string-trim-both text))
             (let ((wpEditToken #f)
                   (wpAutoSummary #f)
                   (wpStarttime #f)
                   (wpEdittime #f))
               (let ((p (-> this get (string-append _base-url "?title=" pagename "&action=edit"))))
                 (let ((xexpr (read-xexpr p)))
                   (xexpr-sax (lambda (element attributes value level inode xexprs)
                                (if (eq? element 'input)
                                    (let ((name (let ((v (get-attr 'name attributes)))
                                                  (if (string? v)
                                                      (string->symbol v)
                                                      v))))
                                      (cond
                                       ((eq? name 'wpEditToken)   (set! wpEditToken   (get-attr 'value attributes)))
                                       ((eq? name 'wpAutoSummary) (set! wpAutoSummary (get-attr 'value attributes)))
                                       ((eq? name 'wpStarttime)   (set! wpStarttime   (get-attr 'value attributes)))
                                       ((eq? name 'wpEdittime)    (set! wpEdittime    (get-attr 'value attributes))))))
                                #t)
                              xexpr)))
               ;;; Submit page
               (-> this form-clear)
               (-> this form-add 'title         pagename)
               (-> this form-add 'action        "submit")
               (-> this form-add 'wpSave        "Pagina opslaan")
               (-> this form-add 'wpEditToken   wpEditToken)
               (-> this form-add 'wpStarttime   wpStarttime)
               (-> this form-add 'wpEdittime    wpStarttime)
               (-> this form-add 'wpTextbox1    text)
               (let ((p (-> this form-post _base-url)))
                 (if (port? p)
                     (close-input-port p)))

               ;;; Check submit
               (let ((check (-> this page pagename)))
                 (display (format "~s~%" check))
                 (display (format "~s~%" text))
                 (if (equal? check text)
                     #t
                     #f)))))

          )
         (constructor)
         )
        
        
        
        ) ;;; module