3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
19 * @author Aaron Schulz
22 use MediaWiki\Logger\LoggerFactory
;
25 * Prepare an edit in shared cache so that it can be reused on edit
27 * This endpoint can be called via AJAX as the user focuses on the edit
28 * summary box. By the time of submission, the parse may have already
29 * finished, and can be immediately used on page save. Certain parser
30 * functions like {{REVISIONID}} or {{CURRENTTIME}} may cause the cache
31 * to not be used on edit. Template and files used are check for changes
32 * since the output was generated. The cache TTL is also kept low for sanity.
37 class ApiStashEdit
extends ApiBase
{
38 const ERROR_NONE
= 'stashed';
39 const ERROR_PARSE
= 'error_parse';
40 const ERROR_CACHE
= 'error_cache';
41 const ERROR_UNCACHEABLE
= 'uncacheable';
43 const PRESUME_FRESH_TTL_SEC
= 30;
44 const MAX_CACHE_TTL
= 300; // 5 minutes
46 public function execute() {
47 $user = $this->getUser();
48 $params = $this->extractRequestParams();
50 if ( $user->isBot() ) { // sanity
51 $this->dieUsage( 'This interface is not supported for bots', 'botsnotsupported' );
54 $page = $this->getTitleOrPageId( $params );
55 $title = $page->getTitle();
57 if ( !ContentHandler
::getForModelID( $params['contentmodel'] )
58 ->isSupportedFormat( $params['contentformat'] )
60 $this->dieUsage( 'Unsupported content model/format', 'badmodelformat' );
63 // Trim and fix newlines so the key SHA1's match (see RequestContext::getText())
64 $text = rtrim( str_replace( "\r\n", "\n", $params['text'] ) );
65 $textContent = ContentHandler
::makeContent(
66 $text, $title, $params['contentmodel'], $params['contentformat'] );
68 $page = WikiPage
::factory( $title );
69 if ( $page->exists() ) {
70 // Page exists: get the merged content with the proposed change
71 $baseRev = Revision
::newFromPageId( $page->getId(), $params['baserevid'] );
73 $this->dieUsage( "No revision ID {$params['baserevid']}", 'missingrev' );
75 $currentRev = $page->getRevision();
77 $this->dieUsage( "No current revision of page ID {$page->getId()}", 'missingrev' );
79 // Merge in the new version of the section to get the proposed version
80 $editContent = $page->replaceSectionAtRev(
83 $params['sectiontitle'],
86 if ( !$editContent ) {
87 $this->dieUsage( 'Could not merge updated section.', 'replacefailed' );
89 if ( $currentRev->getId() == $baseRev->getId() ) {
90 // Base revision was still the latest; nothing to merge
91 $content = $editContent;
93 // Merge the edit into the current version
94 $baseContent = $baseRev->getContent();
95 $currentContent = $currentRev->getContent();
96 if ( !$baseContent ||
!$currentContent ) {
97 $this->dieUsage( "Missing content for page ID {$page->getId()}", 'missingrev' );
99 $handler = ContentHandler
::getForModelID( $baseContent->getModel() );
100 $content = $handler->merge3( $baseContent, $editContent, $currentContent );
103 // New pages: use the user-provided content model
104 $content = $textContent;
107 if ( !$content ) { // merge3() failed
108 $this->getResult()->addValue( null,
109 $this->getModuleName(), [ 'status' => 'editconflict' ] );
113 // The user will abort the AJAX request by pressing "save", so ignore that
114 ignore_user_abort( true );
116 // Use the master DB for fast blocking locks
117 $dbw = wfGetDB( DB_MASTER
);
119 // Get a key based on the source text, format, and user preferences
120 $key = self
::getStashKey( $title, $content, $user );
121 // De-duplicate requests on the same key
122 if ( $user->pingLimiter( 'stashedit' ) ) {
123 $status = 'ratelimited';
124 } elseif ( $dbw->lock( $key, __METHOD__
, 1 ) ) {
125 $status = self
::parseAndStash( $page, $content, $user );
126 $dbw->unlock( $key, __METHOD__
);
131 $this->getStats()->increment( "editstash.cache_stores.$status" );
133 $this->getResult()->addValue( null, $this->getModuleName(), [ 'status' => $status ] );
137 * @param WikiPage $page
138 * @param Content $content
140 * @return integer ApiStashEdit::ERROR_* constant
143 public static function parseAndStash( WikiPage
$page, Content
$content, User
$user ) {
144 $cache = ObjectCache
::getLocalClusterInstance();
145 $logger = LoggerFactory
::getInstance( 'StashEdit' );
147 $format = $content->getDefaultFormat();
148 $editInfo = $page->prepareContentForEdit( $content, null, $user, $format, false );
149 $title = $page->getTitle();
151 if ( $editInfo && $editInfo->output
) {
152 $key = self
::getStashKey( $title, $content, $user );
154 // Let extensions add ParserOutput metadata or warm other caches
155 Hooks
::run( 'ParserOutputStashForEdit', [ $page, $content, $editInfo->output
] );
157 list( $stashInfo, $ttl, $code ) = self
::buildStashValue(
158 $editInfo->pstContent
,
160 $editInfo->timestamp
,
165 $ok = $cache->set( $key, $stashInfo, $ttl );
167 $logger->debug( "Cached parser output for key '$key' ('$title')." );
168 return self
::ERROR_NONE
;
170 $logger->error( "Failed to cache parser output for key '$key' ('$title')." );
171 return self
::ERROR_CACHE
;
174 $logger->info( "Uncacheable parser output for key '$key' ('$title') [$code]." );
175 return self
::ERROR_UNCACHEABLE
;
179 return self
::ERROR_PARSE
;
183 * Attempt to cache PST content and corresponding parser output in passing
185 * This method can be called when the output was already generated for other
186 * reasons. Parsing should not be done just to call this method, however.
187 * $pstOpts must be that of the user doing the edit preview. If $pOpts does
188 * not match the options of WikiPage::makeParserOptions( 'canonical' ), this
189 * will do nothing. Provided the values are cacheable, they will be stored
190 * in memcached so that final edit submission might make use of them.
192 * @param Page|Article|WikiPage $page Page title
193 * @param Content $content Proposed page content
194 * @param Content $pstContent The result of preSaveTransform() on $content
195 * @param ParserOutput $pOut The result of getParserOutput() on $pstContent
196 * @param ParserOptions $pstOpts Options for $pstContent (MUST be for prospective author)
197 * @param ParserOptions $pOpts Options for $pOut
198 * @param string $timestamp TS_MW timestamp of parser output generation
199 * @return bool Success
201 public static function stashEditFromPreview(
202 Page
$page, Content
$content, Content
$pstContent, ParserOutput
$pOut,
203 ParserOptions
$pstOpts, ParserOptions
$pOpts, $timestamp
205 $cache = ObjectCache
::getLocalClusterInstance();
206 $logger = LoggerFactory
::getInstance( 'StashEdit' );
208 // getIsPreview() controls parser function behavior that references things
209 // like user/revision that don't exists yet. The user/text should already
210 // be set correctly by callers, just double check the preview flag.
211 if ( !$pOpts->getIsPreview() ) {
212 return false; // sanity
213 } elseif ( $pOpts->getIsSectionPreview() ) {
214 return false; // short-circuit (need the full content)
217 // PST parser options are for the user (handles signatures, etc...)
218 $user = $pstOpts->getUser();
219 // Get a key based on the source text, format, and user preferences
220 $title = $page->getTitle();
221 $key = self
::getStashKey( $title, $content, $user );
223 // Parser output options must match cannonical options.
224 // Treat some options as matching that are different but don't matter.
225 $canonicalPOpts = $page->makeParserOptions( 'canonical' );
226 $canonicalPOpts->setIsPreview( true ); // force match
227 $canonicalPOpts->setTimestamp( $pOpts->getTimestamp() ); // force match
228 if ( !$pOpts->matches( $canonicalPOpts ) ) {
229 $logger->info( "Uncacheable preview output for key '$key' ('$title') [options]." );
233 // Set the time the output was generated
234 $pOut->setCacheTime( wfTimestampNow() );
236 // Build a value to cache with a proper TTL
237 list( $stashInfo, $ttl ) = self
::buildStashValue( $pstContent, $pOut, $timestamp, $user );
239 $logger->info( "Uncacheable parser output for key '$key' ('$title') [rev/TTL]." );
243 $ok = $cache->set( $key, $stashInfo, $ttl );
245 $logger->error( "Failed to cache preview parser output for key '$key' ('$title')." );
247 $logger->debug( "Cached preview output for key '$key'." );
254 * Check that a prepared edit is in cache and still up-to-date
256 * This method blocks if the prepared edit is already being rendered,
257 * waiting until rendering finishes before doing final validity checks.
259 * The cache is rejected if template or file changes are detected.
260 * Note that foreign template or file transclusions are not checked.
262 * The result is a map (pstContent,output,timestamp) with fields
263 * extracted directly from WikiPage::prepareContentForEdit().
265 * @param Title $title
266 * @param Content $content
267 * @param User $user User to get parser options from
268 * @return stdClass|bool Returns false on cache miss
270 public static function checkCache( Title
$title, Content
$content, User
$user ) {
271 if ( $user->isBot() ) {
272 return false; // bots never stash - don't pollute stats
275 $cache = ObjectCache
::getLocalClusterInstance();
276 $logger = LoggerFactory
::getInstance( 'StashEdit' );
277 $stats = RequestContext
::getMain()->getStats();
279 $key = self
::getStashKey( $title, $content, $user );
280 $editInfo = $cache->get( $key );
281 if ( !is_object( $editInfo ) ) {
282 $start = microtime( true );
283 // We ignore user aborts and keep parsing. Block on any prior parsing
284 // so as to use its results and make use of the time spent parsing.
285 // Skip this logic if there no master connection in case this method
286 // is called on an HTTP GET request for some reason.
288 $dbw = $lb->getAnyOpenConnection( $lb->getWriterIndex() );
289 if ( $dbw && $dbw->lock( $key, __METHOD__
, 30 ) ) {
290 $editInfo = $cache->get( $key );
291 $dbw->unlock( $key, __METHOD__
);
294 $timeMs = 1000 * max( 0, microtime( true ) - $start );
295 $stats->timing( 'editstash.lock_wait_time', $timeMs );
298 if ( !is_object( $editInfo ) ||
!$editInfo->output
) {
299 $stats->increment( 'editstash.cache_misses.no_stash' );
300 $logger->debug( "Empty cache for key '$key' ('$title'); user '{$user->getName()}'." );
304 $age = time() - wfTimestamp( TS_UNIX
, $editInfo->output
->getCacheTime() );
305 if ( $age <= self
::PRESUME_FRESH_TTL_SEC
) {
306 $stats->increment( 'editstash.cache_hits.presumed_fresh' );
307 $logger->debug( "Timestamp-based cache hit for key '$key' (age: $age sec)." );
308 return $editInfo; // assume nothing changed
309 } elseif ( isset( $editInfo->edits
) && $editInfo->edits
=== $user->getEditCount() ) {
310 // Logged-in user made no local upload/template edits in the meantime
311 $stats->increment( 'editstash.cache_hits.presumed_fresh' );
312 $logger->debug( "Edit count based cache hit for key '$key' (age: $age sec)." );
314 } elseif ( $user->isAnon()
315 && self
::lastEditTime( $user ) < $editInfo->output
->getCacheTime()
317 // Logged-out user made no local upload/template edits in the meantime
318 $stats->increment( 'editstash.cache_hits.presumed_fresh' );
319 $logger->debug( "Edit check based cache hit for key '$key' (age: $age sec)." );
323 $stats->increment( 'editstash.cache_misses.proven_stale' );
324 $logger->info( "Stale cache for key '$key'; old key with outside edits. (age: $age sec)" );
331 * @return string|null TS_MW timestamp or null
333 private static function lastEditTime( User
$user ) {
334 $time = wfGetDB( DB_SLAVE
)->selectField(
337 [ 'rc_user_text' => $user->getName() ],
341 return wfTimestampOrNull( TS_MW
, $time );
345 * Get the temporary prepared edit stash key for a user
347 * This key can be used for caching prepared edits provided:
348 * - a) The $user was used for PST options
349 * - b) The parser output was made from the PST using cannonical matching options
351 * @param Title $title
352 * @param Content $content
353 * @param User $user User to get parser options from
356 private static function getStashKey( Title
$title, Content
$content, User
$user ) {
357 $hash = sha1( implode( ':', [
358 // Account for the edit model/text
359 $content->getModel(),
360 $content->getDefaultFormat(),
361 sha1( $content->serialize( $content->getDefaultFormat() ) ),
362 // Account for user name related variables like signatures
364 md5( $user->getName() )
367 return wfMemcKey( 'prepared-edit', md5( $title->getPrefixedDBkey() ), $hash );
371 * Build a value to store in memcached based on the PST content and parser output
373 * This makes a simple version of WikiPage::prepareContentForEdit() as stash info
375 * @param Content $pstContent
376 * @param ParserOutput $parserOutput
377 * @param string $timestamp TS_MW
379 * @return array (stash info array, TTL in seconds, info code) or (null, 0, info code)
381 private static function buildStashValue(
382 Content
$pstContent, ParserOutput
$parserOutput, $timestamp, User
$user
384 // If an item is renewed, mind the cache TTL determined by config and parser functions.
385 // Put an upper limit on the TTL for sanity to avoid extreme template/file staleness.
386 $since = time() - wfTimestamp( TS_UNIX
, $parserOutput->getTimestamp() );
387 $ttl = min( $parserOutput->getCacheExpiry() - $since, self
::MAX_CACHE_TTL
);
389 return [ null, 0, 'no_ttl' ];
390 } elseif ( $parserOutput->getFlag( 'vary-revision' ) ) {
391 return [ null, 0, 'vary_revision' ];
394 // Only store what is actually needed
395 $stashInfo = (object)[
396 'pstContent' => $pstContent,
397 'output' => $parserOutput,
398 'timestamp' => $timestamp,
399 'edits' => $user->getEditCount()
402 return [ $stashInfo, $ttl, 'ok' ];
405 public function getAllowedParams() {
408 ApiBase
::PARAM_TYPE
=> 'string',
409 ApiBase
::PARAM_REQUIRED
=> true
412 ApiBase
::PARAM_TYPE
=> 'string',
415 ApiBase
::PARAM_TYPE
=> 'string'
418 ApiBase
::PARAM_TYPE
=> 'text',
419 ApiBase
::PARAM_REQUIRED
=> true
422 ApiBase
::PARAM_TYPE
=> ContentHandler
::getContentModels(),
423 ApiBase
::PARAM_REQUIRED
=> true
426 ApiBase
::PARAM_TYPE
=> ContentHandler
::getAllContentFormats(),
427 ApiBase
::PARAM_REQUIRED
=> true
430 ApiBase
::PARAM_TYPE
=> 'integer',
431 ApiBase
::PARAM_REQUIRED
=> true
436 public function needsToken() {
440 public function mustBePosted() {
444 public function isWriteMode() {
448 public function isInternal() {