Debian Codegraph @

includes/api/ApiQueryRevisionsBase.php

raw symbols SCIP
1 <?php
2 /**
3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 namespace MediaWiki\Api;
24
25 use MediaWiki\CommentFormatter\CommentFormatter;
26 use MediaWiki\Content\Content;
27 use MediaWiki\Content\IContentHandlerFactory;
28 use MediaWiki\Content\Renderer\ContentRenderer;
29 use MediaWiki\Content\Transform\ContentTransformer;
30 use MediaWiki\Content\WikitextContent;
31 use MediaWiki\Context\DerivativeContext;
32 use MediaWiki\Logger\LoggerFactory;
33 use MediaWiki\MainConfigNames;
34 use MediaWiki\MediaWikiServices;
35 use MediaWiki\Parser\Parser;
36 use MediaWiki\Parser\ParserFactory;
37 use MediaWiki\Parser\ParserOptions;
38 use MediaWiki\Revision\RevisionAccessException;
39 use MediaWiki\Revision\RevisionRecord;
40 use MediaWiki\Revision\RevisionStore;
41 use MediaWiki\Revision\SlotRecord;
42 use MediaWiki\Revision\SlotRoleRegistry;
43 use MediaWiki\Title\Title;
44 use MediaWiki\User\TempUser\TempUserCreator;
45 use MediaWiki\User\UserFactory;
46 use MediaWiki\User\UserNameUtils;
47 use stdClass;
48 use Wikimedia\ParamValidator\ParamValidator;
49 use Wikimedia\ParamValidator\TypeDef\EnumDef;
50 use Wikimedia\ParamValidator\TypeDef\IntegerDef;
51
52 /**
53 * A base class for functions common to producing a list of revisions.
54 *
55 * @stable to extend
56 *
57 * @ingroup API
58 */
59 abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase {
60
61 // region Constants for internal use. Don't use externally.
62 /** @name Constants for internal use. Don't use externally. */
63
64 // Bits to indicate the results of the revdel permission check on a revision,
65 // see self::checkRevDel()
66 private const IS_DELETED = 1; // Whether the field is revision-deleted
67 private const CANNOT_VIEW = 2; // Whether the user cannot view the field due to revdel
68
69 private const LIMIT_PARSE = 1;
70
71 // endregion
72
73 /** @var int|string|null */
74 protected $limit;
75 /** @var int|string|null */
76 protected $diffto;
77 /** @var string|null */
78 protected $difftotext;
79 /** @var bool */
80 protected $difftotextpst;
81 /** @var int|string|null */
82 protected $expandTemplates;
83 /** @var bool */
84 protected $generateXML;
85 /** @var int|string|null */
86 protected $section;
87 /** @var bool */
88 protected $parseContent;
89 /** @var bool */
90 protected $fetchContent;
91 /** @var string */
92 protected $contentFormat;
93 protected bool $setParsedLimit = true;
94 protected ?array $slotRoles = null;
95 /** @var string[] */
96 protected $slotContentFormats;
97 /** @var bool */
98 protected $needSlots;
99
100 protected bool $fld_ids = false;
101 protected bool $fld_flags = false;
102 protected bool $fld_timestamp = false;
103 protected bool $fld_size = false;
104 protected bool $fld_slotsize = false;
105 protected bool $fld_sha1 = false;
106 protected bool $fld_slotsha1 = false;
107 protected bool $fld_comment = false;
108 protected bool $fld_parsedcomment = false;
109 protected bool $fld_user = false;
110 protected bool $fld_userid = false;
111 protected bool $fld_content = false;
112 protected bool $fld_tags = false;
113 protected bool $fld_contentmodel = false;
114 protected bool $fld_roles = false;
115 protected bool $fld_parsetree = false;
116
117 /**
118 * The number of uncached diffs that had to be generated for this request.
119 * @var int
120 */
121 private $numUncachedDiffs = 0;
122
123 private RevisionStore $revisionStore;
124 private IContentHandlerFactory $contentHandlerFactory;
125 private ParserFactory $parserFactory;
126 private SlotRoleRegistry $slotRoleRegistry;
127 private ContentRenderer $contentRenderer;
128 private ContentTransformer $contentTransformer;
129 private CommentFormatter $commentFormatter;
130 private TempUserCreator $tempUserCreator;
131 private UserFactory $userFactory;
132 private UserNameUtils $userNameUtils;
133
134 /**
135 * @since 1.37 Support injection of services
136 * @stable to call
137 */
138 public function __construct(
139 ApiQuery $queryModule,
140 string $moduleName,
141 string $paramPrefix = '',
142 ?RevisionStore $revisionStore = null,
143 ?IContentHandlerFactory $contentHandlerFactory = null,
144 ?ParserFactory $parserFactory = null,
145 ?SlotRoleRegistry $slotRoleRegistry = null,
146 ?ContentRenderer $contentRenderer = null,
147 ?ContentTransformer $contentTransformer = null,
148 ?CommentFormatter $commentFormatter = null,
149 ?TempUserCreator $tempUserCreator = null,
150 ?UserFactory $userFactory = null,
151 ?UserNameUtils $userNameUtils = null
152 ) {
153 parent::__construct( $queryModule, $moduleName, $paramPrefix );
154 // This class is part of the stable interface and
155 // therefor fallback to global state, if services are not provided
156 $services = MediaWikiServices::getInstance();
157 $this->revisionStore = $revisionStore ?? $services->getRevisionStore();
158 $this->contentHandlerFactory = $contentHandlerFactory ?? $services->getContentHandlerFactory();
159 $this->parserFactory = $parserFactory ?? $services->getParserFactory();
160 $this->slotRoleRegistry = $slotRoleRegistry ?? $services->getSlotRoleRegistry();
161 $this->contentRenderer = $contentRenderer ?? $services->getContentRenderer();
162 $this->contentTransformer = $contentTransformer ?? $services->getContentTransformer();
163 $this->commentFormatter = $commentFormatter ?? $services->getCommentFormatter();
164 $this->tempUserCreator = $tempUserCreator ?? $services->getTempUserCreator();
165 $this->userFactory = $userFactory ?? $services->getUserFactory();
166 $this->userNameUtils = $userNameUtils ?? $services->getUserNameUtils();
167 }
168
169 public function execute() {
170 $this->run();
171 }
172
173 public function executeGenerator( $resultPageSet ) {
174 $this->run( $resultPageSet );
175 }
176
177 /**
178 * @param ApiPageSet|null $resultPageSet
179 * @return void
180 */
181 abstract protected function run( ?ApiPageSet $resultPageSet = null );
182
183 /**
184 * Parse the parameters into the various instance fields.
185 *
186 * @param array $params
187 */
188 protected function parseParameters( $params ) {
189 $prop = array_fill_keys( $params['prop'], true );
190
191 $this->fld_ids = isset( $prop['ids'] );
192 $this->fld_flags = isset( $prop['flags'] );
193 $this->fld_timestamp = isset( $prop['timestamp'] );
194 $this->fld_comment = isset( $prop['comment'] );
195 $this->fld_parsedcomment = isset( $prop['parsedcomment'] );
196 $this->fld_size = isset( $prop['size'] );
197 $this->fld_slotsize = isset( $prop['slotsize'] );
198 $this->fld_sha1 = isset( $prop['sha1'] );
199 $this->fld_slotsha1 = isset( $prop['slotsha1'] );
200 $this->fld_content = isset( $prop['content'] );
201 $this->fld_contentmodel = isset( $prop['contentmodel'] );
202 $this->fld_userid = isset( $prop['userid'] );
203 $this->fld_user = isset( $prop['user'] );
204 $this->fld_tags = isset( $prop['tags'] );
205 $this->fld_roles = isset( $prop['roles'] );
206 $this->fld_parsetree = isset( $prop['parsetree'] );
207
208 $this->slotRoles = $params['slots'];
209
210 if ( $this->slotRoles !== null ) {
211 if ( $this->fld_parsetree ) {
212 $this->dieWithError( [
213 'apierror-invalidparammix-cannotusewith',
214 $this->encodeParamName( 'prop=parsetree' ),
215 $this->encodeParamName( 'slots' ),
216 ], 'invalidparammix' );
217 }
218 foreach ( [
219 'expandtemplates', 'generatexml', 'parse', 'diffto', 'difftotext', 'difftotextpst',
220 'contentformat'
221 ] as $p ) {
222 if ( $params[$p] !== null && $params[$p] !== false ) {
223 $this->dieWithError( [
224 'apierror-invalidparammix-cannotusewith',
225 $this->encodeParamName( $p ),
226 $this->encodeParamName( 'slots' ),
227 ], 'invalidparammix' );
228 }
229 }
230 $this->slotContentFormats = [];
231 foreach ( $this->slotRoles as $slotRole ) {
232 if ( isset( $params['contentformat-' . $slotRole] ) ) {
233 $this->slotContentFormats[$slotRole] = $params['contentformat-' . $slotRole];
234 }
235 }
236 }
237
238 if ( !empty( $params['contentformat'] ) ) {
239 $this->contentFormat = $params['contentformat'];
240 }
241
242 $this->limit = $params['limit'];
243
244 if ( $params['difftotext'] !== null ) {
245 $this->difftotext = $params['difftotext'];
246 $this->difftotextpst = $params['difftotextpst'];
247 } elseif ( $params['diffto'] !== null ) {
248 if ( $params['diffto'] == 'cur' ) {
249 $params['diffto'] = 0;
250 }
251 if ( ( !ctype_digit( (string)$params['diffto'] ) || $params['diffto'] < 0 )
252 && $params['diffto'] != 'prev' && $params['diffto'] != 'next'
253 ) {
254 $p = $this->getModulePrefix();
255 $this->dieWithError( [ 'apierror-baddiffto', $p ], 'diffto' );
256 }
257 // Check whether the revision exists and is readable,
258 // DifferenceEngine returns a rather ambiguous empty
259 // string if that's not the case
260 if ( is_numeric( $params['diffto'] ) && $params['diffto'] != 0 ) {
261 $difftoRev = $this->revisionStore->getRevisionById( $params['diffto'] );
262 if ( !$difftoRev ) {
263 $this->dieWithError( [ 'apierror-nosuchrevid', $params['diffto'] ] );
264 }
265 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable T240141
266 $revDel = $this->checkRevDel( $difftoRev, RevisionRecord::DELETED_TEXT );
267 if ( $revDel & self::CANNOT_VIEW ) {
268 $this->addWarning( [ 'apiwarn-difftohidden', $difftoRev->getId() ] );
269 $params['diffto'] = null;
270 }
271 }
272 $this->diffto = $params['diffto'];
273 }
274
275 $this->fetchContent = $this->fld_content || $this->diffto !== null
276 || $this->difftotext !== null || $this->fld_parsetree;
277
278 $smallLimit = false;
279 if ( $this->fetchContent ) {
280 $smallLimit = true;
281 $this->expandTemplates = $params['expandtemplates'];
282 $this->generateXML = $params['generatexml'];
283 $this->parseContent = $params['parse'];
284 if ( $this->parseContent ) {
285 // Must manually initialize unset limit
286 $this->limit ??= self::LIMIT_PARSE;
287 }
288 $this->section = $params['section'] ?? false;
289 }
290
291 $userMax = $this->parseContent ? self::LIMIT_PARSE :
292 ( $smallLimit ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1 );
293 $botMax = $this->parseContent ? self::LIMIT_PARSE :
294 ( $smallLimit ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2 );
295 if ( $this->limit == 'max' ) {
296 $this->limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
297 if ( $this->setParsedLimit ) {
298 $this->getResult()->addParsedLimit( $this->getModuleName(), $this->limit );
299 }
300 }
301
302 $this->limit = $this->getMain()->getParamValidator()->validateValue(
303 $this, 'limit', $this->limit ?? 10, [
304 ParamValidator::PARAM_TYPE => 'limit',
305 IntegerDef::PARAM_MIN => 1,
306 IntegerDef::PARAM_MAX => $userMax,
307 IntegerDef::PARAM_MAX2 => $botMax,
308 IntegerDef::PARAM_IGNORE_RANGE => true,
309 ]
310 );
311
312 $this->needSlots = $this->fetchContent || $this->fld_contentmodel ||
313 $this->fld_slotsize || $this->fld_slotsha1;
314 if ( $this->needSlots && $this->slotRoles === null ) {
315 $encParam = $this->encodeParamName( 'slots' );
316 $name = $this->getModuleName();
317 $parent = $this->getParent();
318 $parentParam = $parent->encodeParamName( $parent->getModuleManager()->getModuleGroup( $name ) );
319 $this->addDeprecation(
320 [ 'apiwarn-deprecation-missingparam', $encParam ],
321 "action=query&{$parentParam}={$name}&!{$encParam}"
322 );
323 }
324 }
325
326 /**
327 * Test revision deletion status
328 * @param RevisionRecord $revision Revision to check
329 * @param int $field One of the RevisionRecord::DELETED_* constants
330 * @return int Revision deletion status flags. Bitwise OR of
331 * self::IS_DELETED and self::CANNOT_VIEW, as appropriate.
332 */
333 private function checkRevDel( RevisionRecord $revision, $field ) {
334 $ret = $revision->isDeleted( $field ) ? self::IS_DELETED : 0;
335 if ( $ret ) {
336 $canSee = $revision->userCan( $field, $this->getAuthority() );
337 $ret |= ( $canSee ? 0 : self::CANNOT_VIEW );
338 }
339 return $ret;
340 }
341
342 /**
343 * Extract information from the RevisionRecord
344 *
345 * @since 1.32, takes a RevisionRecord instead of a Revision
346 * @param RevisionRecord $revision
347 * @param stdClass $row Should have a field 'ts_tags' if $this->fld_tags is set
348 * @return array
349 */
350 protected function extractRevisionInfo( RevisionRecord $revision, $row ) {
351 $vals = [];
352 $anyHidden = false;
353
354 if ( $this->fld_ids ) {
355 $vals['revid'] = (int)$revision->getId();
356 if ( $revision->getParentId() !== null ) {
357 $vals['parentid'] = (int)$revision->getParentId();
358 }
359 }
360
361 if ( $this->fld_flags ) {
362 $vals['minor'] = $revision->isMinor();
363 }
364
365 if ( $this->fld_user || $this->fld_userid ) {
366 $revDel = $this->checkRevDel( $revision, RevisionRecord::DELETED_USER );
367 if ( $revDel & self::IS_DELETED ) {
368 $vals['userhidden'] = true;
369 $anyHidden = true;
370 }
371 if ( !( $revDel & self::CANNOT_VIEW ) ) {
372 $u = $revision->getUser( RevisionRecord::RAW );
373 if ( $this->fld_user ) {
374 $vals['user'] = $u->getName();
375 }
376 if ( $this->userNameUtils->isTemp( $u->getName() ) ) {
377 $vals['temp'] = true;
378 }
379 if ( !$u->isRegistered() ) {
380 $vals['anon'] = true;
381 }
382
383 if ( $this->fld_userid ) {
384 $vals['userid'] = $u->getId();
385 }
386 }
387 }
388
389 if ( $this->fld_timestamp ) {
390 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $revision->getTimestamp() );
391 }
392
393 if ( $this->fld_size ) {
394 try {
395 $vals['size'] = (int)$revision->getSize();
396 } catch ( RevisionAccessException $e ) {
397 // Back compat: If there's no size, return 0.
398 // @todo: Gergő says to mention T198099 as a "todo" here.
399 $vals['size'] = 0;
400 }
401 }
402
403 if ( $this->fld_sha1 ) {
404 $revDel = $this->checkRevDel( $revision, RevisionRecord::DELETED_TEXT );
405 if ( $revDel & self::IS_DELETED ) {
406 $vals['sha1hidden'] = true;
407 $anyHidden = true;
408 }
409 if ( !( $revDel & self::CANNOT_VIEW ) ) {
410 try {
411 $vals['sha1'] = \Wikimedia\base_convert( $revision->getSha1(), 36, 16, 40 );
412 } catch ( RevisionAccessException $e ) {
413 // Back compat: If there's no sha1, return empty string.
414 // @todo: Gergő says to mention T198099 as a "todo" here.
415 $vals['sha1'] = '';
416 }
417 }
418 }
419
420 try {
421 if ( $this->fld_roles ) {
422 $vals['roles'] = $revision->getSlotRoles();
423 }
424
425 if ( $this->needSlots ) {
426 $revDel = $this->checkRevDel( $revision, RevisionRecord::DELETED_TEXT );
427 if ( ( $this->fld_slotsha1 || $this->fetchContent ) && ( $revDel & self::IS_DELETED ) ) {
428 $anyHidden = true;
429 }
430 $vals = array_merge( $vals, $this->extractAllSlotInfo( $revision, $revDel ) );
431 }
432 } catch ( RevisionAccessException $ex ) {
433 // This is here so T212428 doesn't spam the log.
434 // TODO: find out why T212428 happens in the first place!
435 $vals['slotsmissing'] = true;
436
437 LoggerFactory::getInstance( 'api-warning' )->error(
438 'Failed to access revision slots',
439 [ 'revision' => $revision->getId(), 'exception' => $ex, ]
440 );
441 }
442
443 if ( $this->fld_comment || $this->fld_parsedcomment ) {
444 $revDel = $this->checkRevDel( $revision, RevisionRecord::DELETED_COMMENT );
445 if ( $revDel & self::IS_DELETED ) {
446 $vals['commenthidden'] = true;
447 $anyHidden = true;
448 }
449 if ( !( $revDel & self::CANNOT_VIEW ) ) {
450 $comment = $revision->getComment( RevisionRecord::RAW );
451 $comment = $comment->text ?? '';
452
453 if ( $this->fld_comment ) {
454 $vals['comment'] = $comment;
455 }
456
457 if ( $this->fld_parsedcomment ) {
458 $vals['parsedcomment'] = $this->commentFormatter->format(
459 $comment, Title::newFromLinkTarget( $revision->getPageAsLinkTarget() )
460 );
461 }
462 }
463 }
464
465 if ( $this->fld_tags ) {
466 if ( $row->ts_tags ) {
467 $tags = explode( ',', $row->ts_tags );
468 ApiResult::setIndexedTagName( $tags, 'tag' );
469 $vals['tags'] = $tags;
470 } else {
471 $vals['tags'] = [];
472 }
473 }
474
475 if ( $anyHidden && $revision->isDeleted( RevisionRecord::DELETED_RESTRICTED ) ) {
476 $vals['suppressed'] = true;
477 }
478
479 return $vals;
480 }
481
482 /**
483 * Extracts information about all relevant slots.
484 *
485 * @param RevisionRecord $revision
486 * @param int $revDel
487 *
488 * @return array
489 * @throws ApiUsageException
490 */
491 private function extractAllSlotInfo( RevisionRecord $revision, $revDel ): array {
492 $vals = [];
493
494 if ( $this->slotRoles === null ) {
495 try {
496 $slot = $revision->getSlot( SlotRecord::MAIN, RevisionRecord::RAW );
497 } catch ( RevisionAccessException $e ) {
498 // Back compat: If there's no slot, there's no content, so set 'textmissing'
499 // @todo: Gergő says to mention T198099 as a "todo" here.
500 $vals['textmissing'] = true;
501 $slot = null;
502 }
503
504 if ( $slot ) {
505 $content = null;
506 $vals += $this->extractSlotInfo( $slot, $revDel, $content );
507 if ( !empty( $vals['nosuchsection'] ) ) {
508 $this->dieWithError(
509 [
510 'apierror-nosuchsection-what',
511 wfEscapeWikiText( $this->section ),
512 $this->msg( 'revid', $revision->getId() )
513 ],
514 'nosuchsection'
515 );
516 }
517 if ( $content ) {
518 $vals += $this->extractDeprecatedContent( $content, $revision );
519 }
520 }
521 } else {
522 $roles = array_intersect( $this->slotRoles, $revision->getSlotRoles() );
523 $vals['slots'] = [
524 ApiResult::META_KVP_MERGE => true,
525 ];
526 foreach ( $roles as $role ) {
527 try {
528 $slot = $revision->getSlot( $role, RevisionRecord::RAW );
529 } catch ( RevisionAccessException $e ) {
530 // Don't error out here so the client can still process other slots/revisions.
531 // @todo: Gergő says to mention T198099 as a "todo" here.
532 $vals['slots'][$role]['missing'] = true;
533 continue;
534 }
535 $content = null;
536 $vals['slots'][$role] = $this->extractSlotInfo( $slot, $revDel, $content );
537 // @todo Move this into extractSlotInfo() (and remove its $content parameter)
538 // when extractDeprecatedContent() is no more.
539 if ( $content && $this->getAuthority()->authorizeRead( 'read', $revision->getPage() ) ) {
540 /** @var Content $content */
541 $model = $content->getModel();
542 $format = $this->slotContentFormats[$role] ?? $content->getDefaultFormat();
543 if ( !$content->isSupportedFormat( $format ) ) {
544 $this->addWarning( [
545 'apierror-badformat',
546 $format,
547 $model,
548 $this->msg( 'revid', $revision->getId() )
549 ] );
550 $vals['slots'][$role]['badcontentformat'] = true;
551 } else {
552 $vals['slots'][$role]['contentmodel'] = $model;
553 $vals['slots'][$role]['contentformat'] = $format;
554 ApiResult::setContentValue(
555 $vals['slots'][$role],
556 'content',
557 $content->serialize( $format )
558 );
559 }
560 }
561 }
562 ApiResult::setArrayType( $vals['slots'], 'kvp', 'role' );
563 ApiResult::setIndexedTagName( $vals['slots'], 'slot' );
564 }
565 return $vals;
566 }
567
568 /**
569 * Extract information from the SlotRecord
570 *
571 * @param SlotRecord $slot
572 * @param int $revDel Revdel status flags, from self::checkRevDel()
573 * @param Content|null &$content Set to the slot's content, if available
574 * and $this->fetchContent is true
575 * @return array
576 */
577 private function extractSlotInfo( SlotRecord $slot, $revDel, &$content = null ) {
578 $vals = [];
579 ApiResult::setArrayType( $vals, 'assoc' );
580
581 if ( $this->fld_slotsize ) {
582 $vals['size'] = (int)$slot->getSize();
583 }
584
585 if ( $this->fld_slotsha1 ) {
586 if ( $revDel & self::IS_DELETED ) {
587 $vals['sha1hidden'] = true;
588 }
589 if ( !( $revDel & self::CANNOT_VIEW ) ) {
590 if ( $slot->getSha1() != '' ) {
591 $vals['sha1'] = \Wikimedia\base_convert( $slot->getSha1(), 36, 16, 40 );
592 } else {
593 $vals['sha1'] = '';
594 }
595 }
596 }
597
598 if ( $this->fld_contentmodel ) {
599 $vals['contentmodel'] = $slot->getModel();
600 }
601
602 $content = null;
603 if ( $this->fetchContent ) {
604 if ( $revDel & self::IS_DELETED ) {
605 $vals['texthidden'] = true;
606 }
607 if ( !( $revDel & self::CANNOT_VIEW ) ) {
608 try {
609 $content = $slot->getContent();
610 } catch ( RevisionAccessException $e ) {
611 // @todo: Gergő says to mention T198099 as a "todo" here.
612 $vals['textmissing'] = true;
613 }
614 // Expand templates after getting section content because
615 // template-added sections don't count and Parser::preprocess()
616 // will have less input
617 if ( $content && $this->section !== false ) {
618 $content = $content->getSection( $this->section );
619 if ( !$content ) {
620 $vals['nosuchsection'] = true;
621 }
622 }
623 }
624 }
625
626 return $vals;
627 }
628
629 /**
630 * Format a Content using deprecated options
631 * @param Content $content Content to format
632 * @param RevisionRecord $revision Revision being processed
633 * @return array
634 */
635 private function extractDeprecatedContent( Content $content, RevisionRecord $revision ) {
636 $vals = [];
637 $title = Title::newFromLinkTarget( $revision->getPageAsLinkTarget() );
638
639 if ( !$this->getAuthority()->authorizeRead( 'read', $title ) ) {
640 return [];
641 }
642
643 if ( $this->fld_parsetree || ( $this->fld_content && $this->generateXML ) ) {
644 if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
645 /** @var WikitextContent $content */
646 '@phan-var WikitextContent $content';
647 $t = $content->getText(); # note: don't set $text
648
649 $parser = $this->parserFactory->create();
650 $parser->startExternalParse(
651 $title,
652 ParserOptions::newFromContext( $this->getContext() ),
653 Parser::OT_PREPROCESS
654 );
655 $dom = $parser->preprocessToDom( $t );
656 // @phan-suppress-next-line PhanUndeclaredMethodInCallable
657 if ( is_callable( [ $dom, 'saveXML' ] ) ) {
658 // @phan-suppress-next-line PhanUndeclaredMethod
659 $xml = $dom->saveXML();
660 } else {
661 // @phan-suppress-next-line PhanUndeclaredMethod
662 $xml = $dom->__toString();
663 }
664 $vals['parsetree'] = $xml;
665 } else {
666 $vals['badcontentformatforparsetree'] = true;
667 $this->addWarning(
668 [
669 'apierror-parsetree-notwikitext-title',
670 wfEscapeWikiText( $title->getPrefixedText() ),
671 $content->getModel()
672 ],
673 'parsetree-notwikitext'
674 );
675 }
676 }
677
678 if ( $this->fld_content ) {
679 $text = null;
680
681 if ( $this->expandTemplates && !$this->parseContent ) {
682 if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
683 /** @var WikitextContent $content */
684 '@phan-var WikitextContent $content';
685 $text = $content->getText();
686
687 $text = $this->parserFactory->create()->preprocess(
688 $text,
689 $title,
690 ParserOptions::newFromContext( $this->getContext() )
691 );
692 } else {
693 $this->addWarning( [
694 'apierror-templateexpansion-notwikitext',
695 wfEscapeWikiText( $title->getPrefixedText() ),
696 $content->getModel()
697 ] );
698 $vals['badcontentformat'] = true;
699 $text = false;
700 }
701 }
702 if ( $this->parseContent ) {
703 $popts = ParserOptions::newFromContext( $this->getContext() );
704 $po = $this->contentRenderer->getParserOutput(
705 $content,
706 $title,
707 $revision,
708 $popts
709 );
710 // TODO T371004 move runOutputPipeline out of $parserOutput
711 $text = $po->runOutputPipeline( $popts, [] )->getContentHolderText();
712 }
713
714 if ( $text === null ) {
715 $format = $this->contentFormat ?: $content->getDefaultFormat();
716 $model = $content->getModel();
717
718 if ( !$content->isSupportedFormat( $format ) ) {
719 $name = wfEscapeWikiText( $title->getPrefixedText() );
720 $this->addWarning( [ 'apierror-badformat', $this->contentFormat, $model, $name ] );
721 $vals['badcontentformat'] = true;
722 $text = false;
723 } else {
724 $text = $content->serialize( $format );
725 // always include format and model.
726 // Format is needed to deserialize, model is needed to interpret.
727 $vals['contentformat'] = $format;
728 $vals['contentmodel'] = $model;
729 }
730 }
731
732 if ( $text !== false ) {
733 ApiResult::setContentValue( $vals, 'content', $text );
734 }
735 }
736
737 if ( $content && ( $this->diffto !== null || $this->difftotext !== null ) ) {
738 if ( $this->numUncachedDiffs < $this->getConfig()->get( MainConfigNames::APIMaxUncachedDiffs ) ) {
739 $vals['diff'] = [];
740 $context = new DerivativeContext( $this->getContext() );
741 $context->setTitle( $title );
742 $handler = $content->getContentHandler();
743
744 if ( $this->difftotext !== null ) {
745 $model = $title->getContentModel();
746
747 if ( $this->contentFormat
748 && !$this->contentHandlerFactory->getContentHandler( $model )
749 ->isSupportedFormat( $this->contentFormat )
750 ) {
751 $name = wfEscapeWikiText( $title->getPrefixedText() );
752 $this->addWarning( [ 'apierror-badformat', $this->contentFormat, $model, $name ] );
753 $vals['diff']['badcontentformat'] = true;
754 $engine = null;
755 } else {
756 $difftocontent = $this->contentHandlerFactory->getContentHandler( $model )
757 ->unserializeContent( $this->difftotext, $this->contentFormat );
758
759 if ( $this->difftotextpst ) {
760 $popts = ParserOptions::newFromContext( $this->getContext() );
761 $difftocontent = $this->contentTransformer->preSaveTransform(
762 $difftocontent,
763 $title,
764 $this->getUserForPreview(),
765 $popts
766 );
767 }
768
769 $engine = $handler->createDifferenceEngine( $context );
770 $engine->setContent( $content, $difftocontent );
771 }
772 } else {
773 $engine = $handler->createDifferenceEngine( $context, $revision->getId(), $this->diffto );
774 $vals['diff']['from'] = $engine->getOldid();
775 $vals['diff']['to'] = $engine->getNewid();
776 }
777 if ( $engine ) {
778 $difftext = $engine->getDiffBody();
779 ApiResult::setContentValue( $vals['diff'], 'body', $difftext );
780 if ( !$engine->wasCacheHit() ) {
781 $this->numUncachedDiffs++;
782 }
783 foreach ( $engine->getRevisionLoadErrors() as $msg ) {
784 $this->addWarning( $msg );
785 }
786 }
787 } else {
788 $vals['diff']['notcached'] = true;
789 }
790 }
791
792 return $vals;
793 }
794
795 private function getUserForPreview() {
796 $user = $this->getUser();
797 if ( $this->tempUserCreator->shouldAutoCreate( $user, 'edit' ) ) {
798 return $this->userFactory->newUnsavedTempUser(
799 $this->tempUserCreator->getStashedName( $this->getRequest()->getSession() )
800 );
801 }
802 return $user;
803 }
804
805 /**
806 * @stable to override
807 * @param array $params
808 *
809 * @return string
810 */
811 public function getCacheMode( $params ) {
812 if ( $this->userCanSeeRevDel() ) {
813 return 'private';
814 }
815
816 return 'public';
817 }
818
819 /**
820 * @stable to override
821 * @return array
822 */
823 public function getAllowedParams() {
824 $slotRoles = $this->slotRoleRegistry->getKnownRoles();
825 sort( $slotRoles, SORT_STRING );
826 $smallLimit = $this->getMain()->canApiHighLimits() ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_SML1;
827
828 return [
829 'prop' => [
830 ParamValidator::PARAM_ISMULTI => true,
831 ParamValidator::PARAM_DEFAULT => 'ids|timestamp|flags|comment|user',
832 ParamValidator::PARAM_TYPE => [
833 'ids',
834 'flags',
835 'timestamp',
836 'user',
837 'userid',
838 'size',
839 'slotsize',
840 'sha1',
841 'slotsha1',
842 'contentmodel',
843 'comment',
844 'parsedcomment',
845 'content',
846 'tags',
847 'roles',
848 'parsetree',
849 ],
850 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-prop',
851 ApiBase::PARAM_HELP_MSG_PER_VALUE => [
852 'ids' => 'apihelp-query+revisions+base-paramvalue-prop-ids',
853 'flags' => 'apihelp-query+revisions+base-paramvalue-prop-flags',
854 'timestamp' => 'apihelp-query+revisions+base-paramvalue-prop-timestamp',
855 'user' => 'apihelp-query+revisions+base-paramvalue-prop-user',
856 'userid' => 'apihelp-query+revisions+base-paramvalue-prop-userid',
857 'size' => 'apihelp-query+revisions+base-paramvalue-prop-size',
858 'slotsize' => 'apihelp-query+revisions+base-paramvalue-prop-slotsize',
859 'sha1' => 'apihelp-query+revisions+base-paramvalue-prop-sha1',
860 'slotsha1' => 'apihelp-query+revisions+base-paramvalue-prop-slotsha1',
861 'contentmodel' => 'apihelp-query+revisions+base-paramvalue-prop-contentmodel',
862 'comment' => 'apihelp-query+revisions+base-paramvalue-prop-comment',
863 'parsedcomment' => 'apihelp-query+revisions+base-paramvalue-prop-parsedcomment',
864 'content' => [ 'apihelp-query+revisions+base-paramvalue-prop-content', $smallLimit ],
865 'tags' => 'apihelp-query+revisions+base-paramvalue-prop-tags',
866 'roles' => 'apihelp-query+revisions+base-paramvalue-prop-roles',
867 'parsetree' => [ 'apihelp-query+revisions+base-paramvalue-prop-parsetree',
868 CONTENT_MODEL_WIKITEXT, $smallLimit ],
869 ],
870 EnumDef::PARAM_DEPRECATED_VALUES => [
871 'parsetree' => true,
872 ],
873 ],
874 'slots' => [
875 ParamValidator::PARAM_TYPE => $slotRoles,
876 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-slots',
877 ParamValidator::PARAM_ISMULTI => true,
878 ParamValidator::PARAM_ALL => true,
879 ],
880 'contentformat-{slot}' => [
881 ApiBase::PARAM_TEMPLATE_VARS => [ 'slot' => 'slots' ],
882 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-contentformat-slot',
883 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
884 ],
885 'limit' => [
886 ParamValidator::PARAM_TYPE => 'limit',
887 IntegerDef::PARAM_MIN => 1,
888 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
889 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2,
890 ApiBase::PARAM_HELP_MSG => [ 'apihelp-query+revisions+base-param-limit',
891 $smallLimit, self::LIMIT_PARSE ],
892 ],
893 'expandtemplates' => [
894 ParamValidator::PARAM_DEFAULT => false,
895 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-expandtemplates',
896 ParamValidator::PARAM_DEPRECATED => true,
897 ],
898 'generatexml' => [
899 ParamValidator::PARAM_DEFAULT => false,
900 ParamValidator::PARAM_DEPRECATED => true,
901 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-generatexml',
902 ],
903 'parse' => [
904 ParamValidator::PARAM_DEFAULT => false,
905 ApiBase::PARAM_HELP_MSG => [ 'apihelp-query+revisions+base-param-parse', self::LIMIT_PARSE ],
906 ParamValidator::PARAM_DEPRECATED => true,
907 ],
908 'section' => [
909 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-section',
910 ],
911 'diffto' => [
912 ApiBase::PARAM_HELP_MSG => [ 'apihelp-query+revisions+base-param-diffto', $smallLimit ],
913 ParamValidator::PARAM_DEPRECATED => true,
914 ],
915 'difftotext' => [
916 ApiBase::PARAM_HELP_MSG => [ 'apihelp-query+revisions+base-param-difftotext', $smallLimit ],
917 ParamValidator::PARAM_DEPRECATED => true,
918 ],
919 'difftotextpst' => [
920 ParamValidator::PARAM_DEFAULT => false,
921 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-difftotextpst',
922 ParamValidator::PARAM_DEPRECATED => true,
923 ],
924 'contentformat' => [
925 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
926 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-contentformat',
927 ParamValidator::PARAM_DEPRECATED => true,
928 ],
929 ];
930 }
931 }
932
933 /** @deprecated class alias since 1.43 */
934 class_alias( ApiQueryRevisionsBase::class, 'ApiQueryRevisionsBase' );