MediaWiki  1.30.2
compareParserCache.php
Go to the documentation of this file.
1 <?php
22 require_once __DIR__ . '/Maintenance.php';
23 
25 
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  [
50  'page_namespace' => $this->getOption( 'namespace' ),
51  'page_is_redirect' => 0,
52  'page_random >= ' . wfRandom()
53  ],
54  __METHOD__,
55  [
56  'ORDER BY' => 'page_random',
57  ]
58  );
59 
60  if ( !$row ) {
61  continue;
62  }
63  ++$scanned;
64 
65  $title = Title::newFromRow( $row );
66  $page = WikiPage::factory( $title );
67  $revision = $page->getRevision();
68  $content = $revision->getContent( Revision::RAW );
69 
70  $parserOptions = $page->makeParserOptions( 'canonical' );
71 
72  $parserOutputOld = $parserCache->get( $page, $parserOptions );
73 
74  if ( $parserOutputOld ) {
75  $t1 = microtime( true );
76  $parserOutputNew = $content->getParserOutput(
77  $title, $revision->getId(), $parserOptions, false );
78  $sec = microtime( true ) - $t1;
79  $totalsec += $sec;
80 
81  $this->output( "Parsed '{$title->getPrefixedText()}' in $sec seconds.\n" );
82 
83  $this->output( "Found cache entry found for '{$title->getPrefixedText()}'..." );
84  $oldHtml = trim( preg_replace( '#<!-- .+-->#Us', '', $parserOutputOld->getText() ) );
85  $newHtml = trim( preg_replace( '#<!-- .+-->#Us', '', $parserOutputNew->getText() ) );
86  $diff = wfDiff( $oldHtml, $newHtml );
87  if ( strlen( $diff ) ) {
88  $this->output( "differences found:\n\n$diff\n\n" );
89  ++$withdiff;
90  } else {
91  $this->output( "No differences found.\n" );
92  }
93  ++$withcache;
94  } else {
95  $this->output( "No parser cache entry found for '{$title->getPrefixedText()}'.\n" );
96  }
97  }
98 
99  $ave = $totalsec ? $totalsec / $scanned : 0;
100  $this->output( "Checked $scanned pages; $withcache had prior cache entries.\n" );
101  $this->output( "Pages with differences found: $withdiff\n" );
102  $this->output( "Average parse time: $ave sec\n" );
103  }
104 }
105 
106 $maintClass = "CompareParserCache";
107 require_once RUN_MAINTENANCE_IF_MAIN;
CompareParserCache
Definition: compareParserCache.php:29
CompareParserCache\__construct
__construct()
Default constructor.
Definition: compareParserCache.php:30
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
wfDiff
wfDiff( $before, $after, $params='-u')
Returns unified plain-text diff of two texts.
Definition: GlobalFunctions.php:2464
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:287
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:932
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:121
Title\newFromRow
static newFromRow( $row)
Make a Title object from a DB row.
Definition: Title.php:459
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:215
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
Revision\RAW
const RAW
Definition: Revision.php:100
CompareParserCache\execute
execute()
Do the actual work.
Definition: compareParserCache.php:37
wfRandom
wfRandom()
Get a random decimal value between 0 and 1, in a way not likely to give duplicate values for any real...
Definition: GlobalFunctions.php:352
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:250
$dbr
if(! $regexes) $dbr
Definition: cleanup.php:94
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1251
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:373
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
$maintClass
$maintClass
Definition: compareParserCache.php:106