Merge "Fix comment typo in MimeMagic.php"
[lhc/web/wiklou.git] / maintenance / benchmarks / benchmarkParse.php
index b850e63..1753250 100644 (file)
@@ -24,6 +24,8 @@
 
 require __DIR__ . '/../Maintenance.php';
 
+use MediaWiki\MediaWikiServices;
+
 /**
  * Maintenance script to benchmark how long it takes to parse a given title at an optionally
  * specified timestamp
@@ -34,8 +36,15 @@ class BenchmarkParse extends Maintenance {
        /** @var string MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS) */
        private $templateTimestamp = null;
 
+       private $clearLinkCache = false;
+
+       /**
+        * @var LinkCache
+        */
+       private $linkCache;
+
        /** @var array Cache that maps a Title DB key to revision ID for the requested timestamp */
-       private $idCache = array();
+       private $idCache = [];
 
        function __construct() {
                parent::__construct();
@@ -52,14 +61,20 @@ class BenchmarkParse extends Maintenance {
                        'Use templates which were current at the given time (except that moves and ' .
                        'deletes are not handled properly)',
                        false, true );
+               $this->addOption( 'reset-linkcache', 'Reset the LinkCache after every parse.',
+                       false, false );
        }
 
        function execute() {
                if ( $this->hasOption( 'tpl-time' ) ) {
                        $this->templateTimestamp = wfTimestamp( TS_MW, strtotime( $this->getOption( 'tpl-time' ) ) );
-                       Hooks::register( 'BeforeParserFetchTemplateAndtitle', array( $this, 'onFetchTemplate' ) );
+                       Hooks::register( 'BeforeParserFetchTemplateAndtitle', [ $this, 'onFetchTemplate' ] );
                }
 
+               $this->clearLinkCache = $this->hasOption( 'reset-linkcache' );
+               // Set as a member variable to avoid function calls when we're timing the parse
+               $this->linkCache = MediaWikiServices::getInstance()->getLinkCache();
+
                $title = Title::newFromText( $this->getArg() );
                if ( !$title ) {
                        $this->error( "Invalid title" );
@@ -118,19 +133,19 @@ class BenchmarkParse extends Maintenance {
         * @return bool|string Revision ID, or false if not found or error
         */
        function getRevIdForTime( Title $title, $timestamp ) {
-               $dbr = $this->getDB( DB_SLAVE );
+               $dbr = $this->getDB( DB_REPLICA );
 
                $id = $dbr->selectField(
-                       array( 'revision', 'page' ),
+                       [ 'revision', 'page' ],
                        'rev_id',
-                       array(
+                       [
                                'page_namespace' => $title->getNamespace(),
                                'page_title' => $title->getDBkey(),
                                'rev_timestamp <= ' . $dbr->addQuotes( $timestamp )
-                       ),
+                       ],
                        __METHOD__,
-                       array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 1 ),
-                       array( 'revision' => array( 'INNER JOIN', 'rev_page=page_id' ) )
+                       [ 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 1 ],
+                       [ 'revision' => [ 'INNER JOIN', 'rev_page=page_id' ] ]
                );
 
                return $id;
@@ -144,6 +159,9 @@ class BenchmarkParse extends Maintenance {
        function runParser( Revision $revision ) {
                $content = $revision->getContent();
                $content->getParserOutput( $revision->getTitle(), $revision->getId() );
+               if ( $this->clearLinkCache ) {
+                       $this->linkCache->clear();
+               }
        }
 
        /**