Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
UpdateArticleCount
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * Provide a better count of the number of articles
4 * and update the site statistics table, if desired.
5 *
6 * @license GPL-2.0-or-later
7 * @file
8 * @ingroup Maintenance
9 * @author Rob Church <robchur@gmail.com>
10 */
11
12use MediaWiki\Maintenance\Maintenance;
13use MediaWiki\SiteStats\SiteStatsInit;
14
15// @codeCoverageIgnoreStart
16require_once __DIR__ . '/Maintenance.php';
17// @codeCoverageIgnoreEnd
18
19/**
20 * Maintenance script to provide a better count of the number of articles
21 * and update the site statistics table, if desired.
22 *
23 * @ingroup Maintenance
24 */
25class UpdateArticleCount extends Maintenance {
26
27    public function __construct() {
28        parent::__construct();
29        $this->addDescription( 'Count of the number of articles and update the site statistics table' );
30        $this->addOption( 'update', 'Update the site_stats table with the new count' );
31        $this->addOption( 'use-master', 'Count using the primary database' );
32    }
33
34    public function execute() {
35        $this->output( "Counting articles..." );
36
37        if ( $this->hasOption( 'use-master' ) ) {
38            $dbr = $this->getPrimaryDB();
39        } else {
40            $dbr = $this->getDB( DB_REPLICA, 'vslow' );
41        }
42        $counter = new SiteStatsInit( $dbr );
43        $result = $counter->articles();
44
45        $this->output( "found {$result}.\n" );
46        if ( $this->hasOption( 'update' ) ) {
47            $this->output( "Updating site statistics table..." );
48            $dbw = $this->getPrimaryDB();
49            $dbw->newUpdateQueryBuilder()
50                ->update( 'site_stats' )
51                ->set( [ 'ss_good_articles' => $result ] )
52                ->where( [ 'ss_row_id' => 1 ] )
53                ->caller( __METHOD__ )
54                ->execute();
55            $this->output( "done.\n" );
56        } else {
57            $this->output( "To update the site statistics table, run the script "
58                . "with the --update option.\n" );
59        }
60    }
61}
62
63// @codeCoverageIgnoreStart
64$maintClass = UpdateArticleCount::class;
65require_once RUN_MAINTENANCE_IF_MAIN;
66// @codeCoverageIgnoreEnd