<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://chabadpedia.com/index.php?action=history&amp;feed=atom&amp;title=User%3AShia.k%2Ftransclusioncount.py</id>
	<title>User:Shia.k/transclusioncount.py - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://chabadpedia.com/index.php?action=history&amp;feed=atom&amp;title=User%3AShia.k%2Ftransclusioncount.py"/>
	<link rel="alternate" type="text/html" href="https://chabadpedia.com/index.php?title=User:Shia.k/transclusioncount.py&amp;action=history"/>
	<updated>2026-04-24T23:17:50Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>https://chabadpedia.com/index.php?title=User:Shia.k/transclusioncount.py&amp;diff=5373&amp;oldid=prev</id>
		<title>Shia.k: Created page with &quot;{{documentation}}  == transclusioncount.py == &lt;syntaxhighlight lang=&quot;python&quot;&gt; #! ~/venv/bin/python3  ########################### Transclusion count tool ########################### # Updates subpages of the page specified by the &quot;rootpage&quot; variable with      # # transclusion counts for all templates and modules on the wiki specified by  # # the &quot;lang&quot; variable.                                                        # #...&quot;</title>
		<link rel="alternate" type="text/html" href="https://chabadpedia.com/index.php?title=User:Shia.k/transclusioncount.py&amp;diff=5373&amp;oldid=prev"/>
		<updated>2025-03-19T21:02:37Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;{{documentation}}  == transclusioncount.py == &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt; #! ~/venv/bin/python3  ########################### Transclusion count tool ########################### # Updates subpages of the page specified by the &amp;quot;rootpage&amp;quot; variable with      # # transclusion counts for all templates and modules on the wiki specified by  # # the &amp;quot;lang&amp;quot; variable.                                                        # #...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{documentation}}&lt;br /&gt;
&lt;br /&gt;
== transclusioncount.py ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#! ~/venv/bin/python3&lt;br /&gt;
&lt;br /&gt;
########################### Transclusion count tool ###########################&lt;br /&gt;
# Updates subpages of the page specified by the &amp;quot;rootpage&amp;quot; variable with      #&lt;br /&gt;
# transclusion counts for all templates and modules on the wiki specified by  #&lt;br /&gt;
# the &amp;quot;lang&amp;quot; variable.                                                        #&lt;br /&gt;
#                                                                             #&lt;br /&gt;
# This script must be run on toolforge, with both Pywikibot and the toolforge #&lt;br /&gt;
# libraries installed. Pywikibot also relies on the requests library and      #&lt;br /&gt;
# either mwparserfromhell or wikitextparser.  See:                            #&lt;br /&gt;
# //wikitech.wikimedia.org/wiki/Help:Toolforge/My_first_Pywikibot_tool        #&lt;br /&gt;
# //wikitech.wikimedia.org/wiki/User:Legoktm/toolforge_library                #&lt;br /&gt;
# //www.mediawiki.org/wiki/Manual:Pywikibot/Installation/en                   #&lt;br /&gt;
#                                                                             #&lt;br /&gt;
# See the following for setting up Toolforge to run python jobs:              #&lt;br /&gt;
# //wikitech.wikimedia.org/wiki/Help:Toolforge/Python#Jobs                    #&lt;br /&gt;
#                                                                             #&lt;br /&gt;
# When creating the bootstrap_venv.sh file, include the following four lines  #&lt;br /&gt;
# (without the &amp;quot;#&amp;quot;s at the start of each line):                               #&lt;br /&gt;
# pip install toolforge                                                       #&lt;br /&gt;
# pip install pywikibot                                                       #&lt;br /&gt;
# pip install &amp;quot;mwparserfromhell&amp;gt;=0.5.2&amp;quot;                                       #&lt;br /&gt;
# pip install &amp;quot;packaging&amp;quot;                                                     #&lt;br /&gt;
# pip install &amp;quot;requests&amp;gt;=2.20.1&amp;quot;                                              #&lt;br /&gt;
#                                                                             #&lt;br /&gt;
# and, after running chmod ug+x bootstrap_venv.sh, run it using the following #&lt;br /&gt;
# command (without the &amp;quot;#&amp;quot;):                                                  #&lt;br /&gt;
#                                                                             #&lt;br /&gt;
# toolforge jobs run bootstrap-venv --command &amp;quot;cd $PWD &amp;amp;&amp;amp; ./bootstrap_venv.sh&amp;quot; --image python3.9 --wait&lt;br /&gt;
#                                                                             #&lt;br /&gt;
# To run automatically, use the following command:                            #&lt;br /&gt;
#                                                                             #&lt;br /&gt;
# toolforge jobs run transclusioncount --command &amp;quot;pyvenv/bin/python scripts/transclusioncount.py&amp;quot; --image python3.9 --schedule &amp;quot;00 12 * * SUN&amp;quot;&lt;br /&gt;
#                                                                             #&lt;br /&gt;
# I would recommend changing 00 12 to a random time, to avoid running when    #&lt;br /&gt;
# lots of other scripts are using server resources                            #&lt;br /&gt;
###############################################################################&lt;br /&gt;
&lt;br /&gt;
import toolforge&lt;br /&gt;
import pywikibot&lt;br /&gt;
import time&lt;br /&gt;
from math import log10, floor&lt;br /&gt;
&lt;br /&gt;
# set debug = &amp;quot;no&amp;quot; to enable writing to wiki&lt;br /&gt;
class settings:&lt;br /&gt;
	lang = &amp;#039;enwiki&amp;#039;&lt;br /&gt;
	rootpage = &amp;quot;Module:Transclusion count/&amp;quot;&lt;br /&gt;
	editsumm = &amp;quot;[[Wikipedia:BOT|Bot]]: Updated page.&amp;quot;&lt;br /&gt;
	debug = &amp;quot;no&amp;quot;&lt;br /&gt;
	sigfigs = 2&lt;br /&gt;
&lt;br /&gt;
report_title = settings.rootpage + &amp;#039;data/&amp;#039;&lt;br /&gt;
&lt;br /&gt;
report_template = &amp;#039;&amp;#039;&amp;#039;\&lt;br /&gt;
return {&lt;br /&gt;
%s&lt;br /&gt;
}&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
wiki = pywikibot.Site()&lt;br /&gt;
&lt;br /&gt;
query1 = &amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
/* transclusioncount.py SLOW_OK */&lt;br /&gt;
SELECT&lt;br /&gt;
  lt_title,&lt;br /&gt;
  COUNT(*)&lt;br /&gt;
FROM templatelinks JOIN linktarget ON tl_target_id = lt_id&lt;br /&gt;
WHERE lt_namespace = 10&lt;br /&gt;
GROUP BY lt_title&lt;br /&gt;
HAVING COUNT(*) &amp;gt; 2000&lt;br /&gt;
LIMIT 10000;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
query2 = &amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
/* transclusioncount.py SLOW_OK */&lt;br /&gt;
SELECT&lt;br /&gt;
  lt_title,&lt;br /&gt;
  COUNT(*)&lt;br /&gt;
FROM templatelinks JOIN linktarget ON tl_target_id = lt_id&lt;br /&gt;
WHERE lt_namespace = 828&lt;br /&gt;
GROUP BY lt_title&lt;br /&gt;
HAVING COUNT(*) &amp;gt; 2000&lt;br /&gt;
LIMIT 10000;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
if settings.debug != &amp;quot;no&amp;quot;:&lt;br /&gt;
	print(&amp;quot;Query:\n&amp;quot; + query)&lt;br /&gt;
&lt;br /&gt;
connectSuccess = False&lt;br /&gt;
tries = 0&lt;br /&gt;
&lt;br /&gt;
while not connectSuccess:&lt;br /&gt;
	try:&lt;br /&gt;
		conn = toolforge.connect(settings.lang, &amp;#039;analytics&amp;#039;)&lt;br /&gt;
		print(&amp;quot;\nExecuting query1 at %s...&amp;quot; % (time.ctime()))&lt;br /&gt;
		with conn.cursor() as cursor:&lt;br /&gt;
			cursor.execute(query1)&lt;br /&gt;
			result1 = cursor.fetchall()&lt;br /&gt;
		print(&amp;quot;\nExecuting query2 at %s...&amp;quot; % (time.ctime()))&lt;br /&gt;
		with conn.cursor() as cursor:&lt;br /&gt;
			cursor.execute(query2)&lt;br /&gt;
			result2 = cursor.fetchall()&lt;br /&gt;
		connectSuccess = True&lt;br /&gt;
		print(&amp;quot;Success at %s!&amp;quot; % (time.ctime()))&lt;br /&gt;
	except Exception as e:&lt;br /&gt;
		try:&lt;br /&gt;
			cursor.close()&lt;br /&gt;
		except:&lt;br /&gt;
			pass&lt;br /&gt;
		try:&lt;br /&gt;
			conn.close()&lt;br /&gt;
		except:&lt;br /&gt;
			pass&lt;br /&gt;
		print(&amp;quot;Error: &amp;quot;, e)&lt;br /&gt;
		tries += 1&lt;br /&gt;
		if tries &amp;gt; 24:&lt;br /&gt;
			print(&amp;quot;Script failed after 24 tries at %s.&amp;quot;  % (time.ctime()))&lt;br /&gt;
			raise SystemExit(e)&lt;br /&gt;
		else:&lt;br /&gt;
			print(&amp;quot;Waiting 1 hour starting at %s...&amp;quot;  % (time.ctime()))&lt;br /&gt;
			time.sleep(3600)&lt;br /&gt;
&lt;br /&gt;
if settings.debug != &amp;quot;no&amp;quot;:&lt;br /&gt;
	import os&lt;br /&gt;
	try:&lt;br /&gt;
		with open(os.path.join(os.getcwd(), &amp;#039;result1.txt&amp;#039;), &amp;quot;w&amp;quot;) as f:&lt;br /&gt;
			f.write(str(result1))&lt;br /&gt;
		with open(os.path.join(os.getcwd(), &amp;#039;result2.txt&amp;#039;), &amp;quot;w&amp;quot;) as f:&lt;br /&gt;
			f.write(str(result2))&lt;br /&gt;
	except Exception as e:&lt;br /&gt;
		print(&amp;quot;Error writing to file: %s&amp;quot; % (e))&lt;br /&gt;
	print(&amp;quot;\nBuilding output...&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
output = {&amp;quot;A&amp;quot;: [], &amp;quot;B&amp;quot;: [], &amp;quot;C&amp;quot;: [], &amp;quot;D&amp;quot;: [], &amp;quot;E&amp;quot;: [], &amp;quot;F&amp;quot;: [], &amp;quot;G&amp;quot;: [], &amp;quot;H&amp;quot;: [], &amp;quot;I&amp;quot;: [], &amp;quot;J&amp;quot;: [], &amp;quot;K&amp;quot;: [], &amp;quot;L&amp;quot;: [], &amp;quot;M&amp;quot;: [], &amp;quot;N&amp;quot;: [], &amp;quot;O&amp;quot;: [], &amp;quot;P&amp;quot;: [], &amp;quot;Q&amp;quot;: [], &amp;quot;R&amp;quot;: [], &amp;quot;S&amp;quot;: [], &amp;quot;T&amp;quot;: [], &amp;quot;U&amp;quot;: [], &amp;quot;V&amp;quot;: [], &amp;quot;W&amp;quot;: [], &amp;quot;X&amp;quot;: [], &amp;quot;Y&amp;quot;: [], &amp;quot;Z&amp;quot;: [], &amp;quot;other&amp;quot;: []}&lt;br /&gt;
&lt;br /&gt;
for row in result1:&lt;br /&gt;
	try:&lt;br /&gt;
		lt_title = row[0].decode()&lt;br /&gt;
	except:&lt;br /&gt;
		lt_title = str(row[0])&lt;br /&gt;
	index_letter = lt_title[0]&lt;br /&gt;
	if row[1] &amp;lt; 100000: #Use an extra sigfig for very large counts&lt;br /&gt;
		sigfigs = settings.sigfigs - 1&lt;br /&gt;
	else:&lt;br /&gt;
		sigfigs = settings.sigfigs&lt;br /&gt;
	uses = round(row[1], -int(floor(log10(row[1])))+sigfigs)&lt;br /&gt;
	table_row = &amp;#039;&amp;#039;&amp;#039;[&amp;quot;%s&amp;quot;] = %i,&amp;#039;&amp;#039;&amp;#039; % (lt_title.replace(&amp;quot;\\&amp;quot;, &amp;quot;\\\\&amp;quot;).replace(&amp;#039;&amp;quot;&amp;#039;, &amp;#039;\\&amp;quot;&amp;#039;), uses)&lt;br /&gt;
	try:&lt;br /&gt;
		output[index_letter].append(table_row)&lt;br /&gt;
	except:&lt;br /&gt;
		output[&amp;quot;other&amp;quot;].append(table_row)&lt;br /&gt;
&lt;br /&gt;
for row in result2:&lt;br /&gt;
	try:&lt;br /&gt;
		lt_title = row[0].decode()&lt;br /&gt;
	except:&lt;br /&gt;
		lt_title = str(row[0])&lt;br /&gt;
	index_letter = lt_title[0]&lt;br /&gt;
	if row[1] &amp;lt; 100000: #Use an extra sigfig for very large counts&lt;br /&gt;
		sigfigs = settings.sigfigs - 1&lt;br /&gt;
	else:&lt;br /&gt;
		sigfigs = settings.sigfigs&lt;br /&gt;
	uses = round(row[1], -int(floor(log10(row[1])))+sigfigs)&lt;br /&gt;
	table_row = &amp;#039;&amp;#039;&amp;#039;[&amp;quot;Module:%s&amp;quot;] = %i,&amp;#039;&amp;#039;&amp;#039; % (lt_title.replace(&amp;quot;\\&amp;quot;, &amp;quot;\\\\&amp;quot;).replace(&amp;#039;&amp;quot;&amp;#039;, &amp;#039;\\&amp;quot;&amp;#039;), uses)&lt;br /&gt;
	try:&lt;br /&gt;
		output[index_letter].append(table_row)&lt;br /&gt;
	except:&lt;br /&gt;
		output[&amp;quot;other&amp;quot;].append(table_row)&lt;br /&gt;
&lt;br /&gt;
for section in output:&lt;br /&gt;
	report = pywikibot.Page(wiki, report_title + section)&lt;br /&gt;
	old_text = report.text&lt;br /&gt;
	report.text = report_template % (&amp;#039;\n&amp;#039;.join(output[section]))&lt;br /&gt;
	if settings.debug == &amp;quot;no&amp;quot;:&lt;br /&gt;
		# print(&amp;quot;Writing &amp;quot; + report_title + section)&lt;br /&gt;
		try:&lt;br /&gt;
			report.save(settings.editsumm)&lt;br /&gt;
		except Exception as e:&lt;br /&gt;
			print(&amp;quot;Error at %s: %s&amp;quot; % (time.ctime(),e))&lt;br /&gt;
	else:&lt;br /&gt;
		print(&amp;quot;== &amp;quot; + report_title + section + &amp;quot; ==\n\n&amp;quot; + report.text)&lt;br /&gt;
		&lt;br /&gt;
print(&amp;quot;\nDone at %s!&amp;quot; % (time.ctime()))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Shia.k</name></author>
	</entry>
</feed>