Implement extension registration from an extension.json file
[mediawiki.git] / maintenance / compareParserCache.php
blob98441b60237c4033b1a32033d7a69121b085eba7
1 <?php
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.
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.
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
18 * @file
19 * @ingroup Maintenance
22 require_once __DIR__ . '/Maintenance.php';
24 /**
25 * @ingroup Maintenance
27 class CompareParserCache extends Maintenance {
28 public function __construct() {
29 parent::__construct();
30 $this->mDescription = "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 );
35 public function execute() {
36 $pages = $this->getOption( 'maxpages' );
38 $dbr = $this->getDB( DB_SLAVE );
40 $totalsec = 0.0;
41 $scanned = 0;
42 $withcache = 0;
43 $withdiff = 0;
44 while ( $pages-- > 0 ) {
45 $row = $dbr->selectRow( 'page', '*',
46 array(
47 'page_namespace' => $this->getOption( 'namespace' ),
48 'page_is_redirect' => 0,
49 'page_random >= ' . wfRandom()
51 __METHOD__,
52 array(
53 'ORDER BY' => 'page_random',
57 if ( !$row ) {
58 continue;
60 ++$scanned;
62 $title = Title::newFromRow( $row );
63 $page = WikiPage::factory( $title );
64 $revision = $page->getRevision();
65 $content = $revision->getContent( Revision::RAW );
67 $parserOptions = $page->makeParserOptions( 'canonical' );
69 $parserOutputOld = ParserCache::singleton()->get( $page, $parserOptions );
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;
78 $this->output( "Parsed '{$title->getPrefixedText()}' in $sec seconds.\n" );
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 );
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" );
90 ++$withcache;
91 } else {
92 $this->output( "No parser cache entry found for '{$title->getPrefixedText()}'.\n" );
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" );
103 $maintClass = "CompareParserCache";
104 require_once RUN_MAINTENANCE_IF_MAIN;