neingeist
/
neinomaten
Archived
1
0
Fork 0

ability to protect pages

very elegant workarounds
brilliant thoughts
master
neingeist 19 years ago
parent 50e54db5b7
commit e80f95d0d7

@ -1,4 +1,7 @@
#!/usr/bin/env ruby
#------------------------------------------------------------------------------
# TODO:
# - Fix HTML parsing, no regexen! See wpEditToken+wpEdittime
# - EditToken-stuff in its own method?
module MediaWikiBot
@ -12,6 +15,9 @@ class WikiBot
def initialize(wiki)
@wiki = wiki
@client = HTTPAccess2::Client.new()
@client.set_cookie_store("cookie.dat")
end
#------------------------------------------------------------------------------
@ -30,6 +36,13 @@ class WikiBot
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
@ -58,9 +71,11 @@ class WikiBot
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)
@client = HTTPAccess2::Client.new()
@client.set_cookie_store("cookie.dat")
post_form(url_submitlogin(),
{ "wpName" => wiki_name,
@ -119,7 +134,6 @@ class WikiBot
token_page = post_form(url_delete(title),
{ "wpReason" => reason,
"wpConfirm" => "1",})
# FIXME: Uahh.
token = token_page.scan(/name='wpEditToken' value="(.*?)"/)[0][0]
post_form(url_delete(title),
{ "wpReason" => reason,
@ -154,12 +168,17 @@ class WikiBot
$stderr.print("Submitting '", title, "'.\n")
token_page = @client.get_content(url_edit(title))
# FIXME: Uahh.
while ! token_page.match(/value="(.*?)" name="wpEdittime" /)
# FIXME: This workaround loop really fucking sucks.
token_page = @client.get_content(url_edit(title))
end
time = token_page.scan(/value="(.*?)" name="wpEdittime" /)[0][0]
# No token for MediaWiki 1.3.x
if token_page =~ /wpEditToken/
token = token_page.scan(/value="(.*?)" name="wpEditToken" /)[0][0]
end
post_form(url_edit_submit(title),
{ "wpTextbox1" => body,
"wpSummary" => summary,
@ -226,6 +245,24 @@ class WikiBot
get_categories(title).member?(category)
end
def is_protected?(title)
return @client.get_content(url_page(title)) =~ /action=unprotect/
end
def protect(title, reason)
token_page = @client.get_content(url_protect(title))
# No token for MediaWiki 1.3.x
if token_page =~ /wpEditToken/
token = token_page.scan(/name='wpEditToken' value="(.*?)"/)[0][0]
end
post_form(url_protect(title),
{ "wpReasonProtect" => reason,
"wpConfirmProtectB" => "confirm",
"wpEditToken" => token })
end
end # class WikiBot
end # module MediaWikiBot