Page MenuHomePhabricator

globaltrackinglist.py

Authored By
Tacsipacsi
Apr 11 2022, 12:21 AM
Size
3 KB
Referenced Files
None
Subscribers
None

globaltrackinglist.py

#!/usr/bin/python3
"""Script that lists pages in a tracking category across Wikimedia wikis.
Useful to check usage before removing a deprecated feature that was
tracked in the given category.
Usage:
python3 pwb.py globaltrackinglist <msg>
where <msg> is the message key specifying the tracking category.
Return value:
- 0 if there are no pages in the category across Wikimedia
- 2 if there are pages in the category
- 1 if any other error occurs (e.g. missing parameter, exception)
"""
import sys
from typing import Dict, List
import pywikibot
from pywikibot.family import Family
from pywikibot.page import BaseLink
class GlobalTrackingListResult:
def __init__(self, pages: List[pywikibot.Page], issues: Dict[str, Exception]) -> None:
self.pages = pages
self.issues = issues
class GlobalTrackingListBot:
# Families to search
families = [
'commons',
'foundation',
'incubator',
'mediawiki',
'meta',
'species',
'wikibooks',
'wikidata',
'wikimania',
'wikimediachapter',
'wikinews',
'wikipedia',
'wikiquote',
'wikisource',
'wikitech',
'wikiversity',
'wikivoyage',
'wiktionary',
]
def __init__(self, msg: str):
"""Initializer."""
self.msg = msg
def check_wiki(self, site: pywikibot.APISite) -> list:
catname = site.mediawiki_message(self.msg)
categories = set()
if '{{' in catname:
for ns in site.namespaces:
nspagetitle = pywikibot.Page(site, self.msg, ns).title()
categories.add(site.expand_text(catname, nspagetitle))
else:
categories.add(catname)
members = []
for category in categories:
members.extend(pywikibot.Category(BaseLink(category, 14, site)).members())
return members
def check_family(self, family: str) -> GlobalTrackingListResult:
consolemsg = "Processing family '%s'" % family
pywikibot.output(consolemsg + '...')
codes = Family.load(family).codes
pages = []
issues = dict()
for code in codes:
site = pywikibot.Site(code, family)
try:
result = self.check_wiki(site)
except Exception as e:
issues[str(site)] = e
else:
pages.extend(result)
if len(issues):
pywikibot.output(consolemsg + ' finished with %d issue(s).' % len(issues))
else:
pywikibot.output(consolemsg + ' completed.')
return GlobalTrackingListResult(pages, issues)
def run(self) -> GlobalTrackingListResult:
pages = []
issues = dict()
for family in self.families:
result = self.check_family(family)
pages.extend(result.pages)
issues.update(result.issues)
return GlobalTrackingListResult(pages, issues)
def main(*args: str) -> int:
msg = None
for arg in pywikibot.handle_args(args):
msg = arg
if msg is not None:
result = GlobalTrackingListBot(msg).run()
for page in result.pages:
pywikibot.output(page)
pywikibot.output('%d page(s) found in total' % len(result.pages))
if result.issues:
pywikibot.output('%d wiki(s) had issues: %s' % (len(result.issues), list(result.issues.keys())))
return 0 if len(result.pages) == 0 and len(result.issues) == 0 else 2
else:
pywikibot.error('No message specified')
return 1
if __name__ == '__main__':
sys.exit(main())

File Metadata

Mime Type
text/x-python
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
9398183
Default Alt Text
globaltrackinglist.py (3 KB)

Event Timeline