Skip to content

Instantly share code, notes, and snippets.

@mattflaschen
Forked from legoktm/BLANK_ALL_THE_MESSAGES.py
Last active December 31, 2015 00:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattflaschen/7904894 to your computer and use it in GitHub Desktop.
Save mattflaschen/7904894 to your computer and use it in GitHub Desktop.
Script for blanking message pages, unless neither they nor a message they include exists. Based on https://gist.github.com/legoktm/7781996 by legoktm
#!/usr/bin/python -u
# Public domain
# Based on https://gist.github.com/legoktm/7781996, by legoktm
# Further changes by mattflaschen
import sys, argparse
import wikitools
message_pairs = [ {
'main': 'signupstart',
'dependent': 'loginstart'
}, {
'main': 'signupend',
'dependent': 'loginend'
} ]
def get_wiki(domain_url):
domain_url = domain_url.replace('http://', 'https://')
api_url = domain_url + '/w/api.php'
return wikitools.Wiki(api_url)
def matrix(wiki):
params = {
'action': 'sitematrix',
'format': 'json',
}
req = wikitools.api.APIRequest(wiki, params)
data = req.query()
for val in data['sitematrix']:
# Default to empty. If we find an appropriate key,
# we override this before the inner loop
site_list = []
if val.isdigit():
# It's a grouping. For Wikimedia, this generally means by language
# (es.wikipedia, es.wiktionary, etc.)
group = data['sitematrix'][val]
site_list = group['site']
elif val == 'specials':
site_list = data['sitematrix'][val]
for site in site_list:
# Skip private and closed wikis
if (not 'private' in site) and (not 'closed' in site):
yield get_wiki(site['url'])
def print_edit_error(code):
print 'Edit failed: %s' % code
def do_edits(wiki, username, password, edit_summary):
login_result = wiki.login(username, password)
if login_result is not True:
print 'Error logging in'
for pair in message_pairs:
main_name = 'MediaWiki:' + pair['main']
main_page = wikitools.Page(wiki, main_name)
print '%s exists? - %s' % (main_name, main_page.exists)
dependent_name = 'MediaWiki:' + pair['dependent']
dependent_page = wikitools.Page(wiki, dependent_name)
print '%s exists? - %s' % (dependent_name, dependent_page.exists)
# The login* are included into the signup*, so if either exist, the signup may not be blank
if main_page.exists or dependent_page.exists:
print 'Attempting to edit %s/wiki/%s' % (wiki.domain, main_name)
try:
edit_result = main_page.edit(text='', summary=edit_summary)
if edit_result['edit']['result'] == 'Success':
print 'Edit succeeded'
else:
print_edit_error(edit_result['edit'])
except wikitools.api.APIError as ex:
print_edit_error(ex.args[0])
def main():
parser = argparse.ArgumentParser(description='Blanks signup messages on multiple wikis')
parser.add_argument('--api_url', required=True, help='URL of API on one of the wikis in the cluster, such as http://example.org/w/api.php')
parser.add_argument('--username', required=True, help='Username to edit with')
parser.add_argument('--password', required=True, help='Password of user to edit with')
parser.add_argument('--edit_summary', required=True, help='Edit summary')
namespace = parser.parse_args()
initial_wiki = wikitools.Wiki(namespace.api_url)
for wiki in matrix(initial_wiki):
print 'Querying %s' % wiki.domain
do_edits(wiki, namespace.username, namespace.password, namespace.edit_summary)
print ''
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment