Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
WikiBirthday
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 3
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 computeAge
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Prints the birthdate and age of a wiki install.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 * @author Derick Alangi
23 * @author Daniel Kinzler
24 * @since 1.39
25 */
26
27use MediaWiki\Utils\MWTimestamp;
28
29require_once __DIR__ . '/Maintenance.php';
30
31/**
32 * @ingroup Maintenance
33 */
34class WikiBirthday extends Maintenance {
35    public function __construct() {
36        parent::__construct();
37        $this->addDescription( "Print this wiki's birthday and age" );
38    }
39
40    /**
41     * @param string $wikiCreatedAt The date the wiki was created
42     *
43     * @return DateInterval|false The wiki age
44     */
45    private function computeAge( string $wikiCreatedAt ) {
46        return date_diff(
47            date_create(),
48            date_create( $wikiCreatedAt )
49        );
50    }
51
52    public function execute() {
53        $dbr = $this->getReplicaDB();
54
55        $revId = $dbr->newSelectQueryBuilder()
56            ->table( 'revision' )
57            ->field( 'MIN(rev_id)' )
58            ->caller( __METHOD__ )
59            ->fetchField();
60
61        $archiveRevId = $dbr->newSelectQueryBuilder()
62            ->table( 'archive' )
63            ->field( 'MIN(ar_rev_id)' )
64            ->caller( __METHOD__ )
65            ->fetchField();
66
67        if ( $archiveRevId && $archiveRevId < $revId ) {
68            $timestamp = $dbr->newSelectQueryBuilder()
69                ->table( 'archive' )
70                ->field( 'ar_timestamp' )
71                ->where( [ 'ar_rev_id' => (int)$archiveRevId ] )
72                ->caller( __METHOD__ )
73                ->fetchField();
74        } else {
75            $timestamp = $dbr->newSelectQueryBuilder()
76                ->table( 'revision' )
77                ->field( 'rev_timestamp' )
78                ->where( [ 'rev_id' => (int)$revId ] )
79                ->caller( __METHOD__ )
80                ->fetchField();
81        }
82
83        $birthDay = $this->getServiceContainer()->getContentLanguage()
84            ->getHumanTimestamp( MWTimestamp::getInstance( $timestamp ) );
85
86        $text = "Wiki was created on: " . $birthDay . " <age: " .
87            $this->computeAge( $timestamp )->format(
88                "%y yr(s), %m month(s), %d day(s)"
89            ) . " old>.";
90
91        $this->output( $text . "\n" );
92    }
93}
94
95$maintClass = WikiBirthday::class;
96require_once RUN_MAINTENANCE_IF_MAIN;