Debian Codegraph @

includes/api/ApiComparePages.php

raw symbols SCIP
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 namespace MediaWiki\Api;
22
23 use DifferenceEngine;
24 use Exception;
25 use MediaWiki\CommentFormatter\CommentFormatter;
26 use MediaWiki\Content\IContentHandlerFactory;
27 use MediaWiki\Content\Transform\ContentTransformer;
28 use MediaWiki\Context\DerivativeContext;
29 use MediaWiki\Parser\ParserOptions;
30 use MediaWiki\Revision\ArchivedRevisionLookup;
31 use MediaWiki\Revision\MutableRevisionRecord;
32 use MediaWiki\Revision\RevisionArchiveRecord;
33 use MediaWiki\Revision\RevisionRecord;
34 use MediaWiki\Revision\RevisionStore;
35 use MediaWiki\Revision\SlotRecord;
36 use MediaWiki\Revision\SlotRoleRegistry;
37 use MediaWiki\Title\Title;
38 use MediaWiki\User\TempUser\TempUserCreator;
39 use MediaWiki\User\UserFactory;
40 use MWContentSerializationException;
41 use Wikimedia\ParamValidator\ParamValidator;
42 use Wikimedia\RequestTimeout\TimeoutException;
43
44 /**
45 * @ingroup API
46 */
47 class ApiComparePages extends ApiBase {
48
49 private RevisionStore $revisionStore;
50 private ArchivedRevisionLookup $archivedRevisionLookup;
51 private SlotRoleRegistry $slotRoleRegistry;
52
53 /** @var Title|null|false */
54 private $guessedTitle = false;
55 /** @var array<string,true> */
56 private $props;
57
58 private IContentHandlerFactory $contentHandlerFactory;
59 private ContentTransformer $contentTransformer;
60 private CommentFormatter $commentFormatter;
61 private TempUserCreator $tempUserCreator;
62 private UserFactory $userFactory;
63 private DifferenceEngine $differenceEngine;
64
65 public function __construct(
66 ApiMain $mainModule,
67 string $moduleName,
68 RevisionStore $revisionStore,
69 ArchivedRevisionLookup $archivedRevisionLookup,
70 SlotRoleRegistry $slotRoleRegistry,
71 IContentHandlerFactory $contentHandlerFactory,
72 ContentTransformer $contentTransformer,
73 CommentFormatter $commentFormatter,
74 TempUserCreator $tempUserCreator,
75 UserFactory $userFactory
76 ) {
77 parent::__construct( $mainModule, $moduleName );
78 $this->revisionStore = $revisionStore;
79 $this->archivedRevisionLookup = $archivedRevisionLookup;
80 $this->slotRoleRegistry = $slotRoleRegistry;
81 $this->contentHandlerFactory = $contentHandlerFactory;
82 $this->contentTransformer = $contentTransformer;
83 $this->commentFormatter = $commentFormatter;
84 $this->tempUserCreator = $tempUserCreator;
85 $this->userFactory = $userFactory;
86 $this->differenceEngine = new DifferenceEngine;
87 }
88
89 public function execute() {
90 $params = $this->extractRequestParams();
91
92 // Parameter validation
93 $this->requireAtLeastOneParameter(
94 $params, 'fromtitle', 'fromid', 'fromrev', 'fromtext', 'fromslots'
95 );
96 $this->requireAtLeastOneParameter(
97 $params, 'totitle', 'toid', 'torev', 'totext', 'torelative', 'toslots'
98 );
99
100 $this->props = array_fill_keys( $params['prop'], true );
101
102 // Cache responses publicly by default. This may be overridden later.
103 $this->getMain()->setCacheMode( 'public' );
104
105 // Get the 'from' RevisionRecord
106 [ $fromRev, $fromRelRev, $fromValsRev ] = $this->getDiffRevision( 'from', $params );
107
108 // Get the 'to' RevisionRecord
109 if ( $params['torelative'] !== null ) {
110 if ( !$fromRelRev ) {
111 $this->dieWithError( 'apierror-compare-relative-to-nothing' );
112 }
113 if ( $params['torelative'] !== 'cur' && $fromRelRev instanceof RevisionArchiveRecord ) {
114 // RevisionStore's getPreviousRevision/getNextRevision blow up
115 // when passed an RevisionArchiveRecord for a deleted page
116 $this->dieWithError( [ 'apierror-compare-relative-to-deleted', $params['torelative'] ] );
117 }
118 switch ( $params['torelative'] ) {
119 case 'prev':
120 // Swap 'from' and 'to'
121 [ $toRev, $toRelRev, $toValsRev ] = [ $fromRev, $fromRelRev, $fromValsRev ];
122 $fromRev = $this->revisionStore->getPreviousRevision( $toRelRev );
123 $fromRelRev = $fromRev;
124 $fromValsRev = $fromRev;
125 if ( !$fromRev ) {
126 $title = Title::newFromLinkTarget( $toRelRev->getPageAsLinkTarget() );
127 $this->addWarning( [
128 'apiwarn-compare-no-prev',
129 wfEscapeWikiText( $title->getPrefixedText() ),
130 $toRelRev->getId()
131 ] );
132
133 // (T203433) Create an empty dummy revision as the "previous".
134 // The main slot has to exist, the rest will be handled by DifferenceEngine.
135 $fromRev = new MutableRevisionRecord(
136 $title ?: $toRev->getPage()
137 );
138 $fromRev->setContent(
139 SlotRecord::MAIN,
140 $toRelRev->getContent( SlotRecord::MAIN, RevisionRecord::RAW )
141 ->getContentHandler()
142 ->makeEmptyContent()
143 );
144 }
145 break;
146
147 case 'next':
148 $toRev = $this->revisionStore->getNextRevision( $fromRelRev );
149 $toRelRev = $toRev;
150 $toValsRev = $toRev;
151 if ( !$toRev ) {
152 $title = Title::newFromLinkTarget( $fromRelRev->getPageAsLinkTarget() );
153 $this->addWarning( [
154 'apiwarn-compare-no-next',
155 wfEscapeWikiText( $title->getPrefixedText() ),
156 $fromRelRev->getId()
157 ] );
158
159 // (T203433) The web UI treats "next" as "cur" in this case.
160 // Avoid repeating metadata by making a MutableRevisionRecord with no changes.
161 $toRev = MutableRevisionRecord::newFromParentRevision( $fromRelRev );
162 }
163 break;
164
165 case 'cur':
166 $title = $fromRelRev->getPageAsLinkTarget();
167 $toRev = $this->revisionStore->getRevisionByTitle( $title );
168 if ( !$toRev ) {
169 $title = Title::newFromLinkTarget( $title );
170 $this->dieWithError(
171 [ 'apierror-missingrev-title', wfEscapeWikiText( $title->getPrefixedText() ) ],
172 'nosuchrevid'
173 );
174 }
175 $toRelRev = $toRev;
176 $toValsRev = $toRev;
177 break;
178 }
179 } else {
180 [ $toRev, $toRelRev, $toValsRev ] = $this->getDiffRevision( 'to', $params );
181 }
182
183 // Handle missing from or to revisions (should never happen)
184 // @codeCoverageIgnoreStart
185 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable T240141
186 if ( !$fromRev || !$toRev ) {
187 $this->dieWithError( 'apierror-baddiff' );
188 }
189 // @codeCoverageIgnoreEnd
190
191 // Handle revdel
192 if ( !$fromRev->userCan( RevisionRecord::DELETED_TEXT, $this->getAuthority() ) ) {
193 $this->dieWithError( [ 'apierror-missingcontent-revid', $fromRev->getId() ], 'missingcontent' );
194 }
195 if ( !$toRev->userCan( RevisionRecord::DELETED_TEXT, $this->getAuthority() ) ) {
196 $this->dieWithError( [ 'apierror-missingcontent-revid', $toRev->getId() ], 'missingcontent' );
197 }
198
199 // Get the diff
200 $context = new DerivativeContext( $this->getContext() );
201 if ( $fromRelRev && $fromRelRev->getPageAsLinkTarget() ) {
202 $context->setTitle( Title::newFromLinkTarget( $fromRelRev->getPageAsLinkTarget() ) );
203 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable T240141
204 } elseif ( $toRelRev && $toRelRev->getPageAsLinkTarget() ) {
205 $context->setTitle( Title::newFromLinkTarget( $toRelRev->getPageAsLinkTarget() ) );
206 } else {
207 $guessedTitle = $this->guessTitle();
208 if ( $guessedTitle ) {
209 $context->setTitle( $guessedTitle );
210 }
211 }
212 $this->differenceEngine->setContext( $context );
213 $this->differenceEngine->setSlotDiffOptions( [ 'diff-type' => $params['difftype'] ] );
214 $this->differenceEngine->setRevisions( $fromRev, $toRev );
215 if ( $params['slots'] === null ) {
216 $difftext = $this->differenceEngine->getDiffBody();
217 if ( $difftext === false ) {
218 $this->dieWithError( 'apierror-baddiff' );
219 }
220 } else {
221 $difftext = [];
222 foreach ( $params['slots'] as $role ) {
223 $difftext[$role] = $this->differenceEngine->getDiffBodyForRole( $role );
224 }
225 }
226 foreach ( $this->differenceEngine->getRevisionLoadErrors() as $msg ) {
227 $this->addWarning( $msg );
228 }
229
230 // Fill in the response
231 $vals = [];
232 $this->setVals( $vals, 'from', $fromValsRev );
233 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable T240141
234 $this->setVals( $vals, 'to', $toValsRev );
235
236 if ( isset( $this->props['rel'] ) ) {
237 if ( !$fromRev instanceof MutableRevisionRecord ) {
238 $rev = $this->revisionStore->getPreviousRevision( $fromRev );
239 if ( $rev ) {
240 $vals['prev'] = $rev->getId();
241 }
242 }
243 if ( !$toRev instanceof MutableRevisionRecord ) {
244 $rev = $this->revisionStore->getNextRevision( $toRev );
245 if ( $rev ) {
246 $vals['next'] = $rev->getId();
247 }
248 }
249 }
250
251 if ( isset( $this->props['diffsize'] ) ) {
252 $vals['diffsize'] = 0;
253 foreach ( (array)$difftext as $text ) {
254 $vals['diffsize'] += strlen( $text );
255 }
256 }
257 if ( isset( $this->props['diff'] ) ) {
258 if ( is_array( $difftext ) ) {
259 ApiResult::setArrayType( $difftext, 'kvp', 'diff' );
260 $vals['bodies'] = $difftext;
261 } else {
262 ApiResult::setContentValue( $vals, 'body', $difftext );
263 }
264 }
265
266 // Diffs can be really big and there's little point in having
267 // ApiResult truncate it to an empty response since the diff is the
268 // whole reason this module exists. So pass NO_SIZE_CHECK here.
269 $this->getResult()->addValue( null, $this->getModuleName(), $vals, ApiResult::NO_SIZE_CHECK );
270 }
271
272 /**
273 * Load a revision by ID
274 *
275 * Falls back to checking the archive table if appropriate.
276 *
277 * @param int $id
278 * @return RevisionRecord|null
279 */
280 private function getRevisionById( $id ) {
281 $rev = $this->revisionStore->getRevisionById( $id );
282
283 if ( $rev ) {
284 $this->checkTitleUserPermissions( $rev->getPage(), 'read' );
285 }
286
287 if ( !$rev && $this->getAuthority()->isAllowedAny( 'deletedtext', 'undelete' ) ) {
288 // Try the 'archive' table
289 $rev = $this->archivedRevisionLookup->getArchivedRevisionRecord( null, $id );
290
291 if ( $rev ) {
292 $this->checkTitleUserPermissions( $rev->getPage(), 'deletedtext' );
293 }
294 }
295 return $rev;
296 }
297
298 /**
299 * Guess an appropriate default Title for this request
300 *
301 * @return Title|null
302 */
303 private function guessTitle() {
304 if ( $this->guessedTitle !== false ) {
305 return $this->guessedTitle;
306 }
307
308 $this->guessedTitle = null;
309 $params = $this->extractRequestParams();
310
311 foreach ( [ 'from', 'to' ] as $prefix ) {
312 if ( $params["{$prefix}rev"] !== null ) {
313 $rev = $this->getRevisionById( $params["{$prefix}rev"] );
314 if ( $rev ) {
315 $this->guessedTitle = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
316 break;
317 }
318 }
319
320 if ( $params["{$prefix}title"] !== null ) {
321 $title = Title::newFromText( $params["{$prefix}title"] );
322 if ( $title && !$title->isExternal() ) {
323 $this->guessedTitle = $title;
324 break;
325 }
326 }
327
328 if ( $params["{$prefix}id"] !== null ) {
329 $title = Title::newFromID( $params["{$prefix}id"] );
330 if ( $title ) {
331 $this->guessedTitle = $title;
332 break;
333 }
334 }
335 }
336
337 return $this->guessedTitle;
338 }
339
340 /**
341 * Guess an appropriate default content model for this request
342 * @param string $role Slot for which to guess the model
343 * @return string|null Guessed content model
344 */
345 private function guessModel( $role ) {
346 $params = $this->extractRequestParams();
347
348 foreach ( [ 'from', 'to' ] as $prefix ) {
349 if ( $params["{$prefix}rev"] !== null ) {
350 $rev = $this->getRevisionById( $params["{$prefix}rev"] );
351 if ( $rev && $rev->hasSlot( $role ) ) {
352 return $rev->getSlot( $role, RevisionRecord::RAW )->getModel();
353 }
354 }
355 }
356
357 $guessedTitle = $this->guessTitle();
358 if ( $guessedTitle ) {
359 return $this->slotRoleRegistry->getRoleHandler( $role )->getDefaultModel( $guessedTitle );
360 }
361
362 if ( isset( $params["fromcontentmodel-$role"] ) ) {
363 return $params["fromcontentmodel-$role"];
364 }
365 if ( isset( $params["tocontentmodel-$role"] ) ) {
366 return $params["tocontentmodel-$role"];
367 }
368
369 if ( $role === SlotRecord::MAIN ) {
370 if ( isset( $params['fromcontentmodel'] ) ) {
371 return $params['fromcontentmodel'];
372 }
373 if ( isset( $params['tocontentmodel'] ) ) {
374 return $params['tocontentmodel'];
375 }
376 }
377
378 return null;
379 }
380
381 /**
382 * Get the RevisionRecord for one side of the diff
383 *
384 * This uses the appropriate set of parameters to determine what content
385 * should be diffed.
386 *
387 * Returns three values:
388 * - A RevisionRecord holding the content
389 * - The revision specified, if any, even if content was supplied
390 * - The revision to pass to setVals(), if any
391 *
392 * @param string $prefix 'from' or 'to'
393 * @param array $params
394 * @return array [ RevisionRecord|null, RevisionRecord|null, RevisionRecord|null ]
395 */
396 private function getDiffRevision( $prefix, array $params ) {
397 // Back compat params
398 $this->requireMaxOneParameter( $params, "{$prefix}text", "{$prefix}slots" );
399 $this->requireMaxOneParameter( $params, "{$prefix}section", "{$prefix}slots" );
400 if ( $params["{$prefix}text"] !== null ) {
401 $params["{$prefix}slots"] = [ SlotRecord::MAIN ];
402 $params["{$prefix}text-main"] = $params["{$prefix}text"];
403 $params["{$prefix}section-main"] = null;
404 $params["{$prefix}contentmodel-main"] = $params["{$prefix}contentmodel"];
405 $params["{$prefix}contentformat-main"] = $params["{$prefix}contentformat"];
406 }
407
408 $title = null;
409 $rev = null;
410 $suppliedContent = $params["{$prefix}slots"] !== null;
411
412 // Get the revision and title, if applicable
413 $revId = null;
414 if ( $params["{$prefix}rev"] !== null ) {
415 $revId = $params["{$prefix}rev"];
416 } elseif ( $params["{$prefix}title"] !== null || $params["{$prefix}id"] !== null ) {
417 if ( $params["{$prefix}title"] !== null ) {
418 $title = Title::newFromText( $params["{$prefix}title"] );
419 if ( !$title || $title->isExternal() ) {
420 $this->dieWithError(
421 [ 'apierror-invalidtitle', wfEscapeWikiText( $params["{$prefix}title"] ) ]
422 );
423 }
424 } else {
425 $title = Title::newFromID( $params["{$prefix}id"] );
426 if ( !$title ) {
427 $this->dieWithError( [ 'apierror-nosuchpageid', $params["{$prefix}id"] ] );
428 }
429 }
430 $revId = $title->getLatestRevID();
431 if ( !$revId ) {
432 $revId = null;
433 // Only die here if we're not using supplied text
434 if ( !$suppliedContent ) {
435 if ( $title->exists() ) {
436 $this->dieWithError(
437 [ 'apierror-missingrev-title', wfEscapeWikiText( $title->getPrefixedText() ) ],
438 'nosuchrevid'
439 );
440 } else {
441 $this->dieWithError(
442 [ 'apierror-missingtitle-byname', wfEscapeWikiText( $title->getPrefixedText() ) ],
443 'missingtitle'
444 );
445 }
446 }
447 }
448 }
449 if ( $revId !== null ) {
450 $rev = $this->getRevisionById( $revId );
451 if ( !$rev ) {
452 $this->dieWithError( [ 'apierror-nosuchrevid', $revId ] );
453 }
454 $title = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
455
456 // If we don't have supplied content, return here. Otherwise,
457 // continue on below with the supplied content.
458 if ( !$suppliedContent ) {
459 $newRev = $rev;
460
461 // Deprecated 'fromsection'/'tosection'
462 if ( isset( $params["{$prefix}section"] ) ) {
463 $section = $params["{$prefix}section"];
464 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable T240141
465 $newRev = MutableRevisionRecord::newFromParentRevision( $rev );
466 $content = $rev->getContent( SlotRecord::MAIN, RevisionRecord::FOR_THIS_USER,
467 $this->getAuthority() );
468 if ( !$content ) {
469 $this->dieWithError(
470 [ 'apierror-missingcontent-revid-role', $rev->getId(), SlotRecord::MAIN ], 'missingcontent'
471 );
472 }
473 $content = $content->getSection( $section );
474 if ( !$content ) {
475 $this->dieWithError(
476 [ "apierror-compare-nosuch{$prefix}section", wfEscapeWikiText( $section ) ],
477 "nosuch{$prefix}section"
478 );
479 }
480 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable T240141
481 $newRev->setContent( SlotRecord::MAIN, $content );
482 }
483
484 return [ $newRev, $rev, $rev ];
485 }
486 }
487
488 // Override $content based on supplied text
489 if ( !$title ) {
490 $title = $this->guessTitle();
491 }
492 if ( $rev ) {
493 $newRev = MutableRevisionRecord::newFromParentRevision( $rev );
494 } else {
495 $newRev = new MutableRevisionRecord( $title ?: Title::newMainPage() );
496 }
497 foreach ( $params["{$prefix}slots"] as $role ) {
498 $text = $params["{$prefix}text-{$role}"];
499 if ( $text === null ) {
500 // The SlotRecord::MAIN role can't be deleted
501 if ( $role === SlotRecord::MAIN ) {
502 $this->dieWithError( [ 'apierror-compare-maintextrequired', $prefix ] );
503 }
504
505 // These parameters make no sense without text. Reject them to avoid
506 // confusion.
507 foreach ( [ 'section', 'contentmodel', 'contentformat' ] as $param ) {
508 if ( isset( $params["{$prefix}{$param}-{$role}"] ) ) {
509 $this->dieWithError( [
510 'apierror-compare-notext',
511 wfEscapeWikiText( "{$prefix}{$param}-{$role}" ),
512 wfEscapeWikiText( "{$prefix}text-{$role}" ),
513 ] );
514 }
515 }
516
517 $newRev->removeSlot( $role );
518 continue;
519 }
520
521 $model = $params["{$prefix}contentmodel-{$role}"];
522 $format = $params["{$prefix}contentformat-{$role}"];
523
524 if ( !$model && $rev && $rev->hasSlot( $role ) ) {
525 $model = $rev->getSlot( $role, RevisionRecord::RAW )->getModel();
526 }
527 if ( !$model && $title && $role === SlotRecord::MAIN ) {
528 // @todo: Use SlotRoleRegistry and do this for all slots
529 $model = $title->getContentModel();
530 }
531 if ( !$model ) {
532 $model = $this->guessModel( $role );
533 }
534 if ( !$model ) {
535 $model = CONTENT_MODEL_WIKITEXT;
536 $this->addWarning( [ 'apiwarn-compare-nocontentmodel', $model ] );
537 }
538
539 try {
540 $content = $this->contentHandlerFactory
541 ->getContentHandler( $model )
542 ->unserializeContent( $text, $format );
543 } catch ( MWContentSerializationException $ex ) {
544 $this->dieWithException( $ex, [
545 'wrap' => ApiMessage::create( 'apierror-contentserializationexception', 'parseerror' )
546 ] );
547 }
548
549 if ( $params["{$prefix}pst"] ) {
550 if ( !$title ) {
551 $this->dieWithError( 'apierror-compare-no-title' );
552 }
553 $popts = ParserOptions::newFromContext( $this->getContext() );
554 $content = $this->contentTransformer->preSaveTransform(
555 $content,
556 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable T240141
557 $title,
558 $this->getUserForPreview(),
559 $popts
560 );
561 }
562
563 $section = $params["{$prefix}section-{$role}"];
564 if ( $section !== null && $section !== '' ) {
565 if ( !$rev ) {
566 $this->dieWithError( "apierror-compare-no{$prefix}revision" );
567 }
568 $oldContent = $rev->getContent( $role, RevisionRecord::FOR_THIS_USER, $this->getAuthority() );
569 if ( !$oldContent ) {
570 $this->dieWithError(
571 [ 'apierror-missingcontent-revid-role', $rev->getId(), wfEscapeWikiText( $role ) ],
572 'missingcontent'
573 );
574 }
575 if ( !$oldContent->getContentHandler()->supportsSections() ) {
576 $this->dieWithError( [ 'apierror-sectionsnotsupported', $content->getModel() ] );
577 }
578 try {
579 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable T240141
580 $content = $oldContent->replaceSection( $section, $content, '' );
581 } catch ( TimeoutException $e ) {
582 throw $e;
583 } catch ( Exception $ex ) {
584 // Probably a content model mismatch.
585 $content = null;
586 }
587 if ( !$content ) {
588 $this->dieWithError( [ 'apierror-sectionreplacefailed' ] );
589 }
590 }
591
592 // Deprecated 'fromsection'/'tosection'
593 if ( $role === SlotRecord::MAIN && isset( $params["{$prefix}section"] ) ) {
594 $section = $params["{$prefix}section"];
595 $content = $content->getSection( $section );
596 if ( !$content ) {
597 $this->dieWithError(
598 [ "apierror-compare-nosuch{$prefix}section", wfEscapeWikiText( $section ) ],
599 "nosuch{$prefix}section"
600 );
601 }
602 }
603
604 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable T240141
605 $newRev->setContent( $role, $content );
606 }
607 return [ $newRev, $rev, null ];
608 }
609
610 /**
611 * Set value fields from a RevisionRecord object
612 *
613 * @param array &$vals Result array to set data into
614 * @param string $prefix 'from' or 'to'
615 * @param RevisionRecord|null $rev
616 */
617 private function setVals( &$vals, $prefix, $rev ) {
618 if ( $rev ) {
619 $title = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
620 if ( isset( $this->props['ids'] ) ) {
621 $vals["{$prefix}id"] = $title->getArticleID();
622 $vals["{$prefix}revid"] = $rev->getId();
623 }
624 if ( isset( $this->props['title'] ) ) {
625 ApiQueryBase::addTitleInfo( $vals, $title, $prefix );
626 }
627 if ( isset( $this->props['size'] ) ) {
628 $vals["{$prefix}size"] = $rev->getSize();
629 }
630 if ( isset( $this->props['timestamp'] ) ) {
631 $revTimestamp = $rev->getTimestamp();
632 if ( $revTimestamp ) {
633 $vals["{$prefix}timestamp"] = wfTimestamp( TS_ISO_8601, $revTimestamp );
634 }
635 }
636
637 $anyHidden = false;
638 if ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
639 $vals["{$prefix}texthidden"] = true;
640 $anyHidden = true;
641 }
642
643 if ( $rev->isDeleted( RevisionRecord::DELETED_USER ) ) {
644 $vals["{$prefix}userhidden"] = true;
645 $anyHidden = true;
646 }
647 if ( isset( $this->props['user'] ) ) {
648 $user = $rev->getUser( RevisionRecord::FOR_THIS_USER, $this->getAuthority() );
649 if ( $user ) {
650 $vals["{$prefix}user"] = $user->getName();
651 $vals["{$prefix}userid"] = $user->getId();
652 }
653 }
654
655 if ( $rev->isDeleted( RevisionRecord::DELETED_COMMENT ) ) {
656 $vals["{$prefix}commenthidden"] = true;
657 $anyHidden = true;
658 }
659 if ( isset( $this->props['comment'] ) || isset( $this->props['parsedcomment'] ) ) {
660 $comment = $rev->getComment( RevisionRecord::FOR_THIS_USER, $this->getAuthority() );
661 if ( $comment !== null ) {
662 if ( isset( $this->props['comment'] ) ) {
663 $vals["{$prefix}comment"] = $comment->text;
664 }
665 $vals["{$prefix}parsedcomment"] = $this->commentFormatter->format(
666 $comment->text, $title
667 );
668 }
669 }
670
671 if ( $anyHidden ) {
672 $this->getMain()->setCacheMode( 'private' );
673 if ( $rev->isDeleted( RevisionRecord::DELETED_RESTRICTED ) ) {
674 $vals["{$prefix}suppressed"] = true;
675 }
676 }
677
678 if ( $rev instanceof RevisionArchiveRecord ) {
679 $this->getMain()->setCacheMode( 'private' );
680 $vals["{$prefix}archive"] = true;
681 }
682 }
683 }
684
685 private function getUserForPreview() {
686 $user = $this->getUser();
687 if ( $this->tempUserCreator->shouldAutoCreate( $user, 'edit' ) ) {
688 return $this->userFactory->newUnsavedTempUser(
689 $this->tempUserCreator->getStashedName( $this->getRequest()->getSession() )
690 );
691 }
692 return $user;
693 }
694
695 public function getAllowedParams() {
696 $slotRoles = $this->slotRoleRegistry->getKnownRoles();
697 sort( $slotRoles, SORT_STRING );
698
699 // Parameters for the 'from' and 'to' content
700 $fromToParams = [
701 'title' => null,
702 'id' => [
703 ParamValidator::PARAM_TYPE => 'integer'
704 ],
705 'rev' => [
706 ParamValidator::PARAM_TYPE => 'integer'
707 ],
708
709 'slots' => [
710 ParamValidator::PARAM_TYPE => $slotRoles,
711 ParamValidator::PARAM_ISMULTI => true,
712 ],
713 'text-{slot}' => [
714 ApiBase::PARAM_TEMPLATE_VARS => [ 'slot' => 'slots' ], // fixed below
715 ParamValidator::PARAM_TYPE => 'text',
716 ],
717 'section-{slot}' => [
718 ApiBase::PARAM_TEMPLATE_VARS => [ 'slot' => 'slots' ], // fixed below
719 ParamValidator::PARAM_TYPE => 'string',
720 ],
721 'contentformat-{slot}' => [
722 ApiBase::PARAM_TEMPLATE_VARS => [ 'slot' => 'slots' ], // fixed below
723 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
724 ],
725 'contentmodel-{slot}' => [
726 ApiBase::PARAM_TEMPLATE_VARS => [ 'slot' => 'slots' ], // fixed below
727 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(),
728 ],
729 'pst' => false,
730
731 'text' => [
732 ParamValidator::PARAM_TYPE => 'text',
733 ParamValidator::PARAM_DEPRECATED => true,
734 ],
735 'contentformat' => [
736 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
737 ParamValidator::PARAM_DEPRECATED => true,
738 ],
739 'contentmodel' => [
740 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(),
741 ParamValidator::PARAM_DEPRECATED => true,
742 ],
743 'section' => [
744 ParamValidator::PARAM_DEFAULT => null,
745 ParamValidator::PARAM_DEPRECATED => true,
746 ],
747 ];
748
749 $ret = [];
750 foreach ( $fromToParams as $k => $v ) {
751 if ( isset( $v[ApiBase::PARAM_TEMPLATE_VARS]['slot'] ) ) {
752 $v[ApiBase::PARAM_TEMPLATE_VARS]['slot'] = 'fromslots';
753 }
754 $ret["from$k"] = $v;
755 }
756 foreach ( $fromToParams as $k => $v ) {
757 if ( isset( $v[ApiBase::PARAM_TEMPLATE_VARS]['slot'] ) ) {
758 $v[ApiBase::PARAM_TEMPLATE_VARS]['slot'] = 'toslots';
759 }
760 $ret["to$k"] = $v;
761 }
762
763 $ret = wfArrayInsertAfter(
764 $ret,
765 [ 'torelative' => [ ParamValidator::PARAM_TYPE => [ 'prev', 'next', 'cur' ], ] ],
766 'torev'
767 );
768
769 $ret['prop'] = [
770 ParamValidator::PARAM_DEFAULT => 'diff|ids|title',
771 ParamValidator::PARAM_TYPE => [
772 'diff',
773 'diffsize',
774 'rel',
775 'ids',
776 'title',
777 'user',
778 'comment',
779 'parsedcomment',
780 'size',
781 'timestamp',
782 ],
783 ParamValidator::PARAM_ISMULTI => true,
784 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
785 ];
786
787 $ret['slots'] = [
788 ParamValidator::PARAM_TYPE => $slotRoles,
789 ParamValidator::PARAM_ISMULTI => true,
790 ParamValidator::PARAM_ALL => true,
791 ];
792
793 $ret['difftype'] = [
794 ParamValidator::PARAM_TYPE => $this->differenceEngine->getSupportedFormats(),
795 ParamValidator::PARAM_DEFAULT => 'table',
796 ];
797
798 return $ret;
799 }
800
801 protected function getExamplesMessages() {
802 return [
803 'action=compare&fromrev=1&torev=2'
804 => 'apihelp-compare-example-1',
805 ];
806 }
807
808 public function getHelpUrls() {
809 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Compare';
810 }
811 }
812
813 /** @deprecated class alias since 1.43 */
814 class_alias( ApiComparePages::class, 'ApiComparePages' );