Merge "Handle phpunit being autoloaded from checkLess.php"
[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 * @author Aaron Schulz
20 */
21
22 /**
23 * Prepare and edit in shared cache so that it can be reused on edit
24 *
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.
31 *
32 * @ingroup API
33 * @since 1.25
34 */
35 class ApiStashEdit extends ApiBase {
36 public function execute() {
37 global $wgMemc;
38
39 $user = $this->getUser();
40 $params = $this->extractRequestParams();
41
42 $page = $this->getTitleOrPageId( $params );
43 $title = $page->getTitle();
44
45 if ( !ContentHandler::getForModelID( $params['contentmodel'] )
46 ->isSupportedFormat( $params['contentformat'] )
47 ) {
48 $this->dieUsage( "Unsupported content model/format", 'badmodelformat' );
49 }
50
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'] );
55
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' );
62 }
63 $currentRev = $page->getRevision();
64 if ( !$currentRev ) {
65 $this->dieUsage( "No current revision of page ID {$page->getId()}", 'missingrev' );
66 }
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()
73 );
74 if ( !$editContent ) {
75 $this->dieUsage( "Could not merge updated section.", 'replacefailed' );
76 }
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' );
86 }
87 $handler = ContentHandler::getForModelID( $baseContent->getModel() );
88 $content = $handler->merge3( $baseContent, $editContent, $currentContent );
89 }
90 } else {
91 // New pages: use the user-provided content model
92 $content = $textContent;
93 }
94
95 if ( !$content ) { // merge3() failed
96 $this->getResult()->addValue( null,
97 $this->getModuleName(), array( 'status' => 'editconflict' ) );
98 return;
99 }
100
101 // The user will abort the AJAX request by pressing "save", so ignore that
102 ignore_user_abort( true );
103
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 $contentFormat = $content->getDefaultFormat();
112 $editInfo = $page->prepareContentForEdit( $content, null, $user, $contentFormat );
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';
121 }
122
123 if ( $editInfo && $editInfo->output ) {
124 $parserOutput = $editInfo->output;
125 // If an item is renewed, mind the cache TTL determined by config and parser functions
126 $since = time() - wfTimestamp( TS_UNIX, $parserOutput->getTimestamp() );
127 $ttl = min( $parserOutput->getCacheExpiry() - $since, 5 * 60 );
128 if ( $ttl > 0 && !$parserOutput->getFlag( 'vary-revision' ) ) {
129 // Only store what is actually needed
130 $stashInfo = (object)array(
131 'pstContent' => $editInfo->pstContent,
132 'output' => $editInfo->output,
133 'timestamp' => $editInfo->timestamp
134 );
135 $ok = $wgMemc->set( $key, $stashInfo, $ttl );
136 if ( $ok ) {
137 $status = 'stashed';
138 wfDebugLog( 'StashEdit', "Cached parser output for key '$key'." );
139 } else {
140 $status = 'error';
141 wfDebugLog( 'StashEdit', "Failed to cache parser output for key '$key'." );
142 }
143 } else {
144 $status = 'uncacheable';
145 wfDebugLog( 'StashEdit', "Uncacheable parser output for key '$key'." );
146 }
147 }
148
149 $this->getResult()->addValue( null, $this->getModuleName(), array( 'status' => $status ) );
150 }
151
152 /**
153 * Get the temporary prepared edit stash key for a user
154 *
155 * @param Title $title
156 * @param Content $content
157 * @param User $user User to get parser options from
158 * @return string
159 */
160 protected static function getStashKey(
161 Title $title, Content $content, User $user
162 ) {
163 return wfMemcKey( 'prepared-edit',
164 md5( $title->getPrefixedDBkey() ), // handle rename races
165 $content->getModel(),
166 $content->getDefaultFormat(),
167 sha1( $content->serialize( $content->getDefaultFormat() ) ),
168 $user->getId() ?: md5( $user->getName() ), // account for user parser options
169 $user->getId() ? $user->getTouched() : '-' // handle preference change races
170 );
171 }
172
173 /**
174 * Check that a prepared edit is in cache and still up-to-date
175 *
176 * This method blocks if the prepared edit is already being rendered,
177 * waiting until rendering finishes before doing final validity checks.
178 *
179 * The cache is rejected if template or file changes are detected.
180 * Note that foreign template or file transclusions are not checked.
181 *
182 * The result is a map (pstContent,output,timestamp) with fields
183 * extracted directly from WikiPage::prepareContentForEdit().
184 *
185 * @param Title $title
186 * @param Content $content
187 * @param User $user User to get parser options from
188 * @return stdClass|bool Returns false on cache miss
189 */
190 public static function checkCache( Title $title, Content $content, User $user ) {
191 global $wgMemc;
192
193 $key = self::getStashKey( $title, $content, $user );
194 $editInfo = $wgMemc->get( $key );
195 if ( !is_object( $editInfo ) ) {
196 $start = microtime( true );
197 // We ignore user aborts and keep parsing. Block on any prior parsing
198 // so as to use it's results and make use of the time spent parsing.
199 if ( $wgMemc->lock( $key, 30, 30 ) ) {
200 $editInfo = $wgMemc->get( $key );
201 $wgMemc->unlock( $key );
202 }
203 $sec = microtime( true ) - $start;
204 wfDebugLog( 'StashEdit', "Waited $sec seconds on '$key'." );
205 }
206
207 if ( !is_object( $editInfo ) || !$editInfo->output ) {
208 wfDebugLog( 'StashEdit', "No cache value for key '$key'." );
209 return false;
210 }
211
212 $time = wfTimestamp( TS_UNIX, $editInfo->output->getTimestamp() );
213 if ( ( time() - $time ) <= 3 ) {
214 wfDebugLog( 'StashEdit', "Timestamp-based cache hit for key '$key'." );
215 return $editInfo; // assume nothing changed
216 }
217
218 $dbr = wfGetDB( DB_SLAVE );
219 // Check that no templates used in the output changed...
220 $cWhr = array(); // conditions to find changes/creations
221 $dWhr = array(); // conditions to find deletions
222 foreach ( $editInfo->output->getTemplateIds() as $ns => $stuff ) {
223 foreach ( $stuff as $dbkey => $revId ) {
224 $cWhr[] = array( 'page_namespace' => $ns, 'page_title' => $dbkey,
225 'page_latest != ' . intval( $revId ) );
226 $dWhr[] = array( 'page_namespace' => $ns, 'page_title' => $dbkey );
227 }
228 }
229 $change = $dbr->selectField( 'page', '1', $dbr->makeList( $cWhr, LIST_OR ), __METHOD__ );
230 $n = $dbr->selectField( 'page', 'COUNT(*)', $dbr->makeList( $dWhr, LIST_OR ), __METHOD__ );
231 if ( $change || $n != count( $dWhr ) ) {
232 wfDebugLog( 'StashEdit', "Stale cache for key '$key'; template changed." );
233 return false;
234 }
235
236 // Check that no files used in the output changed...
237 $cWhr = array(); // conditions to find changes/creations
238 $dWhr = array(); // conditions to find deletions
239 foreach ( $editInfo->output->getFileSearchOptions() as $name => $options ) {
240 $cWhr[] = array( 'img_name' => $dbkey,
241 'img_sha1 != ' . $dbr->addQuotes( strval( $options['sha1'] ) ) );
242 $dWhr[] = array( 'img_name' => $dbkey );
243 }
244 $change = $dbr->selectField( 'image', '1', $dbr->makeList( $cWhr, LIST_OR ), __METHOD__ );
245 $n = $dbr->selectField( 'image', 'COUNT(*)', $dbr->makeList( $dWhr, LIST_OR ), __METHOD__ );
246 if ( $change || $n != count( $dWhr ) ) {
247 wfDebugLog( 'StashEdit', "Stale cache for key '$key'; file changed." );
248 return false;
249 }
250
251 wfDebugLog( 'StashEdit', "Cache hit for key '$key'." );
252
253 return $editInfo;
254 }
255
256 public function getAllowedParams() {
257 return array(
258 'title' => array(
259 ApiBase::PARAM_TYPE => 'string',
260 ApiBase::PARAM_REQUIRED => true
261 ),
262 'section' => array(
263 ApiBase::PARAM_TYPE => 'string',
264 ),
265 'sectiontitle' => array(
266 ApiBase::PARAM_TYPE => 'string'
267 ),
268 'text' => array(
269 ApiBase::PARAM_TYPE => 'string',
270 ApiBase::PARAM_REQUIRED => true
271 ),
272 'contentmodel' => array(
273 ApiBase::PARAM_TYPE => ContentHandler::getContentModels(),
274 ApiBase::PARAM_REQUIRED => true
275 ),
276 'contentformat' => array(
277 ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
278 ApiBase::PARAM_REQUIRED => true
279 ),
280 'baserevid' => array(
281 ApiBase::PARAM_TYPE => 'integer',
282 ApiBase::PARAM_REQUIRED => true
283 )
284 );
285 }
286
287 function needsToken() {
288 return 'csrf';
289 }
290
291 function mustBePosted() {
292 return true;
293 }
294
295 function isInternal() {
296 return true;
297 }
298 }