* Use DatabaseBase::selectField() to build the query instead of throwing a raw query...
[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 public function __construct() {
31 parent::__construct();
32 $this->mDescription = "Count of the number of articles and update the site statistics table";
33 $this->addOption( 'update', 'Update the site_stats table with the new count' );
34 }
35
36 public function execute() {
37 $this->output( "Counting articles..." );
38 $result = $this->count();
39
40 if ( $result !== false ) {
41 $this->output( "found {$result}.\n" );
42 if ( $this->hasOption( 'update' ) ) {
43 $this->output( "Updating site statistics table... " );
44 $dbw = wfGetDB( DB_MASTER );
45 $dbw->update( 'site_stats', array( 'ss_good_articles' => $result ), array( 'ss_row_id' => 1 ), __METHOD__ );
46 $this->output( "done.\n" );
47 } else {
48 $this->output( "To update the site statistics table, run the script with the --update option.\n" );
49 }
50 } else {
51 $this->output( "failed.\n" );
52 }
53 }
54
55 /**
56 * Count the number of valid content pages in the wiki
57 *
58 * @return mixed Integer, or false if there's a problem
59 */
60 private function count() {
61 return wfGetDB( DB_SLAVE )->selectField(
62 array( 'page', 'pagelinks' ),
63 'COUNT(DISTINCT page_id)',
64 array(
65 'pl_from=page_id',
66 'page_namespace' => MWNamespace::getContentNamespaces(),
67 'page_is_redirect' => 0,
68 'page_len > 0',
69 ),
70 __METHOD__
71 );
72 }
73 }
74
75 $maintClass = "UpdateArticleCount";
76 require_once( RUN_MAINTENANCE_IF_MAIN );