Fixes issues with complex background rules containing extra information after the...
[lhc/web/wiklou.git] / maintenance / initStats.php
index b622c3f..9dd2721 100644 (file)
@@ -1,78 +1,83 @@
 <?php
-
 /**
  * Maintenance script to re-initialise or update the site statistics table
  *
- * @package MediaWiki
- * @subpackage Maintenance
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Maintenance
  * @author Brion Vibber
  * @author Rob Church <robchur@gmail.com>
- * @licence GNU General Public Licence 2.0 or later
  */
-$options = array( 'help', 'update', 'noviews' );
-require_once( 'commandLine.inc' );
-echo( "Refresh Site Statistics\n\n" );
-$dbr =& wfGetDB( DB_SLAVE );
-$fname = 'initStats';
 
-if( isset( $options['help'] ) ) {
-       showHelp();
-       exit();
-}
+require_once( dirname( __FILE__ ) . '/Maintenance.php' );
 
-echo( "Counting total edits..." );
-$edits = $dbr->selectField( 'revision', 'COUNT(*)', '', $fname );
-echo( "{$edits}\nCounting number of articles..." );
+class InitStats extends Maintenance {
+       public function __construct() {
+               parent::__construct();
+               $this->mDescription = "Re-initialise the site statistics tables";
+               $this->addOption( 'update', 'Update the existing statistics (preserves the ss_total_views field)' );
+               $this->addOption( 'noviews', "Don't update the page view counter" );
+               $this->addOption( 'active', 'Also update active users count' );
+               $this->addOption( 'use-master', 'Count using the master database' );
+       }
 
-global $wgContentNamespaces;
-$good  = $dbr->selectField( 'page', 'COUNT(*)', array( 'page_namespace' => $wgContentNamespaces, 'page_is_redirect' => 0, 'page_len > 0' ), $fname );
-echo( "{$good}\nCounting total pages..." );
+       public function execute() {
+               $this->output( "Refresh Site Statistics\n\n" );
+               $counter = new SiteStatsInit( $this->hasOption( 'use-master' ) );
 
-$pages = $dbr->selectField( 'page', 'COUNT(*)', '', $fname );
-echo( "{$pages}\nCounting number of users..." );
+               $this->output( "Counting total edits..." );
+               $edits = $counter->edits();
+               $this->output( "{$edits}\nCounting number of articles..." );
 
-$users = $dbr->selectField( 'user', 'COUNT(*)', '', $fname );
-echo( "{$users}\nCounting number of admins..." );
+               $good  = $counter->articles();
+               $this->output( "{$good}\nCounting total pages..." );
 
-$admin = $dbr->selectField( 'user_groups', 'COUNT(*)', array( 'ug_group' => 'sysop' ), $fname );
-echo( "{$admin}\nCounting number of images..." );
+               $pages = $counter->pages();
+               $this->output( "{$pages}\nCounting number of users..." );
 
-$image = $dbr->selectField( 'image', 'COUNT(*)', '', $fname );
-echo( "{$image}\n" );
+               $users = $counter->users();
+               $this->output( "{$users}\nCounting number of images..." );
 
-if( !isset( $options['noviews'] ) ) {
-       echo( "Counting total page views..." );
-       $views = $dbr->selectField( 'page', 'SUM(page_counter)', '', $fname );
-       echo( "{$views}\n" );
-}
+               $image = $counter->files();
+               $this->output( "{$image}\n" );
 
-echo( "\nUpdating site statistics..." );
+               if ( !$this->hasOption( 'noviews' ) ) {
+                       $this->output( "Counting total page views..." );
+                       $views = $counter->views();
+                       $this->output( "{$views}\n" );
+               }
 
-$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' => isset( $views ) ? $views : 0 );
-                               
-if( isset( $options['update'] ) ) {
-       $dbw->update( 'site_stats', $values, $conds, $fname );
-} else {
-       $dbw->delete( 'site_stats', $conds, $fname );
-       $dbw->insert( 'site_stats', array_merge( $values, $conds, $views ), $fname );
-}
+               if ( $this->hasOption( 'active' ) ) {
+                       $this->output( "Counting active users..." );
+                       $active = SiteStatsUpdate::cacheUpdate();
+                       $this->output( "{$active}\n" );
+               }
+
+               $this->output( "\nUpdating site statistics..." );
 
-echo( "done.\n\n" );
+               if ( $this->hasOption( 'update' ) ) {
+                       $counter->update();
+               } else {
+                       $counter->refresh();
+               }
 
-function showHelp() {
-       echo( "Re-initialise the site statistics tables.\n\n" );
-       echo( "Usage: php initStats.php [--update|--noviews]\n\n" );
-       echo( " --update : Update the existing statistics (preserves the ss_total_views field)\n" );
-       echo( "--noviews : Don't update the page view counter\n\n" );
+               $this->output( "done.\n" );
+       }
 }
 
-?>
\ No newline at end of file
+$maintClass = "InitStats";
+require_once( DO_MAINTENANCE );