Localisation updates from https://translatewiki.net.
[lhc/web/wiklou.git] / includes / api / ApiComparePages.php
1 <?php
2 /**
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 */
21
22 class ApiComparePages extends ApiBase {
23
24 private $guessed = false, $guessedTitle, $guessedModel, $props;
25
26 public function execute() {
27 $params = $this->extractRequestParams();
28
29 // Parameter validation
30 $this->requireAtLeastOneParameter( $params, 'fromtitle', 'fromid', 'fromrev', 'fromtext' );
31 $this->requireAtLeastOneParameter( $params, 'totitle', 'toid', 'torev', 'totext', 'torelative' );
32
33 $this->props = array_flip( $params['prop'] );
34
35 // Cache responses publicly by default. This may be overridden later.
36 $this->getMain()->setCacheMode( 'public' );
37
38 // Get the 'from' Revision and Content
39 list( $fromRev, $fromContent, $relRev ) = $this->getDiffContent( 'from', $params );
40
41 // Get the 'to' Revision and Content
42 if ( $params['torelative'] !== null ) {
43 if ( !$relRev ) {
44 $this->dieWithError( 'apierror-compare-relative-to-nothing' );
45 }
46 switch ( $params['torelative'] ) {
47 case 'prev':
48 // Swap 'from' and 'to'
49 $toRev = $fromRev;
50 $toContent = $fromContent;
51 $fromRev = $relRev->getPrevious();
52 $fromContent = $fromRev
53 ? $fromRev->getContent( Revision::FOR_THIS_USER, $this->getUser() )
54 : $toContent->getContentHandler()->makeEmptyContent();
55 if ( !$fromContent ) {
56 $this->dieWithError(
57 [ 'apierror-missingcontent-revid', $fromRev->getId() ], 'missingcontent'
58 );
59 }
60 break;
61
62 case 'next':
63 $toRev = $relRev->getNext();
64 $toContent = $toRev
65 ? $toRev->getContent( Revision::FOR_THIS_USER, $this->getUser() )
66 : $fromContent;
67 if ( !$toContent ) {
68 $this->dieWithError( [ 'apierror-missingcontent-revid', $toRev->getId() ], 'missingcontent' );
69 }
70 break;
71
72 case 'cur':
73 $title = $relRev->getTitle();
74 $id = $title->getLatestRevID();
75 $toRev = $id ? Revision::newFromId( $id ) : null;
76 if ( !$toRev ) {
77 $this->dieWithError(
78 [ 'apierror-missingrev-title', wfEscapeWikiText( $title->getPrefixedText() ) ], 'nosuchrevid'
79 );
80 }
81 $toContent = $toRev->getContent( Revision::FOR_THIS_USER, $this->getUser() );
82 if ( !$toContent ) {
83 $this->dieWithError( [ 'apierror-missingcontent-revid', $toRev->getId() ], 'missingcontent' );
84 }
85 break;
86 }
87 $relRev2 = null;
88 } else {
89 list( $toRev, $toContent, $relRev2 ) = $this->getDiffContent( 'to', $params );
90 }
91
92 // Should never happen, but just in case...
93 if ( !$fromContent || !$toContent ) {
94 $this->dieWithError( 'apierror-baddiff' );
95 }
96
97 // Get the diff
98 $context = new DerivativeContext( $this->getContext() );
99 if ( $relRev && $relRev->getTitle() ) {
100 $context->setTitle( $relRev->getTitle() );
101 } elseif ( $relRev2 && $relRev2->getTitle() ) {
102 $context->setTitle( $relRev2->getTitle() );
103 } else {
104 $this->guessTitleAndModel();
105 if ( $this->guessedTitle ) {
106 $context->setTitle( $this->guessedTitle );
107 }
108 }
109 $de = $fromContent->getContentHandler()->createDifferenceEngine(
110 $context,
111 $fromRev ? $fromRev->getId() : 0,
112 $toRev ? $toRev->getId() : 0,
113 /* $rcid = */ null,
114 /* $refreshCache = */ false,
115 /* $unhide = */ true
116 );
117 $de->setContent( $fromContent, $toContent );
118 $difftext = $de->getDiffBody();
119 if ( $difftext === false ) {
120 $this->dieWithError( 'apierror-baddiff' );
121 }
122
123 // Fill in the response
124 $vals = [];
125 $this->setVals( $vals, 'from', $fromRev );
126 $this->setVals( $vals, 'to', $toRev );
127
128 if ( isset( $this->props['rel'] ) ) {
129 if ( $fromRev ) {
130 $rev = $fromRev->getPrevious();
131 if ( $rev ) {
132 $vals['prev'] = $rev->getId();
133 }
134 }
135 if ( $toRev ) {
136 $rev = $toRev->getNext();
137 if ( $rev ) {
138 $vals['next'] = $rev->getId();
139 }
140 }
141 }
142
143 if ( isset( $this->props['diffsize'] ) ) {
144 $vals['diffsize'] = strlen( $difftext );
145 }
146 if ( isset( $this->props['diff'] ) ) {
147 ApiResult::setContentValue( $vals, 'body', $difftext );
148 }
149
150 $this->getResult()->addValue( null, $this->getModuleName(), $vals );
151 }
152
153 /**
154 * Guess an appropriate default Title and content model for this request
155 *
156 * Fills in $this->guessedTitle based on the first of 'fromrev',
157 * 'fromtitle', 'fromid', 'torev', 'totitle', and 'toid' that's present and
158 * valid.
159 *
160 * Fills in $this->guessedModel based on the Revision or Title used to
161 * determine $this->guessedTitle, or the 'fromcontentmodel' or
162 * 'tocontentmodel' parameters if no title was guessed.
163 */
164 private function guessTitleAndModel() {
165 if ( $this->guessed ) {
166 return;
167 }
168
169 $this->guessed = true;
170 $params = $this->extractRequestParams();
171
172 foreach ( [ 'from', 'to' ] as $prefix ) {
173 if ( $params["{$prefix}rev"] !== null ) {
174 $revId = $params["{$prefix}rev"];
175 $rev = Revision::newFromId( $revId );
176 if ( !$rev ) {
177 // Titles of deleted revisions aren't secret, per T51088
178 $arQuery = Revision::getArchiveQueryInfo();
179 $row = $this->getDB()->selectRow(
180 $arQuery['tables'],
181 array_merge(
182 $arQuery['fields'],
183 [ 'ar_namespace', 'ar_title' ]
184 ),
185 [ 'ar_rev_id' => $revId ],
186 __METHOD__,
187 [],
188 $arQuery['joins']
189 );
190 if ( $row ) {
191 $rev = Revision::newFromArchiveRow( $row );
192 }
193 }
194 if ( $rev ) {
195 $this->guessedTitle = $rev->getTitle();
196 $this->guessedModel = $rev->getContentModel();
197 break;
198 }
199 }
200
201 if ( $params["{$prefix}title"] !== null ) {
202 $title = Title::newFromText( $params["{$prefix}title"] );
203 if ( $title && !$title->isExternal() ) {
204 $this->guessedTitle = $title;
205 break;
206 }
207 }
208
209 if ( $params["{$prefix}id"] !== null ) {
210 $title = Title::newFromID( $params["{$prefix}id"] );
211 if ( $title ) {
212 $this->guessedTitle = $title;
213 break;
214 }
215 }
216 }
217
218 if ( !$this->guessedModel ) {
219 if ( $this->guessedTitle ) {
220 $this->guessedModel = $this->guessedTitle->getContentModel();
221 } elseif ( $params['fromcontentmodel'] !== null ) {
222 $this->guessedModel = $params['fromcontentmodel'];
223 } elseif ( $params['tocontentmodel'] !== null ) {
224 $this->guessedModel = $params['tocontentmodel'];
225 }
226 }
227 }
228
229 /**
230 * Get the Revision and Content for one side of the diff
231 *
232 * This uses the appropriate set of 'rev', 'id', 'title', 'text', 'pst',
233 * 'contentmodel', and 'contentformat' parameters to determine what content
234 * should be diffed.
235 *
236 * Returns three values:
237 * - The revision used to retrieve the content, if any
238 * - The content to be diffed
239 * - The revision specified, if any, even if not used to retrieve the
240 * Content
241 *
242 * @param string $prefix 'from' or 'to'
243 * @param array $params
244 * @return array [ Revision|null, Content, Revision|null ]
245 */
246 private function getDiffContent( $prefix, array $params ) {
247 $title = null;
248 $rev = null;
249 $suppliedContent = $params["{$prefix}text"] !== null;
250
251 // Get the revision and title, if applicable
252 $revId = null;
253 if ( $params["{$prefix}rev"] !== null ) {
254 $revId = $params["{$prefix}rev"];
255 } elseif ( $params["{$prefix}title"] !== null || $params["{$prefix}id"] !== null ) {
256 if ( $params["{$prefix}title"] !== null ) {
257 $title = Title::newFromText( $params["{$prefix}title"] );
258 if ( !$title || $title->isExternal() ) {
259 $this->dieWithError(
260 [ 'apierror-invalidtitle', wfEscapeWikiText( $params["{$prefix}title"] ) ]
261 );
262 }
263 } else {
264 $title = Title::newFromID( $params["{$prefix}id"] );
265 if ( !$title ) {
266 $this->dieWithError( [ 'apierror-nosuchpageid', $params["{$prefix}id"] ] );
267 }
268 }
269 $revId = $title->getLatestRevID();
270 if ( !$revId ) {
271 $revId = null;
272 // Only die here if we're not using supplied text
273 if ( !$suppliedContent ) {
274 if ( $title->exists() ) {
275 $this->dieWithError(
276 [ 'apierror-missingrev-title', wfEscapeWikiText( $title->getPrefixedText() ) ], 'nosuchrevid'
277 );
278 } else {
279 $this->dieWithError(
280 [ 'apierror-missingtitle-byname', wfEscapeWikiText( $title->getPrefixedText() ) ],
281 'missingtitle'
282 );
283 }
284 }
285 }
286 }
287 if ( $revId !== null ) {
288 $rev = Revision::newFromId( $revId );
289 if ( !$rev && $this->getUser()->isAllowedAny( 'deletedtext', 'undelete' ) ) {
290 // Try the 'archive' table
291 $arQuery = Revision::getArchiveQueryInfo();
292 $row = $this->getDB()->selectRow(
293 $arQuery['tables'],
294 array_merge(
295 $arQuery['fields'],
296 [ 'ar_namespace', 'ar_title' ]
297 ),
298 [ 'ar_rev_id' => $revId ],
299 __METHOD__,
300 [],
301 $arQuery['joins']
302 );
303 if ( $row ) {
304 $rev = Revision::newFromArchiveRow( $row );
305 $rev->isArchive = true;
306 }
307 }
308 if ( !$rev ) {
309 $this->dieWithError( [ 'apierror-nosuchrevid', $revId ] );
310 }
311 $title = $rev->getTitle();
312
313 // If we don't have supplied content, return here. Otherwise,
314 // continue on below with the supplied content.
315 if ( !$suppliedContent ) {
316 $content = $rev->getContent( Revision::FOR_THIS_USER, $this->getUser() );
317 if ( !$content ) {
318 $this->dieWithError( [ 'apierror-missingcontent-revid', $revId ], 'missingcontent' );
319 }
320 return [ $rev, $content, $rev ];
321 }
322 }
323
324 // Override $content based on supplied text
325 $model = $params["{$prefix}contentmodel"];
326 $format = $params["{$prefix}contentformat"];
327
328 if ( !$model && $rev ) {
329 $model = $rev->getContentModel();
330 }
331 if ( !$model && $title ) {
332 $model = $title->getContentModel();
333 }
334 if ( !$model ) {
335 $this->guessTitleAndModel();
336 $model = $this->guessedModel;
337 }
338 if ( !$model ) {
339 $model = CONTENT_MODEL_WIKITEXT;
340 $this->addWarning( [ 'apiwarn-compare-nocontentmodel', $model ] );
341 }
342
343 if ( !$title ) {
344 $this->guessTitleAndModel();
345 $title = $this->guessedTitle;
346 }
347
348 try {
349 $content = ContentHandler::makeContent( $params["{$prefix}text"], $title, $model, $format );
350 } catch ( MWContentSerializationException $ex ) {
351 $this->dieWithException( $ex, [
352 'wrap' => ApiMessage::create( 'apierror-contentserializationexception', 'parseerror' )
353 ] );
354 }
355
356 if ( $params["{$prefix}pst"] ) {
357 if ( !$title ) {
358 $this->dieWithError( 'apierror-compare-no-title' );
359 }
360 $popts = ParserOptions::newFromContext( $this->getContext() );
361 $content = $content->preSaveTransform( $title, $this->getUser(), $popts );
362 }
363
364 return [ null, $content, $rev ];
365 }
366
367 /**
368 * Set value fields from a Revision object
369 * @param array &$vals Result array to set data into
370 * @param string $prefix 'from' or 'to'
371 * @param Revision|null $rev
372 */
373 private function setVals( &$vals, $prefix, $rev ) {
374 if ( $rev ) {
375 $title = $rev->getTitle();
376 if ( isset( $this->props['ids'] ) ) {
377 $vals["{$prefix}id"] = $title->getArticleId();
378 $vals["{$prefix}revid"] = $rev->getId();
379 }
380 if ( isset( $this->props['title'] ) ) {
381 ApiQueryBase::addTitleInfo( $vals, $title, $prefix );
382 }
383 if ( isset( $this->props['size'] ) ) {
384 $vals["{$prefix}size"] = $rev->getSize();
385 }
386
387 $anyHidden = false;
388 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
389 $vals["{$prefix}texthidden"] = true;
390 $anyHidden = true;
391 }
392
393 if ( $rev->isDeleted( Revision::DELETED_USER ) ) {
394 $vals["{$prefix}userhidden"] = true;
395 $anyHidden = true;
396 }
397 if ( isset( $this->props['user'] ) &&
398 $rev->userCan( Revision::DELETED_USER, $this->getUser() )
399 ) {
400 $vals["{$prefix}user"] = $rev->getUserText( Revision::RAW );
401 $vals["{$prefix}userid"] = $rev->getUser( Revision::RAW );
402 }
403
404 if ( $rev->isDeleted( Revision::DELETED_COMMENT ) ) {
405 $vals["{$prefix}commenthidden"] = true;
406 $anyHidden = true;
407 }
408 if ( $rev->userCan( Revision::DELETED_COMMENT, $this->getUser() ) ) {
409 if ( isset( $this->props['comment'] ) ) {
410 $vals["{$prefix}comment"] = $rev->getComment( Revision::RAW );
411 }
412 if ( isset( $this->props['parsedcomment'] ) ) {
413 $vals["{$prefix}parsedcomment"] = Linker::formatComment(
414 $rev->getComment( Revision::RAW ),
415 $rev->getTitle()
416 );
417 }
418 }
419
420 if ( $anyHidden ) {
421 $this->getMain()->setCacheMode( 'private' );
422 if ( $rev->isDeleted( Revision::DELETED_RESTRICTED ) ) {
423 $vals["{$prefix}suppressed"] = true;
424 }
425 }
426
427 if ( !empty( $rev->isArchive ) ) {
428 $this->getMain()->setCacheMode( 'private' );
429 $vals["{$prefix}archive"] = true;
430 }
431 }
432 }
433
434 public function getAllowedParams() {
435 // Parameters for the 'from' and 'to' content
436 $fromToParams = [
437 'title' => null,
438 'id' => [
439 ApiBase::PARAM_TYPE => 'integer'
440 ],
441 'rev' => [
442 ApiBase::PARAM_TYPE => 'integer'
443 ],
444 'text' => [
445 ApiBase::PARAM_TYPE => 'text'
446 ],
447 'pst' => false,
448 'contentformat' => [
449 ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
450 ],
451 'contentmodel' => [
452 ApiBase::PARAM_TYPE => ContentHandler::getContentModels(),
453 ]
454 ];
455
456 $ret = [];
457 foreach ( $fromToParams as $k => $v ) {
458 $ret["from$k"] = $v;
459 }
460 foreach ( $fromToParams as $k => $v ) {
461 $ret["to$k"] = $v;
462 }
463
464 $ret = wfArrayInsertAfter(
465 $ret,
466 [ 'torelative' => [ ApiBase::PARAM_TYPE => [ 'prev', 'next', 'cur' ], ] ],
467 'torev'
468 );
469
470 $ret['prop'] = [
471 ApiBase::PARAM_DFLT => 'diff|ids|title',
472 ApiBase::PARAM_TYPE => [
473 'diff',
474 'diffsize',
475 'rel',
476 'ids',
477 'title',
478 'user',
479 'comment',
480 'parsedcomment',
481 'size',
482 ],
483 ApiBase::PARAM_ISMULTI => true,
484 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
485 ];
486
487 return $ret;
488 }
489
490 protected function getExamplesMessages() {
491 return [
492 'action=compare&fromrev=1&torev=2'
493 => 'apihelp-compare-example-1',
494 ];
495 }
496 }