MediaWiki REL1_35
HtmlCacheUpdater.php
Go to the documentation of this file.
1<?php
23
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,
91 $useFileCache, $cdnMaxAge
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 ) ) {
130 DeferredUpdates::addUpdate( $update, DeferredUpdates::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 ) ) {
153 DeferredUpdates::addUpdate( $update, DeferredUpdates::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}
Handles purging the appropriate CDN objects given a list of URLs or Title instances.
Class to invalidate the CDN and HTMLFileCache entries associated with URLs/titles.
int $useFileCache
Whether filesystem-based HTML output caching is enabled.
int $cdnMaxAge
Max seconds for CDN to served cached objects without revalidation.
getUrls(Title $title, int $flags=0)
Get a list of URLs to purge from the CDN cache when this page changes.
__construct(HookContainer $hookContainer, $reboundDelay, $useFileCache, $cdnMaxAge)
purgeUrls( $urls, $flags=self::PURGE_PRESEND, array $unless=[])
Purge the CDN for a URL or list of URLs.
purgeTitleUrls( $titles, $flags=self::PURGE_PRESEND, array $unless=[])
Purge the CDN/HTMLFileCache for a title or the titles yielded by an iterator.
fieldHasFlag( $flags, $flag)
int $reboundDelay
Seconds between initial and rebound purges; 0 if disabled.
static newFromTitles( $titles)
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Represents a title within MediaWiki.
Definition Title.php:42