Fix caller name in ApiStashEdit::parseAndStash
[lhc/web/wiklou.git] / includes / api / ApiStashEdit.php
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21 use MediaWiki\Logger\LoggerFactory;
22 use MediaWiki\MediaWikiServices;
23 use Wikimedia\ScopedCallback;
24
25 /**
26 * Prepare an edit in shared cache so that it can be reused on edit
27 *
28 * This endpoint can be called via AJAX as the user focuses on the edit
29 * summary box. By the time of submission, the parse may have already
30 * finished, and can be immediately used on page save. Certain parser
31 * functions like {{REVISIONID}} or {{CURRENTTIME}} may cause the cache
32 * to not be used on edit. Template and files used are check for changes
33 * since the output was generated. The cache TTL is also kept low for sanity.
34 *
35 * @ingroup API
36 * @since 1.25
37 */
38 class ApiStashEdit extends ApiBase {
39 const ERROR_NONE = 'stashed';
40 const ERROR_PARSE = 'error_parse';
41 const ERROR_CACHE = 'error_cache';
42 const ERROR_UNCACHEABLE = 'uncacheable';
43 const ERROR_BUSY = 'busy';
44
45 const PRESUME_FRESH_TTL_SEC = 30;
46 const MAX_CACHE_TTL = 300; // 5 minutes
47 const MAX_SIGNATURE_TTL = 60;
48
49 public function execute() {
50 $user = $this->getUser();
51 $params = $this->extractRequestParams();
52
53 if ( $user->isBot() ) { // sanity
54 $this->dieWithError( 'apierror-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->dieWithError(
65 [ 'apierror-badformat-generic', $params['contentformat'], $params['contentmodel'] ],
66 'badmodelformat'
67 );
68 }
69
70 $this->requireOnlyOneParameter( $params, 'stashedtexthash', 'text' );
71
72 $text = null;
73 $textHash = null;
74 if ( $params['stashedtexthash'] !== null ) {
75 // Load from cache since the client indicates the text is the same as last stash
76 $textHash = $params['stashedtexthash'];
77 if ( !preg_match( '/^[0-9a-f]{40}$/', $textHash ) ) {
78 $this->dieWithError( 'apierror-stashedit-missingtext', 'missingtext' );
79 }
80 $textKey = $cache->makeKey( 'stashedit', 'text', $textHash );
81 $text = $cache->get( $textKey );
82 if ( !is_string( $text ) ) {
83 $this->dieWithError( 'apierror-stashedit-missingtext', 'missingtext' );
84 }
85 } else {
86 // 'text' was passed. Trim and fix newlines so the key SHA1's
87 // match (see WebRequest::getText())
88 $text = rtrim( str_replace( "\r\n", "\n", $params['text'] ) );
89 $textHash = sha1( $text );
90 }
91
92 $textContent = ContentHandler::makeContent(
93 $text, $title, $params['contentmodel'], $params['contentformat'] );
94
95 $page = WikiPage::factory( $title );
96 if ( $page->exists() ) {
97 // Page exists: get the merged content with the proposed change
98 $baseRev = Revision::newFromPageId( $page->getId(), $params['baserevid'] );
99 if ( !$baseRev ) {
100 $this->dieWithError( [ 'apierror-nosuchrevid', $params['baserevid'] ] );
101 }
102 $currentRev = $page->getRevision();
103 if ( !$currentRev ) {
104 $this->dieWithError( [ 'apierror-missingrev-pageid', $page->getId() ], 'missingrev' );
105 }
106 // Merge in the new version of the section to get the proposed version
107 $editContent = $page->replaceSectionAtRev(
108 $params['section'],
109 $textContent,
110 $params['sectiontitle'],
111 $baseRev->getId()
112 );
113 if ( !$editContent ) {
114 $this->dieWithError( 'apierror-sectionreplacefailed', 'replacefailed' );
115 }
116 if ( $currentRev->getId() == $baseRev->getId() ) {
117 // Base revision was still the latest; nothing to merge
118 $content = $editContent;
119 } else {
120 // Merge the edit into the current version
121 $baseContent = $baseRev->getContent();
122 $currentContent = $currentRev->getContent();
123 if ( !$baseContent || !$currentContent ) {
124 $this->dieWithError( [ 'apierror-missingcontent-pageid', $page->getId() ], 'missingrev' );
125 }
126 $handler = ContentHandler::getForModelID( $baseContent->getModel() );
127 $content = $handler->merge3( $baseContent, $editContent, $currentContent );
128 }
129 } else {
130 // New pages: use the user-provided content model
131 $content = $textContent;
132 }
133
134 if ( !$content ) { // merge3() failed
135 $this->getResult()->addValue( null,
136 $this->getModuleName(), [ 'status' => 'editconflict' ] );
137 return;
138 }
139
140 // The user will abort the AJAX request by pressing "save", so ignore that
141 ignore_user_abort( true );
142
143 if ( $user->pingLimiter( 'stashedit' ) ) {
144 $status = 'ratelimited';
145 } else {
146 $status = self::parseAndStash( $page, $content, $user, $params['summary'] );
147 $textKey = $cache->makeKey( 'stashedit', 'text', $textHash );
148 $cache->set( $textKey, $text, self::MAX_CACHE_TTL );
149 }
150
151 $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
152 $stats->increment( "editstash.cache_stores.$status" );
153
154 $ret = [ 'status' => $status ];
155 // If we were rate-limited, we still return the pre-existing valid hash if one was passed
156 if ( $status !== 'ratelimited' || $params['stashedtexthash'] !== null ) {
157 $ret['texthash'] = $textHash;
158 }
159
160 $this->getResult()->addValue( null, $this->getModuleName(), $ret );
161 }
162
163 /**
164 * @param WikiPage $page
165 * @param Content $content Edit content
166 * @param User $user
167 * @param string $summary Edit summary
168 * @return string ApiStashEdit::ERROR_* constant
169 * @since 1.25
170 */
171 public static function parseAndStash( WikiPage $page, Content $content, User $user, $summary ) {
172 $cache = ObjectCache::getLocalClusterInstance();
173 $logger = LoggerFactory::getInstance( 'StashEdit' );
174
175 $title = $page->getTitle();
176 $key = self::getStashKey( $title, self::getContentHash( $content ), $user );
177 $fname = __METHOD__;
178
179 // Use the master DB to allow for fast blocking locks on the "save path" where this
180 // value might actually be used to complete a page edit. If the edit submission request
181 // happens before this edit stash requests finishes, then the submission will block until
182 // the stash request finishes parsing. For the lock acquisition below, there is not much
183 // need to duplicate parsing of the same content/user/summary bundle, so try to avoid
184 // blocking at all here.
185 $dbw = wfGetDB( DB_MASTER );
186 if ( !$dbw->lock( $key, $fname, 0 ) ) {
187 // De-duplicate requests on the same key
188 return self::ERROR_BUSY;
189 }
190 /** @noinspection PhpUnusedLocalVariableInspection */
191 $unlocker = new ScopedCallback( function () use ( $dbw, $key, $fname ) {
192 $dbw->unlock( $key, $fname );
193 } );
194
195 $cutoffTime = time() - self::PRESUME_FRESH_TTL_SEC;
196
197 // Reuse any freshly build matching edit stash cache
198 $editInfo = $cache->get( $key );
199 if ( $editInfo && wfTimestamp( TS_UNIX, $editInfo->timestamp ) >= $cutoffTime ) {
200 $alreadyCached = true;
201 } else {
202 $format = $content->getDefaultFormat();
203 $editInfo = $page->prepareContentForEdit( $content, null, $user, $format, false );
204 $alreadyCached = false;
205 }
206
207 if ( $editInfo && $editInfo->output ) {
208 // Let extensions add ParserOutput metadata or warm other caches
209 Hooks::run( 'ParserOutputStashForEdit',
210 [ $page, $content, $editInfo->output, $summary, $user ] );
211
212 $titleStr = (string)$title;
213 if ( $alreadyCached ) {
214 $logger->debug( "Already cached parser output for key '{cachekey}' ('{title}').",
215 [ 'cachekey' => $key, 'title' => $titleStr ] );
216 return self::ERROR_NONE;
217 }
218
219 list( $stashInfo, $ttl, $code ) = self::buildStashValue(
220 $editInfo->pstContent,
221 $editInfo->output,
222 $editInfo->timestamp,
223 $user
224 );
225
226 if ( $stashInfo ) {
227 $ok = $cache->set( $key, $stashInfo, $ttl );
228 if ( $ok ) {
229 $logger->debug( "Cached parser output for key '{cachekey}' ('{title}').",
230 [ 'cachekey' => $key, 'title' => $titleStr ] );
231 return self::ERROR_NONE;
232 } else {
233 $logger->error( "Failed to cache parser output for key '{cachekey}' ('{title}').",
234 [ 'cachekey' => $key, 'title' => $titleStr ] );
235 return self::ERROR_CACHE;
236 }
237 } else {
238 // @todo Doesn't seem reachable, see @todo in buildStashValue
239 $logger->info( "Uncacheable parser output for key '{cachekey}' ('{title}') [{code}].",
240 [ 'cachekey' => $key, 'title' => $titleStr, 'code' => $code ] );
241 return self::ERROR_UNCACHEABLE;
242 }
243 }
244
245 return self::ERROR_PARSE;
246 }
247
248 /**
249 * Check that a prepared edit is in cache and still up-to-date
250 *
251 * This method blocks if the prepared edit is already being rendered,
252 * waiting until rendering finishes before doing final validity checks.
253 *
254 * The cache is rejected if template or file changes are detected.
255 * Note that foreign template or file transclusions are not checked.
256 *
257 * The result is a map (pstContent,output,timestamp) with fields
258 * extracted directly from WikiPage::prepareContentForEdit().
259 *
260 * @param Title $title
261 * @param Content $content
262 * @param User $user User to get parser options from
263 * @return stdClass|bool Returns false on cache miss
264 */
265 public static function checkCache( Title $title, Content $content, User $user ) {
266 if ( $user->isBot() ) {
267 return false; // bots never stash - don't pollute stats
268 }
269
270 $cache = ObjectCache::getLocalClusterInstance();
271 $logger = LoggerFactory::getInstance( 'StashEdit' );
272 $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
273
274 $key = self::getStashKey( $title, self::getContentHash( $content ), $user );
275 $editInfo = $cache->get( $key );
276 if ( !is_object( $editInfo ) ) {
277 $start = microtime( true );
278 // We ignore user aborts and keep parsing. Block on any prior parsing
279 // so as to use its results and make use of the time spent parsing.
280 // Skip this logic if there no master connection in case this method
281 // is called on an HTTP GET request for some reason.
282 $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
283 $dbw = $lb->getAnyOpenConnection( $lb->getWriterIndex() );
284 if ( $dbw && $dbw->lock( $key, __METHOD__, 30 ) ) {
285 $editInfo = $cache->get( $key );
286 $dbw->unlock( $key, __METHOD__ );
287 }
288
289 $timeMs = 1000 * max( 0, microtime( true ) - $start );
290 $stats->timing( 'editstash.lock_wait_time', $timeMs );
291 }
292
293 if ( !is_object( $editInfo ) || !$editInfo->output ) {
294 $stats->increment( 'editstash.cache_misses.no_stash' );
295 $logger->debug( "Empty cache for key '$key' ('$title'); user '{$user->getName()}'." );
296 return false;
297 }
298
299 $age = time() - wfTimestamp( TS_UNIX, $editInfo->output->getCacheTime() );
300 if ( $age <= self::PRESUME_FRESH_TTL_SEC ) {
301 // Assume nothing changed in this time
302 $stats->increment( 'editstash.cache_hits.presumed_fresh' );
303 $logger->debug( "Timestamp-based cache hit for key '$key' (age: $age sec)." );
304 } elseif ( isset( $editInfo->edits ) && $editInfo->edits === $user->getEditCount() ) {
305 // Logged-in user made no local upload/template edits in the meantime
306 $stats->increment( 'editstash.cache_hits.presumed_fresh' );
307 $logger->debug( "Edit count based cache hit for key '$key' (age: $age sec)." );
308 } elseif ( $user->isAnon()
309 && self::lastEditTime( $user ) < $editInfo->output->getCacheTime()
310 ) {
311 // Logged-out user made no local upload/template edits in the meantime
312 $stats->increment( 'editstash.cache_hits.presumed_fresh' );
313 $logger->debug( "Edit check based cache hit for key '$key' (age: $age sec)." );
314 } else {
315 // User may have changed included content
316 $editInfo = false;
317 }
318
319 if ( !$editInfo ) {
320 $stats->increment( 'editstash.cache_misses.proven_stale' );
321 $logger->info( "Stale cache for key '$key'; old key with outside edits. (age: $age sec)" );
322 } elseif ( $editInfo->output->getFlag( 'vary-revision' ) ) {
323 // This can be used for the initial parse, e.g. for filters or doEditContent(),
324 // but a second parse will be triggered in doEditUpdates(). This is not optimal.
325 $logger->info( "Cache for key '$key' ('$title') has vary_revision." );
326 } elseif ( $editInfo->output->getFlag( 'vary-revision-id' ) ) {
327 // Similar to the above if we didn't guess the ID correctly.
328 $logger->info( "Cache for key '$key' ('$title') has vary_revision_id." );
329 }
330
331 return $editInfo;
332 }
333
334 /**
335 * @param User $user
336 * @return string|null TS_MW timestamp or null
337 */
338 private static function lastEditTime( User $user ) {
339 $db = wfGetDB( DB_REPLICA );
340 $actorQuery = ActorMigration::newMigration()->getWhere( $db, 'rc_user', $user, false );
341 $time = $db->selectField(
342 [ 'recentchanges' ] + $actorQuery['tables'],
343 'MAX(rc_timestamp)',
344 [ $actorQuery['conds'] ],
345 __METHOD__,
346 [],
347 $actorQuery['joins']
348 );
349
350 return wfTimestampOrNull( TS_MW, $time );
351 }
352
353 /**
354 * Get hash of the content, factoring in model/format
355 *
356 * @param Content $content
357 * @return string
358 */
359 private static function getContentHash( Content $content ) {
360 return sha1( implode( "\n", [
361 $content->getModel(),
362 $content->getDefaultFormat(),
363 $content->serialize( $content->getDefaultFormat() )
364 ] ) );
365 }
366
367 /**
368 * Get the temporary prepared edit stash key for a user
369 *
370 * This key can be used for caching prepared edits provided:
371 * - a) The $user was used for PST options
372 * - b) The parser output was made from the PST using cannonical matching options
373 *
374 * @param Title $title
375 * @param string $contentHash Result of getContentHash()
376 * @param User $user User to get parser options from
377 * @return string
378 */
379 private static function getStashKey( Title $title, $contentHash, User $user ) {
380 return ObjectCache::getLocalClusterInstance()->makeKey(
381 'prepared-edit',
382 md5( $title->getPrefixedDBkey() ),
383 // Account for the edit model/text
384 $contentHash,
385 // Account for user name related variables like signatures
386 md5( $user->getId() . "\n" . $user->getName() )
387 );
388 }
389
390 /**
391 * Build a value to store in memcached based on the PST content and parser output
392 *
393 * This makes a simple version of WikiPage::prepareContentForEdit() as stash info
394 *
395 * @param Content $pstContent Pre-Save transformed content
396 * @param ParserOutput $parserOutput
397 * @param string $timestamp TS_MW
398 * @param User $user
399 * @return array (stash info array, TTL in seconds, info code) or (null, 0, info code)
400 */
401 private static function buildStashValue(
402 Content $pstContent, ParserOutput $parserOutput, $timestamp, User $user
403 ) {
404 // If an item is renewed, mind the cache TTL determined by config and parser functions.
405 // Put an upper limit on the TTL for sanity to avoid extreme template/file staleness.
406 $since = time() - wfTimestamp( TS_UNIX, $parserOutput->getCacheTime() );
407 $ttl = min( $parserOutput->getCacheExpiry() - $since, self::MAX_CACHE_TTL );
408
409 // Avoid extremely stale user signature timestamps (T84843)
410 if ( $parserOutput->getFlag( 'user-signature' ) ) {
411 $ttl = min( $ttl, self::MAX_SIGNATURE_TTL );
412 }
413
414 if ( $ttl <= 0 ) {
415 // @todo It doesn't seem like this can occur, because it would mean an entry older than
416 // getCacheExpiry() seconds, which is much longer than PRESUME_FRESH_TTL_SEC, and
417 // anything older than PRESUME_FRESH_TTL_SEC will have been thrown out already.
418 return [ null, 0, 'no_ttl' ];
419 }
420
421 // Only store what is actually needed
422 $stashInfo = (object)[
423 'pstContent' => $pstContent,
424 'output' => $parserOutput,
425 'timestamp' => $timestamp,
426 'edits' => $user->getEditCount()
427 ];
428
429 return [ $stashInfo, $ttl, 'ok' ];
430 }
431
432 public function getAllowedParams() {
433 return [
434 'title' => [
435 ApiBase::PARAM_TYPE => 'string',
436 ApiBase::PARAM_REQUIRED => true
437 ],
438 'section' => [
439 ApiBase::PARAM_TYPE => 'string',
440 ],
441 'sectiontitle' => [
442 ApiBase::PARAM_TYPE => 'string'
443 ],
444 'text' => [
445 ApiBase::PARAM_TYPE => 'text',
446 ApiBase::PARAM_DFLT => null
447 ],
448 'stashedtexthash' => [
449 ApiBase::PARAM_TYPE => 'string',
450 ApiBase::PARAM_DFLT => null
451 ],
452 'summary' => [
453 ApiBase::PARAM_TYPE => 'string',
454 ],
455 'contentmodel' => [
456 ApiBase::PARAM_TYPE => ContentHandler::getContentModels(),
457 ApiBase::PARAM_REQUIRED => true
458 ],
459 'contentformat' => [
460 ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
461 ApiBase::PARAM_REQUIRED => true
462 ],
463 'baserevid' => [
464 ApiBase::PARAM_TYPE => 'integer',
465 ApiBase::PARAM_REQUIRED => true
466 ]
467 ];
468 }
469
470 public function needsToken() {
471 return 'csrf';
472 }
473
474 public function mustBePosted() {
475 return true;
476 }
477
478 public function isWriteMode() {
479 return true;
480 }
481
482 public function isInternal() {
483 return true;
484 }
485 }