<?php
+/**
+ * Maintenance script to re-initialise or update the site statistics table
+ *
+ * @package MediaWiki
+ * @subpackage Maintenance
+ * @author Brion Vibber
+ * @author Rob Church <robchur@gmail.com>
+ * @licence GNU General Public Licence 2.0 or later
+ */
+
+$options = array( 'help', 'update' );
require_once( 'commandLine.inc' );
-
+echo( "Refresh Site Statistics\n\n" );
$dbr =& wfGetDB( DB_SLAVE );
+$fname = 'initStats';
+
+echo( "Counting total edits..." );
+$edits = $dbr->selectField( 'revision', 'COUNT(*)', '', $fname );
+echo( "{$edits}\nCounting number of articles..." );
+
+$good = $dbr->selectField( 'page', 'COUNT(*)', array( 'page_namespace' => 0, 'page_is_redirect' => 0, 'page_len > 0' ), $fname );
+echo( "{$good}\nCounting total pages..." );
-$edits = $dbr->selectField( 'revision', 'COUNT(rev_id)', '' );
-$pages = $dbr->selectField( 'page', 'COUNT(page_id)',
- array(
- 'page_namespace' => 0,
- 'page_is_redirect' => 0,
- 'page_len > 0',
- )
-); // HACK APPROXIMATION
+$pages = $dbr->selectField( 'page', 'COUNT(*)', '', $fname );
+echo( "{$pages}\nCounting number of users..." );
-echo "$wgDBname: setting edits $edits, pages $pages\n";
+$users = $dbr->selectField( 'user', 'COUNT(*)', '', $fname );
+echo( "{$users}\nCounting number of admins..." );
+
+$admin = $dbr->selectField( 'user_groups', 'COUNT(*)', array( 'ug_group' => 'sysop' ), $fname );
+echo( "{$admin}\nCounting number of images..." );
+
+$image = $dbr->selectField( 'image', 'COUNT(*)', '', $fname );
+echo( "{$image}\n\nUpdating site statistics..." );
$dbw =& wfGetDB( DB_MASTER );
+$values = array( 'ss_total_edits' => $edits,
+ 'ss_good_articles' => $good,
+ 'ss_total_pages' => $pages,
+ 'ss_users' => $users,
+ 'ss_admins' => $admin,
+ 'ss_images' => $image );
+$conds = array( 'ss_row_id' => 1 );
+$views = array( 'ss_total_views' => 0 );
+
if( isset( $options['update'] ) ) {
- echo "(updating...)\n";
- $dbw->update( 'site_stats',
- array( 'ss_total_edits' => $edits,
- 'ss_good_articles' => $pages ),
- array( 'ss_row_id' => 1 ) );
+ $dbw->update( 'site_stats', $values, $conds, $fname );
} else {
- $dbw->delete( 'site_stats', array( 'ss_row_id' => 1 ) );
- $dbw->insert( 'site_stats',
- array( 'ss_row_id'=> 1,
- 'ss_total_views' => 0,
- 'ss_total_edits' => $edits,
- 'ss_good_articles' => $pages ) );
+ $dbw->delete( 'site_stats', $conds, $fname );
+ $dbw->insert( 'site_stats', array_merge( $values, $conds, $views ), $fname );
}
+echo( "done.\n\n" );
+
?>
\ No newline at end of file
+++ /dev/null
-<?php
-
-/**
- * Maintenance script to update the site_stats.ss_images field
- *
- * @package MediaWiki
- * @subpackage Maintenance
- * @author Rob Church <robchur@gmail.com>
- * @licence GNU General Public Licence 2.0 or later
- */
-
-require_once( 'commandLine.inc' );
-
-function recountImages() {
- $dbw =& wfGetDB( DB_MASTER );
- $count = $dbw->selectField( 'images', 'COUNT(*)', '', 'recountImages' );
- # Replication safe update; set to NULL first to force the change to slaves
- $dbw->update( 'site_stats', array( 'ss_images' => NULL ), array( 'ss_row_id' => 1 ), 'recountImages' );
- $dbw->update( 'site_stats', array( 'ss_images' => $count ), array( 'ss_row_id' => 1 ), 'recountImages' );
- return $count;
-}
-
-echo( "Updating image count in site statistics..." );
-recountImages();
-echo( "set to {$count}.\n\n" );
-
-?>
\ No newline at end of file