MediaWiki  REL1_24
compareParserCache.php
Go to the documentation of this file.
00001 <?php
00022 require_once __DIR__ . '/Maintenance.php';
00023 
00027 class CompareParserCache extends Maintenance {
00028     public function __construct() {
00029         parent::__construct();
00030         $this->mDescription = "Parse random pages and compare output to cache.";
00031         $this->addOption( 'namespace', 'Page namespace number', true, true );
00032         $this->addOption( 'maxpages', 'Number of pages to try', true, true );
00033     }
00034 
00035     public function execute() {
00036         $pages = $this->getOption( 'maxpages' );
00037 
00038         $dbr = $this->getDB( DB_SLAVE );
00039 
00040         $totalsec = 0.0;
00041         $scanned = 0;
00042         $withcache = 0;
00043         $withdiff = 0;
00044         while ( $pages-- > 0 ) {
00045             $row = $dbr->selectRow( 'page', '*',
00046                 array(
00047                     'page_namespace' => $this->getOption( 'namespace' ),
00048                     'page_is_redirect' => 0,
00049                     'page_random >= ' . wfRandom()
00050                 ),
00051                 __METHOD__,
00052                 array(
00053                     'ORDER BY' => 'page_random',
00054                 )
00055             );
00056 
00057             if ( !$row ) {
00058                 continue;
00059             }
00060             ++$scanned;
00061 
00062             $title = Title::newFromRow( $row );
00063             $page = WikiPage::factory( $title );
00064             $revision = $page->getRevision();
00065             $content = $revision->getContent( Revision::RAW );
00066 
00067             $parserOptions = $page->makeParserOptions( 'canonical' );
00068 
00069             $parserOutputOld = ParserCache::singleton()->get( $page, $parserOptions );
00070 
00071             if ( $parserOutputOld ) {
00072                 $t1 = microtime( true );
00073                 $parserOutputNew = $content->getParserOutput(
00074                     $title, $revision->getId(), $parserOptions, false );
00075                 $sec = microtime( true ) - $t1;
00076                 $totalsec += $sec;
00077 
00078                 $this->output( "Parsed '{$title->getPrefixedText()}' in $sec seconds.\n" );
00079 
00080                 $this->output( "Found cache entry found for '{$title->getPrefixedText()}'..." );
00081                 $oldHtml = trim( preg_replace( '#<!-- .+-->#Us', '', $parserOutputOld->getText() ) );
00082                 $newHtml = trim( preg_replace( '#<!-- .+-->#Us', '', $parserOutputNew->getText() ) );
00083                 $diff = wfDiff( $oldHtml, $newHtml );
00084                 if ( strlen( $diff ) ) {
00085                     $this->output( "differences found:\n\n$diff\n\n" );
00086                     ++$withdiff;
00087                 } else {
00088                     $this->output( "No differences found.\n" );
00089                 }
00090                 ++$withcache;
00091             } else {
00092                 $this->output( "No parser cache entry found for '{$title->getPrefixedText()}'.\n" );
00093             }
00094         }
00095 
00096         $ave = $totalsec ? $totalsec / $scanned : 0;
00097         $this->output( "Checked $scanned pages; $withcache had prior cache entries.\n" );
00098         $this->output( "Pages with differences found: $withdiff\n" );
00099         $this->output( "Average parse time: $ave sec\n" );
00100     }
00101 }
00102 
00103 $maintClass = "CompareParserCache";
00104 require_once RUN_MAINTENANCE_IF_MAIN;