#!/usr/bin/env python # -*- coding: utf-8 -*- """ Provides a count of the number of transclusions of a page. Parameters: db - The name of the database. Examples: frwiki for the French Wikipedia, jawiktionary for Japanese Wiktionary Default: enwiki callback - If specified, wraps the output into a given function call and changes Content-Type to application/json for script use. filterredir - How to filter for redirects One value: all, redirects, nonredirects Default: all ns - The assumed namespace of the page Default: 0 title - Title to search. Examples: http://toolserver.org/~dispenser/cgi-bin/embeddedincount.py?title=Template:Stub http://toolserver.org/~dispenser/cgi-bin/embeddedincount.py?title=Stub&ns=10&callback=transclusionCount&filterredir=nonredirects&db=enwikinews Notes: * The "What links here" page combines the results of both backlinks and embeddedin (transclusions). * For a backlink count use backlinkscount.py tool. * This tool does not support namespace localization, use English namespaces or the ns parameter instead. * Source code is at http://toolserver.org/~dispenser/sources/embeddedincount.py """ # I, Dispenser, hereby release this program into the public domain # November 2009 import MySQLdb, cgi import cgitb; cgitb.enable(logdir='tracebacks') namespaces = { 'Media': -2, 'Special': -1, '': 0, 'Talk': 1, 'User': 2, 'User_talk': 3, 'Wikipedia': 4, 'Wikipedia_talk': 5, 'Image': 6, 'Image_talk': 7, 'Mediawiki': 8, # name fudged 'Mediawiki_talk': 9, # name fudged 'Template': 10, 'Template_talk': 11, 'Help': 12, 'Help_talk': 13, 'Category': 14, 'Category_talk': 15, } def main(): form = cgi.FieldStorage() dbName = form.getfirst('db', 'enwiki').replace('_p', '') ns = form.getfirst('ns', '0') title = form.getfirst('title', '').replace(' ', '_') callback = form.getfirst('callback') if title == '': print "Content-Type: text/plain" print __doc__ return if ':' in title: name, t = title.split(':', 1) ns = namespaces.get(name.capitalize().strip('_:'), ns) if ns != 0: title = t db = MySQLdb.connect(db=dbName+'_p', host=dbName.replace('_', '-') + '-p.db.toolserver.org', read_default_file="/home/dispenser/.my.cnf") c = db.cursor() # The redirect pages themselves aren't included if you combine these queries count = 0 if form.getfirst('filterredir') != 'nonredirects': c.execute("SELECT COUNT(*) FROM templatelinks WHERE tl_namespace = %s AND tl_title=%s", (ns, title)) count += c.fetchone()[0] if form.getfirst('filterredir') != 'redirects': c.execute("SELECT COUNT(*) FROM templatelinks AS pg JOIN page ON (tl_from=page_id AND page_is_redirect=1) JOIN templatelinks AS rd ON (rd.tl_title=page_title AND rd.tl_namespace=page_namespace) WHERE pg.tl_namespace = %s AND pg.tl_title=%s", (ns, title)) count += c.fetchone()[0] c.close() if callback is not None: # getfirst treats &callback=& as None anyway print 'Content-Type: application/json' print print '%s(%d)'%(callback, count) else: print 'Content-Type: text/plain' print print count if __name__ == "__main__": main()