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