-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bot for fixing CS1 (currently only removing wikilinks)
- Loading branch information
Showing
1 changed file
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Published by Hazard-SJ (https://meta.wikimedia.org/wiki/User:Hazard-SJ) | ||
# under the terms of Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) | ||
# https://creativecommons.org/licenses/by-sa/3.0/ | ||
|
||
import pywikibot | ||
import mwparserfromhell | ||
from catlib import Category | ||
|
||
class CitationStyleRobot(object): | ||
def __init__(self): | ||
self.site = pywikibot.Site("en", "wikipedia") | ||
self.categories = [ | ||
Category(self.site, "Category:Pages with citations having wikilinks embedded in URL titles"), | ||
] | ||
self.doTaskPage = pywikibot.Page(self.wd, 'User:Hazard-Bot/DoTask/21') | ||
|
||
def checkDoTaskPage(self): | ||
try: | ||
text = self.doTaskPage.get(force = True) | ||
except pywikibot.IsRedirectPage: | ||
raise Exception(u"The 'do-task page' (%s) is a redirect." % self.doTaskPage.title(asLink = True)) | ||
except pywikibot.NoPage: | ||
raise Exception(u"The 'do-task page' (%s) does not exist." % self.doTaskPage.title(asLink = True)) | ||
else: | ||
if text.strip().lower() == u"true": | ||
return True | ||
else: | ||
raise Exception(u"The task has been disabled from the 'do-task page' (%s)." % self.doTaskPage.title(asLink = True)) | ||
|
||
def removeWikilinks(self): | ||
def replaceWikilinks(template, URL, title): | ||
if template.has_param(URL): | ||
if template.has_param(title): | ||
links = template.get(title).value.filter_links() | ||
if links: | ||
for link in links: | ||
if link.text: | ||
self.code.replace(link, link.text) | ||
else: | ||
self.code.replace(link, link.title) | ||
## @fixme: Use a function, and use {{Cite doi}} as well, just in case... | ||
#template = pywikibot.Page(self.site, "Template:Cite web") | ||
#templates = list() | ||
#templates.append(template.title(withNamespace = False).lower()) | ||
#references = template.getReferences(redirectsOnly = True) | ||
#for reference in references: | ||
# templates.append(reference.title(withNamespace = False).lower()) | ||
for template in self.code.filter_templates(): | ||
if template.name.lower().startswith("cite") or template.name.lower().startswith("web"): | ||
replaceWikilinks(template, "url", "title" ) | ||
replaceWikilinks(template, "chapterurl", "chapter" ) | ||
replaceWikilinks(template, "chapterurl", "contribution" ) | ||
replaceWikilinks(template, "chapterurl", "entry" ) | ||
replaceWikilinks(template, "chapterurl", "article" ) | ||
replaceWikilinks(template, "conferenceurl", "conference" ) | ||
replaceWikilinks(template, "contributionurl", "chapter" ) | ||
replaceWikilinks(template, "contributionurl", "contribution" ) | ||
replaceWikilinks(template, "contributionurl", "entry" ) | ||
replaceWikilinks(template, "contributionurl", "article" ) | ||
replaceWikilinks(template, "transcripturl", "transcript" ) | ||
def run(self): | ||
for category in self.categories: | ||
self.checkDoTaskPage() | ||
for self.page in category.articles(): | ||
try: | ||
text = self.page.get() | ||
self.code = mwparserfromhell.parse(text) | ||
self.removeWikilinks() | ||
# and all the others, once they're written, if they're written | ||
if text != self.code: | ||
pywikibot.showDiff(text, unicode(self.code)) | ||
self.page.put(self.code, u"[[Wikipedia:Bots|Robot]]: Fixed [[Help:Citation Style 1|citation style]] errors") | ||
else: | ||
print u"Skipping: No change was made" | ||
except: | ||
print u"Skipping: Error encountered" | ||
|
||
def main(): | ||
bot = CitationStyleRobot() | ||
bot.run() | ||
|
||
if __name__ == "__main__": | ||
try: | ||
main() | ||
finally: | ||
pywikibot.stopme() |