Skip to content

Instantly share code, notes, and snippets.

@xZise
Created February 12, 2014 17:14
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 xZise/8960037 to your computer and use it in GitHub Desktop.
Save xZise/8960037 to your computer and use it in GitHub Desktop.
Adding {{Data template used}}
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pywikibot
import re
from pywikibot import textlib
site = pywikibot.getSite()
data_template = re.compile(".*/(Data|Box|RefFrame|Param)")
yes_all = False
for arg in pywikibot.handleArgs():
if arg in ["--all", "-a"]:
yes_all = True
for page in site.allpages():
match = data_template.search(page.title())
if match:
content = page.text
templates = textlib.extract_templates_and_params(content)
for template in templates:
if template[0] == "Data template used":
print("Already contain template in '{}'. Skipped.".format(page.title()))
break
else:
# for didn't find
page.text = "<noinclude>{{Data template used}}</noinclude>" + content
if not yes_all:
answer = pywikibot.inputChoice("Add template to '{}'?".format(page.title()), ['Yes', 'No', 'All'], ['y', 'n', 'a'], 'n')
if answer == 'a':
yes_all = True
if yes_all or answer != 'n':
page.save(comment="+add 'data template used' template;")
print("Edited page '{}'.".format(page.title()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment