commit d9736095c629ac056066b0464e6f87e8727d743f Author: neingeist Date: Fri Sep 30 02:46:35 2005 +0000 neinomaten-farm diff --git a/mediawikibot.rb b/mediawikibot.rb new file mode 100755 index 0000000..ed9597a --- /dev/null +++ b/mediawikibot.rb @@ -0,0 +1,183 @@ +#!/usr/bin/env ruby + +module MediaWikiBot + +require 'http-access2' +require 'erb' +require 'parsedate' +include ParseDate + +class WikiBot + + include ERB::Util # for url_encode() + + def initialize(wiki) + @wiki = wiki + 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 + + #------------------------------------------------------------------------------ + # 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_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 login(wiki_name, wiki_password) + @client = HTTPAccess2::Client.new() + @client.set_cookie_store("cookie.dat") + + post_form(url_submitlogin(), + { "wpName" => wiki_name, + "wpPassword" => wiki_password }) + 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 = 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(/
  • reason, + "wpConfirm" => "1",}) + # FIXME: Uahh. + token = token_page.scan(/name='wpEditToken' value="(.*?)"/)[0][0] + post_form(url_delete(title), + { "wpReason" => reason, + "wpConfirm" => "1", + "wpEditToken" => token }) + end + + def replace (replace_where, replace_what, replace_with, reason) + # " " could be "_" + # FIXME: Shouldn't be done here + replace_what = replace_what.gsub(/ /, "[ _]") + + $stderr.print("Replacing /", replace_what, "/ with '", + replace_with, "' in '", replace_where, "'.\n") + + replace_what_re = Regexp.new(replace_what) + before = get_raw(replace_where) + after = before.gsub(replace_what_re, replace_with) + + edit(replace_where, after, reason) + end + + def url_edit(title) + return @wiki + "index.php?title=" + url_encode(title)+ "&action=edit" + end + + def url_edit_submit(title) + return @wiki + "index.php?title=" + url_encode(title)+ "&action=submit" + end + + def edit (title, body, summary) + $stderr.print("Submitting '", title, "'.\n") + + token_page = @client.get_content(url_edit(title)) + # FIXME: Uahh. + time = token_page.scan(/value="(.*?)" name="wpEdittime" /)[0][0] + token = token_page.scan(/value="(.*?)" name="wpEditToken" /)[0][0] + post_form(url_edit_submit(title), + { "wpTextbox1" => body, + "wpSummary" => summary, + "wpEditToken" => token, + "wpEdittime" => time, + "wpSave" => "save" }) + end + + def get_what_uses_template(title) + return get_what_links_here(title) + end + + def parse_template(title, template) + template_re = Regexp.new("\\\{\\\{" + template + "(.*?)\\\}\\\}") + + fields = {} + + fields_string = get_raw(title).scan(template_re)[0][0] + fields_string.split(/\|/).each do |field| + x = field.split(/=/) + key = x[0]; value = x[1] + fields[key] = value + end + + return fields + end +end +end diff --git a/neinomat-radio-chaotica b/neinomat-radio-chaotica new file mode 100755 index 0000000..dd47103 --- /dev/null +++ b/neinomat-radio-chaotica @@ -0,0 +1,23 @@ +#!/usr/bin/env ruby +require 'mediawikibot' + +if ARGV.size() != 2 + $stderr.print($0 + " \n") + exit(1) +end + +b = MediaWikiBot::WikiBot.new("http://www.entropia.de/wiki/") +b.login(ARGV[0], ARGV[1]) + +body = "" +template = "Vorlage:Radio Chaotica-Sendung" +b.get_what_uses_template(template).each do |page| + if erste_sendung = b.parse_template(page, template)["erste_sendung"] + datum = erste_sendung.gsub(/\s*um\s*\d+:\d+$/,"") + body += "* " + datum + " [[" + page + "]]\n" + else + $stderr.print(page, ": erste_sendung nicht gesetzt") + end +end + +b.edit("Benutzer:Neingeist/Radio Chaotica-Liste", body, "Test") diff --git a/neinomat-twiki-killer b/neinomat-twiki-killer new file mode 100755 index 0000000..25db6c5 --- /dev/null +++ b/neinomat-twiki-killer @@ -0,0 +1,38 @@ +#!/usr/bin/env ruby +require 'mediawikibot' + +if ARGV.size() != 2 + $stderr.print($0 + " \n") + exit(1) +end + +#------------------------------------------------------------------------------ +# TWiki stuff: specific things for our dirty wiki + +$twiki_re = /^(Main|Termine|Wir) / + +def is_twiki_link? (title) + return title =~ $twiki_re +end + +#------------------------------------------------------------------------------ +# Rock! + +b = MediaWikiBot::WikiBot.new("http://www.entropia.de/wiki/") +b.login(ARGV[0], ARGV[1]) + +allpages = b.get_allpages() +allpages.each do |title| + $stderr.print("Processing '", title, "'\n") + if is_twiki_link?(title) && b.is_redirect?(title) + if b.is_not_linked?(title) + b.delete(title, + "Old TWiki topic redirect and not linked anywhere in this wiki.") + else + b.get_what_links_here(title).each do |replace_where| + b.replace(replace_where, title, get_redirect(title), + "Old TWiki topic redirect, now fixed.") + end + end + end +end