Change from global to parameter.
[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 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @ingroup Maintenance
22 * @author Rob Church <robchur@gmail.com>
23 */
24
25 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
26
27 class UpdateArticleCount extends Maintenance {
28
29 // Content namespaces
30 private $namespaces;
31
32 public function __construct() {
33 parent::__construct();
34 $this->mDescription = "Count of the number of articles and update the site statistics table";
35 $this->addOption( 'update', 'Update the site_stats table with the new count' );
36 }
37
38 public function execute() {
39 global $wgContentNamespaces;
40 $this->namespaces = $wgContentNamespaces;
41 $this->output( "Counting articles..." );
42 $result = $this->count();
43
44 if ( $result !== false ) {
45 $this->output( "found {$result}.\n" );
46 if ( $this->hasOption( 'update' ) ) {
47 $this->output( "Updating site statistics table... " );
48 $dbw = wfGetDB( DB_MASTER );
49 $dbw->update( 'site_stats', array( 'ss_good_articles' => $result ), array( 'ss_row_id' => 1 ), __METHOD__ );
50 $this->output( "done.\n" );
51 } else {
52 $this->output( "To update the site statistics table, run the script with the --update option.\n" );
53 }
54 } else {
55 $this->output( "failed.\n" );
56 }
57 }
58
59 /**
60 * Produce a comma-delimited set of namespaces
61 * Includes paranoia
62 *
63 * @return string
64 */
65 private function makeNsSet() {
66 foreach ( $this->namespaces as $namespace )
67 $namespaces[] = intval( $namespace );
68 return implode( ', ', $namespaces );
69 }
70
71 /**
72 * Produce SQL for the query
73 *
74 * @param $dbr Database handle
75 * @return string
76 */
77 private function makeSql( $dbr ) {
78 list( $page, $pagelinks ) = $dbr->tableNamesN( 'page', 'pagelinks' );
79 $nsset = $this->makeNsSet();
80 return "SELECT COUNT(DISTINCT page_id) AS pagecount " .
81 "FROM $page, $pagelinks " .
82 "WHERE pl_from=page_id and page_namespace IN ( $nsset ) " .
83 "AND page_is_redirect = 0 AND page_len > 0";
84 }
85
86 /**
87 * Count the number of valid content pages in the wiki
88 *
89 * @return mixed Integer, or false if there's a problem
90 */
91 private function count() {
92 $dbr = wfGetDB( DB_SLAVE );
93 $res = $dbr->query( $this->makeSql( $dbr ), __METHOD__ );
94 $row = $dbr->fetchObject( $res );
95 $dbr->freeResult( $res );
96 return $row ? $row->pagecount : false;
97 }
98 }
99
100 $maintClass = "UpdateArticleCount";
101 require_once( DO_MAINTENANCE );