MediaWiki  master
PageEditStash.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\Storage;
24 
27 use Content;
28 use Hooks;
33 use Title;
34 use User;
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->getConnection( 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 
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 
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() - 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  }
284 
285  return $editInfo;
286  }
287 
292  private function incrStatsByContent( $subkey, Content $content ) {
293  $this->stats->increment( 'editstash.' . $subkey ); // overall for b/c
294  $this->stats->increment( 'editstash_by_model.' . $content->getModel() . '.' . $subkey );
295  }
296 
301  private function getAndWaitForStashValue( $key ) {
302  $editInfo = $this->getStashValue( $key );
303 
304  if ( !$editInfo ) {
305  $start = microtime( true );
306  // We ignore user aborts and keep parsing. Block on any prior parsing
307  // so as to use its results and make use of the time spent parsing.
308  // Skip this logic if there no master connection in case this method
309  // is called on an HTTP GET request for some reason.
310  $dbw = $this->lb->getAnyOpenConnection( $this->lb->getWriterIndex() );
311  if ( $dbw && $dbw->lock( $key, __METHOD__, 30 ) ) {
312  $editInfo = $this->getStashValue( $key );
313  $dbw->unlock( $key, __METHOD__ );
314  }
315 
316  $timeMs = 1000 * max( 0, microtime( true ) - $start );
317  $this->stats->timing( 'editstash.lock_wait_time', $timeMs );
318  }
319 
320  return $editInfo;
321  }
322 
327  public function fetchInputText( $textHash ) {
328  $textKey = $this->cache->makeKey( 'stashedit', 'text', $textHash );
329 
330  return $this->cache->get( $textKey );
331  }
332 
338  public function stashInputText( $text, $textHash ) {
339  $textKey = $this->cache->makeKey( 'stashedit', 'text', $textHash );
340 
341  return $this->cache->set( $textKey, $text, self::MAX_CACHE_TTL );
342  }
343 
348  private function lastEditTime( User $user ) {
349  $db = $this->lb->getConnection( DB_REPLICA );
350  $actorQuery = ActorMigration::newMigration()->getWhere( $db, 'rc_user', $user, false );
351  $time = $db->selectField(
352  [ 'recentchanges' ] + $actorQuery['tables'],
353  'MAX(rc_timestamp)',
354  [ $actorQuery['conds'] ],
355  __METHOD__,
356  [],
357  $actorQuery['joins']
358  );
359 
360  return wfTimestampOrNull( TS_MW, $time );
361  }
362 
369  private function getContentHash( Content $content ) {
370  return sha1( implode( "\n", [
371  $content->getModel(),
372  $content->getDefaultFormat(),
373  $content->serialize( $content->getDefaultFormat() )
374  ] ) );
375  }
376 
389  private function getStashKey( Title $title, $contentHash, User $user ) {
390  return $this->cache->makeKey(
391  'stashed-edit-info',
392  md5( $title->getPrefixedDBkey() ),
393  // Account for the edit model/text
394  $contentHash,
395  // Account for user name related variables like signatures
396  md5( $user->getId() . "\n" . $user->getName() )
397  );
398  }
399 
404  private function getStashParserOutputKey( $hash ) {
405  return $this->cache->makeKey( 'stashed-edit-output', $hash );
406  }
407 
412  private function getStashValue( $key ) {
413  $stashInfo = $this->cache->get( $key );
414  if ( !is_object( $stashInfo ) ) {
415  return false;
416  }
417 
418  $parserOutputKey = $this->getStashParserOutputKey( $stashInfo->outputID );
419  $parserOutput = $this->cache->get( $parserOutputKey );
420  if ( $parserOutput instanceof ParserOutput ) {
421  $stashInfo->output = $parserOutput;
422 
423  return $stashInfo;
424  }
425 
426  return false;
427  }
428 
441  private function storeStashValue(
442  $key,
443  Content $pstContent,
444  ParserOutput $parserOutput,
445  $timestamp,
446  User $user
447  ) {
448  // If an item is renewed, mind the cache TTL determined by config and parser functions.
449  // Put an upper limit on the TTL for sanity to avoid extreme template/file staleness.
450  $age = time() - wfTimestamp( TS_UNIX, $parserOutput->getCacheTime() );
451  $ttl = min( $parserOutput->getCacheExpiry() - $age, self::MAX_CACHE_TTL );
452  // Avoid extremely stale user signature timestamps (T84843)
453  if ( $parserOutput->getFlag( 'user-signature' ) ) {
454  $ttl = min( $ttl, self::MAX_SIGNATURE_TTL );
455  }
456 
457  if ( $ttl <= 0 ) {
458  return 'uncacheable'; // low TTL due to a tag, magic word, or signature?
459  }
460 
461  // Store what is actually needed and split the output into another key (T204742)
462  $parserOutputID = md5( $key );
463  $stashInfo = (object)[
464  'pstContent' => $pstContent,
465  'outputID' => $parserOutputID,
466  'timestamp' => $timestamp,
467  'edits' => $user->getEditCount()
468  ];
469 
470  $ok = $this->cache->set( $key, $stashInfo, $ttl );
471  if ( $ok ) {
472  $ok = $this->cache->set(
473  $this->getStashParserOutputKey( $parserOutputID ),
474  $parserOutput,
475  $ttl
476  );
477  }
478 
479  if ( $ok ) {
480  // These blobs can waste slots in low cardinality memcached slabs
481  $this->pruneExcessStashedEntries( $user, $key );
482  }
483 
484  return $ok ? true : 'store_error';
485  }
486 
491  private function pruneExcessStashedEntries( User $user, $newKey ) {
492  $key = $this->cache->makeKey( 'stash-edit-recent', sha1( $user->getName() ) );
493 
494  $keyList = $this->cache->get( $key ) ?: [];
495  if ( count( $keyList ) >= self::MAX_CACHE_RECENT ) {
496  $oldestKey = array_shift( $keyList );
497  $this->cache->delete( $oldestKey );
498  }
499 
500  $keyList[] = $newKey;
501  $this->cache->set( $key, $keyList, 2 * self::MAX_CACHE_TTL );
502  }
503 
508  private function recentStashEntryCount( User $user ) {
509  $key = $this->cache->makeKey( 'stash-edit-recent', sha1( $user->getName() ) );
510 
511  return count( $this->cache->get( $key ) ?: [] );
512  }
513 }
getContentHash(Content $content)
Get hash of the content, factoring in model/format.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Class for managing stashed edits used by the page updater classes.
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest object
Definition: globals.txt:25
getPrefixedText()
Get the prefixed title with spaces.
Definition: Title.php:1696
wfTimestampOrNull( $outputtype=TS_UNIX, $ts=null)
Return a formatted timestamp, or null if input is null.
incrStatsByContent( $subkey, Content $content)
see documentation in includes Linker php for Linker::makeImageLink & $time
Definition: hooks.txt:1799
const DB_MASTER
Definition: defines.php:26
storeStashValue( $key, Content $pstContent, ParserOutput $parserOutput, $timestamp, User $user)
Build a value to store in memcached based on the PST content and parser output.
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2311
parseAndCache(WikiPage $page, Content $content, User $user, $summary)
The User object encapsulates all of the user-specific settings (user_id, name, rights, email address, options, last login time).
Definition: User.php:50
you have access to all of the normal MediaWiki so you can get a DB use the cache
Definition: maintenance.txt:52
StatsdDataFactoryInterface $stats
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1982
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
getCacheExpiry()
Returns the number of seconds after which this object should expire.
Definition: CacheTime.php:129
static newMigration()
Static constructor.
IContextSource $context
Definition: MediaWiki.php:38
isAnon()
Get whether the user is anonymous.
Definition: User.php:3675
getCacheTime()
Definition: CacheTime.php:60
stashInputText( $text, $textHash)
getTitle()
Get the title object of the article.
Definition: WikiPage.php:294
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
getStashKey(Title $title, $contentHash, User $user)
Get the temporary prepared edit stash key for a user.
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
isBot()
Definition: User.php:3683
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition: hooks.txt:780
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings, LocalSettings).
Definition: Setup.php:123
pruneExcessStashedEntries(User $user, $newKey)
getRequest()
Get the WebRequest object to use with this object.
Definition: User.php:3777
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Database cluster connection, tracking, load balancing, and transaction manager interface.
getId()
Get the user&#39;s ID.
Definition: User.php:2284
__construct(BagOStuff $cache, ILoadBalancer $lb, LoggerInterface $logger, StatsdDataFactoryInterface $stats, $initiator)
getEditCount()
Get the user&#39;s edit count.
Definition: User.php:3553
checkCache(Title $title, Content $content, User $user)
Check that a prepared edit is in cache and still up-to-date.
const DB_REPLICA
Definition: defines.php:25
getModel()
Returns the ID of the content model used by this Content object.
getFlag( $flag)
$content
Definition: pageupdater.txt:72
prepareContentForEdit(Content $content, $revision=null, User $user=null, $serialFormat=null, $useCache=true)
Prepare content which is about to be saved.
Definition: WikiPage.php:1975
getDefaultFormat()
Convenience method that returns the default serialization format for the content model that this Cont...
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1473
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
getPrefixedDBkey()
Get the prefixed database key form.
Definition: Title.php:1684
serialize( $format=null)
Convenience method for serializing this Content object.