CompareParserCache::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
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 27 and the first side effect is on line 22.

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
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License as published by
5
 * the Free Software Foundation; either version 2 of the License, or
6
 * (at your option) any later version.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
 * GNU General Public License for more details.
12
 *
13
 * You should have received a copy of the GNU General Public License along
14
 * with this program; if not, write to the Free Software Foundation, Inc.,
15
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16
 * http://www.gnu.org/copyleft/gpl.html
17
 *
18
 * @file
19
 * @ingroup Maintenance
20
 */
21
22
require_once __DIR__ . '/Maintenance.php';
23
24
/**
25
 * @ingroup Maintenance
26
 */
27
class CompareParserCache extends Maintenance {
28
	public function __construct() {
29
		parent::__construct();
30
		$this->addDescription( 'Parse random pages and compare output to cache.' );
31
		$this->addOption( 'namespace', 'Page namespace number', true, true );
32
		$this->addOption( 'maxpages', 'Number of pages to try', true, true );
33
	}
34
35
	public function execute() {
36
		$pages = $this->getOption( 'maxpages' );
37
38
		$dbr = $this->getDB( DB_REPLICA );
39
40
		$totalsec = 0.0;
41
		$scanned = 0;
42
		$withcache = 0;
43
		$withdiff = 0;
44
		while ( $pages-- > 0 ) {
45
			$row = $dbr->selectRow( 'page', '*',
46
				[
47
					'page_namespace' => $this->getOption( 'namespace' ),
48
					'page_is_redirect' => 0,
49
					'page_random >= ' . wfRandom()
50
				],
51
				__METHOD__,
52
				[
53
					'ORDER BY' => 'page_random',
54
				]
55
			);
56
57
			if ( !$row ) {
58
				continue;
59
			}
60
			++$scanned;
61
62
			$title = Title::newFromRow( $row );
63
			$page = WikiPage::factory( $title );
64
			$revision = $page->getRevision();
65
			$content = $revision->getContent( Revision::RAW );
66
67
			$parserOptions = $page->makeParserOptions( 'canonical' );
68
69
			$parserOutputOld = ParserCache::singleton()->get( $page, $parserOptions );
0 ignored issues
show
Bug introduced by
It seems like $page defined by \WikiPage::factory($title) on line 63 can be null; however, ParserCache::get() 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...
70
71
			if ( $parserOutputOld ) {
72
				$t1 = microtime( true );
73
				$parserOutputNew = $content->getParserOutput(
74
					$title, $revision->getId(), $parserOptions, false );
75
				$sec = microtime( true ) - $t1;
76
				$totalsec += $sec;
77
78
				$this->output( "Parsed '{$title->getPrefixedText()}' in $sec seconds.\n" );
79
80
				$this->output( "Found cache entry found for '{$title->getPrefixedText()}'..." );
81
				$oldHtml = trim( preg_replace( '#<!-- .+-->#Us', '', $parserOutputOld->getText() ) );
82
				$newHtml = trim( preg_replace( '#<!-- .+-->#Us', '', $parserOutputNew->getText() ) );
83
				$diff = wfDiff( $oldHtml, $newHtml );
0 ignored issues
show
Deprecated Code introduced by
The function wfDiff() has been deprecated with message: since 1.25, use DiffEngine/UnifiedDiffFormatter directly

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
84
				if ( strlen( $diff ) ) {
85
					$this->output( "differences found:\n\n$diff\n\n" );
86
					++$withdiff;
87
				} else {
88
					$this->output( "No differences found.\n" );
89
				}
90
				++$withcache;
91
			} else {
92
				$this->output( "No parser cache entry found for '{$title->getPrefixedText()}'.\n" );
93
			}
94
		}
95
96
		$ave = $totalsec ? $totalsec / $scanned : 0;
97
		$this->output( "Checked $scanned pages; $withcache had prior cache entries.\n" );
98
		$this->output( "Pages with differences found: $withdiff\n" );
99
		$this->output( "Average parse time: $ave sec\n" );
100
	}
101
}
102
103
$maintClass = "CompareParserCache";
104
require_once RUN_MAINTENANCE_IF_MAIN;
105