Fix hook situation for Skin::doEditSectionLink
[mediawiki.git] / includes / api / ApiStashEdit.php
blob09489e4f9a9119172d1e639576a0df3b6d0d8e0a
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.
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
18 * @file
19 * @author Aaron Schulz
22 /**
23 * Prepare and edit in shared cache so that it can be reused on edit
25 * This endpoint can be called via AJAX as the user focuses on the edit
26 * summary box. By the time of submission, the parse may have already
27 * finished, and can be immediately used on page save. Certain parser
28 * functions like {{REVISIONID}} or {{CURRENTTIME}} may cause the cache
29 * to not be used on edit. Template and files used are check for changes
30 * since the output was generated. The cache TTL is also kept low for sanity.
32 * @ingroup API
33 * @since 1.25
35 class ApiStashEdit extends ApiBase {
36 public function execute() {
37 global $wgMemc;
39 $user = $this->getUser();
40 $params = $this->extractRequestParams();
42 $page = $this->getTitleOrPageId( $params );
43 $title = $page->getTitle();
45 if ( !ContentHandler::getForModelID( $params['contentmodel'] )
46 ->isSupportedFormat( $params['contentformat'] )
47 ) {
48 $this->dieUsage( "Unsupported content model/format", 'badmodelformat' );
51 // Trim and fix newlines so the key SHA1's match (see RequestContext::getText())
52 $text = rtrim( str_replace( "\r\n", "\n", $params['text'] ) );
53 $textContent = ContentHandler::makeContent(
54 $text, $title, $params['contentmodel'], $params['contentformat'] );
56 $page = WikiPage::factory( $title );
57 if ( $page->exists() ) {
58 // Page exists: get the merged content with the proposed change
59 $baseRev = Revision::newFromPageId( $page->getId(), $params['baserevid'] );
60 if ( !$baseRev ) {
61 $this->dieUsage( "No revision ID {$params['baserevid']}", 'missingrev' );
63 $currentRev = $page->getRevision();
64 if ( !$currentRev ) {
65 $this->dieUsage( "No current revision of page ID {$page->getId()}", 'missingrev' );
67 // Merge in the new version of the section to get the proposed version
68 $editContent = $page->replaceSectionAtRev(
69 $params['section'],
70 $textContent,
71 $params['sectiontitle'],
72 $baseRev->getId()
74 if ( !$editContent ) {
75 $this->dieUsage( "Could not merge updated section.", 'replacefailed' );
77 if ( $currentRev->getId() == $baseRev->getId() ) {
78 // Base revision was still the latest; nothing to merge
79 $content = $editContent;
80 } else {
81 // Merge the edit into the current version
82 $baseContent = $baseRev->getContent();
83 $currentContent = $currentRev->getContent();
84 if ( !$baseContent || !$currentContent ) {
85 $this->dieUsage( "Missing content for page ID {$page->getId()}", 'missingrev' );
87 $handler = ContentHandler::getForModelID( $baseContent->getModel() );
88 $content = $handler->merge3( $baseContent, $editContent, $currentContent );
90 } else {
91 // New pages: use the user-provided content model
92 $content = $textContent;
95 if ( !$content ) { // merge3() failed
96 $this->getResult()->addValue( null,
97 $this->getModuleName(), array( 'status' => 'editconflict' ) );
98 return;
101 // The user will abort the AJAX request by pressing "save", so ignore that
102 ignore_user_abort( true );
104 // Get a key based on the source text, format, and user preferences
105 $key = self::getStashKey( $title, $content, $user );
106 // De-duplicate requests on the same key
107 if ( $user->pingLimiter( 'stashedit' ) ) {
108 $editInfo = false;
109 $status = 'ratelimited';
110 } elseif ( $wgMemc->lock( $key, 0, 30 ) ) {
111 $format = $content->getDefaultFormat();
112 $editInfo = $page->prepareContentForEdit( $content, null, $user, $format, false );
113 $status = 'error'; // default
114 $unlocker = new ScopedCallback( function() use ( $key ) {
115 global $wgMemc;
116 $wgMemc->unlock( $key );
117 } );
118 } else {
119 $editInfo = false;
120 $status = 'busy';
123 if ( $editInfo && $editInfo->output ) {
124 list( $stashInfo, $ttl ) = self::buildStashValue(
125 $editInfo->pstContent, $editInfo->output, $editInfo->timestamp
127 if ( $stashInfo ) {
128 $ok = $wgMemc->set( $key, $stashInfo, $ttl );
129 if ( $ok ) {
130 $status = 'stashed';
131 wfDebugLog( 'StashEdit', "Cached parser output for key '$key'." );
132 } else {
133 $status = 'error';
134 wfDebugLog( 'StashEdit', "Failed to cache parser output for key '$key'." );
136 } else {
137 $status = 'uncacheable';
138 wfDebugLog( 'StashEdit', "Uncacheable parser output for key '$key'." );
142 $this->getResult()->addValue( null, $this->getModuleName(), array( 'status' => $status ) );
146 * Attempt to cache PST content and corresponding parser output in passing
148 * This method can be called when the output was already generated for other
149 * reasons. Parsing should not be done just to call this method, however.
150 * $pstOpts must be that of the user doing the edit preview. If $pOpts does
151 * not match the options of WikiPage::makeParserOptions( 'canonical' ), this
152 * will do nothing. Provided the values are cacheable, they will be stored
153 * in memcached so that final edit submission might make use of them.
155 * @param Article|WikiPage $page Page title
156 * @param Content $content Proposed page content
157 * @param Content $pstContent The result of preSaveTransform() on $content
158 * @param ParserOutput $pOut The result of getParserOutput() on $pstContent
159 * @param ParserOptions $pstOpts Options for $pstContent (MUST be for prospective author)
160 * @param ParserOptions $pOpts Options for $pOut
161 * @param string $timestamp TS_MW timestamp of parser output generation
162 * @return bool Success
164 public static function stashEditFromPreview(
165 Page $page, Content $content, Content $pstContent, ParserOutput $pOut,
166 ParserOptions $pstOpts, ParserOptions $pOpts, $timestamp
168 global $wgMemc;
170 // getIsPreview() controls parser function behavior that references things
171 // like user/revision that don't exists yet. The user/text should already
172 // be set correctly by callers, just double check the preview flag.
173 if ( !$pOpts->getIsPreview() ) {
174 return false; // sanity
175 } elseif ( $pOpts->getIsSectionPreview() ) {
176 return false; // short-circuit (need the full content)
179 // PST parser options are for the user (handles signatures, etc...)
180 $user = $pstOpts->getUser();
181 // Get a key based on the source text, format, and user preferences
182 $key = self::getStashKey( $page->getTitle(), $content, $user );
184 // Parser output options must match cannonical options.
185 // Treat some options as matching that are different but don't matter.
186 $canonicalPOpts = $page->makeParserOptions( 'canonical' );
187 $canonicalPOpts->setIsPreview( true ); // force match
188 $canonicalPOpts->setTimestamp( $pOpts->getTimestamp() ); // force match
189 if ( !$pOpts->matches( $canonicalPOpts ) ) {
190 wfDebugLog( 'StashEdit', "Uncacheable preview output for key '$key' (options)." );
191 return false;
194 // Build a value to cache with a proper TTL
195 list( $stashInfo, $ttl ) = self::buildStashValue( $pstContent, $pOut, $timestamp );
196 if ( !$stashInfo ) {
197 wfDebugLog( 'StashEdit', "Uncacheable parser output for key '$key' (rev/TTL)." );
198 return false;
201 $ok = $wgMemc->set( $key, $stashInfo, $ttl );
202 if ( !$ok ) {
203 wfDebugLog( 'StashEdit', "Failed to cache preview parser output for key '$key'." );
204 } else {
205 wfDebugLog( 'StashEdit', "Cached preview output for key '$key'." );
208 return $ok;
212 * Check that a prepared edit is in cache and still up-to-date
214 * This method blocks if the prepared edit is already being rendered,
215 * waiting until rendering finishes before doing final validity checks.
217 * The cache is rejected if template or file changes are detected.
218 * Note that foreign template or file transclusions are not checked.
220 * The result is a map (pstContent,output,timestamp) with fields
221 * extracted directly from WikiPage::prepareContentForEdit().
223 * @param Title $title
224 * @param Content $content
225 * @param User $user User to get parser options from
226 * @return stdClass|bool Returns false on cache miss
228 public static function checkCache( Title $title, Content $content, User $user ) {
229 global $wgMemc;
231 $key = self::getStashKey( $title, $content, $user );
232 $editInfo = $wgMemc->get( $key );
233 if ( !is_object( $editInfo ) ) {
234 $start = microtime( true );
235 // We ignore user aborts and keep parsing. Block on any prior parsing
236 // so as to use it's results and make use of the time spent parsing.
237 if ( $wgMemc->lock( $key, 30, 30 ) ) {
238 $editInfo = $wgMemc->get( $key );
239 $wgMemc->unlock( $key );
241 $sec = microtime( true ) - $start;
242 if ( $sec > .01 ) {
243 wfDebugLog( 'StashEdit', "Waited $sec seconds on '$key'." );
247 if ( !is_object( $editInfo ) || !$editInfo->output ) {
248 wfDebugLog( 'StashEdit', "No cache value for key '$key'." );
249 return false;
252 $time = wfTimestamp( TS_UNIX, $editInfo->output->getTimestamp() );
253 if ( ( time() - $time ) <= 3 ) {
254 wfDebugLog( 'StashEdit', "Timestamp-based cache hit for key '$key'." );
255 return $editInfo; // assume nothing changed
258 $dbr = wfGetDB( DB_SLAVE );
259 // Check that no templates used in the output changed...
260 $cWhr = array(); // conditions to find changes/creations
261 $dWhr = array(); // conditions to find deletions
262 foreach ( $editInfo->output->getTemplateIds() as $ns => $stuff ) {
263 foreach ( $stuff as $dbkey => $revId ) {
264 $cWhr[] = array( 'page_namespace' => $ns, 'page_title' => $dbkey,
265 'page_latest != ' . intval( $revId ) );
266 $dWhr[] = array( 'page_namespace' => $ns, 'page_title' => $dbkey );
269 $change = $dbr->selectField( 'page', '1', $dbr->makeList( $cWhr, LIST_OR ), __METHOD__ );
270 $n = $dbr->selectField( 'page', 'COUNT(*)', $dbr->makeList( $dWhr, LIST_OR ), __METHOD__ );
271 if ( $change || $n != count( $dWhr ) ) {
272 wfDebugLog( 'StashEdit', "Stale cache for key '$key'; template changed." );
273 return false;
276 // Check that no files used in the output changed...
277 $cWhr = array(); // conditions to find changes/creations
278 $dWhr = array(); // conditions to find deletions
279 foreach ( $editInfo->output->getFileSearchOptions() as $name => $options ) {
280 $cWhr[] = array( 'img_name' => $dbkey,
281 'img_sha1 != ' . $dbr->addQuotes( strval( $options['sha1'] ) ) );
282 $dWhr[] = array( 'img_name' => $dbkey );
284 $change = $dbr->selectField( 'image', '1', $dbr->makeList( $cWhr, LIST_OR ), __METHOD__ );
285 $n = $dbr->selectField( 'image', 'COUNT(*)', $dbr->makeList( $dWhr, LIST_OR ), __METHOD__ );
286 if ( $change || $n != count( $dWhr ) ) {
287 wfDebugLog( 'StashEdit', "Stale cache for key '$key'; file changed." );
288 return false;
291 wfDebugLog( 'StashEdit', "Cache hit for key '$key'." );
293 return $editInfo;
297 * Get the temporary prepared edit stash key for a user
299 * This key can be used for caching prepared edits provided:
300 * - a) The $user was used for PST options
301 * - b) The parser output was made from the PST using cannonical matching options
303 * @param Title $title
304 * @param Content $content
305 * @param User $user User to get parser options from
306 * @return string
308 protected static function getStashKey( Title $title, Content $content, User $user ) {
309 $hash = sha1( implode( ':', array(
310 $content->getModel(),
311 $content->getDefaultFormat(),
312 sha1( $content->serialize( $content->getDefaultFormat() ) ),
313 $user->getId() ?: md5( $user->getName() ), // account for user parser options
314 $user->getId() ? $user->getTouched() : '-' // handle preference change races
315 ) ) );
317 return wfMemcKey( 'prepared-edit', md5( $title->getPrefixedDBkey() ), $hash );
321 * Build a value to store in memcached based on the PST content and parser output
323 * This makes a simple version of WikiPage::prepareContentForEdit() as stash info
325 * @param Content $pstContent
326 * @param ParserOutput $parserOutput
327 * @param string $timestamp TS_MW
328 * @return array (stash info array, TTL in seconds) or (null, 0)
330 protected static function buildStashValue(
331 Content $pstContent, ParserOutput $parserOutput, $timestamp
333 // If an item is renewed, mind the cache TTL determined by config and parser functions
334 $since = time() - wfTimestamp( TS_UNIX, $parserOutput->getTimestamp() );
335 $ttl = min( $parserOutput->getCacheExpiry() - $since, 5 * 60 );
337 // Note: ParserOutput with that contains secondary data update callbacks can not be
338 // stashed, since the callbacks are not serializable (see ParserOutput::__sleep).
339 $hasCustomDataUpdates = $parserOutput->hasCustomDataUpdates();
341 if ( $ttl > 0 && !$parserOutput->getFlag( 'vary-revision' ) && !$hasCustomDataUpdates ) {
342 // Only store what is actually needed
343 $stashInfo = (object)array(
344 'pstContent' => $pstContent,
345 'output' => $parserOutput,
346 'timestamp' => $timestamp
348 return array( $stashInfo, $ttl );
351 return array( null, 0 );
354 public function getAllowedParams() {
355 return array(
356 'title' => array(
357 ApiBase::PARAM_TYPE => 'string',
358 ApiBase::PARAM_REQUIRED => true
360 'section' => array(
361 ApiBase::PARAM_TYPE => 'string',
363 'sectiontitle' => array(
364 ApiBase::PARAM_TYPE => 'string'
366 'text' => array(
367 ApiBase::PARAM_TYPE => 'string',
368 ApiBase::PARAM_REQUIRED => true
370 'contentmodel' => array(
371 ApiBase::PARAM_TYPE => ContentHandler::getContentModels(),
372 ApiBase::PARAM_REQUIRED => true
374 'contentformat' => array(
375 ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
376 ApiBase::PARAM_REQUIRED => true
378 'baserevid' => array(
379 ApiBase::PARAM_TYPE => 'integer',
380 ApiBase::PARAM_REQUIRED => true
385 function needsToken() {
386 return 'csrf';
389 function mustBePosted() {
390 return true;
393 function isInternal() {
394 return true;