Merge "Warn if stateful ParserOutput transforms are used"
[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 use MediaWiki\MediaWikiServices;
25
26 /**
27 * @ingroup Maintenance
28 */
29 class CompareParserCache extends Maintenance {
30 public function __construct() {
31 parent::__construct();
32 $this->addDescription( 'Parse random pages and compare output to cache.' );
33 $this->addOption( 'namespace', 'Page namespace number', true, true );
34 $this->addOption( 'maxpages', 'Number of pages to try', true, true );
35 }
36
37 public function execute() {
38 $pages = $this->getOption( 'maxpages' );
39
40 $dbr = $this->getDB( DB_REPLICA );
41
42 $totalsec = 0.0;
43 $scanned = 0;
44 $withcache = 0;
45 $withdiff = 0;
46 $parserCache = MediaWikiServices::getInstance()->getParserCache();
47 while ( $pages-- > 0 ) {
48 $row = $dbr->selectRow( 'page',
49 // @todo Title::selectFields() or Title::getQueryInfo() or something
50 [
51 'page_namespace', 'page_title', 'page_id',
52 'page_len', 'page_is_redirect', 'page_latest',
53 ],
54 [
55 'page_namespace' => $this->getOption( 'namespace' ),
56 'page_is_redirect' => 0,
57 'page_random >= ' . wfRandom()
58 ],
59 __METHOD__,
60 [
61 'ORDER BY' => 'page_random',
62 ]
63 );
64
65 if ( !$row ) {
66 continue;
67 }
68 ++$scanned;
69
70 $title = Title::newFromRow( $row );
71 $page = WikiPage::factory( $title );
72 $revision = $page->getRevision();
73 $content = $revision->getContent( Revision::RAW );
74
75 $parserOptions = $page->makeParserOptions( 'canonical' );
76
77 $parserOutputOld = $parserCache->get( $page, $parserOptions );
78
79 if ( $parserOutputOld ) {
80 $t1 = microtime( true );
81 $parserOutputNew = $content->getParserOutput(
82 $title, $revision->getId(), $parserOptions, false );
83 $sec = microtime( true ) - $t1;
84 $totalsec += $sec;
85
86 $this->output( "Parsed '{$title->getPrefixedText()}' in $sec seconds.\n" );
87
88 $this->output( "Found cache entry found for '{$title->getPrefixedText()}'..." );
89 $oldHtml = trim( preg_replace( '#<!-- .+-->#Us', '', $parserOutputOld->getText() ) );
90 $newHtml = trim( preg_replace( '#<!-- .+-->#Us', '', $parserOutputNew->getText() ) );
91 $diff = wfDiff( $oldHtml, $newHtml );
92 if ( strlen( $diff ) ) {
93 $this->output( "differences found:\n\n$diff\n\n" );
94 ++$withdiff;
95 } else {
96 $this->output( "No differences found.\n" );
97 }
98 ++$withcache;
99 } else {
100 $this->output( "No parser cache entry found for '{$title->getPrefixedText()}'.\n" );
101 }
102 }
103
104 $ave = $totalsec ? $totalsec / $scanned : 0;
105 $this->output( "Checked $scanned pages; $withcache had prior cache entries.\n" );
106 $this->output( "Pages with differences found: $withdiff\n" );
107 $this->output( "Average parse time: $ave sec\n" );
108 }
109 }
110
111 $maintClass = CompareParserCache::class;
112 require_once RUN_MAINTENANCE_IF_MAIN;