Skip to content

Instantly share code, notes, and snippets.

@Ladsgroup
Created February 8, 2021 08:27
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 Ladsgroup/b437858f4b5480fa58e89d8ec0d1d0db to your computer and use it in GitHub Desktop.
Save Ladsgroup/b437858f4b5480fa58e89d8ec0d1d0db to your computer and use it in GitHub Desktop.
import pywikibot
import sys
import re
from pywikibot import editor
import requests
variableName = 'wgGlobalGroups'
def treat_page(page):
print('Checking', page)
try:
old_text = page.get()
except KeyboardInterrupt:
sys.exit()
except BaseException:
return
if re.search(r'var '+variableName+r'[^a-zA-Z0-9]', old_text):
return
new_text = old_text
context = 0
cleaned_text = re.sub(r'\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$', r'\1', old_text, re.M)
cleaned_text = re.sub(r'^\s+|\s+$', '', cleaned_text)
cleaned_text = re.sub(r'(\s+|^)//.+', '', cleaned_text)
if not re.search(r'([ (=+,])' + variableName, cleaned_text):
return
new_text = re.sub(
r'([ (=+,])' + variableName+r'([^a-zA-Z0-9])',
r"\1mw.config.get('" + variableName + r"')\2",
new_text)
if new_text == old_text:
return
while True:
print(page)
pywikibot.showDiff(old_text, new_text, context=context)
choice = pywikibot.input_choice(
'Do you want to accept these changes?',
[('Yes', 'y'), ('No', 'n'), ('Edit original', 'e'),
('edit Latest', 'l'), ('open in Browser', 'b'),
('More context', 'm')],
default='N')
if choice == 'm':
context = context * 3 if context else 3
continue
if choice in ('e', 'l'):
text_editor = editor.TextEditor()
edit_text = old_text if choice == 'e' else new_text
as_edited = text_editor.edit(edit_text)
# if user didn't press Cancel
if as_edited and as_edited != new_text:
new_text = as_edited
if choice == 'l':
# prevent changes from being applied again
last_text = new_text
continue
if choice == 'b':
pywikibot.bot.open_webbrowser(page)
try:
old_text = page.get(get_redirect=True, force=True)
except pywikibot.NoPage:
pywikibot.output('Page {0} has been deleted.'
.format(page.title()))
break
new_text = old_text
last_text = None
continue
if choice == 'y':
try:
page.put(
new_text,
summary='Maintenance: Replacing legacy global variable ([[phab:T72470]])')
except Exception as exception:
print('Error in saving', exception)
pass
break
# choice must be 'N'
break
def treat_site(site):
#namespaces=[2,4,8]
namespaces=[8]
try:
gen = site.search(
'insource:/[ (=+,]' + variableName + '/ intitle:.js',
namespaces=namespaces)
except KeyboardInterrupt:
sys.exit()
except BaseException:
return
for page in gen:
treat_page(page)
def main():
families = ['meta', 'commons', 'wikidata', 'wikipedia', 'wiktionary', 'wikiquote', 'wikibooks', 'wikinews', 'wikiversity', 'wikivoyage', 'wikisource']
special = {
'meta': 'meta',
'commons': 'commons',
'wikidata': 'wikidata'
}
for family in families:
site = pywikibot.Site(special.get(family, 'en'), family)
for lang in site.family.langs:
if lang == 'beta':
continue
treat_site(pywikibot.Site(lang, family))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment