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->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 
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() 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  ];
286  foreach ( $flagsMaybeReparse as $flag ) {
287  if ( $editInfo->output->getFlag( $flag ) ) {
288  $logger->debug(
289  "Cache for key '{key}' has $flag; post-insertion parse possible.",
290  $context
291  );
292  }
293  }
294  }
295 
296  return $editInfo;
297  }
298 
303  private function incrStatsByContent( $subkey, Content $content ) {
304  $this->stats->increment( 'editstash.' . $subkey ); // overall for b/c
305  $this->stats->increment( 'editstash_by_model.' . $content->getModel() . '.' . $subkey );
306  }
307 
312  private function getAndWaitForStashValue( $key ) {
313  $editInfo = $this->getStashValue( $key );
314 
315  if ( !$editInfo ) {
316  $start = microtime( true );
317  // We ignore user aborts and keep parsing. Block on any prior parsing
318  // so as to use its results and make use of the time spent parsing.
319  // Skip this logic if there no master connection in case this method
320  // is called on an HTTP GET request for some reason.
321  $dbw = $this->lb->getAnyOpenConnection( $this->lb->getWriterIndex() );
322  if ( $dbw && $dbw->lock( $key, __METHOD__, 30 ) ) {
323  $editInfo = $this->getStashValue( $key );
324  $dbw->unlock( $key, __METHOD__ );
325  }
326 
327  $timeMs = 1000 * max( 0, microtime( true ) - $start );
328  $this->stats->timing( 'editstash.lock_wait_time', $timeMs );
329  }
330 
331  return $editInfo;
332  }
333 
338  public function fetchInputText( $textHash ) {
339  $textKey = $this->cache->makeKey( 'stashedit', 'text', $textHash );
340 
341  return $this->cache->get( $textKey );
342  }
343 
349  public function stashInputText( $text, $textHash ) {
350  $textKey = $this->cache->makeKey( 'stashedit', 'text', $textHash );
351 
352  return $this->cache->set(
353  $textKey,
354  $text,
355  self::MAX_CACHE_TTL,
357  );
358  }
359 
364  private function lastEditTime( User $user ) {
365  $db = $this->lb->getConnectionRef( DB_REPLICA );
366 
367  $actorQuery = ActorMigration::newMigration()->getWhere( $db, 'rc_user', $user, false );
368  $time = $db->selectField(
369  [ 'recentchanges' ] + $actorQuery['tables'],
370  'MAX(rc_timestamp)',
371  [ $actorQuery['conds'] ],
372  __METHOD__,
373  [],
374  $actorQuery['joins']
375  );
376 
377  return wfTimestampOrNull( TS_MW, $time );
378  }
379 
386  private function getContentHash( Content $content ) {
387  return sha1( implode( "\n", [
388  $content->getModel(),
389  $content->getDefaultFormat(),
390  $content->serialize( $content->getDefaultFormat() )
391  ] ) );
392  }
393 
406  private function getStashKey( Title $title, $contentHash, User $user ) {
407  return $this->cache->makeKey(
408  'stashedit-info-v1',
409  md5( $title->getPrefixedDBkey() ),
410  // Account for the edit model/text
411  $contentHash,
412  // Account for user name related variables like signatures
413  md5( $user->getId() . "\n" . $user->getName() )
414  );
415  }
416 
421  private function getStashValue( $key ) {
422  $stashInfo = $this->cache->get( $key );
423  if ( is_object( $stashInfo ) && $stashInfo->output instanceof ParserOutput ) {
424  return $stashInfo;
425  }
426 
427  return false;
428  }
429 
442  private function storeStashValue(
443  $key,
444  Content $pstContent,
445  ParserOutput $parserOutput,
446  $timestamp,
447  User $user
448  ) {
449  // If an item is renewed, mind the cache TTL determined by config and parser functions.
450  // Put an upper limit on the TTL for sanity to avoid extreme template/file staleness.
451  $age = time() - wfTimestamp( TS_UNIX, $parserOutput->getCacheTime() );
452  $ttl = min( $parserOutput->getCacheExpiry() - $age, self::MAX_CACHE_TTL );
453  // Avoid extremely stale user signature timestamps (T84843)
454  if ( $parserOutput->getFlag( 'user-signature' ) ) {
455  $ttl = min( $ttl, self::MAX_SIGNATURE_TTL );
456  }
457 
458  if ( $ttl <= 0 ) {
459  return 'uncacheable'; // low TTL due to a tag, magic word, or signature?
460  }
461 
462  // Store what is actually needed and split the output into another key (T204742)
463  $stashInfo = (object)[
464  'pstContent' => $pstContent,
465  'output' => $parserOutput,
466  'timestamp' => $timestamp,
467  'edits' => $user->getEditCount()
468  ];
469 
470  $ok = $this->cache->set( $key, $stashInfo, $ttl, BagOStuff::WRITE_ALLOW_SEGMENTS );
471  if ( $ok ) {
472  // These blobs can waste slots in low cardinality memcached slabs
473  $this->pruneExcessStashedEntries( $user, $key );
474  }
475 
476  return $ok ? true : 'store_error';
477  }
478 
483  private function pruneExcessStashedEntries( User $user, $newKey ) {
484  $key = $this->cache->makeKey( 'stash-edit-recent', sha1( $user->getName() ) );
485 
486  $keyList = $this->cache->get( $key ) ?: [];
487  if ( count( $keyList ) >= self::MAX_CACHE_RECENT ) {
488  $oldestKey = array_shift( $keyList );
489  $this->cache->delete( $oldestKey, BagOStuff::WRITE_PRUNE_SEGMENTS );
490  }
491 
492  $keyList[] = $newKey;
493  $this->cache->set( $key, $keyList, 2 * self::MAX_CACHE_TTL );
494  }
495 
500  private function recentStashEntryCount( User $user ) {
501  $key = $this->cache->makeKey( 'stash-edit-recent', sha1( $user->getName() ) );
502 
503  return count( $this->cache->get( $key ) ?: [] );
504  }
505 }
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.
const WRITE_PRUNE_SEGMENTS
Definition: BagOStuff.php:85
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:1723
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:1781
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:2251
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:51
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:1972
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:3582
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:767
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:912
isBot()
Definition: User.php:3590
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:767
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings, LocalSettings).
Definition: Setup.php:131
pruneExcessStashedEntries(User $user, $newKey)
getRequest()
Get the WebRequest object to use with this object.
Definition: User.php:3685
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
const WRITE_ALLOW_SEGMENTS
Definition: BagOStuff.php:84
Database cluster connection, tracking, load balancing, and transaction manager interface.
getId()
Get the user&#39;s ID.
Definition: User.php:2224
__construct(BagOStuff $cache, ILoadBalancer $lb, LoggerInterface $logger, StatsdDataFactoryInterface $stats, $initiator)
getEditCount()
Get the user&#39;s edit count.
Definition: User.php:3462
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:1980
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:1454
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:1711
serialize( $format=null)
Convenience method for serializing this Content object.