00001 <?php
00025 require_once( dirname(__FILE__) . '/Maintenance.php' );
00026
00027 class UpdateArticleCount extends Maintenance {
00028
00029
00030 private $namespaces;
00031
00032 public function __construct() {
00033 parent::__construct();
00034 $this->mDescription = "Count of the number of articles and update the site statistics table";
00035 $this->addOption( 'update', 'Update the site_stats table with the new count' );
00036 }
00037
00038 public function execute() {
00039 global $wgContentNamespaces;
00040 $this->namespaces = $wgContentNamespaces;
00041 $this->output( "Counting articles..." );
00042 $result = $this->count();
00043
00044 if( $result !== false ) {
00045 $this->output( "found {$result}.\n" );
00046 if( $this->hasOption( 'update' ) ) {
00047 $this->output( "Updating site statistics table... " );
00048 $dbw = wfGetDB( DB_MASTER );
00049 $dbw->update( 'site_stats', array( 'ss_good_articles' => $result ), array( 'ss_row_id' => 1 ), __METHOD__ );
00050 $this->output( "done.\n" );
00051 } else {
00052 $this->output( "To update the site statistics table, run the script with the --update option.\n" );
00053 }
00054 } else {
00055 $this->output( "failed.\n" );
00056 }
00057 }
00058
00065 private function makeNsSet() {
00066 foreach( $this->namespaces as $namespace )
00067 $namespaces[] = intval( $namespace );
00068 return implode( ', ', $namespaces );
00069 }
00070
00077 private function makeSql( $dbr ) {
00078 list( $page, $pagelinks ) = $dbr->tableNamesN( 'page', 'pagelinks' );
00079 $nsset = $this->makeNsSet();
00080 return "SELECT COUNT(DISTINCT page_id) AS pagecount " .
00081 "FROM $page, $pagelinks " .
00082 "WHERE pl_from=page_id and page_namespace IN ( $nsset ) " .
00083 "AND page_is_redirect = 0 AND page_len > 0";
00084 }
00085
00091 private function count() {
00092 $dbr = wfGetDB( DB_SLAVE );
00093 $res = $dbr->query( $this->makeSql( $dbr ), __METHOD__ );
00094 $row = $dbr->fetchObject( $res );
00095 $dbr->freeResult( $res );
00096 return $row ? $row->pagecount : false;
00097 }
00098 }
00099
00100 $maintClass = "UpdateArticleCount";
00101 require_once( DO_MAINTENANCE );