File: HTMLCacheUpdater.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (261 lines) | stat: -rw-r--r-- 9,351 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 */

namespace MediaWiki\Cache;

use MediaWiki\Deferred\CdnCacheUpdate;
use MediaWiki\Deferred\DeferredUpdates;
use MediaWiki\Deferred\HtmlFileCacheUpdate;
use MediaWiki\HookContainer\HookContainer;
use MediaWiki\HookContainer\HookRunner;
use MediaWiki\Page\PageIdentity;
use MediaWiki\Page\PageReference;
use MediaWiki\Title\TitleFactory;
use Traversable;

/**
 * Class to invalidate the CDN and HTMLFileCache entries associated with URLs/titles
 *
 * @ingroup Cache
 * @since 1.35
 */
class HTMLCacheUpdater {
	/** @var int Seconds between initial and rebound purges; 0 if disabled */
	private $reboundDelay;
	/** @var bool Whether filesystem-based HTML output caching is enabled */
	private $useFileCache;
	/** @var int Max seconds for CDN to served cached objects without revalidation */
	private $cdnMaxAge;

	/** @var HookRunner */
	private $hookRunner;

	/** @var int Issue purge immediately and do not schedule a rebound purge */
	public const PURGE_NAIVE = 0;
	/**
	 * @var int Defer purge via PRESEND deferred updates. The pending DeferrableUpdate instances
	 *  will combined/de-duplicated into a single DeferrableUpdate instance; this lowers overhead
	 *  and improves HTTP PURGE request pipelining.
	 */
	public const PURGE_PRESEND = 1;
	/**
	 * @var int Upon purge, schedule a delayed CDN purge if rebound purges are enabled
	 *  ($wgCdnReboundPurgeDelay). Rebound purges are schedule via the job queue.
	 */
	public const PURGE_REBOUND = 2;

	/**
	 * @var int Defer purge until no LBFactory transaction round is pending and then schedule
	 *  a delayed rebound purge if rebound purges are enabled ($wgCdnReboundPurgeDelay). This is
	 *  useful for CDN purges triggered by data changes in the current or last transaction round.
	 *  Even if the origin server uses lagged replicas, the use of rebound purges corrects the
	 *  cache in cases where lag is less than the rebound delay. If the lag is anywhere near the
	 *  rebound delay, then auxiliary mechanisms should lower the cache TTL ($wgCdnMaxageLagged).
	 */
	public const PURGE_INTENT_TXROUND_REFLECTED = self::PURGE_PRESEND | self::PURGE_REBOUND;

	/**
	 * Reduce set of URLs to be purged to only those that may be affected by
	 * change propagation from LinksUpdate (e.g. after a used template was edited).
	 *
	 * Specifically, this means URLs only affected by direct revision edits,
	 * will not be purged.
	 * @var int
	 */
	public const PURGE_URLS_LINKSUPDATE_ONLY = 4;

	/**
	 * Do not bother purging cache items if the default max cache TTL implies that no objects
	 * can still be in cache from before the given timestamp.
	 *
	 * @var string
	 */
	public const UNLESS_CACHE_MTIME_AFTER = 'unless-timestamp-exceeds';

	/** @var TitleFactory */
	private $titleFactory;

	/**
	 * @param HookContainer $hookContainer
	 * @param TitleFactory $titleFactory
	 * @param int $reboundDelay $wgCdnReboundPurgeDelay
	 * @param bool $useFileCache $wgUseFileCache
	 * @param int $cdnMaxAge $wgCdnMaxAge
	 *
	 * @internal For use with MediaWikiServices->getHtmlCacheUpdater()
	 */
	public function __construct(
		HookContainer $hookContainer,
		TitleFactory $titleFactory,
		$reboundDelay,
		$useFileCache,
		$cdnMaxAge
	) {
		$this->hookRunner = new HookRunner( $hookContainer );
		$this->titleFactory = $titleFactory;
		$this->reboundDelay = $reboundDelay;
		$this->useFileCache = $useFileCache;
		$this->cdnMaxAge = $cdnMaxAge;
	}

	/**
	 * @param int $flags Bit field
	 * @param int $flag Constant to check for
	 * @return bool If $flags contains $flag
	 */
	private function fieldHasFlag( $flags, $flag ) {
		return ( ( $flags & $flag ) === $flag );
	}

	/**
	 * Purge the CDN for a URL or list of URLs
	 *
	 * @param string[]|string $urls URL or list of URLs
	 * @param int $flags Bit field of class PURGE_* constants
	 *  [Default: HTMLCacheUpdater::PURGE_PRESEND]
	 * @param mixed[] $unless Optional map of (HTMLCacheUpdater::UNLESS_* constant => value)
	 */
	public function purgeUrls( $urls, $flags = self::PURGE_PRESEND, array $unless = [] ) {
		$minFreshCacheMtime = $unless[self::UNLESS_CACHE_MTIME_AFTER] ?? null;
		if ( $minFreshCacheMtime && time() > ( $minFreshCacheMtime + $this->cdnMaxAge ) ) {
			return;
		}

		$urls = is_string( $urls ) ? [ $urls ] : $urls;

		$reboundDelay = $this->fieldHasFlag( $flags, self::PURGE_REBOUND )
			? $this->reboundDelay
			: 0; // no second purge

		$update = new CdnCacheUpdate( $urls, [ 'reboundDelay' => $reboundDelay ] );
		if ( $this->fieldHasFlag( $flags, self::PURGE_PRESEND ) ) {
			DeferredUpdates::addUpdate( $update, DeferredUpdates::PRESEND );
		} else {
			$update->doUpdate();
		}
	}

	/**
	 * Purge the CDN/HTMLFileCache for a title or the titles yielded by an iterator
	 *
	 * All cacheable canonical URLs associated with the titles will be purged from CDN.
	 * All cacheable actions associated with the titles will be purged from HTMLFileCache.
	 *
	 * @param Traversable|PageReference[]|PageReference $pages PageReference or iterator yielding
	 *        PageReference instances
	 * @param int $flags Bit field of class PURGE_* constants
	 *  [Default: HTMLCacheUpdater::PURGE_PRESEND]
	 * @param mixed[] $unless Optional map of (HTMLCacheUpdater::UNLESS_* constant => value)
	 */
	public function purgeTitleUrls( $pages, $flags = self::PURGE_PRESEND, array $unless = [] ) {
		$pages = is_iterable( $pages ) ? $pages : [ $pages ];
		$pageIdentities = [];

		foreach ( $pages as $page ) {
			// TODO: We really only need to cast to PageIdentity. We could use a LinkBatch for that.
			$title = $this->titleFactory->newFromPageReference( $page );

			if ( $title->canExist() ) {
				$pageIdentities[] = $title;
			}
		}

		if ( !$pageIdentities ) {
			return;
		}

		if ( $this->useFileCache ) {
			$update = HtmlFileCacheUpdate::newFromPages( $pageIdentities );
			if ( $this->fieldHasFlag( $flags, self::PURGE_PRESEND ) ) {
				DeferredUpdates::addUpdate( $update, DeferredUpdates::PRESEND );
			} else {
				$update->doUpdate();
			}
		}

		$minFreshCacheMtime = $unless[self::UNLESS_CACHE_MTIME_AFTER] ?? null;
		if ( !$minFreshCacheMtime || time() <= ( $minFreshCacheMtime + $this->cdnMaxAge ) ) {
			$urls = [];
			foreach ( $pageIdentities as $pi ) {
				/** @var PageIdentity $pi */
				$urls = array_merge( $urls, $this->getUrls( $pi, $flags ) );
			}
			$this->purgeUrls( $urls, $flags );
		}
	}

	/**
	 * Get a list of URLs to purge from the CDN cache when this page changes.
	 *
	 * @param PageReference $page
	 * @param int $flags Bit field of `PURGE_URLS_*` class constants (optional).
	 * @return string[] URLs
	 */
	public function getUrls( PageReference $page, int $flags = 0 ): array {
		$title = $this->titleFactory->newFromPageReference( $page );

		if ( !$title->canExist() ) {
			return [];
		}

		// These urls are affected both by direct revisions as well,
		// as re-rendering of the same content during a LinksUpdate.
		$urls = [
			$title->getInternalURL()
		];
		// Language variant page views are currently not cached
		// and thus not purged (T250511).

		// These urls are only affected by direct revisions, and do not require
		// purging when a LinksUpdate merely rerenders the same content.
		// This exists to avoid large amounts of redundant PURGE traffic (T250261).
		if ( !$this->fieldHasFlag( $flags, self::PURGE_URLS_LINKSUPDATE_ONLY ) ) {
			$urls[] = $title->getInternalURL( 'action=history' );

			// Canonical action=raw URLs for user and site config pages (T58874, T261371).
			if ( $title->isUserJsConfigPage() || $title->isSiteJsConfigPage() ) {
				$urls[] = $title->getInternalURL( 'action=raw&ctype=text/javascript' );
			} elseif ( $title->isUserJsonConfigPage() || $title->isSiteJsonConfigPage() ) {
				$urls[] = $title->getInternalURL( 'action=raw&ctype=application/json' );
			} elseif ( $title->isUserCssConfigPage() || $title->isSiteCssConfigPage() ) {
				$urls[] = $title->getInternalURL( 'action=raw&ctype=text/css' );
			}
		}

		// Extensions may add novel ways to access this content
		$append = [];
		$mode = $flags & self::PURGE_URLS_LINKSUPDATE_ONLY;
		$this->hookRunner->onHtmlCacheUpdaterAppendUrls( $title, $mode, $append );
		$urls = array_merge( $urls, $append );

		// Extensions may add novel ways to access the site overall
		$append = [];
		$this->hookRunner->onHtmlCacheUpdaterVaryUrls( $urls, $append );
		$urls = array_merge( $urls, $append );

		// Legacy. TODO: Deprecate this
		$this->hookRunner->onTitleSquidURLs( $title, $urls );

		return $urls;
	}
}

/** @deprecated class alias since 1.42 */
class_alias( HTMLCacheUpdater::class, 'HtmlCacheUpdater' );