Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ShowSiteStats
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * Show the cached statistics.
5 * Give out the same output as [[Special:Statistics]]
6 *
7 * @license GPL-2.0-or-later
8 * @file
9 * @ingroup Maintenance
10 * @author Antoine Musso <hashar at free dot fr>
11 * Based on initSiteStats.php by:
12 * @author Brooke Vibber
13 * @author Rob Church <robchur@gmail.com>
14 *
15 * @license GPL-2.0-or-later
16 */
17
18use MediaWiki\Maintenance\Maintenance;
19
20// @codeCoverageIgnoreStart
21require_once __DIR__ . '/Maintenance.php';
22// @codeCoverageIgnoreEnd
23
24/**
25 * Maintenance script to show the cached statistics.
26 *
27 * @ingroup Maintenance
28 */
29class ShowSiteStats extends Maintenance {
30    public function __construct() {
31        parent::__construct();
32        $this->addDescription( 'Show the cached statistics' );
33    }
34
35    public function execute() {
36        $fields = [
37            'ss_total_edits' => 'Total edits',
38            'ss_good_articles' => 'Number of articles',
39            'ss_total_pages' => 'Total pages',
40            'ss_users' => 'Number of users',
41            'ss_active_users' => 'Active users',
42            'ss_images' => 'Number of images',
43        ];
44
45        // Get cached stats from a replica DB
46        $dbr = $this->getReplicaDB();
47        $stats = $dbr->newSelectQueryBuilder()
48            ->select( '*' )
49            ->from( 'site_stats' )
50            ->caller( __METHOD__ )->fetchRow();
51
52        // Get maximum size for each column
53        $max_length_value = $max_length_desc = 0;
54        foreach ( $fields as $field => $desc ) {
55            $max_length_value = max( $max_length_value, strlen( $stats->$field ) );
56            $max_length_desc = max( $max_length_desc, strlen( $desc ) );
57        }
58
59        // Show them
60        foreach ( $fields as $field => $desc ) {
61            $this->output( sprintf(
62                "%-{$max_length_desc}s: %{$max_length_value}d\n",
63                $desc,
64                $stats->$field
65            ) );
66        }
67    }
68}
69
70// @codeCoverageIgnoreStart
71$maintClass = ShowSiteStats::class;
72require_once RUN_MAINTENANCE_IF_MAIN;
73// @codeCoverageIgnoreEnd