Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
WikiBirthday
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
3 / 3
6
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
 computeAge
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2/**
3 * Prints the birthdate and age of a wiki install.
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 * @ingroup Maintenance
8 * @author Derick Alangi
9 * @author Daniel Kinzler
10 * @since 1.39
11 */
12
13use MediaWiki\Maintenance\Maintenance;
14use MediaWiki\Utils\MWTimestamp;
15
16// @codeCoverageIgnoreStart
17require_once __DIR__ . '/Maintenance.php';
18// @codeCoverageIgnoreEnd
19
20/**
21 * @ingroup Maintenance
22 */
23class WikiBirthday extends Maintenance {
24    public function __construct() {
25        parent::__construct();
26        $this->addDescription( "Print this wiki's birthday and age" );
27    }
28
29    /**
30     * @param string $wikiCreatedAt The date the wiki was created
31     *
32     * @return DateInterval|false The wiki age
33     */
34    private function computeAge( string $wikiCreatedAt ) {
35        return date_diff(
36            date_create( MWTimestamp::now() ),
37            date_create( $wikiCreatedAt )
38        );
39    }
40
41    public function execute() {
42        $dbr = $this->getReplicaDB();
43
44        $revId = $dbr->newSelectQueryBuilder()
45            ->table( 'revision' )
46            ->field( 'MIN(rev_id)' )
47            ->caller( __METHOD__ )
48            ->fetchField();
49
50        $archiveRevId = $dbr->newSelectQueryBuilder()
51            ->table( 'archive' )
52            ->field( 'MIN(ar_rev_id)' )
53            ->caller( __METHOD__ )
54            ->fetchField();
55
56        if ( $archiveRevId && ( $archiveRevId < $revId || !$revId ) ) {
57            $timestamp = $dbr->newSelectQueryBuilder()
58                ->table( 'archive' )
59                ->field( 'ar_timestamp' )
60                ->where( [ 'ar_rev_id' => (int)$archiveRevId ] )
61                ->caller( __METHOD__ )
62                ->fetchField();
63        } else {
64            $timestamp = $dbr->newSelectQueryBuilder()
65                ->table( 'revision' )
66                ->field( 'rev_timestamp' )
67                ->where( [ 'rev_id' => (int)$revId ] )
68                ->caller( __METHOD__ )
69                ->fetchField();
70        }
71
72        $birthDay = $this->getServiceContainer()->getContentLanguage()
73            ->getHumanTimestamp( MWTimestamp::getInstance( $timestamp ) );
74
75        $text = "Wiki was created on: " . $birthDay . " <age: " .
76            $this->computeAge( $timestamp )->format(
77                "%y yr(s), %m month(s), %d day(s)"
78            ) . " old>.";
79
80        $this->output( $text . "\n" );
81    }
82}
83
84// @codeCoverageIgnoreStart
85$maintClass = WikiBirthday::class;
86require_once RUN_MAINTENANCE_IF_MAIN;
87// @codeCoverageIgnoreEnd