AttachLatest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
C execute() 0 45 7
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 35 and the first side effect is on line 27.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Corrects wrong values in the `page_latest` field in the database.
4
 *
5
 * Copyright © 2005 Brion Vibber <[email protected]>
6
 * https://www.mediawiki.org/
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 2 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License along
19
 * with this program; if not, write to the Free Software Foundation, Inc.,
20
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
 * http://www.gnu.org/copyleft/gpl.html
22
 *
23
 * @file
24
 * @ingroup Maintenance
25
 */
26
27
require_once __DIR__ . '/Maintenance.php';
28
29
/**
30
 * Maintenance script to correct wrong values in the `page_latest` field
31
 * in the database.
32
 *
33
 * @ingroup Maintenance
34
 */
35
class AttachLatest extends Maintenance {
36
	public function __construct() {
37
		parent::__construct();
38
		$this->addOption( "fix", "Actually fix the entries, will dry run otherwise" );
39
		$this->addOption( "regenerate-all",
40
			"Regenerate the page_latest field for all records in table page" );
41
		$this->addDescription( 'Fix page_latest entries in the page table' );
42
	}
43
44
	public function execute() {
45
		$this->output( "Looking for pages with page_latest set to 0...\n" );
46
		$dbw = $this->getDB( DB_MASTER );
47
		$conds = [ 'page_latest' => 0 ];
48
		if ( $this->hasOption( 'regenerate-all' ) ) {
49
			$conds = '';
50
		}
51
		$result = $dbw->select( 'page',
52
			[ 'page_id', 'page_namespace', 'page_title' ],
53
			$conds,
0 ignored issues
show
Bug introduced by
It seems like $conds defined by array('page_latest' => 0) on line 47 can also be of type array<string,integer,{"page_latest":"integer"}>; however, Database::select() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
54
			__METHOD__ );
55
56
		$n = 0;
57
		foreach ( $result as $row ) {
58
			$pageId = intval( $row->page_id );
59
			$title = Title::makeTitle( $row->page_namespace, $row->page_title );
60
			$name = $title->getPrefixedText();
61
			$latestTime = $dbw->selectField( 'revision',
62
				'MAX(rev_timestamp)',
63
				[ 'rev_page' => $pageId ],
64
				__METHOD__ );
65
			if ( !$latestTime ) {
66
				$this->output( wfWikiID() . " $pageId [[$name]] can't find latest rev time?!\n" );
67
				continue;
68
			}
69
70
			$revision = Revision::loadFromTimestamp( $dbw, $title, $latestTime );
0 ignored issues
show
Bug introduced by
It seems like $dbw defined by $this->getDB(DB_MASTER) on line 46 can be null; however, Revision::loadFromTimestamp() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
71
			if ( is_null( $revision ) ) {
72
				$this->output( wfWikiID()
73
					. " $pageId [[$name]] latest time $latestTime, can't find revision id\n" );
74
				continue;
75
			}
76
			$id = $revision->getId();
77
			$this->output( wfWikiID() . " $pageId [[$name]] latest time $latestTime, rev id $id\n" );
78
			if ( $this->hasOption( 'fix' ) ) {
79
				$page = WikiPage::factory( $title );
80
				$page->updateRevisionOn( $dbw, $revision );
0 ignored issues
show
Bug introduced by
It seems like $dbw defined by $this->getDB(DB_MASTER) on line 46 can be null; however, WikiPage::updateRevisionOn() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
81
			}
82
			$n++;
83
		}
84
		$this->output( "Done! Processed $n pages.\n" );
85
		if ( !$this->hasOption( 'fix' ) ) {
86
			$this->output( "This was a dry run; rerun with --fix to update page_latest.\n" );
87
		}
88
	}
89
}
90
91
$maintClass = "AttachLatest";
92
require_once RUN_MAINTENANCE_IF_MAIN;
93