HTMLCacheUpdateJob.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * HTML cache invalidation of all pages linking to a given title.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup JobQueue
  22. * @ingroup Cache
  23. */
  24. use MediaWiki\MediaWikiServices;
  25. /**
  26. * Job to purge the cache for all pages that link to or use another page or file
  27. *
  28. * This job comes in a few variants:
  29. * - a) Recursive jobs to purge caches for backlink pages for a given title.
  30. * These jobs have (recursive:true,table:<table>) set.
  31. * - b) Jobs to purge caches for a set of titles (the job title is ignored).
  32. * These jobs have (pages:(<page ID>:(<namespace>,<title>),...) set.
  33. *
  34. * @ingroup JobQueue
  35. */
  36. class HTMLCacheUpdateJob extends Job {
  37. function __construct( Title $title, array $params ) {
  38. parent::__construct( 'htmlCacheUpdate', $title, $params );
  39. // Avoid the overhead of de-duplication when it would be pointless.
  40. // Note that these jobs always set page_touched to the current time,
  41. // so letting the older existing job "win" is still correct.
  42. $this->removeDuplicates = (
  43. // Ranges rarely will line up
  44. !isset( $params['range'] ) &&
  45. // Multiple pages per job make matches unlikely
  46. !( isset( $params['pages'] ) && count( $params['pages'] ) != 1 )
  47. );
  48. $this->params += [ 'causeAction' => 'unknown', 'causeAgent' => 'unknown' ];
  49. }
  50. /**
  51. * @param Title $title Title to purge backlink pages from
  52. * @param string $table Backlink table name
  53. * @param array $params Additional job parameters
  54. * @return HTMLCacheUpdateJob
  55. */
  56. public static function newForBacklinks( Title $title, $table, $params = [] ) {
  57. return new self(
  58. $title,
  59. [
  60. 'table' => $table,
  61. 'recursive' => true
  62. ] + Job::newRootJobParams( // "overall" refresh links job info
  63. "htmlCacheUpdate:{$table}:{$title->getPrefixedText()}"
  64. ) + $params
  65. );
  66. }
  67. function run() {
  68. global $wgUpdateRowsPerJob, $wgUpdateRowsPerQuery;
  69. if ( isset( $this->params['table'] ) && !isset( $this->params['pages'] ) ) {
  70. $this->params['recursive'] = true; // b/c; base job
  71. }
  72. // Job to purge all (or a range of) backlink pages for a page
  73. if ( !empty( $this->params['recursive'] ) ) {
  74. // Carry over information for de-duplication
  75. $extraParams = $this->getRootJobParams();
  76. // Carry over cause information for logging
  77. $extraParams['causeAction'] = $this->params['causeAction'];
  78. $extraParams['causeAgent'] = $this->params['causeAgent'];
  79. // Convert this into no more than $wgUpdateRowsPerJob HTMLCacheUpdateJob per-title
  80. // jobs and possibly a recursive HTMLCacheUpdateJob job for the rest of the backlinks
  81. $jobs = BacklinkJobUtils::partitionBacklinkJob(
  82. $this,
  83. $wgUpdateRowsPerJob,
  84. $wgUpdateRowsPerQuery, // jobs-per-title
  85. // Carry over information for de-duplication
  86. [ 'params' => $extraParams ]
  87. );
  88. JobQueueGroup::singleton()->push( $jobs );
  89. // Job to purge pages for a set of titles
  90. } elseif ( isset( $this->params['pages'] ) ) {
  91. $this->invalidateTitles( $this->params['pages'] );
  92. // Job to update a single title
  93. } else {
  94. $t = $this->title;
  95. $this->invalidateTitles( [
  96. $t->getArticleID() => [ $t->getNamespace(), $t->getDBkey() ]
  97. ] );
  98. }
  99. return true;
  100. }
  101. /**
  102. * @param array $pages Map of (page ID => (namespace, DB key)) entries
  103. */
  104. protected function invalidateTitles( array $pages ) {
  105. global $wgUpdateRowsPerQuery, $wgUseFileCache, $wgPageLanguageUseDB;
  106. // Get all page IDs in this query into an array
  107. $pageIds = array_keys( $pages );
  108. if ( !$pageIds ) {
  109. return;
  110. }
  111. // Bump page_touched to the current timestamp. This used to use the root job timestamp
  112. // (e.g. template/file edit time), which was a bit more efficient when template edits are
  113. // rare and don't effect the same pages much. However, this way allows for better
  114. // de-duplication, which is much more useful for wikis with high edit rates. Note that
  115. // RefreshLinksJob, which is enqueued alongside HTMLCacheUpdateJob, saves the parser output
  116. // since it has to parse anyway. We assume that vast majority of the cache jobs finish
  117. // before the link jobs, so using the current timestamp instead of the root timestamp is
  118. // not expected to invalidate these cache entries too often.
  119. $touchTimestamp = wfTimestampNow();
  120. // If page_touched is higher than this, then something else already bumped it after enqueue
  121. $condTimestamp = $this->params['rootJobTimestamp'] ?? $touchTimestamp;
  122. $dbw = wfGetDB( DB_MASTER );
  123. $factory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
  124. $ticket = $factory->getEmptyTransactionTicket( __METHOD__ );
  125. // Update page_touched (skipping pages already touched since the root job).
  126. // Check $wgUpdateRowsPerQuery for sanity; batch jobs are sized by that already.
  127. $batches = array_chunk( $pageIds, $wgUpdateRowsPerQuery );
  128. foreach ( $batches as $batch ) {
  129. $dbw->update( 'page',
  130. [ 'page_touched' => $dbw->timestamp( $touchTimestamp ) ],
  131. [ 'page_id' => $batch,
  132. // don't invalidated pages that were already invalidated
  133. "page_touched < " . $dbw->addQuotes( $dbw->timestamp( $condTimestamp ) )
  134. ],
  135. __METHOD__
  136. );
  137. if ( count( $batches ) > 1 ) {
  138. $factory->commitAndWaitForReplication( __METHOD__, $ticket );
  139. }
  140. }
  141. // Get the list of affected pages (races only mean something else did the purge)
  142. $titleArray = TitleArray::newFromResult( $dbw->select(
  143. 'page',
  144. array_merge(
  145. [ 'page_namespace', 'page_title' ],
  146. $wgPageLanguageUseDB ? [ 'page_lang' ] : []
  147. ),
  148. [ 'page_id' => $pageIds, 'page_touched' => $dbw->timestamp( $touchTimestamp ) ],
  149. __METHOD__
  150. ) );
  151. // Update CDN; call purge() directly so as to not bother with secondary purges
  152. $urls = [];
  153. foreach ( $titleArray as $title ) {
  154. /** @var Title $title */
  155. $urls = array_merge( $urls, $title->getCdnUrls() );
  156. }
  157. CdnCacheUpdate::purge( $urls );
  158. // Update file cache
  159. if ( $wgUseFileCache ) {
  160. foreach ( $titleArray as $title ) {
  161. HTMLFileCache::clearFileCache( $title );
  162. }
  163. }
  164. }
  165. public function getDeduplicationInfo() {
  166. $info = parent::getDeduplicationInfo();
  167. if ( is_array( $info['params'] ) ) {
  168. // For per-pages jobs, the job title is that of the template that changed
  169. // (or similar), so remove that since it ruins duplicate detection
  170. if ( isset( $info['params']['pages'] ) ) {
  171. unset( $info['namespace'] );
  172. unset( $info['title'] );
  173. }
  174. }
  175. return $info;
  176. }
  177. public function workItemCount() {
  178. if ( !empty( $this->params['recursive'] ) ) {
  179. return 0; // nothing actually purged
  180. } elseif ( isset( $this->params['pages'] ) ) {
  181. return count( $this->params['pages'] );
  182. }
  183. return 1; // one title
  184. }
  185. }