User:Inductiveload/Scripts/template usage

From Wikisource
Jump to navigation Jump to search

There are no parameters to this script. You run like this:

python get_template_stats.py

Any changes to the destination file, etc should be made in the "__init__" function.

#!/usr/bin/env python

'''
This script generates a (wiki) table of all templates on the wiki and
the number of usages of each of them.

It outputs to a local file, and optionally uploads to a page on the wiki.
'''

import pw_script_header
import wikipedia

import query_ext
import codecs

class GetTemplateStats():

    def get_all_templates(self):
        """Gets an iterator of all the templates on a site."""

        template_iterator =  query_ext.PreloadingPagesStartswith('Template:',
                                                            self.preload_limit)
        return template_iterator


    def get_all_usages(self, template_name):
        """Gets an iterator of all the usages of a template"""

        usage_iterator = query_ext.PreloadingEmbeddedIn(template_name,
                                                            self.preload_limit)
        return usage_iterator

    def get_usage_link(self, page):
        """Returns the link to the "What links here" page of a given page"""

        'http://%s.%s.org/wiki/Special:WhatLinksHere/%s'\
                                                % (self.lang, self.wiki,
                                                page.replace(' ', '_') )

    def add_row(self, name, count):

        row = '| {{tl|%s}} [%s {{smaller|usage}}]\n'\
                            % ( name.replace('Template:', ''),
                                self.get_usage_link(name))
        row += '| %d\n' % count
        row += '|-\n'

        self.text += row


    def enumerate_iterator(self, iterator):
        """Returns the number of items in the iterator"""

        count = 0
        for k in iterator:
            count += 1

        return count


    def write_data(self):

        if self.save_local:
            if self.debug:
                print '\n(INF) Saving list to file: %s' % self.outfile_name

            self.outfile.write(self.text)

    def upload_data(self):

        if self.upload :

            if self.debug:
                print '\n(INF) Uploading page to %s' % self.upload_destination

            page = wikipedia.Page(self.site, self.upload_destination)
            page.put(self.text)


    def process_templates(self):

        self.text = '''
This data was generated by the script that can be found [[User:Inductiveload/
scripts/template_usage]]. It relies on [[User:Phe|Phe]]s
[[User:Phe/Scripts/query ext.py|query_ext.py]] to query the database for the
relavant data, and pywikipedia to do the uploading to my user page.

If you want to regenerate this data, please feel free to use this script, but
don't overwrite this page, as I may be using it for something. You can change
the parameter "self.upload_destination" below to allow you to place the page
where you like, or just use the data out of the text file.

Alternatively, if you don't know how to run this program, drop a note on
[[User_talk:Inductiveload|my talk page]] asking me to re-run the script.

{| class="wikitable sortable"\n\
! Template !! Usage\n\
|-\n
'''
        template_iterator = self.get_all_templates()

        # total number of templates to process
        template_count = self.enumerate_iterator(template_iterator)

        if self.debug:
            print '(INF) %d templates to process' % template_count

        count = 0 #counter for processed templates
        for template in template_iterator:
            count += 1

            template_name = template[u'title']
            if template_name[-4:] == '/doc':
                continue

            if self.debug:
                print u'(INF) Template %d/%d %s' \
                                % (count, template_count, template_name)

            usages = self.get_all_usages(template_name)

            # total usages of this template
            usage_count = self.enumerate_iterator(usages)

            if self.debug:
                print u'\t(INF) %d usages' % usage_count

            self.add_row(template_name, usage_count)

        self.text += '|}'

        self.write_data()
        self.upload_data()


    def __init__(self):

        self.debug = True

        self.lang = 'en'
        self.wiki = 'wikisource'

        self.save_local = True #save to a local file
        self.outfile_name = '/home/john/src/pw/zz_templates.txt'

        self.upload = True      #upload data to a wiki page
        self.upload_destination=u'User:Inductiveload/templates'


        # you probably don't need to edit under here
        self.outfile = codecs.open(self.outfile_name, 'w', 'utf-8')
        self.site = wikipedia.getSite()
        self.preload_limit = 1000

if __name__ == "__main__":
    c = GetTemplateStats()
    c.process_templates()