Avoid per-title page_lang DB queries HTMLCacheUpdateJob
[lhc/web/wiklou.git] / includes / jobqueue / jobs / HTMLCacheUpdateJob.php
index 2d816f9..e8edab5 100644 (file)
@@ -38,8 +38,15 @@ use MediaWiki\MediaWikiServices;
 class HTMLCacheUpdateJob extends Job {
        function __construct( Title $title, array $params ) {
                parent::__construct( 'htmlCacheUpdate', $title, $params );
-               // Base backlink purge jobs can be de-duplicated
-               $this->removeDuplicates = ( !isset( $params['range'] ) && !isset( $params['pages'] ) );
+               // Avoid the overhead of de-duplication when it would be pointless.
+               // Note that these jobs always set page_touched to the current time,
+               // so letting the older existing job "win" is still correct.
+               $this->removeDuplicates = (
+                       // Ranges rarely will line up
+                       !isset( $params['range'] ) &&
+                       // Multiple pages per job make matches unlikely
+                       !( isset( $params['pages'] ) && count( $params['pages'] ) != 1 )
+               );
        }
 
        /**
@@ -96,7 +103,7 @@ class HTMLCacheUpdateJob extends Job {
         * @param array $pages Map of (page ID => (namespace, DB key)) entries
         */
        protected function invalidateTitles( array $pages ) {
-               global $wgUpdateRowsPerQuery, $wgUseFileCache;
+               global $wgUpdateRowsPerQuery, $wgUseFileCache, $wgPageLanguageUseDB;
 
                // Get all page IDs in this query into an array
                $pageIds = array_keys( $pages );
@@ -113,6 +120,10 @@ class HTMLCacheUpdateJob extends Job {
                // before the link jobs, so using the current timestamp instead of the root timestamp is
                // not expected to invalidate these cache entries too often.
                $touchTimestamp = wfTimestampNow();
+               // If page_touched is higher than this, then something else already bumped it after enqueue
+               $condTimestamp = isset( $this->params['rootJobTimestamp'] )
+                       ? $this->params['rootJobTimestamp']
+                       : $touchTimestamp;
 
                $dbw = wfGetDB( DB_MASTER );
                $factory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
@@ -126,7 +137,7 @@ class HTMLCacheUpdateJob extends Job {
                                [ 'page_touched' => $dbw->timestamp( $touchTimestamp ) ],
                                [ 'page_id' => $batch,
                                        // don't invalidated pages that were already invalidated
-                                       "page_touched < " . $dbw->addQuotes( $dbw->timestamp( $touchTimestamp ) )
+                                       "page_touched < " . $dbw->addQuotes( $dbw->timestamp( $condTimestamp ) )
                                ],
                                __METHOD__
                        );
@@ -134,14 +145,21 @@ class HTMLCacheUpdateJob extends Job {
                // Get the list of affected pages (races only mean something else did the purge)
                $titleArray = TitleArray::newFromResult( $dbw->select(
                        'page',
-                       [ 'page_namespace', 'page_title' ],
+                       array_merge(
+                               [ 'page_namespace', 'page_title' ],
+                               $wgPageLanguageUseDB ? [ 'page_lang' ] : []
+                       ),
                        [ 'page_id' => $pageIds, 'page_touched' => $dbw->timestamp( $touchTimestamp ) ],
                        __METHOD__
                ) );
 
-               // Update CDN
-               $u = CdnCacheUpdate::newFromTitles( $titleArray );
-               $u->doUpdate();
+               // Update CDN; call purge() directly so as to not bother with secondary purges
+               $urls = [];
+               foreach ( $titleArray as $title ) {
+                       /** @var Title $title */
+                       $urls = array_merge( $urls, $title->getCdnUrls() );
+               }
+               CdnCacheUpdate::purge( $urls );
 
                // Update file cache
                if ( $wgUseFileCache ) {
@@ -152,6 +170,12 @@ class HTMLCacheUpdateJob extends Job {
        }
 
        public function workItemCount() {
-               return isset( $this->params['pages'] ) ? count( $this->params['pages'] ) : 1;
+               if ( !empty( $this->params['recursive'] ) ) {
+                       return 0; // nothing actually purged
+               } elseif ( isset( $this->params['pages'] ) ) {
+                       return count( $this->params['pages'] );
+               }
+
+               return 1; // one title
        }
 }