MediaWiki  master
HtmlCacheUpdater.php
Go to the documentation of this file.
1 <?php
23 
32  private $reboundDelay;
34  private $useFileCache;
36  private $cdnMaxAge;
37 
39  private $hookRunner;
40 
42  public const PURGE_NAIVE = 0;
48  public const PURGE_PRESEND = 1;
53  public const PURGE_REBOUND = 2;
54 
63  public const PURGE_INTENT_TXROUND_REFLECTED = self::PURGE_PRESEND | self::PURGE_REBOUND;
64 
73  public const PURGE_URLS_LINKSUPDATE_ONLY = 4;
74 
81  public const UNLESS_CACHE_MTIME_AFTER = 'unless-timestamp-exceeds';
82 
90  public function __construct( HookContainer $hookContainer, $reboundDelay,
92  ) {
93  $this->hookRunner = new HookRunner( $hookContainer );
94  $this->reboundDelay = $reboundDelay;
95  $this->useFileCache = $useFileCache;
96  $this->cdnMaxAge = $cdnMaxAge;
97  }
98 
104  private function fieldHasFlag( $flags, $flag ) {
105  return ( ( $flags & $flag ) === $flag );
106  }
107 
116  public function purgeUrls( $urls, $flags = self::PURGE_PRESEND, array $unless = [] ) {
117  $minFreshCacheMtime = $unless[self::UNLESS_CACHE_MTIME_AFTER] ?? null;
118  if ( $minFreshCacheMtime && time() > ( $minFreshCacheMtime + $this->cdnMaxAge ) ) {
119  return;
120  }
121 
122  $urls = is_string( $urls ) ? [ $urls ] : $urls;
123 
124  $reboundDelay = $this->fieldHasFlag( $flags, self::PURGE_REBOUND )
125  ? $this->reboundDelay
126  : 0; // no second purge
127 
128  $update = new CdnCacheUpdate( $urls, [ 'reboundDelay' => $reboundDelay ] );
129  if ( $this->fieldHasFlag( $flags, self::PURGE_PRESEND ) ) {
131  } else {
132  $update->doUpdate();
133  }
134  }
135 
147  public function purgeTitleUrls( $titles, $flags = self::PURGE_PRESEND, array $unless = [] ) {
148  $titles = $titles instanceof Title ? [ $titles ] : $titles;
149 
150  if ( $this->useFileCache ) {
151  $update = HtmlFileCacheUpdate::newFromTitles( $titles );
152  if ( $this->fieldHasFlag( $flags, self::PURGE_PRESEND ) ) {
154  } else {
155  $update->doUpdate();
156  }
157  }
158 
159  $minFreshCacheMtime = $unless[self::UNLESS_CACHE_MTIME_AFTER] ?? null;
160  if ( !$minFreshCacheMtime || time() <= ( $minFreshCacheMtime + $this->cdnMaxAge ) ) {
161  $urls = [];
162  foreach ( $titles as $title ) {
164  $urls = array_merge( $urls, $this->getUrls( $title, $flags ) );
165  }
166  $this->purgeUrls( $urls, $flags );
167  }
168  }
169 
177  public function getUrls( Title $title, int $flags = 0 ) : array {
178  // These urls are affected both by direct revisions as well,
179  // as re-rendering of the same content during a LinksUpdate.
180  $urls = [
181  $title->getInternalURL()
182  ];
183  // Language variant page views are currently not cached
184  // and thus not purged (T250511).
185 
186  // These urls are only affected by direct revisions, and do not require
187  // purging when a LinksUpdate merely rerenders the same content.
188  // This exists to avoid large amounts of redundant PURGE traffic (T250261).
189  if ( !$this->fieldHasFlag( $flags, self::PURGE_URLS_LINKSUPDATE_ONLY ) ) {
190  $urls[] = $title->getInternalURL( 'action=history' );
191 
192  // Canonical action=raw URLs for user config pages
193  if ( $title->isUserJsConfigPage() ) {
194  $urls[] = $title->getInternalURL( 'action=raw&ctype=text/javascript' );
195  } elseif ( $title->isUserJsonConfigPage() ) {
196  $urls[] = $title->getInternalURL( 'action=raw&ctype=application/json' );
197  } elseif ( $title->isUserCssConfigPage() ) {
198  $urls[] = $title->getInternalURL( 'action=raw&ctype=text/css' );
199  }
200  }
201 
202  // Extensions may add novel ways to access this content
203  $append = [];
204  $mode = $flags & self::PURGE_URLS_LINKSUPDATE_ONLY;
205  $this->hookRunner->onHtmlCacheUpdaterAppendUrls( $title, $mode, $append );
206  $urls = array_merge( $urls, $append );
207 
208  // Extensions may add novel ways to access the site overall
209  $append = [];
210  $this->hookRunner->onHtmlCacheUpdaterVaryUrls( $urls, $append );
211  $urls = array_merge( $urls, $append );
212 
213  // Legacy. TODO: Deprecate this
214  $this->hookRunner->onTitleSquidURLs( $title, $urls );
215 
216  return $urls;
217  }
218 }
HtmlCacheUpdater\purgeTitleUrls
purgeTitleUrls( $titles, $flags=self::PURGE_PRESEND, array $unless=[])
Purge the CDN/HTMLFileCache for a title or the titles yielded by an iterator.
Definition: HtmlCacheUpdater.php:147
HtmlCacheUpdater\$hookRunner
HookRunner $hookRunner
Definition: HtmlCacheUpdater.php:39
DeferredUpdates\addUpdate
static addUpdate(DeferrableUpdate $update, $stage=self::POSTSEND)
Add an update to the deferred update queue for execution at the appropriate time.
Definition: DeferredUpdates.php:106
HtmlCacheUpdater\fieldHasFlag
fieldHasFlag( $flags, $flag)
Definition: HtmlCacheUpdater.php:104
HtmlCacheUpdater\__construct
__construct(HookContainer $hookContainer, $reboundDelay, $useFileCache, $cdnMaxAge)
Definition: HtmlCacheUpdater.php:90
HtmlFileCacheUpdate\newFromTitles
static newFromTitles( $titles)
Definition: HtmlFileCacheUpdate.php:52
HtmlCacheUpdater\getUrls
getUrls(Title $title, int $flags=0)
Get a list of URLs to purge from the CDN cache when this page changes.
Definition: HtmlCacheUpdater.php:177
$title
$title
Definition: testCompression.php:38
$urls
$urls
Definition: opensearch_desc.php:82
HtmlCacheUpdater\purgeUrls
purgeUrls( $urls, $flags=self::PURGE_PRESEND, array $unless=[])
Purge the CDN for a URL or list of URLs.
Definition: HtmlCacheUpdater.php:116
HtmlCacheUpdater\$cdnMaxAge
int $cdnMaxAge
Max seconds for CDN to served cached objects without revalidation.
Definition: HtmlCacheUpdater.php:36
HtmlCacheUpdater\$reboundDelay
int $reboundDelay
Seconds between initial and rebound purges; 0 if disabled.
Definition: HtmlCacheUpdater.php:32
CdnCacheUpdate
Handles purging the appropriate CDN objects given a list of URLs or Title instances.
Definition: CdnCacheUpdate.php:28
HtmlCacheUpdater
Class to invalidate the CDN and HTMLFileCache entries associated with URLs/titles.
Definition: HtmlCacheUpdater.php:30
Title
Represents a title within MediaWiki.
Definition: Title.php:42
HtmlCacheUpdater\$useFileCache
int $useFileCache
Whether filesystem-based HTML output caching is enabled.
Definition: HtmlCacheUpdater.php:34
DeferredUpdates\PRESEND
const PRESEND
Definition: DeferredUpdates.php:84
MediaWiki\HookContainer\HookContainer
HookContainer class.
Definition: HookContainer.php:44
MediaWiki\HookContainer\HookRunner
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition: HookRunner.php:563