MediaWiki  1.29.2
compareParserCache.php
Go to the documentation of this file.
1 <?php
22 require_once __DIR__ . '/Maintenance.php';
23 
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 );
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;
CompareParserCache
Definition: compareParserCache.php:27
CompareParserCache\__construct
__construct()
Default constructor.
Definition: compareParserCache.php:28
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
wfDiff
wfDiff( $before, $after, $params='-u')
Returns unified plain-text diff of two texts.
Definition: GlobalFunctions.php:2665
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:287
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:934
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:120
Title\newFromRow
static newFromRow( $row)
Make a Title object from a DB row.
Definition: Title.php:453
$content
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content $content
Definition: hooks.txt:1049
$page
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition: hooks.txt:2536
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
ParserCache\singleton
static singleton()
Get an instance of this object.
Definition: ParserCache.php:36
Revision\RAW
const RAW
Definition: Revision.php:100
CompareParserCache\execute
execute()
Do the actual work.
Definition: compareParserCache.php:35
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:318
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
$maintClass
$maintClass
Definition: compareParserCache.php:103