3 * Predictive edit preparation system for MediaWiki page.
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.
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.
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
23 namespace MediaWiki\Storage
;
29 use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface
;
31 use Psr\Log\LoggerInterface
;
35 use Wikimedia\Rdbms\ILoadBalancer
;
36 use Wikimedia\ScopedCallback
;
40 * Class for managing stashed edits used by the page updater classes
47 /** @var ILoadBalancer */
49 /** @var LoggerInterface */
51 /** @var StatsdDataFactoryInterface */
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';
62 const PRESUME_FRESH_TTL_SEC
= 30;
63 const MAX_CACHE_TTL
= 300; // 5 minutes
64 const MAX_SIGNATURE_TTL
= 60;
66 const MAX_CACHE_RECENT
= 2;
68 const INITIATOR_USER
= 1;
69 const INITIATOR_JOB_OR_CLI
= 2;
72 * @param BagOStuff $cache
73 * @param ILoadBalancer $lb
74 * @param LoggerInterface $logger
75 * @param StatsdDataFactoryInterface $stats
76 * @param int $initiator Class INITIATOR__* constant
78 public function __construct(
81 LoggerInterface
$logger,
82 StatsdDataFactoryInterface
$stats,
85 $this->cache
= $cache;
87 $this->logger
= $logger;
88 $this->stats
= $stats;
89 $this->initiator
= $initiator;
93 * @param WikiPage $page
94 * @param Content $content Edit content
96 * @param string $summary Edit summary
97 * @return string Class ERROR_* constant
99 public function parseAndCache( WikiPage
$page, Content
$content, User
$user, $summary ) {
100 $logger = $this->logger
;
102 $title = $page->getTitle();
103 $key = $this->getStashKey( $title, $this->getContentHash( $content ), $user );
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
;
117 /** @noinspection PhpUnusedLocalVariableInspection */
118 $unlocker = new ScopedCallback( function () use ( $dbw, $key, $fname ) {
119 $dbw->unlock( $key, $fname );
122 $cutoffTime = time() - self
::PRESUME_FRESH_TTL_SEC
;
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;
129 $format = $content->getDefaultFormat();
130 $editInfo = $page->prepareContentForEdit( $content, null, $user, $format, false );
131 $editInfo->output
->setCacheTime( $editInfo->timestamp
);
132 $alreadyCached = false;
135 $context = [ 'cachekey' => $key, 'title' => $title->getPrefixedText() ];
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 ] );
142 if ( $alreadyCached ) {
143 $logger->debug( "Parser output for key '{cachekey}' already cached.", $context );
145 return self
::ERROR_NONE
;
148 $code = $this->storeStashValue(
150 $editInfo->pstContent
,
152 $editInfo->timestamp
,
156 if ( $code === true ) {
157 $logger->debug( "Cached parser output for key '{cachekey}'.", $context );
159 return self
::ERROR_NONE
;
160 } elseif ( $code === 'uncacheable' ) {
162 "Uncacheable parser output for key '{cachekey}' [{code}].",
163 $context +
[ 'code' => $code ]
166 return self
::ERROR_UNCACHEABLE
;
169 "Failed to cache parser output for key '{cachekey}'.",
170 $context +
[ 'code' => $code ]
173 return self
::ERROR_CACHE
;
177 return self
::ERROR_PARSE
;
181 * Check that a prepared edit is in cache and still up-to-date
183 * This method blocks if the prepared edit is already being rendered,
184 * waiting until rendering finishes before doing final validity checks.
186 * The cache is rejected if template or file changes are detected.
187 * Note that foreign template or file transclusions are not checked.
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
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
200 public function checkCache( Title
$title, Content
$content, User
$user ) {
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
209 // Avoid wasted queries and statsd pollution
213 $logger = $this->logger
;
215 $key = $this->getStashKey( $title, $this->getContentHash( $content ), $user );
218 'title' => $title->getPrefixedText(),
219 'user' => $user->getName()
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 );
228 $logger->debug( "Empty cache for key '{key}'.", $context );
234 $age = time() - wfTimestamp( TS_UNIX
, $editInfo->output
->getCacheTime() );
235 $context['age'] = $age;
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 );
250 $isCacheUsable = false;
251 $this->incrStatsByContent( 'cache_misses.proven_stale', $content );
252 $logger->info( "Stale cache for key '{key}' due to outside edits.", $context );
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 );
260 $isCacheUsable = false;
261 $this->incrStatsByContent( 'cache_misses.proven_stale', $content );
262 $logger->info( "Stale cache for key '{key}'due to outside edits.", $context );
266 if ( !$isCacheUsable ) {
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
274 "Cache for key '{key}' has vary-revision; post-insertion parse inevitable.",
278 static $flagsMaybeReparse = [
279 // Similar to the above if we didn't guess the ID correctly
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
288 foreach ( $flagsMaybeReparse as $flag ) {
289 if ( $editInfo->output
->getFlag( $flag ) ) {
291 "Cache for key '{key}' has $flag; post-insertion parse possible.",
302 * @param string $subkey
303 * @param Content $content
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 );
312 * @return bool|stdClass
314 private function getAndWaitForStashValue( $key ) {
315 $editInfo = $this->getStashValue( $key );
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__
);
329 $timeMs = 1000 * max( 0, microtime( true ) - $start );
330 $this->stats
->timing( 'editstash.lock_wait_time', $timeMs );
337 * @param string $textHash
338 * @return string|bool Text or false if missing
340 public function fetchInputText( $textHash ) {
341 $textKey = $this->cache
->makeKey( 'stashedit', 'text', $textHash );
343 return $this->cache
->get( $textKey );
347 * @param string $text
348 * @param string $textHash
349 * @return bool Success
351 public function stashInputText( $text, $textHash ) {
352 $textKey = $this->cache
->makeKey( 'stashedit', 'text', $textHash );
354 return $this->cache
->set(
358 BagOStuff
::WRITE_ALLOW_SEGMENTS
364 * @return string|null TS_MW timestamp or null
366 private function lastEditTime( User
$user ) {
367 $db = $this->lb
->getConnectionRef( DB_REPLICA
);
369 $actorQuery = ActorMigration
::newMigration()->getWhere( $db, 'rc_user', $user, false );
370 $time = $db->selectField(
371 [ 'recentchanges' ] +
$actorQuery['tables'],
373 [ $actorQuery['conds'] ],
379 return wfTimestampOrNull( TS_MW
, $time );
383 * Get hash of the content, factoring in model/format
385 * @param Content $content
388 private function getContentHash( Content
$content ) {
389 return sha1( implode( "\n", [
390 $content->getModel(),
391 $content->getDefaultFormat(),
392 $content->serialize( $content->getDefaultFormat() )
397 * Get the temporary prepared edit stash key for a user
399 * This key can be used for caching prepared edits provided:
400 * - a) The $user was used for PST options
401 * - b) The parser output was made from the PST using cannonical matching options
403 * @param Title $title
404 * @param string $contentHash Result of getContentHash()
405 * @param User $user User to get parser options from
408 private function getStashKey( Title
$title, $contentHash, User
$user ) {
409 return $this->cache
->makeKey(
411 md5( $title->getPrefixedDBkey() ),
412 // Account for the edit model/text
414 // Account for user name related variables like signatures
415 md5( $user->getId() . "\n" . $user->getName() )
421 * @return stdClass|bool Object map (pstContent,output,outputID,timestamp,edits) or false
423 private function getStashValue( $key ) {
424 $stashInfo = $this->cache
->get( $key );
425 if ( is_object( $stashInfo ) && $stashInfo->output
instanceof ParserOutput
) {
433 * Build a value to store in memcached based on the PST content and parser output
435 * This makes a simple version of WikiPage::prepareContentForEdit() as stash info
438 * @param Content $pstContent Pre-Save transformed content
439 * @param ParserOutput $parserOutput
440 * @param string $timestamp TS_MW
442 * @return string|bool True or an error code
444 private function storeStashValue(
447 ParserOutput
$parserOutput,
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() - 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
);
461 return 'uncacheable'; // low TTL due to a tag, magic word, or signature?
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()
472 $ok = $this->cache
->set( $key, $stashInfo, $ttl, BagOStuff
::WRITE_ALLOW_SEGMENTS
);
474 // These blobs can waste slots in low cardinality memcached slabs
475 $this->pruneExcessStashedEntries( $user, $key );
478 return $ok ?
true : 'store_error';
483 * @param string $newKey
485 private function pruneExcessStashedEntries( User
$user, $newKey ) {
486 $key = $this->cache
->makeKey( 'stash-edit-recent', sha1( $user->getName() ) );
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
);
494 $keyList[] = $newKey;
495 $this->cache
->set( $key, $keyList, 2 * self
::MAX_CACHE_TTL
);
502 private function recentStashEntryCount( User
$user ) {
503 $key = $this->cache
->makeKey( 'stash-edit-recent', sha1( $user->getName() ) );
505 return count( $this->cache
->get( $key ) ?
: [] );