#------------------------------------------------------------------------------ # TODO: # - Fix HTML parsing, no regexen! See wpEditToken+wpEdittime # - EditToken-stuff in its own method? module MediaWikiBot require 'cgi' require 'erb' require 'http-access2' class WikiBot include ERB::Util # for url_encode() def initialize(wiki) @wiki = wiki @client = HTTPAccess2::Client.new() @client.set_cookie_store("cookie.dat") end #------------------------------------------------------------------------------ # FIXME: This should really be in HTTPAccess2::Client def post_form (url, post_vars) body = "" post_vars.keys.each do |var| body += "&" unless body == "" body += url_encode(var) + "=" + url_encode(post_vars[var]) end result = @client.post(url, body, [[ "Content-Type", "application/x-www-form-urlencoded" ]]) return result.content end #------------------------------------------------------------------------------ # HTTP authentication def set_basic_auth(user_id, passwd) @client.set_basic_auth(@wiki, user_id, passwd) end #------------------------------------------------------------------------------ # MediaWiki stuff: Could be useful for other wiki bots def url_no_redirect (title) return @wiki + "index.php?title=" + url_encode(title) + "&redirect=no" end def url_page (title) return @wiki + "index.php?title=" + url_encode(title) end def url_raw (title) return @wiki + "index.php?title=" + url_encode(title) + "&action=raw" end def url_what_links_here (title) return @wiki \ + "index.php?title=Special:Whatlinkshere&target=" + url_encode(title) end def url_delete (title) return @wiki + "index.php?title=" + url_encode(title) + "&action=delete" end def url_submitlogin return @wiki + "index.php?title=Special:Userlogin&action=submitlogin" end def url_protect(title) return @wiki + "index.php?title=" + url_encode(title) + "&action=protect" end def login(wiki_name, wiki_password) post_form(url_submitlogin(), { "wpName" => wiki_name, "wpPassword" => wiki_password, "wpLoginattempt" => "" }) end def is_redirect? (title) return get_raw(title) =~ /^#REDIRECT[: ]/ end def is_not_linked? (title) return get_what_links_here(title).size() == 0 end def get_raw (title) return @client.get_content(url_raw(title)) end def get_allpages # FIXME: There seems to be no MediaWiki API? Let's scrape it up from # HTML for now. allpages = [] allpages_page = @client.get_content(@wiki + "Special:Allpages") allpages_page.scan(/title="(.*?)"/) do |m| title = CGI.unescapeHTML(m[0]) allpages.push(title) end return allpages end def get_redirect (title) return get_raw(title).scan(/^#REDIRECT[: ]\[\[(.*)\]\]/)[0][0] # first match end def get_what_links_here (title) # FIXME: There seems to be no MediaWiki API? Let's scrape it up from # HTML for now. what_links_here = [] what_links_here_page = @client.get_content(url_what_links_here(title)) what_links_here_page.scan(/