MediaWiki master
ApiQueryRevisionsBase.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Api;
10
34use stdClass;
38use Wikimedia\Timestamp\TimestampFormat as TS;
39
48
49 // region Constants for internal use. Don't use externally.
52 // Bits to indicate the results of the revdel permission check on a revision,
53 // see self::checkRevDel()
54 private const IS_DELETED = 1; // Whether the field is revision-deleted
55 private const CANNOT_VIEW = 2; // Whether the user cannot view the field due to revdel
56
57 private const LIMIT_PARSE = 1;
58
59 // endregion
60
62 protected $limit;
64 protected $diffto;
66 protected $difftotext;
68 protected $difftotextpst;
72 protected $generateXML;
74 protected $section;
76 protected $parseContent;
78 protected $fetchContent;
80 protected $contentFormat;
81 protected bool $setParsedLimit = true;
82 protected ?array $slotRoles = null;
86 protected $needSlots;
87
88 protected bool $fld_ids = false;
89 protected bool $fld_flags = false;
90 protected bool $fld_timestamp = false;
91 protected bool $fld_size = false;
92 protected bool $fld_slotsize = false;
93 protected bool $fld_sha1 = false;
94 protected bool $fld_slotsha1 = false;
95 protected bool $fld_comment = false;
96 protected bool $fld_parsedcomment = false;
97 protected bool $fld_user = false;
98 protected bool $fld_userid = false;
99 protected bool $fld_content = false;
100 protected bool $fld_tags = false;
101 protected bool $fld_contentmodel = false;
102 protected bool $fld_roles = false;
103 protected bool $fld_parsetree = false;
104
109 private $numUncachedDiffs = 0;
110
111 private RevisionStore $revisionStore;
112 private IContentHandlerFactory $contentHandlerFactory;
113 private ParserFactory $parserFactory;
114 private SlotRoleRegistry $slotRoleRegistry;
115 private ContentRenderer $contentRenderer;
116 private ContentTransformer $contentTransformer;
117 private CommentFormatter $commentFormatter;
118 private TempUserCreator $tempUserCreator;
119 private UserFactory $userFactory;
120 private UserNameUtils $userNameUtils;
121
126 public function __construct(
127 ApiQuery $queryModule,
128 string $moduleName,
129 string $paramPrefix = '',
130 ?RevisionStore $revisionStore = null,
131 ?IContentHandlerFactory $contentHandlerFactory = null,
132 ?ParserFactory $parserFactory = null,
133 ?SlotRoleRegistry $slotRoleRegistry = null,
134 ?ContentRenderer $contentRenderer = null,
135 ?ContentTransformer $contentTransformer = null,
136 ?CommentFormatter $commentFormatter = null,
137 ?TempUserCreator $tempUserCreator = null,
138 ?UserFactory $userFactory = null,
139 ?UserNameUtils $userNameUtils = null
140 ) {
141 parent::__construct( $queryModule, $moduleName, $paramPrefix );
142 // This class is part of the stable interface and
143 // therefor fallback to global state, if services are not provided
144 $services = MediaWikiServices::getInstance();
145 $this->revisionStore = $revisionStore ?? $services->getRevisionStore();
146 $this->contentHandlerFactory = $contentHandlerFactory ?? $services->getContentHandlerFactory();
147 $this->parserFactory = $parserFactory ?? $services->getParserFactory();
148 $this->slotRoleRegistry = $slotRoleRegistry ?? $services->getSlotRoleRegistry();
149 $this->contentRenderer = $contentRenderer ?? $services->getContentRenderer();
150 $this->contentTransformer = $contentTransformer ?? $services->getContentTransformer();
151 $this->commentFormatter = $commentFormatter ?? $services->getCommentFormatter();
152 $this->tempUserCreator = $tempUserCreator ?? $services->getTempUserCreator();
153 $this->userFactory = $userFactory ?? $services->getUserFactory();
154 $this->userNameUtils = $userNameUtils ?? $services->getUserNameUtils();
155 }
156
157 public function execute() {
158 $this->run();
159 }
160
162 public function executeGenerator( $resultPageSet ) {
163 $this->run( $resultPageSet );
164 }
165
170 abstract protected function run( ?ApiPageSet $resultPageSet = null );
171
177 protected function parseParameters( $params ) {
178 $prop = array_fill_keys( $params['prop'], true );
179
180 $this->fld_ids = isset( $prop['ids'] );
181 $this->fld_flags = isset( $prop['flags'] );
182 $this->fld_timestamp = isset( $prop['timestamp'] );
183 $this->fld_comment = isset( $prop['comment'] );
184 $this->fld_parsedcomment = isset( $prop['parsedcomment'] );
185 $this->fld_size = isset( $prop['size'] );
186 $this->fld_slotsize = isset( $prop['slotsize'] );
187 $this->fld_sha1 = isset( $prop['sha1'] );
188 $this->fld_slotsha1 = isset( $prop['slotsha1'] );
189 $this->fld_content = isset( $prop['content'] );
190 $this->fld_contentmodel = isset( $prop['contentmodel'] );
191 $this->fld_userid = isset( $prop['userid'] );
192 $this->fld_user = isset( $prop['user'] );
193 $this->fld_tags = isset( $prop['tags'] );
194 $this->fld_roles = isset( $prop['roles'] );
195 $this->fld_parsetree = isset( $prop['parsetree'] );
196
197 $this->slotRoles = $params['slots'];
198
199 if ( $this->slotRoles !== null ) {
200 if ( $this->fld_parsetree ) {
201 $this->dieWithError( [
202 'apierror-invalidparammix-cannotusewith',
203 $this->encodeParamName( 'prop=parsetree' ),
204 $this->encodeParamName( 'slots' ),
205 ], 'invalidparammix' );
206 }
207 foreach ( [
208 'expandtemplates', 'generatexml', 'parse', 'diffto', 'difftotext', 'difftotextpst',
209 'contentformat'
210 ] as $p ) {
211 if ( $params[$p] !== null && $params[$p] !== false ) {
212 $this->dieWithError( [
213 'apierror-invalidparammix-cannotusewith',
214 $this->encodeParamName( $p ),
215 $this->encodeParamName( 'slots' ),
216 ], 'invalidparammix' );
217 }
218 }
219 $this->slotContentFormats = [];
220 foreach ( $this->slotRoles as $slotRole ) {
221 if ( isset( $params['contentformat-' . $slotRole] ) ) {
222 $this->slotContentFormats[$slotRole] = $params['contentformat-' . $slotRole];
223 }
224 }
225 }
226
227 if ( !empty( $params['contentformat'] ) ) {
228 $this->contentFormat = $params['contentformat'];
229 }
230
231 $this->limit = $params['limit'];
232
233 if ( $params['difftotext'] !== null ) {
234 $this->difftotext = $params['difftotext'];
235 $this->difftotextpst = $params['difftotextpst'];
236 } elseif ( $params['diffto'] !== null ) {
237 if ( $params['diffto'] == 'cur' ) {
238 $params['diffto'] = 0;
239 }
240 if ( ( !ctype_digit( (string)$params['diffto'] ) || $params['diffto'] < 0 )
241 && $params['diffto'] != 'prev' && $params['diffto'] != 'next'
242 ) {
243 $p = $this->getModulePrefix();
244 $this->dieWithError( [ 'apierror-baddiffto', $p ], 'diffto' );
245 }
246 // Check whether the revision exists and is readable,
247 // DifferenceEngine returns a rather ambiguous empty
248 // string if that's not the case
249 if ( is_numeric( $params['diffto'] ) && $params['diffto'] != 0 ) {
250 $difftoRev = $this->revisionStore->getRevisionById( $params['diffto'] );
251 if ( !$difftoRev ) {
252 $this->dieWithError( [ 'apierror-nosuchrevid', $params['diffto'] ] );
253 }
254 $revDel = $this->checkRevDel( $difftoRev, RevisionRecord::DELETED_TEXT );
255 if ( $revDel & self::CANNOT_VIEW ) {
256 $this->addWarning( [ 'apiwarn-difftohidden', $difftoRev->getId() ] );
257 $params['diffto'] = null;
258 }
259 }
260 $this->diffto = $params['diffto'];
261 }
262
263 $this->fetchContent = $this->fld_content || $this->diffto !== null
264 || $this->difftotext !== null || $this->fld_parsetree;
265
266 $smallLimit = false;
267 if ( $this->fetchContent ) {
268 $smallLimit = true;
269 $this->expandTemplates = $params['expandtemplates'];
270 $this->generateXML = $params['generatexml'];
271 $this->parseContent = $params['parse'];
272 if ( $this->parseContent ) {
273 // Must manually initialize unset limit
274 $this->limit ??= self::LIMIT_PARSE;
275 }
276 $this->section = $params['section'] ?? false;
277 }
278
279 $userMax = $this->parseContent ? self::LIMIT_PARSE :
280 ( $smallLimit ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1 );
281 $botMax = $this->parseContent ? self::LIMIT_PARSE :
282 ( $smallLimit ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2 );
283 if ( $this->limit == 'max' ) {
284 $this->limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
285 if ( $this->setParsedLimit ) {
286 $this->getResult()->addParsedLimit( $this->getModuleName(), $this->limit );
287 }
288 }
289
290 $this->limit = $this->getMain()->getParamValidator()->validateValue(
291 $this, 'limit', $this->limit ?? 10, [
292 ParamValidator::PARAM_TYPE => 'limit',
293 IntegerDef::PARAM_MIN => 1,
294 IntegerDef::PARAM_MAX => $userMax,
295 IntegerDef::PARAM_MAX2 => $botMax,
296 IntegerDef::PARAM_IGNORE_RANGE => true,
297 ]
298 );
299
300 $this->needSlots = $this->fetchContent || $this->fld_contentmodel ||
301 $this->fld_slotsize || $this->fld_slotsha1;
302 }
303
311 private function checkRevDel( RevisionRecord $revision, $field ) {
312 $ret = $revision->isDeleted( $field ) ? self::IS_DELETED : 0;
313 if ( $ret ) {
314 $canSee = $revision->userCan( $field, $this->getAuthority() );
315 $ret |= ( $canSee ? 0 : self::CANNOT_VIEW );
316 }
317 return $ret;
318 }
319
328 protected function extractRevisionInfo( RevisionRecord $revision, $row ) {
329 $vals = [];
330 $anyHidden = false;
331
332 if ( $this->fld_ids ) {
333 $vals['revid'] = (int)$revision->getId();
334 if ( $revision->getParentId() !== null ) {
335 $vals['parentid'] = (int)$revision->getParentId();
336 }
337 }
338
339 if ( $this->fld_flags ) {
340 $vals['minor'] = $revision->isMinor();
341 }
342
343 if ( $this->fld_user || $this->fld_userid ) {
344 $revDel = $this->checkRevDel( $revision, RevisionRecord::DELETED_USER );
345 if ( $revDel & self::IS_DELETED ) {
346 $vals['userhidden'] = true;
347 $anyHidden = true;
348 }
349 if ( !( $revDel & self::CANNOT_VIEW ) ) {
350 $u = $revision->getUser( RevisionRecord::RAW );
351 if ( $this->fld_user ) {
352 $vals['user'] = $u->getName();
353 }
354 if ( $this->userNameUtils->isTemp( $u->getName() ) ) {
355 $vals['temp'] = true;
356 }
357 if ( !$u->isRegistered() ) {
358 $vals['anon'] = true;
359 }
360
361 if ( $this->fld_userid ) {
362 $vals['userid'] = $u->getId();
363 }
364 }
365 }
366
367 if ( $this->fld_timestamp ) {
368 $vals['timestamp'] = wfTimestamp( TS::ISO_8601, $revision->getTimestamp() );
369 }
370
371 if ( $this->fld_size ) {
372 try {
373 $vals['size'] = (int)$revision->getSize();
374 } catch ( RevisionAccessException ) {
375 // Back compat: If there's no size, return 0.
376 // @todo: Gergő says to mention T198099 as a "todo" here.
377 $vals['size'] = 0;
378 }
379 }
380
381 if ( $this->fld_sha1 ) {
382 $revDel = $this->checkRevDel( $revision, RevisionRecord::DELETED_TEXT );
383 if ( $revDel & self::IS_DELETED ) {
384 $vals['sha1hidden'] = true;
385 $anyHidden = true;
386 }
387 if ( !( $revDel & self::CANNOT_VIEW ) ) {
388 try {
389 $vals['sha1'] = \Wikimedia\base_convert( $revision->getSha1(), 36, 16, 40 );
390 } catch ( RevisionAccessException ) {
391 // Back compat: If there's no sha1, return empty string.
392 // @todo: Gergő says to mention T198099 as a "todo" here.
393 $vals['sha1'] = '';
394 }
395 }
396 }
397
398 try {
399 if ( $this->fld_roles ) {
400 $vals['roles'] = $revision->getSlotRoles();
401 }
402
403 if ( $this->needSlots ) {
404 $revDel = $this->checkRevDel( $revision, RevisionRecord::DELETED_TEXT );
405 if ( ( $this->fld_slotsha1 || $this->fetchContent ) && ( $revDel & self::IS_DELETED ) ) {
406 $anyHidden = true;
407 }
408 $vals = array_merge( $vals, $this->extractAllSlotInfo( $revision, $revDel ) );
409 }
410 } catch ( RevisionAccessException $ex ) {
411 // This is here so T212428 doesn't spam the log.
412 // TODO: find out why T212428 happens in the first place!
413 $vals['slotsmissing'] = true;
414
415 LoggerFactory::getInstance( 'api-warning' )->error(
416 'Failed to access revision slots',
417 [ 'revision' => $revision->getId(), 'exception' => $ex, ]
418 );
419 }
420
421 if ( $this->fld_comment || $this->fld_parsedcomment ) {
422 $revDel = $this->checkRevDel( $revision, RevisionRecord::DELETED_COMMENT );
423 if ( $revDel & self::IS_DELETED ) {
424 $vals['commenthidden'] = true;
425 $anyHidden = true;
426 }
427 if ( !( $revDel & self::CANNOT_VIEW ) ) {
428 $comment = $revision->getComment( RevisionRecord::RAW );
429 $comment = $comment->text ?? '';
430
431 if ( $this->fld_comment ) {
432 $vals['comment'] = $comment;
433 }
434
435 if ( $this->fld_parsedcomment ) {
436 $vals['parsedcomment'] = $this->commentFormatter->format(
437 $comment, $revision->getPageAsLinkTarget()
438 );
439 }
440 }
441 }
442
443 if ( $this->fld_tags ) {
444 if ( $row->ts_tags ) {
445 $tags = explode( ',', $row->ts_tags );
446 ApiResult::setIndexedTagName( $tags, 'tag' );
447 $vals['tags'] = $tags;
448 } else {
449 $vals['tags'] = [];
450 }
451 }
452
453 if ( $anyHidden && $revision->isDeleted( RevisionRecord::DELETED_RESTRICTED ) ) {
454 $vals['suppressed'] = true;
455 }
456
457 return $vals;
458 }
459
469 private function extractAllSlotInfo( RevisionRecord $revision, $revDel ): array {
470 $vals = [];
471
472 if ( $this->slotRoles === null ) {
473 try {
474 $slot = $revision->getSlot( SlotRecord::MAIN, RevisionRecord::RAW );
475 } catch ( RevisionAccessException ) {
476 // Back compat: If there's no slot, there's no content, so set 'textmissing'
477 // @todo: Gergő says to mention T198099 as a "todo" here.
478 $vals['textmissing'] = true;
479 $slot = null;
480 }
481
482 if ( $slot ) {
483 $content = null;
484 $vals += $this->extractSlotInfo( $slot, $revDel, $content );
485 if ( !empty( $vals['nosuchsection'] ) ) {
486 $this->dieWithError(
487 [
488 'apierror-nosuchsection-what',
489 wfEscapeWikiText( $this->section ),
490 $this->msg( 'revid', $revision->getId() )
491 ],
492 'nosuchsection'
493 );
494 }
495 if ( $content ) {
496 $vals += $this->extractDeprecatedContent( $content, $revision );
497 }
498 }
499 } else {
500 $roles = array_intersect( $this->slotRoles, $revision->getSlotRoles() );
501 $vals['slots'] = [
503 ];
504 foreach ( $roles as $role ) {
505 try {
506 $slot = $revision->getSlot( $role, RevisionRecord::RAW );
507 } catch ( RevisionAccessException ) {
508 // Don't error out here so the client can still process other slots/revisions.
509 // @todo: Gergő says to mention T198099 as a "todo" here.
510 $vals['slots'][$role]['missing'] = true;
511 continue;
512 }
513 $content = null;
514 $vals['slots'][$role] = $this->extractSlotInfo( $slot, $revDel, $content );
515 // @todo Move this into extractSlotInfo() (and remove its $content parameter)
516 // when extractDeprecatedContent() is no more.
517 if ( $content && $this->getAuthority()->authorizeRead( 'read', $revision->getPage() ) ) {
519 $model = $content->getModel();
520 $format = $this->slotContentFormats[$role] ?? $content->getDefaultFormat();
521 if ( !$content->isSupportedFormat( $format ) ) {
522 $this->addWarning( [
523 'apierror-badformat',
524 $format,
525 $model,
526 $this->msg( 'revid', $revision->getId() )
527 ] );
528 $vals['slots'][$role]['badcontentformat'] = true;
529 } else {
530 $vals['slots'][$role]['contentmodel'] = $model;
531 $vals['slots'][$role]['contentformat'] = $format;
533 $vals['slots'][$role],
534 'content',
535 $content->serialize( $format )
536 );
537 }
538 }
539 }
540 ApiResult::setArrayType( $vals['slots'], 'kvp', 'role' );
541 ApiResult::setIndexedTagName( $vals['slots'], 'slot' );
542 }
543 return $vals;
544 }
545
555 private function extractSlotInfo( SlotRecord $slot, $revDel, &$content = null ) {
556 $vals = [];
557 ApiResult::setArrayType( $vals, 'assoc' );
558
559 if ( $this->fld_slotsize ) {
560 $vals['size'] = (int)$slot->getSize();
561 }
562
563 if ( $this->fld_slotsha1 ) {
564 if ( $revDel & self::IS_DELETED ) {
565 $vals['sha1hidden'] = true;
566 }
567 if ( !( $revDel & self::CANNOT_VIEW ) ) {
568 if ( $slot->getSha1() != '' ) {
569 $vals['sha1'] = \Wikimedia\base_convert( $slot->getSha1(), 36, 16, 40 );
570 } else {
571 $vals['sha1'] = '';
572 }
573 }
574 }
575
576 if ( $this->fld_contentmodel ) {
577 $vals['contentmodel'] = $slot->getModel();
578 }
579
580 $content = null;
581 if ( $this->fetchContent ) {
582 if ( $revDel & self::IS_DELETED ) {
583 $vals['texthidden'] = true;
584 }
585 if ( !( $revDel & self::CANNOT_VIEW ) ) {
586 try {
587 $content = $slot->getContent();
588 } catch ( RevisionAccessException ) {
589 // @todo: Gergő says to mention T198099 as a "todo" here.
590 $vals['textmissing'] = true;
591 }
592 // Expand templates after getting section content because
593 // template-added sections don't count and Parser::preprocess()
594 // will have less input
595 if ( $content && $this->section !== false ) {
596 $content = $content->getSection( $this->section );
597 if ( !$content ) {
598 $vals['nosuchsection'] = true;
599 }
600 }
601 }
602 }
603
604 return $vals;
605 }
606
613 private function extractDeprecatedContent( Content $content, RevisionRecord $revision ) {
614 $vals = [];
615 $title = Title::newFromPageIdentity( $revision->getPage() );
616
617 if ( !$this->getAuthority()->authorizeRead( 'read', $title ) ) {
618 return [];
619 }
620
621 if ( $this->fld_parsetree || ( $this->fld_content && $this->generateXML ) ) {
622 if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
624 '@phan-var WikitextContent $content';
625 $t = $content->getText(); # note: don't set $text
626
627 $parser = $this->parserFactory->create();
628 $parser->startExternalParse(
629 $title,
630 ParserOptions::newFromContext( $this->getContext() ),
631 Parser::OT_PREPROCESS
632 );
633 $dom = $parser->preprocessToDom( $t );
634 if ( is_callable( [ $dom, 'saveXML' ] ) ) {
635 // @phan-suppress-next-line PhanUndeclaredMethod
636 $xml = $dom->saveXML();
637 } else {
638 // @phan-suppress-next-line PhanUndeclaredMethod
639 $xml = $dom->__toString();
640 }
641 $vals['parsetree'] = $xml;
642 } else {
643 $vals['badcontentformatforparsetree'] = true;
644 $this->addWarning(
645 [
646 'apierror-parsetree-notwikitext-title',
647 wfEscapeWikiText( $title->getPrefixedText() ),
648 $content->getModel()
649 ],
650 'parsetree-notwikitext'
651 );
652 }
653 }
654
655 if ( $this->fld_content ) {
656 $text = null;
657
658 if ( $this->expandTemplates && !$this->parseContent ) {
659 if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
661 '@phan-var WikitextContent $content';
662 $text = $content->getText();
663
664 $text = $this->parserFactory->create()->preprocess(
665 $text,
666 $title,
667 ParserOptions::newFromContext( $this->getContext() )
668 );
669 } else {
670 $this->addWarning( [
671 'apierror-templateexpansion-notwikitext',
672 wfEscapeWikiText( $title->getPrefixedText() ),
673 $content->getModel()
674 ] );
675 $vals['badcontentformat'] = true;
676 $text = false;
677 }
678 }
679 if ( $this->parseContent ) {
680 $popts = ParserOptions::newFromContext( $this->getContext() );
681 $po = $this->contentRenderer->getParserOutput(
682 $content,
683 $title,
684 $revision,
685 $popts
686 );
687 // TODO T371004 move runOutputPipeline out of $parserOutput
688 $text = $po->runOutputPipeline( $popts, [] )->getContentHolderText();
689 }
690
691 if ( $text === null ) {
692 $format = $this->contentFormat ?: $content->getDefaultFormat();
693 $model = $content->getModel();
694
695 if ( !$content->isSupportedFormat( $format ) ) {
696 $name = wfEscapeWikiText( $title->getPrefixedText() );
697 $this->addWarning( [ 'apierror-badformat', $this->contentFormat, $model, $name ] );
698 $vals['badcontentformat'] = true;
699 $text = false;
700 } else {
701 $text = $content->serialize( $format );
702 // always include format and model.
703 // Format is needed to deserialize, model is needed to interpret.
704 $vals['contentformat'] = $format;
705 $vals['contentmodel'] = $model;
706 }
707 }
708
709 if ( $text !== false ) {
710 ApiResult::setContentValue( $vals, 'content', $text );
711 }
712 }
713
714 if ( $content && ( $this->diffto !== null || $this->difftotext !== null ) ) {
715 if ( $this->numUncachedDiffs < $this->getConfig()->get( MainConfigNames::APIMaxUncachedDiffs ) ) {
716 $vals['diff'] = [];
717 $context = new DerivativeContext( $this->getContext() );
718 $context->setTitle( $title );
719 $handler = $content->getContentHandler();
720
721 if ( $this->difftotext !== null ) {
722 $model = $title->getContentModel();
723
724 if ( $this->contentFormat
725 && !$this->contentHandlerFactory->getContentHandler( $model )
726 ->isSupportedFormat( $this->contentFormat )
727 ) {
728 $name = wfEscapeWikiText( $title->getPrefixedText() );
729 $this->addWarning( [ 'apierror-badformat', $this->contentFormat, $model, $name ] );
730 $vals['diff']['badcontentformat'] = true;
731 $engine = null;
732 } else {
733 $difftocontent = $this->contentHandlerFactory->getContentHandler( $model )
734 ->unserializeContent( $this->difftotext, $this->contentFormat );
735
736 if ( $this->difftotextpst ) {
737 $popts = ParserOptions::newFromContext( $this->getContext() );
738 $difftocontent = $this->contentTransformer->preSaveTransform(
739 $difftocontent,
740 $title,
741 $this->getUserForPreview(),
742 $popts
743 );
744 }
745
746 $engine = $handler->createDifferenceEngine( $context );
747 $engine->setContent( $content, $difftocontent );
748 }
749 } else {
750 $engine = $handler->createDifferenceEngine( $context, $revision->getId(), $this->diffto );
751 $vals['diff']['from'] = $engine->getOldid();
752 $vals['diff']['to'] = $engine->getNewid();
753 }
754 if ( $engine ) {
755 $difftext = $engine->getDiffBody();
756 ApiResult::setContentValue( $vals['diff'], 'body', $difftext );
757 if ( !$engine->wasCacheHit() ) {
758 $this->numUncachedDiffs++;
759 }
760 foreach ( $engine->getRevisionLoadErrors() as $msg ) {
761 $this->addWarning( $msg );
762 }
763 }
764 } else {
765 $vals['diff']['notcached'] = true;
766 }
767 }
768
769 return $vals;
770 }
771
772 private function getUserForPreview(): UserIdentity {
773 $user = $this->getUser();
774 if ( $this->tempUserCreator->shouldAutoCreate( $user, 'edit' ) ) {
775 return $this->userFactory->newUnsavedTempUser(
776 $this->tempUserCreator->getStashedName( $this->getRequest()->getSession() )
777 );
778 }
779 return $user;
780 }
781
788 public function getCacheMode( $params ) {
789 if ( $this->userCanSeeRevDel() ) {
790 return 'private';
791 }
792
793 if ( isset( $params['prop'] ) && in_array( 'tags', $params['prop'], true ) ) {
794 // ts_tags output varies by the viewer's rights when restricted
795 // tags are configured; the mode must not depend on configuration
796 return 'anon-public-user-private';
797 }
798
799 return 'public';
800 }
801
806 public function getAllowedParams() {
807 $slotRoles = $this->slotRoleRegistry->getKnownRoles();
808 sort( $slotRoles, SORT_STRING );
809 $smallLimit = $this->getMain()->canApiHighLimits() ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_SML1;
810
811 return [
812 'prop' => [
813 ParamValidator::PARAM_ISMULTI => true,
814 ParamValidator::PARAM_DEFAULT => 'ids|timestamp|flags|comment|user',
815 ParamValidator::PARAM_TYPE => [
816 'ids',
817 'flags',
818 'timestamp',
819 'user',
820 'userid',
821 'size',
822 'slotsize',
823 'sha1',
824 'slotsha1',
825 'contentmodel',
826 'comment',
827 'parsedcomment',
828 'content',
829 'tags',
830 'roles',
831 'parsetree',
832 ],
833 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-prop',
835 'ids' => 'apihelp-query+revisions+base-paramvalue-prop-ids',
836 'flags' => 'apihelp-query+revisions+base-paramvalue-prop-flags',
837 'timestamp' => 'apihelp-query+revisions+base-paramvalue-prop-timestamp',
838 'user' => 'apihelp-query+revisions+base-paramvalue-prop-user',
839 'userid' => 'apihelp-query+revisions+base-paramvalue-prop-userid',
840 'size' => 'apihelp-query+revisions+base-paramvalue-prop-size',
841 'slotsize' => 'apihelp-query+revisions+base-paramvalue-prop-slotsize',
842 'sha1' => 'apihelp-query+revisions+base-paramvalue-prop-sha1',
843 'slotsha1' => 'apihelp-query+revisions+base-paramvalue-prop-slotsha1',
844 'contentmodel' => 'apihelp-query+revisions+base-paramvalue-prop-contentmodel',
845 'comment' => 'apihelp-query+revisions+base-paramvalue-prop-comment',
846 'parsedcomment' => 'apihelp-query+revisions+base-paramvalue-prop-parsedcomment',
847 'content' => [ 'apihelp-query+revisions+base-paramvalue-prop-content', $smallLimit ],
848 'tags' => 'apihelp-query+revisions+base-paramvalue-prop-tags',
849 'roles' => 'apihelp-query+revisions+base-paramvalue-prop-roles',
850 'parsetree' => [ 'apihelp-query+revisions+base-paramvalue-prop-parsetree',
851 CONTENT_MODEL_WIKITEXT, $smallLimit ],
852 ],
853 EnumDef::PARAM_DEPRECATED_VALUES => [
854 'parsetree' => true,
855 ],
856 ],
857 'slots' => [
858 ParamValidator::PARAM_TYPE => $slotRoles,
859 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-slots',
860 ParamValidator::PARAM_ISMULTI => true,
861 ParamValidator::PARAM_ALL => true,
862 ],
863 'contentformat-{slot}' => [
864 ApiBase::PARAM_TEMPLATE_VARS => [ 'slot' => 'slots' ],
865 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-contentformat-slot',
866 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
867 ],
868 'limit' => [
869 ParamValidator::PARAM_TYPE => 'limit',
870 IntegerDef::PARAM_MIN => 1,
871 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
872 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2,
873 ApiBase::PARAM_HELP_MSG => [ 'apihelp-query+revisions+base-param-limit',
874 $smallLimit, self::LIMIT_PARSE ],
875 ],
876 'expandtemplates' => [
877 ParamValidator::PARAM_DEFAULT => false,
878 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-expandtemplates',
879 ParamValidator::PARAM_DEPRECATED => true,
880 ],
881 'generatexml' => [
882 ParamValidator::PARAM_DEFAULT => false,
883 ParamValidator::PARAM_DEPRECATED => true,
884 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-generatexml',
885 ],
886 'parse' => [
887 ParamValidator::PARAM_DEFAULT => false,
888 ApiBase::PARAM_HELP_MSG => [ 'apihelp-query+revisions+base-param-parse', self::LIMIT_PARSE ],
889 ParamValidator::PARAM_DEPRECATED => true,
890 ],
891 'section' => [
892 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-section',
893 ],
894 'diffto' => [
895 ApiBase::PARAM_HELP_MSG => [ 'apihelp-query+revisions+base-param-diffto', $smallLimit ],
896 ParamValidator::PARAM_DEPRECATED => true,
897 ],
898 'difftotext' => [
899 ApiBase::PARAM_HELP_MSG => [ 'apihelp-query+revisions+base-param-difftotext', $smallLimit ],
900 ParamValidator::PARAM_DEPRECATED => true,
901 ],
902 'difftotextpst' => [
903 ParamValidator::PARAM_DEFAULT => false,
904 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-difftotextpst',
905 ParamValidator::PARAM_DEPRECATED => true,
906 ],
907 'contentformat' => [
908 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
909 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-contentformat',
910 ParamValidator::PARAM_DEPRECATED => true,
911 ],
912 ];
913 }
914}
915
917class_alias( ApiQueryRevisionsBase::class, 'ApiQueryRevisionsBase' );
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:235
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.
const LIMIT_SML1
Slow query, standard limit.
Definition ApiBase.php:235
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1522
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition ApiBase.php:566
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:557
getMain()
Get the main module.
Definition ApiBase.php:575
getResult()
Get the result object.
Definition ApiBase.php:696
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:206
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition ApiBase.php:1439
const LIMIT_SML2
Slow query, apihighlimits limit.
Definition ApiBase.php:237
const PARAM_TEMPLATE_VARS
(array) Indicate that this is a templated parameter, and specify replacements.
Definition ApiBase.php:224
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:166
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:233
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:231
This class contains a list of pages that the client has requested.
encodeParamName( $paramName)
Overrides ApiBase to prepend 'g' to every generator parameter.
A base class for functions common to producing a list of revisions.
bool $fld_parsetree
The number of uncached diffs that had to be generated for this request.
string[] $slotContentFormats
The number of uncached diffs that had to be generated for this request.
bool $fld_slotsize
The number of uncached diffs that had to be generated for this request.
bool $fld_comment
The number of uncached diffs that had to be generated for this request.
bool $fld_tags
The number of uncached diffs that had to be generated for this request.
bool $fld_roles
The number of uncached diffs that had to be generated for this request.
bool $fld_sha1
The number of uncached diffs that had to be generated for this request.
bool $generateXML
The number of uncached diffs that had to be generated for this request.
bool $fld_slotsha1
The number of uncached diffs that had to be generated for this request.
string null $difftotext
The number of uncached diffs that had to be generated for this request.
array $slotRoles
The number of uncached diffs that had to be generated for this request.
string $contentFormat
The number of uncached diffs that had to be generated for this request.
run(?ApiPageSet $resultPageSet=null)
bool $fld_content
The number of uncached diffs that had to be generated for this request.
__construct(ApiQuery $queryModule, string $moduleName, string $paramPrefix='', ?RevisionStore $revisionStore=null, ?IContentHandlerFactory $contentHandlerFactory=null, ?ParserFactory $parserFactory=null, ?SlotRoleRegistry $slotRoleRegistry=null, ?ContentRenderer $contentRenderer=null, ?ContentTransformer $contentTransformer=null, ?CommentFormatter $commentFormatter=null, ?TempUserCreator $tempUserCreator=null, ?UserFactory $userFactory=null, ?UserNameUtils $userNameUtils=null)
execute()
The number of uncached diffs that had to be generated for this request.
bool $needSlots
The number of uncached diffs that had to be generated for this request.
bool $fld_user
The number of uncached diffs that had to be generated for this request.
extractRevisionInfo(RevisionRecord $revision, $row)
Extract information from the RevisionRecord.
parseParameters( $params)
Parse the parameters into the various instance fields.
int string null $diffto
The number of uncached diffs that had to be generated for this request.
bool $fld_contentmodel
The number of uncached diffs that had to be generated for this request.
bool $setParsedLimit
The number of uncached diffs that had to be generated for this request.
bool $fld_size
The number of uncached diffs that had to be generated for this request.
int string null $section
The number of uncached diffs that had to be generated for this request.
bool $fetchContent
The number of uncached diffs that had to be generated for this request.
int string null $expandTemplates
The number of uncached diffs that had to be generated for this request.
bool $fld_timestamp
The number of uncached diffs that had to be generated for this request.
int string null $limit
The number of uncached diffs that had to be generated for this request.
bool $fld_ids
The number of uncached diffs that had to be generated for this request.
bool $fld_parsedcomment
The number of uncached diffs that had to be generated for this request.
bool $parseContent
The number of uncached diffs that had to be generated for this request.
bool $fld_userid
The number of uncached diffs that had to be generated for this request.
bool $fld_flags
The number of uncached diffs that had to be generated for this request.
executeGenerator( $resultPageSet)
Execute this module as a generator.
bool $difftotextpst
The number of uncached diffs that had to be generated for this request.
This is the main query class.
Definition ApiQuery.php:36
const META_KVP_MERGE
Key for the metadata item that indicates that the KVP key should be added into an assoc value,...
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
static setArrayType(array &$arr, $type, $kvpKeyName=null)
Set the array data type.
static setContentValue(array &$arr, $name, $value, $flags=0)
Add an output value to the array by name and mark as META_CONTENT.
This is the main service interface for converting single-line comments from various DB comment fields...
Content object for wiki text pages.
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
An IContextSource implementation which will inherit context from another source but allow individual ...
Create PSR-3 logger objects.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Set options of the Parser.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:139
Exception representing a failure to look up a revision.
Page revision base class.
getSlot( $role, $audience=self::FOR_PUBLIC, ?Authority $performer=null)
Returns meta-data for the given slot.
getSize()
Returns the nominal size of this revision, in bogo-bytes.
getUser( $audience=self::FOR_PUBLIC, ?Authority $performer=null)
Fetch revision's author's user identity, if it's available to the specified audience.
getSlotRoles()
Returns the slot names (roles) of all slots present in this revision.
getPage()
Returns the page this revision belongs to.
getParentId( $wikiId=self::LOCAL)
Get parent revision ID (the original previous page revision).
getComment( $audience=self::FOR_PUBLIC, ?Authority $performer=null)
Fetch revision comment, if it's available to the specified audience.
getTimestamp()
MCR migration note: this replaced Revision::getTimestamp.
getSha1()
Returns the base36 sha1 of this revision.
isMinor()
MCR migration note: this replaced Revision::isMinor.
getPageAsLinkTarget()
Returns the title of the page this revision is associated with as a LinkTarget object.
userCan( $field, Authority $performer)
Determine if the given authority is allowed to view a particular field of this revision,...
isDeleted( $field)
MCR migration note: this replaced Revision::isDeleted.
getId( $wikiId=self::LOCAL)
Get revision ID.
Service for looking up page revisions.
Value object representing a content slot associated with a page revision.
A registry service for SlotRoleHandlers, used to define which slot roles are available on which page.
Represents a title within MediaWiki.
Definition Title.php:69
Service for temporary user creation.
Create User objects.
UserNameUtils service.
Service for formatting and validating API parameters.
Type definition for enumeration types.
Definition EnumDef.php:32
Type definition for integer types.
Content objects represent page content, e.g.
Definition Content.php:28
Interface for objects representing user identity.