Wikistats

= How To Generate Automatic Wiki Statistics =

Mediawikis
mysql> describe wikistats; +--+--+--+-+-++ +--+--+--+-+-++ +--+--+--+-+-++ 15 rows in set (0.03 sec)
 * Field   | Type         | Null | Key | Default             | Extra          |
 * id      | int(11)      |      | PRI | NULL                | auto_increment |
 * name    | varchar(64)  | YES  |     | NULL                |                |
 * label   | varchar(128) | YES  |     | NULL                |                |
 * url     | varchar(255) | YES  |     | NULL                |                |
 * total   | int(11)      | YES  |     | NULL                |                |
 * good    | int(11)      |      |     | 0                   |                |
 * views   | int(11)      | YES  |     | NULL                |                |
 * edits   | int(11)      | YES  |     | NULL                |                |
 * users   | int(11)      | YES  |     | NULL                |                |
 * admins  | int(11)      | YES  |     | NULL                |                |
 * started | date         | YES  |     | NULL                |                |
 * host    | varchar(128) | YES  |     | NULL                |                |
 * ts      | timestamp    | YES  |     | 0000-00-00 00:00:00 |                |
 * color   | varchar(32)  | YES  |     | NULL                |                |
 * linktype | tinyint(4)  | YES  |     | NULL                |                |

Wikipedias
mysql> describe wikipedias; +--+--+--+-+-++ +--+--+--+-+-++ +--+--+--+-+-++ 13 rows in set (0.03 sec)
 * Field   | Type         | Null | Key | Default             | Extra          |
 * id      | int(11)      |      | PRI | NULL                | auto_increment |
 * lang    | varchar(128) | YES  |     | NULL                |                |
 * prefix  | varchar(16)  | YES  | MUL | NULL                |                |
 * total   | int(11)      | YES  |     | NULL                |                |
 * good    | int(11)      | YES  |     | NULL                |                |
 * views   | int(11)      | YES  |     | NULL                |                |
 * edits   | int(11)      | YES  |     | NULL                |                |
 * users   | int(11)      | YES  |     | NULL                |                |
 * admins  | int(11)      | YES  |     | NULL                |                |
 * started | date         | YES  |     | NULL                |                |
 * ts      | timestamp    | YES  |     | 0000-00-00 00:00:00 |                |
 * loclang | varchar(128) | YES  |     | NULL                |                |
 * mainpage | varchar(64) | YES  |     | NULL                |                |

Fill it initally
--> Wikistats/Wikipedia Languages

script
to be called from the commandline like

php update_wikipedias.php

<?php
 * 1) Wikistats by mutante - 2006-02-08 - S23 Wiki - http://s23.org

ini_set('user_agent','http://s23.org/wikistats/ | http://meta.wikimedia.org/wiki/User:mutante | mutante@s23.org');
 * 1) set user agent

require_once("config.php");
 * 1) include config

mysql_connect("$dbhost", "$dbname", "$dbpass") or die(mysql_error); echo "Wikistats Updater by S23-mutante \n Connected to MySQL. \n";
 * 1) connect db

mysql_select_db("wikistats") or die(mysql_error); echo "Selected database 'wikistats' \n.";
 * 1) select db

$query = "select * from wikipedias order by good asc"; $result = mysql_query("$query") or die(mysql_error); echo "Sent query: '$query'.\n";
 * 1) select all ordered by good

while($row = mysql_fetch_array( $result )) { if ($row['prefix']!="") { $url="http://".$row['prefix'].".wikipedia.org/wiki/Special:Statistics?action=raw"; $remotefile=fopen("$url","r");
 * 1) main loop

if (!$remotefile) { echo "Unable to open remote file $url \n"; fclose ($remotefile); } else { echo "Ok opening $url \n"; $buffer = fgets($remotefile); echo "Buffer: $buffer"; $pieces = explode(";",$buffer); $total = explode("=",$pieces[0]); $total = $total[1]; $good = explode("=",$pieces[1]); $good = $good[1];

if ($good>2147483647) { $good=0; }
 * 1) bug protection

$views = explode("=",$pieces[2]); $views = $views[1]; $edits = explode("=",$pieces[3]); $edits = $edits[1]; $users = explode("=",$pieces[4]); $users = $users[1]; $admins = explode("=",$pieces[5]); $admins = $admins[1]; $admins = trim($admins); echo "Total: $total Good: $good Views: $views Edits: $edits Users: $users Admins: $admins \n"; $query2="update wikipedias set total=\"$total\",good=\"$good\",views=\"$views\",edits=\"$edits\",users=\"$users\",admins=\"$admins\",ts=NOW where id=\"".$row['id']."\";"; echo "Query: $query2 \n"; $result2 = mysql_query("$query2") or die(mysql_error); echo "Updated MySQL. Return Code: $result2 \n \n"; fclose ($remotefile); } } } mysql_close; echo "Done. Fnord."; ?>

cronjob
Every 4 hours:

crontab -l 00 */4 * * * /usr/local/sbin/update_all.sh
 * 1) m h  dom mon dow   command

which calls (among others)

php /var/www/wikistats/update_wikis.php

Wikipedias


&lt;pre&gt; 
 * style="text-align: right;" | 
 * style="text-align: right;" | 


 * style="text-align: right; | 
 * style="text-align: right; | 


 * style="text-align: right; | 


 * style="text-align: right;" | ]
 * style="text-align: right;" | 

1) { echo "|} \n"; } ?>
 * style="text-align: right;" | ]
 * style="text-align: right;" | ]
 * style="text-align: right;" | ]