MediaWiki REL1_28
ApiStashEdit.php
Go to the documentation of this file.
1<?php
24use Wikimedia\ScopedCallback;
25
39class ApiStashEdit extends ApiBase {
40 const ERROR_NONE = 'stashed';
41 const ERROR_PARSE = 'error_parse';
42 const ERROR_CACHE = 'error_cache';
43 const ERROR_UNCACHEABLE = 'uncacheable';
44 const ERROR_BUSY = 'busy';
45
47 const MAX_CACHE_TTL = 300; // 5 minutes
48
49 public function execute() {
50 $user = $this->getUser();
52
53 if ( $user->isBot() ) { // sanity
54 $this->dieUsage( 'This interface is not supported for bots', 'botsnotsupported' );
55 }
56
57 $cache = ObjectCache::getLocalClusterInstance();
58 $page = $this->getTitleOrPageId( $params );
59 $title = $page->getTitle();
60
61 if ( !ContentHandler::getForModelID( $params['contentmodel'] )
62 ->isSupportedFormat( $params['contentformat'] )
63 ) {
64 $this->dieUsage( 'Unsupported content model/format', 'badmodelformat' );
65 }
66
67 $text = null;
68 $textHash = null;
69 if ( strlen( $params['stashedtexthash'] ) ) {
70 // Load from cache since the client indicates the text is the same as last stash
71 $textHash = $params['stashedtexthash'];
72 $textKey = $cache->makeKey( 'stashedit', 'text', $textHash );
73 $text = $cache->get( $textKey );
74 if ( !is_string( $text ) ) {
75 $this->dieUsage( 'No stashed text found with the given hash', 'missingtext' );
76 }
77 } elseif ( $params['text'] !== null ) {
78 // Trim and fix newlines so the key SHA1's match (see WebRequest::getText())
79 $text = rtrim( str_replace( "\r\n", "\n", $params['text'] ) );
80 $textHash = sha1( $text );
81 } else {
82 $this->dieUsage(
83 'The text or stashedtexthash parameter must be given', 'missingtextparam' );
84 }
85
86 $textContent = ContentHandler::makeContent(
87 $text, $title, $params['contentmodel'], $params['contentformat'] );
88
90 if ( $page->exists() ) {
91 // Page exists: get the merged content with the proposed change
92 $baseRev = Revision::newFromPageId( $page->getId(), $params['baserevid'] );
93 if ( !$baseRev ) {
94 $this->dieUsage( "No revision ID {$params['baserevid']}", 'missingrev' );
95 }
96 $currentRev = $page->getRevision();
97 if ( !$currentRev ) {
98 $this->dieUsage( "No current revision of page ID {$page->getId()}", 'missingrev' );
99 }
100 // Merge in the new version of the section to get the proposed version
101 $editContent = $page->replaceSectionAtRev(
102 $params['section'],
103 $textContent,
104 $params['sectiontitle'],
105 $baseRev->getId()
106 );
107 if ( !$editContent ) {
108 $this->dieUsage( 'Could not merge updated section.', 'replacefailed' );
109 }
110 if ( $currentRev->getId() == $baseRev->getId() ) {
111 // Base revision was still the latest; nothing to merge
112 $content = $editContent;
113 } else {
114 // Merge the edit into the current version
115 $baseContent = $baseRev->getContent();
116 $currentContent = $currentRev->getContent();
117 if ( !$baseContent || !$currentContent ) {
118 $this->dieUsage( "Missing content for page ID {$page->getId()}", 'missingrev' );
119 }
120 $handler = ContentHandler::getForModelID( $baseContent->getModel() );
121 $content = $handler->merge3( $baseContent, $editContent, $currentContent );
122 }
123 } else {
124 // New pages: use the user-provided content model
125 $content = $textContent;
126 }
127
128 if ( !$content ) { // merge3() failed
129 $this->getResult()->addValue( null,
130 $this->getModuleName(), [ 'status' => 'editconflict' ] );
131 return;
132 }
133
134 // The user will abort the AJAX request by pressing "save", so ignore that
135 ignore_user_abort( true );
136
137 if ( $user->pingLimiter( 'stashedit' ) ) {
138 $status = 'ratelimited';
139 } else {
141 $textKey = $cache->makeKey( 'stashedit', 'text', $textHash );
142 $cache->set( $textKey, $text, self::MAX_CACHE_TTL );
143 }
144
145 $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
146 $stats->increment( "editstash.cache_stores.$status" );
147
148 $this->getResult()->addValue(
149 null,
150 $this->getModuleName(),
151 [
152 'status' => $status,
153 'texthash' => $textHash
154 ]
155 );
156 }
157
166 public static function parseAndStash( WikiPage $page, Content $content, User $user, $summary ) {
167 $cache = ObjectCache::getLocalClusterInstance();
168 $logger = LoggerFactory::getInstance( 'StashEdit' );
169
170 $title = $page->getTitle();
171 $key = self::getStashKey( $title, self::getContentHash( $content ), $user );
172
173 // Use the master DB for fast blocking locks
174 $dbw = wfGetDB( DB_MASTER );
175 if ( !$dbw->lock( $key, __METHOD__, 1 ) ) {
176 // De-duplicate requests on the same key
177 return self::ERROR_BUSY;
178 }
180 $unlocker = new ScopedCallback( function () use ( $dbw, $key ) {
181 $dbw->unlock( $key, __METHOD__ );
182 } );
183
184 $cutoffTime = time() - self::PRESUME_FRESH_TTL_SEC;
185
186 // Reuse any freshly build matching edit stash cache
187 $editInfo = $cache->get( $key );
188 if ( $editInfo && wfTimestamp( TS_UNIX, $editInfo->timestamp ) >= $cutoffTime ) {
189 $alreadyCached = true;
190 } else {
191 $format = $content->getDefaultFormat();
192 $editInfo = $page->prepareContentForEdit( $content, null, $user, $format, false );
193 $alreadyCached = false;
194 }
195
196 if ( $editInfo && $editInfo->output ) {
197 // Let extensions add ParserOutput metadata or warm other caches
198 Hooks::run( 'ParserOutputStashForEdit',
199 [ $page, $content, $editInfo->output, $summary, $user ] );
200
201 if ( $alreadyCached ) {
202 $logger->debug( "Already cached parser output for key '$key' ('$title')." );
203 return self::ERROR_NONE;
204 }
205
206 list( $stashInfo, $ttl, $code ) = self::buildStashValue(
207 $editInfo->pstContent,
208 $editInfo->output,
209 $editInfo->timestamp,
210 $user
211 );
212
213 if ( $stashInfo ) {
214 $ok = $cache->set( $key, $stashInfo, $ttl );
215 if ( $ok ) {
216 $logger->debug( "Cached parser output for key '$key' ('$title')." );
217 return self::ERROR_NONE;
218 } else {
219 $logger->error( "Failed to cache parser output for key '$key' ('$title')." );
220 return self::ERROR_CACHE;
221 }
222 } else {
223 $logger->info( "Uncacheable parser output for key '$key' ('$title') [$code]." );
225 }
226 }
227
228 return self::ERROR_PARSE;
229 }
230
248 public static function checkCache( Title $title, Content $content, User $user ) {
249 if ( $user->isBot() ) {
250 return false; // bots never stash - don't pollute stats
251 }
252
253 $cache = ObjectCache::getLocalClusterInstance();
254 $logger = LoggerFactory::getInstance( 'StashEdit' );
255 $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
256
257 $key = self::getStashKey( $title, self::getContentHash( $content ), $user );
258 $editInfo = $cache->get( $key );
259 if ( !is_object( $editInfo ) ) {
260 $start = microtime( true );
261 // We ignore user aborts and keep parsing. Block on any prior parsing
262 // so as to use its results and make use of the time spent parsing.
263 // Skip this logic if there no master connection in case this method
264 // is called on an HTTP GET request for some reason.
265 $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
266 $dbw = $lb->getAnyOpenConnection( $lb->getWriterIndex() );
267 if ( $dbw && $dbw->lock( $key, __METHOD__, 30 ) ) {
268 $editInfo = $cache->get( $key );
269 $dbw->unlock( $key, __METHOD__ );
270 }
271
272 $timeMs = 1000 * max( 0, microtime( true ) - $start );
273 $stats->timing( 'editstash.lock_wait_time', $timeMs );
274 }
275
276 if ( !is_object( $editInfo ) || !$editInfo->output ) {
277 $stats->increment( 'editstash.cache_misses.no_stash' );
278 $logger->debug( "Empty cache for key '$key' ('$title'); user '{$user->getName()}'." );
279 return false;
280 }
281
282 $age = time() - wfTimestamp( TS_UNIX, $editInfo->output->getCacheTime() );
283 if ( $age <= self::PRESUME_FRESH_TTL_SEC ) {
284 // Assume nothing changed in this time
285 $stats->increment( 'editstash.cache_hits.presumed_fresh' );
286 $logger->debug( "Timestamp-based cache hit for key '$key' (age: $age sec)." );
287 } elseif ( isset( $editInfo->edits ) && $editInfo->edits === $user->getEditCount() ) {
288 // Logged-in user made no local upload/template edits in the meantime
289 $stats->increment( 'editstash.cache_hits.presumed_fresh' );
290 $logger->debug( "Edit count based cache hit for key '$key' (age: $age sec)." );
291 } elseif ( $user->isAnon()
292 && self::lastEditTime( $user ) < $editInfo->output->getCacheTime()
293 ) {
294 // Logged-out user made no local upload/template edits in the meantime
295 $stats->increment( 'editstash.cache_hits.presumed_fresh' );
296 $logger->debug( "Edit check based cache hit for key '$key' (age: $age sec)." );
297 } else {
298 // User may have changed included content
299 $editInfo = false;
300 }
301
302 if ( !$editInfo ) {
303 $stats->increment( 'editstash.cache_misses.proven_stale' );
304 $logger->info( "Stale cache for key '$key'; old key with outside edits. (age: $age sec)" );
305 } elseif ( $editInfo->output->getFlag( 'vary-revision' ) ) {
306 // This can be used for the initial parse, e.g. for filters or doEditContent(),
307 // but a second parse will be triggered in doEditUpdates(). This is not optimal.
308 $logger->info( "Cache for key '$key' ('$title') has vary_revision." );
309 } elseif ( $editInfo->output->getFlag( 'vary-revision-id' ) ) {
310 // Similar to the above if we didn't guess the ID correctly.
311 $logger->info( "Cache for key '$key' ('$title') has vary_revision_id." );
312 }
313
314 return $editInfo;
315 }
316
321 private static function lastEditTime( User $user ) {
322 $time = wfGetDB( DB_REPLICA )->selectField(
323 'recentchanges',
324 'MAX(rc_timestamp)',
325 [ 'rc_user_text' => $user->getName() ],
326 __METHOD__
327 );
328
329 return wfTimestampOrNull( TS_MW, $time );
330 }
331
338 private static function getContentHash( Content $content ) {
339 return sha1( implode( "\n", [
340 $content->getModel(),
341 $content->getDefaultFormat(),
342 $content->serialize( $content->getDefaultFormat() )
343 ] ) );
344 }
345
358 private static function getStashKey( Title $title, $contentHash, User $user ) {
359 return ObjectCache::getLocalClusterInstance()->makeKey(
360 'prepared-edit',
361 md5( $title->getPrefixedDBkey() ),
362 // Account for the edit model/text
363 $contentHash,
364 // Account for user name related variables like signatures
365 md5( $user->getId() . "\n" . $user->getName() )
366 );
367 }
368
380 private static function buildStashValue(
382 ) {
383 // If an item is renewed, mind the cache TTL determined by config and parser functions.
384 // Put an upper limit on the TTL for sanity to avoid extreme template/file staleness.
385 $since = time() - wfTimestamp( TS_UNIX, $parserOutput->getTimestamp() );
386 $ttl = min( $parserOutput->getCacheExpiry() - $since, self::MAX_CACHE_TTL );
387 if ( $ttl <= 0 ) {
388 return [ null, 0, 'no_ttl' ];
389 }
390
391 // Only store what is actually needed
392 $stashInfo = (object)[
393 'pstContent' => $pstContent,
394 'output' => $parserOutput,
395 'timestamp' => $timestamp,
396 'edits' => $user->getEditCount()
397 ];
398
399 return [ $stashInfo, $ttl, 'ok' ];
400 }
401
402 public function getAllowedParams() {
403 return [
404 'title' => [
405 ApiBase::PARAM_TYPE => 'string',
407 ],
408 'section' => [
409 ApiBase::PARAM_TYPE => 'string',
410 ],
411 'sectiontitle' => [
412 ApiBase::PARAM_TYPE => 'string'
413 ],
414 'text' => [
415 ApiBase::PARAM_TYPE => 'text',
416 ApiBase::PARAM_DFLT => null
417 ],
418 'stashedtexthash' => [
419 ApiBase::PARAM_TYPE => 'string',
420 ApiBase::PARAM_DFLT => null
421 ],
422 'summary' => [
423 ApiBase::PARAM_TYPE => 'string',
424 ],
425 'contentmodel' => [
428 ],
429 'contentformat' => [
432 ],
433 'baserevid' => [
434 ApiBase::PARAM_TYPE => 'integer',
436 ]
437 ];
438 }
439
440 public function needsToken() {
441 return 'csrf';
442 }
443
444 public function mustBePosted() {
445 return true;
446 }
447
448 public function isWriteMode() {
449 return true;
450 }
451
452 public function isInternal() {
453 return true;
454 }
455}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
wfTimestampOrNull( $outputtype=TS_UNIX, $ts=null)
Return a formatted timestamp, or null if input is null.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:39
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition ApiBase.php:112
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition ApiBase.php:88
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition ApiBase.php:50
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:685
getResult()
Get the result object.
Definition ApiBase.php:584
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:464
getTitleOrPageId( $params, $load=false)
Get a WikiPage object from a title or pageid param, if possible.
Definition ApiBase.php:840
dieUsage( $description, $errorCode, $httpRespCode=0, $extradata=null)
Throw a UsageException, which will (if uncaught) call the main module's error handler and die with an...
Definition ApiBase.php:1585
Prepare an edit in shared cache so that it can be reused on edit.
const PRESUME_FRESH_TTL_SEC
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
const ERROR_UNCACHEABLE
static getStashKey(Title $title, $contentHash, User $user)
Get the temporary prepared edit stash key for a user.
static parseAndStash(WikiPage $page, Content $content, User $user, $summary)
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
static lastEditTime(User $user)
isInternal()
Indicates whether this module is "internal" Internal API modules are not (yet) intended for 3rd party...
static getContentHash(Content $content)
Get hash of the content, factoring in model/format.
needsToken()
Returns the token type this module requires in order to execute.
static buildStashValue(Content $pstContent, ParserOutput $parserOutput, $timestamp, User $user)
Build a value to store in memcached based on the PST content and parser output.
const MAX_CACHE_TTL
isWriteMode()
Indicates whether this module requires write mode.
static checkCache(Title $title, Content $content, User $user)
Check that a prepared edit is in cache and still up-to-date.
mustBePosted()
Indicates whether this module must be called with a POST request.
static getAllContentFormats()
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
static getForModelID( $modelId)
Returns the ContentHandler singleton for the given model ID.
static getContentModels()
getUser()
Get the User object.
PSR-3 logger instance factory.
MediaWikiServices is the service locator for the application scope of MediaWiki.
static newFromPageId( $pageId, $revId=0, $flags=0)
Load either the current, or a specified, revision that's attached to a given page ID.
Definition Revision.php:159
Represents a title within MediaWiki.
Definition Title.php:36
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:48
Class representing a MediaWiki article and history.
Definition WikiPage.php:32
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition WikiPage.php:115
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
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 For a description of the see design txt $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:64
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition hooks.txt:1049
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition hooks.txt:249
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context $parserOutput
Definition hooks.txt:1090
see documentation in includes Linker php for Linker::makeImageLink & $time
Definition hooks.txt:1752
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:986
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content $content
Definition hooks.txt:1094
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:1950
namespace are movable Hooks may change this value to override the return value of MWNamespace::isMovable(). 'NewDifferenceEngine' do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition hooks.txt:2534
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub 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 modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves $handler
Definition hooks.txt:925
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub 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:887
if( $limit) $timestamp
$summary
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:37
Base interface for content objects.
Definition Content.php:34
$cache
Definition mcc.php:33
const DB_REPLICA
Definition defines.php:22
const DB_MASTER
Definition defines.php:23
$params
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
Definition defines.php:6
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition defines.php:11