This is a request for a protect page feature. On
en.wikipedia, several pages are being protected if they
are on the Main Page. It would be nice to have bot
that can assist in the manual protection and
unprotection of pages.
#!/usr/local/bin/python
#
#
# PROTECT.PY -- PROTECT AND UNPROTECT PAGES
# Gdr, 2005-05-12
#
#
# INTRODUCTION
#
# This module implements automatic protection and unprotection of
# wikipedia pages. It is designed to be used with the Python Wikipedia
# Robot Framework (http://sourceforge.net/projects/pywikipediabot/).
#
#
# USAGE
#
# You must use login.py to log in as a user with administrator
# privileges in order for this module to be effective. Then you can use
# it like this:
#
# import protect
# protect.protectPage('en', 'Page name', 'reason for protecting')
# protect.unprotectPage('en', 'Page name', 'reason for unprotecting')
#
#
# LICENCES
#
# This software can be used and reproduced under the terms and
# conditions of the Python Software Foundation license under which
# recent copies of the python-2.3 interpreter can be used.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
import re
import wikipedia
def applyAction(code, name, action, predata):
"""Apply 'action' to page 'name' on the 'code' language wikipedia."""
import httplib
# Check whether we are not too quickly after the previous putPage,
and
# wait a bit until the interval is acceptable
wikipedia.put_throttle()
# Which web-site host are we submitting to?
host = wikipedia.family.hostname(code)
# Get the address of the page on that host.
address = '/w/wiki.phtml?title=%s&action=%s'%(name,action)
# Get the page and scan it for the edittoken.
text, charset = wikipedia.getUrl(host, address)
m = re.search('value=[\'"]([0-9a-z]+)[\'"]
name=[\'"]wpEditToken[\'"]',text)
if m:
token = m.group(1)
else:
m = re.search('name=[\'"]wpEditToken[\'"] value=[\'"]([0-9a-
z]+)[\'"]',text)
if m:
token = m.group(1)
else:
token = "0"
print "token = ", token
predata.append(('wpEditToken', token))
data = wikipedia.urlencode(tuple(predata))
wikipedia.output(wikipedia.url2unicode("Apply %s to page
%s:%s"%(action,code,name), language = code))
# Submit the prepared information
conn = httplib.HTTPConnection(host)
conn.putrequest("POST", address)
conn.putheader('Content-Length', str(len(data)))
conn.putheader("Content-type", "application/x-www-form-urlencoded")
conn.putheader("User-agent", "RobHooftWikiRobot/1.0")
if wikipedia.cookies and code == wikipedia.mylang:
conn.putheader('Cookie',wikipedia.cookies)
conn.endheaders()
conn.send(data)
# Prepare the return values
response = conn.getresponse()
data = response.read()
conn.close()
if data != '':
wikipedia.output(data, decoder = wikipedia.myencoding())
return response.status, response.reason, data
def protectPage(code, name, reason = None, moveonly = False):
"""Protect page 'name' on the 'code' language wikipedia."""
wpmoveonly = "0"
if moveonly:
wpmoveonly = "1"
applyAction(code, name, 'protect',
[('wpReasonProtect', reason),
('wpConfirmProtect', '1'),
('wpMoveOnly', wpmoveonly),
('wpConfirmProtectB', 'Confirm')])
def unprotectPage(code, name, reason = None, moveonly = False):
"""Unprotect page 'name' on the 'code' language wikipedia."""
applyAction(code, name, 'unprotect',
[('wpReasonProtect', reason),
('wpConfirmProtect', '1'),
('wpConfirmProtectB', 'Confirm')])
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This patch is wildly outdated, apparently it predates the family transition. Since the functionality is useful, it might be a good idea to update the patch. I have added a generic action address to the Family class as a start.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-01-13
status: open --> closed
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-01-13
Logged In: YES
user_id=1136737
Originator: NO
create in wikipedia.py 1.782
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Logged In: YES
user_id=880694
The protect() function would also be very useful for the
wikipedia:de DVD project which will start in about 1 week.
Daniel
Logged In: YES
user_id=1010260
#!/usr/local/bin/python
#
#
# PROTECT.PY -- PROTECT AND UNPROTECT PAGES
# Gdr, 2005-05-12
#
#
# INTRODUCTION
#
# This module implements automatic protection and unprotection of
# wikipedia pages. It is designed to be used with the Python Wikipedia
# Robot Framework (http://sourceforge.net/projects/pywikipediabot/).
#
#
# USAGE
#
# You must use login.py to log in as a user with administrator
# privileges in order for this module to be effective. Then you can use
# it like this:
#
# import protect
# protect.protectPage('en', 'Page name', 'reason for protecting')
# protect.unprotectPage('en', 'Page name', 'reason for unprotecting')
#
#
# LICENCES
#
# This software can be used and reproduced under the terms and
# conditions of the Python Software Foundation license under which
# recent copies of the python-2.3 interpreter can be used.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
import re
import wikipedia
def applyAction(code, name, action, predata):
"""Apply 'action' to page 'name' on the 'code' language wikipedia."""
import httplib
# Check whether we are not too quickly after the previous putPage,
and
# wait a bit until the interval is acceptable
wikipedia.put_throttle()
# Which web-site host are we submitting to?
host = wikipedia.family.hostname(code)
# Get the address of the page on that host.
address = '/w/wiki.phtml?title=%s&action=%s'%(name,action)
# Get the page and scan it for the edittoken.
text, charset = wikipedia.getUrl(host, address)
m = re.search('value=[\'"]([0-9a-z]+)[\'"]
name=[\'"]wpEditToken[\'"]',text)
if m:
token = m.group(1)
else:
m = re.search('name=[\'"]wpEditToken[\'"] value=[\'"]([0-9a-
z]+)[\'"]',text)
if m:
token = m.group(1)
else:
token = "0"
print "token = ", token
predata.append(('wpEditToken', token))
data = wikipedia.urlencode(tuple(predata))
wikipedia.output(wikipedia.url2unicode("Apply %s to page
%s:%s"%(action,code,name), language = code))
# Submit the prepared information
conn = httplib.HTTPConnection(host)
conn.putrequest("POST", address)
conn.putheader('Content-Length', str(len(data)))
conn.putheader("Content-type", "application/x-www-form-urlencoded")
conn.putheader("User-agent", "RobHooftWikiRobot/1.0")
if wikipedia.cookies and code == wikipedia.mylang:
conn.putheader('Cookie',wikipedia.cookies)
conn.endheaders()
conn.send(data)
# Prepare the return values
response = conn.getresponse()
data = response.read()
conn.close()
if data != '':
wikipedia.output(data, decoder = wikipedia.myencoding())
return response.status, response.reason, data
def protectPage(code, name, reason = None, moveonly = False):
"""Protect page 'name' on the 'code' language wikipedia."""
wpmoveonly = "0"
if moveonly:
wpmoveonly = "1"
applyAction(code, name, 'protect',
[('wpReasonProtect', reason),
('wpConfirmProtect', '1'),
('wpMoveOnly', wpmoveonly),
('wpConfirmProtectB', 'Confirm')])
def unprotectPage(code, name, reason = None, moveonly = False):
"""Unprotect page 'name' on the 'code' language wikipedia."""
applyAction(code, name, 'unprotect',
[('wpReasonProtect', reason),
('wpConfirmProtect', '1'),
('wpConfirmProtectB', 'Confirm')])
Logged In: YES
user_id=47476
Originator: NO
This patch is wildly outdated, apparently it predates the family transition. Since the functionality is useful, it might be a good idea to update the patch. I have added a generic action address to the Family class as a start.
Logged In: YES
user_id=1136737
Originator: NO
create in wikipedia.py 1.782