3b704ebf981d6c86ec95660eba4983567a97fbee
[lhc/web/wiklou.git] / maintenance / updateArticleCount.php
1 <?php
2 /**
3 * Maintenance script to provide a better count of the number of articles
4 * and update the site statistics table, if desired
5 *
6 * @ingroup Maintenance
7 * @author Rob Church <robchur@gmail.com>
8 */
9
10 require_once( "Maintenance.php" );
11
12 class UpdateArticleCount extends Maintenance {
13
14 // Content namespaces
15 private $namespaces;
16
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;
23 }
24
25 public function execute() {
26 $this->output( "Counting articles..." );
27 $result = $this->count();
28
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" );
36 } else {
37 $this->output( "To update the site statistics table, run the script with the --update option.\n" );
38 }
39 } else {
40 $this->output( "failed.\n" );
41 }
42 }
43
44 /**
45 * Produce a comma-delimited set of namespaces
46 * Includes paranoia
47 *
48 * @return string
49 */
50 private function makeNsSet() {
51 foreach( $this->namespaces as $namespace )
52 $namespaces[] = intval( $namespace );
53 return implode( ', ', $namespaces );
54 }
55
56 /**
57 * Produce SQL for the query
58 *
59 * @param $dbr Database handle
60 * @return string
61 */
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";
69 }
70
71 /**
72 * Count the number of valid content pages in the wiki
73 *
74 * @return mixed Integer, or false if there's a problem
75 */
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;
82 }
83 }
84
85 $maintClass = "UpdateArticleCount";
86 require_once( DO_MAINTENANCE );