#!/usr/bin/env python3"""Display the list of pages transcluding a given list of templates.It can also be used to simply count the number of pages (rather thanlisting each individually).Syntax: python pwb.py templatecount options templatesCommand line options:-count Counts the number of times each template (passed in as an argument) is transcluded.-list Gives the list of all of the pages transcluding the templates (rather than just counting them).-namespace: Filters the search to a given namespace. If this is specified multiple times it will search all given namespacesExamples--------Counts how many times {{ref}} and {{note}} are transcluded in articles: python pwb.py templatecount -count -namespace:0 ref noteLists all the category pages that transclude {{cfd}} and {{cfdu}}: python pwb.py templatecount -list -namespace:14 cfd cfdu"""## (C) Pywikibot team, 2006-2024## Distributed under the terms of the MIT license.#from__future__importannotationsimportpywikibotfrompywikibot.backportsimportGenerator
[docs]@classmethoddefcount_templates(cls,templates,namespaces)->None:"""Display number of transclusions for a list of templates. Displays the number of transcluded page in the given 'namespaces' for each template given by 'templates' list. :param templates: list of template names :type templates: list :param namespaces: list of namespace numbers :type namespaces: list """formatstr='{0:<10}: {1:>5}'template_dict=cls.template_dict(templates,namespaces)pywikibot.stdout('\nNumber of transclusions per template')pywikibot.stdout('-'*36)total=0forkeyintemplate_dict:count=len(template_dict[key])pywikibot.stdout(formatstr.format(key,count))total+=countpywikibot.stdout(formatstr.format('TOTAL',total))pywikibot.stdout(f'Report generated on {pywikibot.Timestamp.nowutc().isoformat()}')
[docs]@classmethoddeflist_templates(cls,templates,namespaces)->None:"""Display transcluded pages for a list of templates. Displays each transcluded page in the given 'namespaces' for each template given by 'templates' list. :param templates: list of template names :type templates: list :param namespaces: list of namespace numbers :type namespaces: list """template_dict=cls.template_dict(templates,namespaces)pywikibot.stdout('\nList of pages transcluding templates:')forkeyintemplates:pywikibot.info('* '+key)pywikibot.stdout('-'*36)total=0forkeyintemplate_dict:forpageintemplate_dict[key]:pywikibot.stdout(page.title())total+=1pywikibot.info(f'Total page count: {total}')pywikibot.stdout(f'Report generated on {pywikibot.Timestamp.nowutc().isoformat()}')
[docs]@classmethoddeftemplate_dict(cls,templates,namespaces)->dict[str,list[pywikibot.Page]]:"""Create a dict of templates and its transcluded pages. The names of the templates are the keys, and lists of pages transcluding templates in the given namespaces are the values. :param templates: list of template names :type templates: list :param namespaces: list of namespace numbers :type namespaces: list """returndict(cls.template_dict_generator(templates,namespaces))
[docs]@staticmethoddeftemplate_dict_generator(templates,namespaces)->Generator[tuple[str,list[pywikibot.Page]],None,None]:"""Yield transclusions of each template in 'templates'. For each template in 'templates', yield a tuple (template, transclusions), where 'transclusions' is a list of all pages in 'namespaces' where the template has been transcluded. :param templates: list of template names :type templates: list :param namespaces: list of namespace numbers :type namespaces: list """mysite=pywikibot.Site()mytpl=mysite.namespaces.TEMPLATEfortemplateintemplates:gen=pywikibot.Page(mysite,template,ns=mytpl).getReferences(namespaces=namespaces,only_template_inclusion=True)yieldtemplate,list(gen)
[docs]defmain(*args:str)->None:"""Process command line arguments and invoke bot. If args is an empty list, sys.argv is used. :param args: command line arguments """operation=Noneargs_list=[]namespaces=[]forarginpywikibot.handle_args(args):ifargin('-count','-list'):operation=arg[1:]elifarg.startswith('-namespace:'):try:namespaces.append(int(arg[len('-namespace:'):]))exceptValueError:namespaces.append(arg[len('-namespace:'):])else:args_list.append(arg)ifnotoperation:pywikibot.bot.suggest_help(missing_parameters=['operation'])returnrobot=TemplateCountRobot()ifnotargs_list:args_list=['ref','note','ref label','note label','reflist']if'reflist'inargs_list:pywikibot.info('NOTE: it will take a long time to count "reflist".')choice=pywikibot.input_choice('Proceed anyway?',[('yes','y'),('no','n'),('skip','s')],'y',automatic_quit=False)ifchoice=='s':args_list.remove('reflist')elifchoice=='n':returnifoperation=='count':robot.count_templates(args_list,namespaces)elifoperation=='list':robot.list_templates(args_list,namespaces)