Jump to content

User:Benc/Scripts/what links here count.py

From Wikipedia, the free encyclopedia
# what_links_here_count.py
# Author: Benc
# Purpose: Count the number of pages that link to a page by namespace and by
#          redirect.  Simply parses the list generated by a
#          Special:Whatlinkshere page.

# Below: cut and paste everything from below "The following pages link to here:"
# Don't worry about extra linefeeds before and after the list.
txt="""

"""

NAMESPACES = [   'Talk',
	'Category',  'Category talk',
	'Help',      'Help talk',
	'Image',     'Image talk',
	'MediaWiki', 'MediaWiki talk',
	'Template',  'Template talk',
	'User',      'User talk',
	'Wikipedia', 'Wikipedia talk']
REDIRECT = '(redirect page)'

totalCount = 0
mainCount = 0
redirectCount = 0
namespaceCount = [0] * len(NAMESPACES)

# Remove leading and trailing whitespace; change to array by newline
txt = txt.strip().split('\n')

for cur in txt:
	if len(cur) == 0:
		continue
	totalCount += 1
	if cur.find(REDIRECT) >= 0:
		redirectCount += 1
	else:
		colon = cur.find(':')
		if colon >= 0 and cur[:colon] in NAMESPACES:
			namespaceCount[NAMESPACES.index(cur[:colon])] += 1
		else:
			mainCount += 1

print 'Count by namespace (%d total):' % (totalCount)
print '%4d from Main' % (mainCount)
for (i, count) in enumerate(namespaceCount):
	if count > 0:
		print '%4d from %s' % (count, NAMESPACES[i])
if redirectCount > 0:
	print '%4d redirects' % (redirectCount)



  ←back to main page    talk