\n to eof
[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 * @file
22 * @ingroup Maintenance
23 * @author Rob Church <robchur@gmail.com>
24 */
25
26 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
27
28 class UpdateArticleCount extends Maintenance {
29
30 // Content namespaces
31 private $namespaces;
32
33 public function __construct() {
34 parent::__construct();
35 $this->mDescription = "Count of the number of articles and update the site statistics table";
36 $this->addOption( 'update', 'Update the site_stats table with the new count' );
37 }
38
39 public function execute() {
40 global $wgContentNamespaces;
41 $this->namespaces = $wgContentNamespaces;
42 $this->output( "Counting articles..." );
43 $result = $this->count();
44
45 if ( $result !== false ) {
46 $this->output( "found {$result}.\n" );
47 if ( $this->hasOption( 'update' ) ) {
48 $this->output( "Updating site statistics table... " );
49 $dbw = wfGetDB( DB_MASTER );
50 $dbw->update( 'site_stats', array( 'ss_good_articles' => $result ), array( 'ss_row_id' => 1 ), __METHOD__ );
51 $this->output( "done.\n" );
52 } else {
53 $this->output( "To update the site statistics table, run the script with the --update option.\n" );
54 }
55 } else {
56 $this->output( "failed.\n" );
57 }
58 }
59
60 /**
61 * Produce a comma-delimited set of namespaces
62 * Includes paranoia
63 *
64 * @return string
65 */
66 private function makeNsSet() {
67 foreach ( $this->namespaces as $namespace )
68 $namespaces[] = intval( $namespace );
69 return implode( ', ', $namespaces );
70 }
71
72 /**
73 * Produce SQL for the query
74 *
75 * @param $dbr Database handle
76 * @return string
77 */
78 private function makeSql( $dbr ) {
79 list( $page, $pagelinks ) = $dbr->tableNamesN( 'page', 'pagelinks' );
80 $nsset = $this->makeNsSet();
81 return "SELECT COUNT(DISTINCT page_id) AS pagecount " .
82 "FROM $page, $pagelinks " .
83 "WHERE pl_from=page_id and page_namespace IN ( $nsset ) " .
84 "AND page_is_redirect = 0 AND page_len > 0";
85 }
86
87 /**
88 * Count the number of valid content pages in the wiki
89 *
90 * @return mixed Integer, or false if there's a problem
91 */
92 private function count() {
93 $dbr = wfGetDB( DB_SLAVE );
94 $res = $dbr->query( $this->makeSql( $dbr ), __METHOD__ );
95 $row = $dbr->fetchObject( $res );
96 return $row ? $row->pagecount : false;
97 }
98 }
99
100 $maintClass = "UpdateArticleCount";
101 require_once( DO_MAINTENANCE );