http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10954
Revision: 10954 Author: multichill Date: 2013-01-19 12:57:37 +0000 (Sat, 19 Jan 2013) Log Message: ----------- Implement some logic to update links when a category at Commons gets deleted. If a category is moved and the deleting admin provided a decent deletion summary, the bot will use this to update links. I hope this will fix a lot of broken links to Commons.
Modified Paths: -------------- trunk/pywikipedia/commonscat.py
Modified: trunk/pywikipedia/commonscat.py =================================================================== --- trunk/pywikipedia/commonscat.py 2013-01-19 09:55:14 UTC (rev 10953) +++ trunk/pywikipedia/commonscat.py 2013-01-19 12:57:37 UTC (rev 10954) @@ -501,14 +501,30 @@ if pywikibot.verbose: pywikibot.output("getCommonscat: " + name ) try: + commonsSite = pywikibot.getSite("commons", "commons") #This can throw a pywikibot.BadTitle - commonsPage = pywikibot.Page(pywikibot.getSite("commons", "commons"), - "Category:" + name) + commonsPage = pywikibot.Page(commonsSite, "Category:" + name)
if not commonsPage.exists(): - if pywikibot.verbose: - pywikibot.output(u"getCommonscat: The category doesnt exist.") - return u'' + logpages = commonsSite.logpages(mode='delete', title=commonsPage.title()) + try: + logitem = logpages.next() + (logpage, loguser, logtimestamp, logcomment) = logitem + # Some logic to extract the target page. + regex = u'moved to [[:?Category:(?P<newcat1>[^|}]+)(|[^}]+)?]]|Robot: Changing Category:(.+) to Category:(?P<newcat2>.+)' + m = re.search(regex, logcomment) + if m: + if m.group('newcat1'): + return self.checkCommonscatLink(m.group('newcat1')) + elif m.group('newcat2'): + return self.checkCommonscatLink(m.group('newcat2')) + else: + pywikibot.output(u'getCommonscat: Deleted by %s. Couldn't find move target in " %s "' % (loguser, logcomment)) + return u'' + except StopIteration: + if pywikibot.verbose: + pywikibot.output(u"getCommonscat: The category doesnt exist and nothing found in the deletion log.") + return u'' elif commonsPage.isRedirectPage(): if pywikibot.verbose: pywikibot.output(u"getCommonscat: The category is a redirect")