MediaWiki  1.34.4
PageEditStash.php
Go to the documentation of this file.
1 <?php
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;
36 use Wikimedia\ScopedCallback;
37 use WikiPage;
38 
46  private $cache;
48  private $lb;
50  private $logger;
52  private $stats;
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 
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;
70 
78  public function __construct(
81  LoggerInterface $logger,
82  StatsdDataFactoryInterface $stats,
84  ) {
85  $this->cache = $cache;
86  $this->lb = $lb;
87  $this->logger = $logger;
88  $this->stats = $stats;
89  $this->initiator = $initiator;
90  }
91 
99  public function parseAndCache( WikiPage $page, Content $content, User $user, $summary ) {
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  }
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 
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 
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 
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() - (int)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() no matter what
273  $logger->info(
274  "Cache for key '{key}' has vary-revision; post-insertion parse inevitable.",
275  $context
276  );
277  } else {
278  static $flagsMaybeReparse = [
279  // Similar to the above if we didn't guess the ID correctly
280  'vary-revision-id',
281  // Similar to the above if we didn't guess the timestamp correctly
282  'vary-revision-timestamp',
283  // Similar to the above if we didn't guess the content correctly
284  'vary-revision-sha1',
285  // Similar to the above if we didn't guess page ID correctly
286  'vary-page-id'
287  ];
288  foreach ( $flagsMaybeReparse as $flag ) {
289  if ( $editInfo->output->getFlag( $flag ) ) {
290  $logger->debug(
291  "Cache for key '{key}' has $flag; post-insertion parse possible.",
292  $context
293  );
294  }
295  }
296  }
297 
298  return $editInfo;
299  }
300 
305  private function incrStatsByContent( $subkey, Content $content ) {
306  $this->stats->increment( 'editstash.' . $subkey ); // overall for b/c
307  $this->stats->increment( 'editstash_by_model.' . $content->getModel() . '.' . $subkey );
308  }
309 
314  private function getAndWaitForStashValue( $key ) {
315  $editInfo = $this->getStashValue( $key );
316 
317  if ( !$editInfo ) {
318  $start = microtime( true );
319  // We ignore user aborts and keep parsing. Block on any prior parsing
320  // so as to use its results and make use of the time spent parsing.
321  // Skip this logic if there no master connection in case this method
322  // is called on an HTTP GET request for some reason.
323  $dbw = $this->lb->getAnyOpenConnection( $this->lb->getWriterIndex() );
324  if ( $dbw && $dbw->lock( $key, __METHOD__, 30 ) ) {
325  $editInfo = $this->getStashValue( $key );
326  $dbw->unlock( $key, __METHOD__ );
327  }
328 
329  $timeMs = 1000 * max( 0, microtime( true ) - $start );
330  $this->stats->timing( 'editstash.lock_wait_time', $timeMs );
331  }
332 
333  return $editInfo;
334  }
335 
340  public function fetchInputText( $textHash ) {
341  $textKey = $this->cache->makeKey( 'stashedit', 'text', $textHash );
342 
343  return $this->cache->get( $textKey );
344  }
345 
351  public function stashInputText( $text, $textHash ) {
352  $textKey = $this->cache->makeKey( 'stashedit', 'text', $textHash );
353 
354  return $this->cache->set(
355  $textKey,
356  $text,
357  self::MAX_CACHE_TTL,
359  );
360  }
361 
366  private function lastEditTime( User $user ) {
367  $db = $this->lb->getConnectionRef( DB_REPLICA );
368 
369  $actorQuery = ActorMigration::newMigration()->getWhere( $db, 'rc_user', $user, false );
370  $time = $db->selectField(
371  [ 'recentchanges' ] + $actorQuery['tables'],
372  'MAX(rc_timestamp)',
373  [ $actorQuery['conds'] ],
374  __METHOD__,
375  [],
376  $actorQuery['joins']
377  );
378 
379  return wfTimestampOrNull( TS_MW, $time );
380  }
381 
388  private function getContentHash( Content $content ) {
389  return sha1( implode( "\n", [
390  $content->getModel(),
391  $content->getDefaultFormat(),
392  $content->serialize( $content->getDefaultFormat() )
393  ] ) );
394  }
395 
408  private function getStashKey( Title $title, $contentHash, User $user ) {
409  return $this->cache->makeKey(
410  'stashedit-info-v1',
411  md5( $title->getPrefixedDBkey() ),
412  // Account for the edit model/text
413  $contentHash,
414  // Account for user name related variables like signatures
415  md5( $user->getId() . "\n" . $user->getName() )
416  );
417  }
418 
423  private function getStashValue( $key ) {
424  $stashInfo = $this->cache->get( $key );
425  if ( is_object( $stashInfo ) && $stashInfo->output instanceof ParserOutput ) {
426  return $stashInfo;
427  }
428 
429  return false;
430  }
431 
444  private function storeStashValue(
445  $key,
446  Content $pstContent,
447  ParserOutput $parserOutput,
448  $timestamp,
449  User $user
450  ) {
451  // If an item is renewed, mind the cache TTL determined by config and parser functions.
452  // Put an upper limit on the TTL for sanity to avoid extreme template/file staleness.
453  $age = time() - (int)wfTimestamp( TS_UNIX, $parserOutput->getCacheTime() );
454  $ttl = min( $parserOutput->getCacheExpiry() - $age, self::MAX_CACHE_TTL );
455  // Avoid extremely stale user signature timestamps (T84843)
456  if ( $parserOutput->getFlag( 'user-signature' ) ) {
457  $ttl = min( $ttl, self::MAX_SIGNATURE_TTL );
458  }
459 
460  if ( $ttl <= 0 ) {
461  return 'uncacheable'; // low TTL due to a tag, magic word, or signature?
462  }
463 
464  // Store what is actually needed and split the output into another key (T204742)
465  $stashInfo = (object)[
466  'pstContent' => $pstContent,
467  'output' => $parserOutput,
468  'timestamp' => $timestamp,
469  'edits' => $user->getEditCount()
470  ];
471 
472  $ok = $this->cache->set( $key, $stashInfo, $ttl, BagOStuff::WRITE_ALLOW_SEGMENTS );
473  if ( $ok ) {
474  // These blobs can waste slots in low cardinality memcached slabs
475  $this->pruneExcessStashedEntries( $user, $key );
476  }
477 
478  return $ok ? true : 'store_error';
479  }
480 
485  private function pruneExcessStashedEntries( User $user, $newKey ) {
486  $key = $this->cache->makeKey( 'stash-edit-recent', sha1( $user->getName() ) );
487 
488  $keyList = $this->cache->get( $key ) ?: [];
489  if ( count( $keyList ) >= self::MAX_CACHE_RECENT ) {
490  $oldestKey = array_shift( $keyList );
491  $this->cache->delete( $oldestKey, BagOStuff::WRITE_PRUNE_SEGMENTS );
492  }
493 
494  $keyList[] = $newKey;
495  $this->cache->set( $key, $keyList, 2 * self::MAX_CACHE_TTL );
496  }
497 
502  private function recentStashEntryCount( User $user ) {
503  $key = $this->cache->makeKey( 'stash-edit-recent', sha1( $user->getName() ) );
504 
505  return count( $this->cache->get( $key ) ?: [] );
506  }
507 }
MediaWiki\Storage\PageEditStash\stashInputText
stashInputText( $text, $textHash)
Definition: PageEditStash.php:351
MediaWiki\Storage\PageEditStash\fetchInputText
fetchInputText( $textHash)
Definition: PageEditStash.php:340
MediaWiki\Storage\PageEditStash\__construct
__construct(BagOStuff $cache, ILoadBalancer $lb, LoggerInterface $logger, StatsdDataFactoryInterface $stats, $initiator)
Definition: PageEditStash.php:78
CacheTime\getCacheExpiry
getCacheExpiry()
Returns the number of seconds after which this object should expire.
Definition: CacheTime.php:129
MediaWiki\Storage\PageEditStash\lastEditTime
lastEditTime(User $user)
Definition: PageEditStash.php:366
ParserOutput
Definition: ParserOutput.php:25
User\getId
getId()
Get the user's ID.
Definition: User.php:2335
User\isAnon
isAnon()
Get whether the user is anonymous.
Definition: User.php:3638
MediaWiki\Storage\PageEditStash\ERROR_PARSE
const ERROR_PARSE
Definition: PageEditStash.php:57
User\isBot
isBot()
Definition: User.php:3646
User\getEditCount
getEditCount()
Get the user's edit count.
Definition: User.php:3518
true
return true
Definition: router.php:92
MediaWiki\Storage\PageEditStash\getContentHash
getContentHash(Content $content)
Get hash of the content, factoring in model/format.
Definition: PageEditStash.php:388
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1849
MediaWiki\Storage\PageEditStash\pruneExcessStashedEntries
pruneExcessStashedEntries(User $user, $newKey)
Definition: PageEditStash.php:485
BagOStuff\WRITE_ALLOW_SEGMENTS
const WRITE_ALLOW_SEGMENTS
Definition: BagOStuff.php:84
MediaWiki\Storage\PageEditStash\incrStatsByContent
incrStatsByContent( $subkey, Content $content)
Definition: PageEditStash.php:305
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:47
MediaWiki\Storage\PageEditStash\getStashKey
getStashKey(Title $title, $contentHash, User $user)
Get the temporary prepared edit stash key for a user.
Definition: PageEditStash.php:408
MediaWiki\Storage\PageEditStash\ERROR_CACHE
const ERROR_CACHE
Definition: PageEditStash.php:58
MediaWiki\Storage\PageEditStash\parseAndCache
parseAndCache(WikiPage $page, Content $content, User $user, $summary)
Definition: PageEditStash.php:99
BagOStuff
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:63
ActorMigration
This class handles the logic for the actor table migration.
Definition: ActorMigration.php:38
MediaWiki\Storage\PageEditStash\$logger
LoggerInterface $logger
Definition: PageEditStash.php:50
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:136
User\getRequest
getRequest()
Get the WebRequest object to use with this object.
Definition: User.php:3737
MediaWiki\Storage\PageEditStash\PRESUME_FRESH_TTL_SEC
const PRESUME_FRESH_TTL_SEC
Definition: PageEditStash.php:62
MediaWiki\Storage\PageEditStash
Class for managing stashed edits used by the page updater classes.
Definition: PageEditStash.php:44
ParserOutput\getFlag
getFlag( $flag)
Definition: ParserOutput.php:989
WikiPage\getTitle
getTitle()
Get the title object of the article.
Definition: WikiPage.php:298
wfTimestampOrNull
wfTimestampOrNull( $outputtype=TS_UNIX, $ts=null)
Return a formatted timestamp, or null if input is null.
Definition: GlobalFunctions.php:1865
$title
$title
Definition: testCompression.php:34
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
MediaWiki\Storage\PageEditStash\$initiator
int $initiator
Definition: PageEditStash.php:54
BagOStuff\WRITE_PRUNE_SEGMENTS
const WRITE_PRUNE_SEGMENTS
Definition: BagOStuff.php:85
DB_MASTER
const DB_MASTER
Definition: defines.php:26
MediaWiki\Storage\PageEditStash\recentStashEntryCount
recentStashEntryCount(User $user)
Definition: PageEditStash.php:502
MediaWiki\Storage\PageEditStash\getAndWaitForStashValue
getAndWaitForStashValue( $key)
Definition: PageEditStash.php:314
MediaWiki\Storage\PageEditStash\storeStashValue
storeStashValue( $key, Content $pstContent, ParserOutput $parserOutput, $timestamp, User $user)
Build a value to store in memcached based on the PST content and parser output.
Definition: PageEditStash.php:444
$content
$content
Definition: router.php:78
MediaWiki\Storage\PageEditStash\$stats
StatsdDataFactoryInterface $stats
Definition: PageEditStash.php:52
MediaWiki\Storage\PageEditStash\getStashValue
getStashValue( $key)
Definition: PageEditStash.php:423
MediaWiki\Storage
Definition: BlobAccessException.php:23
Content
Base interface for content objects.
Definition: Content.php:34
Title
Represents a title within MediaWiki.
Definition: Title.php:42
MediaWiki\Storage\PageEditStash\INITIATOR_USER
const INITIATOR_USER
Definition: PageEditStash.php:68
MediaWiki\Storage\PageEditStash\ERROR_BUSY
const ERROR_BUSY
Definition: PageEditStash.php:60
MediaWiki\Storage\PageEditStash\ERROR_UNCACHEABLE
const ERROR_UNCACHEABLE
Definition: PageEditStash.php:59
WikiPage\prepareContentForEdit
prepareContentForEdit(Content $content, $revision=null, User $user=null, $serialFormat=null, $useCache=true)
Prepare content which is about to be saved.
Definition: WikiPage.php:1991
MediaWiki\Storage\PageEditStash\$lb
ILoadBalancer $lb
Definition: PageEditStash.php:48
MediaWiki\Storage\PageEditStash\MAX_SIGNATURE_TTL
const MAX_SIGNATURE_TTL
Definition: PageEditStash.php:64
MediaWiki\$context
IContextSource $context
Definition: MediaWiki.php:38
MediaWiki\Storage\PageEditStash\checkCache
checkCache(Title $title, Content $content, User $user)
Check that a prepared edit is in cache and still up-to-date.
Definition: PageEditStash.php:200
CacheTime\getCacheTime
getCacheTime()
Definition: CacheTime.php:60
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
MediaWiki\Storage\PageEditStash\MAX_CACHE_RECENT
const MAX_CACHE_RECENT
Definition: PageEditStash.php:66
MediaWiki\Storage\PageEditStash\INITIATOR_JOB_OR_CLI
const INITIATOR_JOB_OR_CLI
Definition: PageEditStash.php:69
MediaWiki\Storage\PageEditStash\MAX_CACHE_TTL
const MAX_CACHE_TTL
Definition: PageEditStash.php:63
User\getName
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2364
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81
Hooks
Hooks class.
Definition: Hooks.php:34
MediaWiki\Storage\PageEditStash\$cache
BagOStuff $cache
Definition: PageEditStash.php:46
MediaWiki\Storage\PageEditStash\ERROR_NONE
const ERROR_NONE
Definition: PageEditStash.php:56