25require_once __DIR__ .
'/../Maintenance.php';
41 private $templateTimestamp =
null;
43 private $clearLinkCache =
false;
51 private $idCache = [];
54 parent::__construct();
56 $this->
addArg(
'title',
'The name of the page to parse' );
57 $this->
addOption(
'warmup',
'Repeat the parse operation this number of times to warm the cache',
59 $this->
addOption(
'loops',
'Number of times to repeat parse operation post-warmup',
62 'Use the version of the page which was current at the given time',
65 'Use templates which were current at the given time (except that moves and ' .
66 'deletes are not handled properly)',
68 $this->
addOption(
'reset-linkcache',
'Reset the LinkCache after every parse.',
75 Hooks::register(
'BeforeParserFetchTemplateRevisionRecord', [ $this,
'onFetchTemplate' ] );
78 $this->clearLinkCache = $this->
hasOption(
'reset-linkcache' );
80 $this->linkCache = MediaWikiServices::getInstance()->getLinkCache();
87 $revLookup = MediaWikiServices::getInstance()->getRevisionLookup();
90 $id = $this->getRevIdForTime(
$title, $pageTimestamp );
92 $this->
fatalError(
"The page did not exist at that time" );
95 $revision = $revLookup->getRevisionById( (
int)$id );
97 $revision = $revLookup->getRevisionByTitle(
$title );
101 $this->
fatalError(
"Unable to load revision, incorrect title?" );
104 $warmup = $this->
getOption(
'warmup', 1 );
105 for ( $i = 0; $i < $warmup; $i++ ) {
106 $this->runParser( $revision );
111 $this->
fatalError(
'Invalid number of loops specified' );
113 $startUsage = getrusage();
114 $startTime = microtime(
true );
115 for ( $i = 0; $i < $loops; $i++ ) {
116 $this->runParser( $revision );
118 $endUsage = getrusage();
119 $endTime = microtime(
true );
121 printf(
"CPU time = %.3f s, wall clock time = %.3f s\n",
123 ( $endUsage[
'ru_utime.tv_sec'] + $endUsage[
'ru_utime.tv_usec'] * 1e-6
124 - $startUsage[
'ru_utime.tv_sec'] - $startUsage[
'ru_utime.tv_usec'] * 1e-6 ) / $loops,
126 ( $endTime - $startTime ) / $loops
137 private function getRevIdForTime(
Title $title, $timestamp ) {
140 $id =
$dbr->selectField(
141 [
'revision',
'page' ],
144 'page_namespace' =>
$title->getNamespace(),
145 'page_title' =>
$title->getDBkey(),
146 'rev_timestamp <= ' .
$dbr->addQuotes( $timestamp )
149 [
'ORDER BY' =>
'rev_timestamp DESC' ],
150 [
'revision' => [
'JOIN',
'rev_page=page_id' ] ]
163 $contentRenderer = MediaWikiServices::getInstance()->getContentRenderer();
166 if ( $this->clearLinkCache ) {
167 $this->linkCache->clear();
181 private function onFetchTemplate(
187 $title =
Title::castFromLinkTarget( $titleTarget );
190 if ( !isset( $this->idCache[$pdbk] ) ) {
191 $proposedId = $this->getRevIdForTime(
$title, $this->templateTimestamp );
192 $this->idCache[$pdbk] = $proposedId;
194 if ( $this->idCache[$pdbk] !==
false ) {
195 $revLookup = MediaWikiServices::getInstance()->getRevisionLookup();
196 $revRecord = $revLookup->getRevisionById( $this->idCache[$pdbk] );
204require_once RUN_MAINTENANCE_IF_MAIN;
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Maintenance script to benchmark how long it takes to parse a given title at an optionally specified t...
__construct()
Default constructor.
execute()
Do the actual work.
Cache for article titles (prefixed DB keys) and ids linked from one source.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
hasOption( $name)
Checks to see if a particular option was set.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.