Merge "objectcache: make BagOStuff::getMulti() preserve order and omit keys with...
[lhc/web/wiklou.git] / includes / Storage / PageEditStash.php
1 <?php
2 /**
3 * Predictive edit preparation system for MediaWiki page.
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 */
22
23 namespace MediaWiki\Storage;
24
25 use ActorMigration;
26 use BagOStuff;
27 use Content;
28 use Hooks;
29 use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
30 use ParserOutput;
31 use Psr\Log\LoggerInterface;
32 use stdClass;
33 use Title;
34 use User;
35 use Wikimedia\Rdbms\ILoadBalancer;
36 use Wikimedia\ScopedCallback;
37 use WikiPage;
38
39 /**
40 * Class for managing stashed edits used by the page updater classes
41 *
42 * @since 1.34
43 */
44 class PageEditStash {
45 /** @var BagOStuff */
46 private $cache;
47 /** @var ILoadBalancer */
48 private $lb;
49 /** @var LoggerInterface */
50 private $logger;
51 /** @var StatsdDataFactoryInterface */
52 private $stats;
53 /** @var int */
54 private $initiator;
55
56 const ERROR_NONE = 'stashed';
57 const ERROR_PARSE = 'error_parse';
58 const ERROR_CACHE = 'error_cache';
59 const ERROR_UNCACHEABLE = 'uncacheable';
60 const ERROR_BUSY = 'busy';
61
62 const PRESUME_FRESH_TTL_SEC = 30;
63 const MAX_CACHE_TTL = 300; // 5 minutes
64 const MAX_SIGNATURE_TTL = 60;
65
66 const MAX_CACHE_RECENT = 2;
67
68 const INITIATOR_USER = 1;
69 const INITIATOR_JOB_OR_CLI = 2;
70
71 /**
72 * @param BagOStuff $cache
73 * @param ILoadBalancer $lb
74 * @param LoggerInterface $logger
75 * @param StatsdDataFactoryInterface $stats
76 * @param int $initiator Class INITIATOR__* constant
77 */
78 public function __construct(
79 BagOStuff $cache,
80 ILoadBalancer $lb,
81 LoggerInterface $logger,
82 StatsdDataFactoryInterface $stats,
83 $initiator
84 ) {
85 $this->cache = $cache;
86 $this->lb = $lb;
87 $this->logger = $logger;
88 $this->stats = $stats;
89 $this->initiator = $initiator;
90 }
91
92 /**
93 * @param WikiPage $page
94 * @param Content $content Edit content
95 * @param User $user
96 * @param string $summary Edit summary
97 * @return string Class ERROR_* constant
98 */
99 public function parseAndCache( WikiPage $page, Content $content, User $user, $summary ) {
100 $logger = $this->logger;
101
102 $title = $page->getTitle();
103 $key = $this->getStashKey( $title, $this->getContentHash( $content ), $user );
104 $fname = __METHOD__;
105
106 // Use the master DB to allow for fast blocking locks on the "save path" where this
107 // value might actually be used to complete a page edit. If the edit submission request
108 // happens before this edit stash requests finishes, then the submission will block until
109 // the stash request finishes parsing. For the lock acquisition below, there is not much
110 // need to duplicate parsing of the same content/user/summary bundle, so try to avoid
111 // blocking at all here.
112 $dbw = $this->lb->getConnectionRef( DB_MASTER );
113 if ( !$dbw->lock( $key, $fname, 0 ) ) {
114 // De-duplicate requests on the same key
115 return self::ERROR_BUSY;
116 }
117 /** @noinspection PhpUnusedLocalVariableInspection */
118 $unlocker = new ScopedCallback( function () use ( $dbw, $key, $fname ) {
119 $dbw->unlock( $key, $fname );
120 } );
121
122 $cutoffTime = time() - self::PRESUME_FRESH_TTL_SEC;
123
124 // Reuse any freshly build matching edit stash cache
125 $editInfo = $this->getStashValue( $key );
126 if ( $editInfo && wfTimestamp( TS_UNIX, $editInfo->timestamp ) >= $cutoffTime ) {
127 $alreadyCached = true;
128 } else {
129 $format = $content->getDefaultFormat();
130 $editInfo = $page->prepareContentForEdit( $content, null, $user, $format, false );
131 $editInfo->output->setCacheTime( $editInfo->timestamp );
132 $alreadyCached = false;
133 }
134
135 $context = [ 'cachekey' => $key, 'title' => $title->getPrefixedText() ];
136
137 if ( $editInfo && $editInfo->output ) {
138 // Let extensions add ParserOutput metadata or warm other caches
139 Hooks::run( 'ParserOutputStashForEdit',
140 [ $page, $content, $editInfo->output, $summary, $user ] );
141
142 if ( $alreadyCached ) {
143 $logger->debug( "Parser output for key '{cachekey}' already cached.", $context );
144
145 return self::ERROR_NONE;
146 }
147
148 $code = $this->storeStashValue(
149 $key,
150 $editInfo->pstContent,
151 $editInfo->output,
152 $editInfo->timestamp,
153 $user
154 );
155
156 if ( $code === true ) {
157 $logger->debug( "Cached parser output for key '{cachekey}'.", $context );
158
159 return self::ERROR_NONE;
160 } elseif ( $code === 'uncacheable' ) {
161 $logger->info(
162 "Uncacheable parser output for key '{cachekey}' [{code}].",
163 $context + [ 'code' => $code ]
164 );
165
166 return self::ERROR_UNCACHEABLE;
167 } else {
168 $logger->error(
169 "Failed to cache parser output for key '{cachekey}'.",
170 $context + [ 'code' => $code ]
171 );
172
173 return self::ERROR_CACHE;
174 }
175 }
176
177 return self::ERROR_PARSE;
178 }
179
180 /**
181 * Check that a prepared edit is in cache and still up-to-date
182 *
183 * This method blocks if the prepared edit is already being rendered,
184 * waiting until rendering finishes before doing final validity checks.
185 *
186 * The cache is rejected if template or file changes are detected.
187 * Note that foreign template or file transclusions are not checked.
188 *
189 * This returns an object with the following fields:
190 * - pstContent: the Content after pre-save-transform
191 * - output: the ParserOutput instance
192 * - timestamp: the timestamp of the parse
193 * - edits: author edit count if they are logged in or NULL otherwise
194 *
195 * @param Title $title
196 * @param Content $content
197 * @param User $user User to get parser options from
198 * @return stdClass|bool Returns edit stash object or false on cache miss
199 */
200 public function checkCache( Title $title, Content $content, User $user ) {
201 if (
202 // The context is not an HTTP POST request
203 !$user->getRequest()->wasPosted() ||
204 // The context is a CLI script or a job runner HTTP POST request
205 $this->initiator !== self::INITIATOR_USER ||
206 // The editor account is a known bot
207 $user->isBot()
208 ) {
209 // Avoid wasted queries and statsd pollution
210 return false;
211 }
212
213 $logger = $this->logger;
214
215 $key = $this->getStashKey( $title, $this->getContentHash( $content ), $user );
216 $context = [
217 'key' => $key,
218 'title' => $title->getPrefixedText(),
219 'user' => $user->getName()
220 ];
221
222 $editInfo = $this->getAndWaitForStashValue( $key );
223 if ( !is_object( $editInfo ) || !$editInfo->output ) {
224 $this->incrStatsByContent( 'cache_misses.no_stash', $content );
225 if ( $this->recentStashEntryCount( $user ) > 0 ) {
226 $logger->info( "Empty cache for key '{key}' but not for user.", $context );
227 } else {
228 $logger->debug( "Empty cache for key '{key}'.", $context );
229 }
230
231 return false;
232 }
233
234 $age = time() - wfTimestamp( TS_UNIX, $editInfo->output->getCacheTime() );
235 $context['age'] = $age;
236
237 $isCacheUsable = true;
238 if ( $age <= self::PRESUME_FRESH_TTL_SEC ) {
239 // Assume nothing changed in this time
240 $this->incrStatsByContent( 'cache_hits.presumed_fresh', $content );
241 $logger->debug( "Timestamp-based cache hit for key '{key}'.", $context );
242 } elseif ( $user->isAnon() ) {
243 $lastEdit = $this->lastEditTime( $user );
244 $cacheTime = $editInfo->output->getCacheTime();
245 if ( $lastEdit < $cacheTime ) {
246 // Logged-out user made no local upload/template edits in the meantime
247 $this->incrStatsByContent( 'cache_hits.presumed_fresh', $content );
248 $logger->debug( "Edit check based cache hit for key '{key}'.", $context );
249 } else {
250 $isCacheUsable = false;
251 $this->incrStatsByContent( 'cache_misses.proven_stale', $content );
252 $logger->info( "Stale cache for key '{key}' due to outside edits.", $context );
253 }
254 } else {
255 if ( $editInfo->edits === $user->getEditCount() ) {
256 // Logged-in user made no local upload/template edits in the meantime
257 $this->incrStatsByContent( 'cache_hits.presumed_fresh', $content );
258 $logger->debug( "Edit count based cache hit for key '{key}'.", $context );
259 } else {
260 $isCacheUsable = false;
261 $this->incrStatsByContent( 'cache_misses.proven_stale', $content );
262 $logger->info( "Stale cache for key '{key}'due to outside edits.", $context );
263 }
264 }
265
266 if ( !$isCacheUsable ) {
267 return false;
268 }
269
270 if ( $editInfo->output->getFlag( 'vary-revision' ) ) {
271 // This can be used for the initial parse, e.g. for filters or doEditContent(),
272 // but a second parse will be triggered in doEditUpdates(). This is not optimal.
273 $logger->info(
274 "Cache for key '{key}' has vary_revision; post-insertion parse inevitable.",
275 $context
276 );
277 } elseif ( $editInfo->output->getFlag( 'vary-revision-id' ) ) {
278 // Similar to the above if we didn't guess the ID correctly.
279 $logger->debug(
280 "Cache for key '{key}' has vary_revision_id; post-insertion parse possible.",
281 $context
282 );
283 } elseif ( $editInfo->output->getFlag( 'vary-revision-timestamp' ) ) {
284 // Similar to the above if we didn't guess the timestamp correctly.
285 $logger->debug(
286 "Cache for key '{key}' has vary_revision_timestamp; post-insertion parse possible.",
287 $context
288 );
289 }
290
291 return $editInfo;
292 }
293
294 /**
295 * @param string $subkey
296 * @param Content $content
297 */
298 private function incrStatsByContent( $subkey, Content $content ) {
299 $this->stats->increment( 'editstash.' . $subkey ); // overall for b/c
300 $this->stats->increment( 'editstash_by_model.' . $content->getModel() . '.' . $subkey );
301 }
302
303 /**
304 * @param string $key
305 * @return bool|stdClass
306 */
307 private function getAndWaitForStashValue( $key ) {
308 $editInfo = $this->getStashValue( $key );
309
310 if ( !$editInfo ) {
311 $start = microtime( true );
312 // We ignore user aborts and keep parsing. Block on any prior parsing
313 // so as to use its results and make use of the time spent parsing.
314 // Skip this logic if there no master connection in case this method
315 // is called on an HTTP GET request for some reason.
316 $dbw = $this->lb->getAnyOpenConnection( $this->lb->getWriterIndex() );
317 if ( $dbw && $dbw->lock( $key, __METHOD__, 30 ) ) {
318 $editInfo = $this->getStashValue( $key );
319 $dbw->unlock( $key, __METHOD__ );
320 }
321
322 $timeMs = 1000 * max( 0, microtime( true ) - $start );
323 $this->stats->timing( 'editstash.lock_wait_time', $timeMs );
324 }
325
326 return $editInfo;
327 }
328
329 /**
330 * @param string $textHash
331 * @return string|bool Text or false if missing
332 */
333 public function fetchInputText( $textHash ) {
334 $textKey = $this->cache->makeKey( 'stashedit', 'text', $textHash );
335
336 return $this->cache->get( $textKey );
337 }
338
339 /**
340 * @param string $text
341 * @param string $textHash
342 * @return bool Success
343 */
344 public function stashInputText( $text, $textHash ) {
345 $textKey = $this->cache->makeKey( 'stashedit', 'text', $textHash );
346
347 return $this->cache->set(
348 $textKey,
349 $text,
350 self::MAX_CACHE_TTL,
351 BagOStuff::WRITE_ALLOW_SEGMENTS
352 );
353 }
354
355 /**
356 * @param User $user
357 * @return string|null TS_MW timestamp or null
358 */
359 private function lastEditTime( User $user ) {
360 $db = $this->lb->getConnectionRef( DB_REPLICA );
361
362 $actorQuery = ActorMigration::newMigration()->getWhere( $db, 'rc_user', $user, false );
363 $time = $db->selectField(
364 [ 'recentchanges' ] + $actorQuery['tables'],
365 'MAX(rc_timestamp)',
366 [ $actorQuery['conds'] ],
367 __METHOD__,
368 [],
369 $actorQuery['joins']
370 );
371
372 return wfTimestampOrNull( TS_MW, $time );
373 }
374
375 /**
376 * Get hash of the content, factoring in model/format
377 *
378 * @param Content $content
379 * @return string
380 */
381 private function getContentHash( Content $content ) {
382 return sha1( implode( "\n", [
383 $content->getModel(),
384 $content->getDefaultFormat(),
385 $content->serialize( $content->getDefaultFormat() )
386 ] ) );
387 }
388
389 /**
390 * Get the temporary prepared edit stash key for a user
391 *
392 * This key can be used for caching prepared edits provided:
393 * - a) The $user was used for PST options
394 * - b) The parser output was made from the PST using cannonical matching options
395 *
396 * @param Title $title
397 * @param string $contentHash Result of getContentHash()
398 * @param User $user User to get parser options from
399 * @return string
400 */
401 private function getStashKey( Title $title, $contentHash, User $user ) {
402 return $this->cache->makeKey(
403 'stashedit-info-v1',
404 md5( $title->getPrefixedDBkey() ),
405 // Account for the edit model/text
406 $contentHash,
407 // Account for user name related variables like signatures
408 md5( $user->getId() . "\n" . $user->getName() )
409 );
410 }
411
412 /**
413 * @param string $key
414 * @return stdClass|bool Object map (pstContent,output,outputID,timestamp,edits) or false
415 */
416 private function getStashValue( $key ) {
417 $stashInfo = $this->cache->get( $key );
418 if ( is_object( $stashInfo ) && $stashInfo->output instanceof ParserOutput ) {
419 return $stashInfo;
420 }
421
422 return false;
423 }
424
425 /**
426 * Build a value to store in memcached based on the PST content and parser output
427 *
428 * This makes a simple version of WikiPage::prepareContentForEdit() as stash info
429 *
430 * @param string $key
431 * @param Content $pstContent Pre-Save transformed content
432 * @param ParserOutput $parserOutput
433 * @param string $timestamp TS_MW
434 * @param User $user
435 * @return string|bool True or an error code
436 */
437 private function storeStashValue(
438 $key,
439 Content $pstContent,
440 ParserOutput $parserOutput,
441 $timestamp,
442 User $user
443 ) {
444 // If an item is renewed, mind the cache TTL determined by config and parser functions.
445 // Put an upper limit on the TTL for sanity to avoid extreme template/file staleness.
446 $age = time() - wfTimestamp( TS_UNIX, $parserOutput->getCacheTime() );
447 $ttl = min( $parserOutput->getCacheExpiry() - $age, self::MAX_CACHE_TTL );
448 // Avoid extremely stale user signature timestamps (T84843)
449 if ( $parserOutput->getFlag( 'user-signature' ) ) {
450 $ttl = min( $ttl, self::MAX_SIGNATURE_TTL );
451 }
452
453 if ( $ttl <= 0 ) {
454 return 'uncacheable'; // low TTL due to a tag, magic word, or signature?
455 }
456
457 // Store what is actually needed and split the output into another key (T204742)
458 $stashInfo = (object)[
459 'pstContent' => $pstContent,
460 'output' => $parserOutput,
461 'timestamp' => $timestamp,
462 'edits' => $user->getEditCount()
463 ];
464
465 $ok = $this->cache->set( $key, $stashInfo, $ttl, BagOStuff::WRITE_ALLOW_SEGMENTS );
466 if ( $ok ) {
467 // These blobs can waste slots in low cardinality memcached slabs
468 $this->pruneExcessStashedEntries( $user, $key );
469 }
470
471 return $ok ? true : 'store_error';
472 }
473
474 /**
475 * @param User $user
476 * @param string $newKey
477 */
478 private function pruneExcessStashedEntries( User $user, $newKey ) {
479 $key = $this->cache->makeKey( 'stash-edit-recent', sha1( $user->getName() ) );
480
481 $keyList = $this->cache->get( $key ) ?: [];
482 if ( count( $keyList ) >= self::MAX_CACHE_RECENT ) {
483 $oldestKey = array_shift( $keyList );
484 $this->cache->delete( $oldestKey, BagOStuff::WRITE_PRUNE_SEGMENTS );
485 }
486
487 $keyList[] = $newKey;
488 $this->cache->set( $key, $keyList, 2 * self::MAX_CACHE_TTL );
489 }
490
491 /**
492 * @param User $user
493 * @return int
494 */
495 private function recentStashEntryCount( User $user ) {
496 $key = $this->cache->makeKey( 'stash-edit-recent', sha1( $user->getName() ) );
497
498 return count( $this->cache->get( $key ) ?: [] );
499 }
500 }