97fd3ffcc95b6493e64504380a1e5d7b01a3e2ca
[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->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 );
33 }
34
35 public function execute() {
36 $pages = $this->getOption( 'maxpages' );
37
38 $dbr = $this->getDB( DB_SLAVE );
39
40 $totalsec = 0.0;
41 $scanned = 0;
42 $withcache = 0;
43 while ( $pages-- > 0 ) {
44 $row = $dbr->selectRow( 'page', '*',
45 array(
46 'page_namespace' => $this->getOption( 'namespace' ),
47 'page_is_redirect' => 0,
48 'page_random >= ' . wfRandom()
49 ),
50 __METHOD__,
51 array(
52 'ORDER BY' => 'page_random',
53 )
54 );
55
56 if ( !$row ) {
57 continue;
58 }
59 ++$scanned;
60
61 $title = Title::newFromRow( $row );
62 $page = WikiPage::factory( $title );
63 $revision = $page->getRevision();
64 $content = $revision->getContent( Revision::RAW );
65
66 $parserOptions = $page->makeParserOptions( 'canonical' );
67
68 $parserOutputOld = ParserCache::singleton()->get( $page, $parserOptions );
69
70 $t1 = microtime( true );
71 $parserOutputNew = $content->getParserOutput(
72 $title, $revision->getId(), $parserOptions, false );
73 $sec = microtime( true ) - $t1;
74 $totalsec += $sec;
75
76 $this->output( "Parsed '{$title->getPrefixedText()}' in $sec seconds.\n" );
77
78 if ( $parserOutputOld ) {
79 $this->output( "Found cache entry found for '{$title->getPrefixedText()}'..." );
80 $oldHtml = trim( preg_replace( '#<!-- .+-->#Us', '', $parserOutputOld->getText() ) );
81 $newHtml = trim( preg_replace( '#<!-- .+-->#Us', '',$parserOutputNew->getText() ) );
82 $diff = wfDiff( $oldHtml, $newHtml );
83 if ( strlen( $diff ) ) {
84 $this->output( "differences found:\n\n$diff\n\n" );
85 } else {
86 $this->output( "No differences found.\n" );
87 }
88 ++$withcache;
89 } else {
90 $this->output( "No parser cache entry found for '{$title->getPrefixedText()}'.\n" );
91 }
92 }
93
94 $ave = $scanned ? $totalsec / $scanned : 0;
95 $this->output( "Checked $scanned pages; $withcache had prior cache entries.\n" );
96 $this->output( "Average parse time: $ave sec\n" );
97 }
98 }
99
100 $maintClass = "CompareParserCache";
101 require_once RUN_MAINTENANCE_IF_MAIN;