Merge "Re add wpScrolltop id in EditPage"
[lhc/web/wiklou.git] / maintenance / compareParserCache.php
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.
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 );
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 );
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;