3 * Maintenance script to provide a better count of the number of articles
4 * and update the site statistics table, if desired
7 * @author Rob Church <robchur@gmail.com>
10 require_once( "Maintenance.php" );
12 class UpdateArticleCount
extends Maintenance
{
17 public function __construct() {
18 global $wgContentNamespaces;
19 parent
::__construct();
20 $this->mDescription
= "Count of the number of articles and update the site statistics table";
21 $this->addParam( 'update', 'Update the site_stats table with the new count' );
22 $this->namespaces
= $wgContentNamespaces;
25 public function execute() {
26 $this->output( "Counting articles..." );
27 $result = $this->count();
29 if( $result !== false ) {
30 $this->output( "found {$result}.\n" );
31 if( isset( $options['update'] ) && $options['update'] ) {
32 $this->output( "Updating site statistics table... " );
33 $dbw = wfGetDB( DB_MASTER
);
34 $dbw->update( 'site_stats', array( 'ss_good_articles' => $result ), array( 'ss_row_id' => 1 ), __METHOD__
);
35 $this->output( "done.\n" );
37 $this->output( "To update the site statistics table, run the script with the --update option.\n" );
40 $this->output( "failed.\n" );
45 * Produce a comma-delimited set of namespaces
50 private function makeNsSet() {
51 foreach( $this->namespaces
as $namespace )
52 $namespaces[] = intval( $namespace );
53 return implode( ', ', $namespaces );
57 * Produce SQL for the query
59 * @param $dbr Database handle
62 private function makeSql( $dbr ) {
63 list( $page, $pagelinks ) = $dbr->tableNamesN( 'page', 'pagelinks' );
64 $nsset = $this->makeNsSet();
65 return "SELECT COUNT(DISTINCT page_namespace, page_title) AS pagecount " .
66 "FROM $page, $pagelinks " .
67 "WHERE pl_from=page_id and page_namespace IN ( $nsset ) " .
68 "AND page_is_redirect = 0 AND page_len > 0";
72 * Count the number of valid content pages in the wiki
74 * @return mixed Integer, or false if there's a problem
76 private function count() {
77 $dbr = wfGetDB( DB_SLAVE
);
78 $res = $dbr->query( $this->makeSql( $dbr ), __METHOD__
);
79 $row = $dbr->fetchObject( $res );
80 $dbr->freeResult( $res );
81 return $row->pagecount
;
85 $maintClass = "UpdateArticleCount";
86 require_once( DO_MAINTENANCE
);