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 if ( $this->needSlots && $this->slotRoles === null ) {
303 $encParam = $this->encodeParamName( 'slots' );
304 $name = $this->getModuleName();
305 $parent = $this->getParent();
306 $parentParam = $parent->encodeParamName( $parent->getModuleManager()->getModuleGroup( $name ) );
307 $this->addDeprecation(
308 [ 'apiwarn-deprecation-missingparam', $encParam ],
309 "action=query&{$parentParam}={$name}&!{$encParam}"
310 );
311 }
312 }
313
321 private function checkRevDel( RevisionRecord $revision, $field ) {
322 $ret = $revision->isDeleted( $field ) ? self::IS_DELETED : 0;
323 if ( $ret ) {
324 $canSee = $revision->userCan( $field, $this->getAuthority() );
325 $ret |= ( $canSee ? 0 : self::CANNOT_VIEW );
326 }
327 return $ret;
328 }
329
338 protected function extractRevisionInfo( RevisionRecord $revision, $row ) {
339 $vals = [];
340 $anyHidden = false;
341
342 if ( $this->fld_ids ) {
343 $vals['revid'] = (int)$revision->getId();
344 if ( $revision->getParentId() !== null ) {
345 $vals['parentid'] = (int)$revision->getParentId();
346 }
347 }
348
349 if ( $this->fld_flags ) {
350 $vals['minor'] = $revision->isMinor();
351 }
352
353 if ( $this->fld_user || $this->fld_userid ) {
354 $revDel = $this->checkRevDel( $revision, RevisionRecord::DELETED_USER );
355 if ( $revDel & self::IS_DELETED ) {
356 $vals['userhidden'] = true;
357 $anyHidden = true;
358 }
359 if ( !( $revDel & self::CANNOT_VIEW ) ) {
360 $u = $revision->getUser( RevisionRecord::RAW );
361 if ( $this->fld_user ) {
362 $vals['user'] = $u->getName();
363 }
364 if ( $this->userNameUtils->isTemp( $u->getName() ) ) {
365 $vals['temp'] = true;
366 }
367 if ( !$u->isRegistered() ) {
368 $vals['anon'] = true;
369 }
370
371 if ( $this->fld_userid ) {
372 $vals['userid'] = $u->getId();
373 }
374 }
375 }
376
377 if ( $this->fld_timestamp ) {
378 $vals['timestamp'] = wfTimestamp( TS::ISO_8601, $revision->getTimestamp() );
379 }
380
381 if ( $this->fld_size ) {
382 try {
383 $vals['size'] = (int)$revision->getSize();
384 } catch ( RevisionAccessException ) {
385 // Back compat: If there's no size, return 0.
386 // @todo: Gergő says to mention T198099 as a "todo" here.
387 $vals['size'] = 0;
388 }
389 }
390
391 if ( $this->fld_sha1 ) {
392 $revDel = $this->checkRevDel( $revision, RevisionRecord::DELETED_TEXT );
393 if ( $revDel & self::IS_DELETED ) {
394 $vals['sha1hidden'] = true;
395 $anyHidden = true;
396 }
397 if ( !( $revDel & self::CANNOT_VIEW ) ) {
398 try {
399 $vals['sha1'] = \Wikimedia\base_convert( $revision->getSha1(), 36, 16, 40 );
400 } catch ( RevisionAccessException ) {
401 // Back compat: If there's no sha1, return empty string.
402 // @todo: Gergő says to mention T198099 as a "todo" here.
403 $vals['sha1'] = '';
404 }
405 }
406 }
407
408 try {
409 if ( $this->fld_roles ) {
410 $vals['roles'] = $revision->getSlotRoles();
411 }
412
413 if ( $this->needSlots ) {
414 $revDel = $this->checkRevDel( $revision, RevisionRecord::DELETED_TEXT );
415 if ( ( $this->fld_slotsha1 || $this->fetchContent ) && ( $revDel & self::IS_DELETED ) ) {
416 $anyHidden = true;
417 }
418 $vals = array_merge( $vals, $this->extractAllSlotInfo( $revision, $revDel ) );
419 }
420 } catch ( RevisionAccessException $ex ) {
421 // This is here so T212428 doesn't spam the log.
422 // TODO: find out why T212428 happens in the first place!
423 $vals['slotsmissing'] = true;
424
425 LoggerFactory::getInstance( 'api-warning' )->error(
426 'Failed to access revision slots',
427 [ 'revision' => $revision->getId(), 'exception' => $ex, ]
428 );
429 }
430
431 if ( $this->fld_comment || $this->fld_parsedcomment ) {
432 $revDel = $this->checkRevDel( $revision, RevisionRecord::DELETED_COMMENT );
433 if ( $revDel & self::IS_DELETED ) {
434 $vals['commenthidden'] = true;
435 $anyHidden = true;
436 }
437 if ( !( $revDel & self::CANNOT_VIEW ) ) {
438 $comment = $revision->getComment( RevisionRecord::RAW );
439 $comment = $comment->text ?? '';
440
441 if ( $this->fld_comment ) {
442 $vals['comment'] = $comment;
443 }
444
445 if ( $this->fld_parsedcomment ) {
446 $vals['parsedcomment'] = $this->commentFormatter->format(
447 $comment, $revision->getPageAsLinkTarget()
448 );
449 }
450 }
451 }
452
453 if ( $this->fld_tags ) {
454 if ( $row->ts_tags ) {
455 $tags = explode( ',', $row->ts_tags );
456 ApiResult::setIndexedTagName( $tags, 'tag' );
457 $vals['tags'] = $tags;
458 } else {
459 $vals['tags'] = [];
460 }
461 }
462
463 if ( $anyHidden && $revision->isDeleted( RevisionRecord::DELETED_RESTRICTED ) ) {
464 $vals['suppressed'] = true;
465 }
466
467 return $vals;
468 }
469
479 private function extractAllSlotInfo( RevisionRecord $revision, $revDel ): array {
480 $vals = [];
481
482 if ( $this->slotRoles === null ) {
483 try {
484 $slot = $revision->getSlot( SlotRecord::MAIN, RevisionRecord::RAW );
485 } catch ( RevisionAccessException ) {
486 // Back compat: If there's no slot, there's no content, so set 'textmissing'
487 // @todo: Gergő says to mention T198099 as a "todo" here.
488 $vals['textmissing'] = true;
489 $slot = null;
490 }
491
492 if ( $slot ) {
493 $content = null;
494 $vals += $this->extractSlotInfo( $slot, $revDel, $content );
495 if ( !empty( $vals['nosuchsection'] ) ) {
496 $this->dieWithError(
497 [
498 'apierror-nosuchsection-what',
499 wfEscapeWikiText( $this->section ),
500 $this->msg( 'revid', $revision->getId() )
501 ],
502 'nosuchsection'
503 );
504 }
505 if ( $content ) {
506 $vals += $this->extractDeprecatedContent( $content, $revision );
507 }
508 }
509 } else {
510 $roles = array_intersect( $this->slotRoles, $revision->getSlotRoles() );
511 $vals['slots'] = [
513 ];
514 foreach ( $roles as $role ) {
515 try {
516 $slot = $revision->getSlot( $role, RevisionRecord::RAW );
517 } catch ( RevisionAccessException ) {
518 // Don't error out here so the client can still process other slots/revisions.
519 // @todo: Gergő says to mention T198099 as a "todo" here.
520 $vals['slots'][$role]['missing'] = true;
521 continue;
522 }
523 $content = null;
524 $vals['slots'][$role] = $this->extractSlotInfo( $slot, $revDel, $content );
525 // @todo Move this into extractSlotInfo() (and remove its $content parameter)
526 // when extractDeprecatedContent() is no more.
527 if ( $content && $this->getAuthority()->authorizeRead( 'read', $revision->getPage() ) ) {
529 $model = $content->getModel();
530 $format = $this->slotContentFormats[$role] ?? $content->getDefaultFormat();
531 if ( !$content->isSupportedFormat( $format ) ) {
532 $this->addWarning( [
533 'apierror-badformat',
534 $format,
535 $model,
536 $this->msg( 'revid', $revision->getId() )
537 ] );
538 $vals['slots'][$role]['badcontentformat'] = true;
539 } else {
540 $vals['slots'][$role]['contentmodel'] = $model;
541 $vals['slots'][$role]['contentformat'] = $format;
543 $vals['slots'][$role],
544 'content',
545 $content->serialize( $format )
546 );
547 }
548 }
549 }
550 ApiResult::setArrayType( $vals['slots'], 'kvp', 'role' );
551 ApiResult::setIndexedTagName( $vals['slots'], 'slot' );
552 }
553 return $vals;
554 }
555
565 private function extractSlotInfo( SlotRecord $slot, $revDel, &$content = null ) {
566 $vals = [];
567 ApiResult::setArrayType( $vals, 'assoc' );
568
569 if ( $this->fld_slotsize ) {
570 $vals['size'] = (int)$slot->getSize();
571 }
572
573 if ( $this->fld_slotsha1 ) {
574 if ( $revDel & self::IS_DELETED ) {
575 $vals['sha1hidden'] = true;
576 }
577 if ( !( $revDel & self::CANNOT_VIEW ) ) {
578 if ( $slot->getSha1() != '' ) {
579 $vals['sha1'] = \Wikimedia\base_convert( $slot->getSha1(), 36, 16, 40 );
580 } else {
581 $vals['sha1'] = '';
582 }
583 }
584 }
585
586 if ( $this->fld_contentmodel ) {
587 $vals['contentmodel'] = $slot->getModel();
588 }
589
590 $content = null;
591 if ( $this->fetchContent ) {
592 if ( $revDel & self::IS_DELETED ) {
593 $vals['texthidden'] = true;
594 }
595 if ( !( $revDel & self::CANNOT_VIEW ) ) {
596 try {
597 $content = $slot->getContent();
598 } catch ( RevisionAccessException ) {
599 // @todo: Gergő says to mention T198099 as a "todo" here.
600 $vals['textmissing'] = true;
601 }
602 // Expand templates after getting section content because
603 // template-added sections don't count and Parser::preprocess()
604 // will have less input
605 if ( $content && $this->section !== false ) {
606 $content = $content->getSection( $this->section );
607 if ( !$content ) {
608 $vals['nosuchsection'] = true;
609 }
610 }
611 }
612 }
613
614 return $vals;
615 }
616
623 private function extractDeprecatedContent( Content $content, RevisionRecord $revision ) {
624 $vals = [];
625 $title = Title::newFromPageIdentity( $revision->getPage() );
626
627 if ( !$this->getAuthority()->authorizeRead( 'read', $title ) ) {
628 return [];
629 }
630
631 if ( $this->fld_parsetree || ( $this->fld_content && $this->generateXML ) ) {
632 if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
634 '@phan-var WikitextContent $content';
635 $t = $content->getText(); # note: don't set $text
636
637 $parser = $this->parserFactory->create();
638 $parser->startExternalParse(
639 $title,
640 ParserOptions::newFromContext( $this->getContext() ),
641 Parser::OT_PREPROCESS
642 );
643 $dom = $parser->preprocessToDom( $t );
644 if ( is_callable( [ $dom, 'saveXML' ] ) ) {
645 // @phan-suppress-next-line PhanUndeclaredMethod
646 $xml = $dom->saveXML();
647 } else {
648 // @phan-suppress-next-line PhanUndeclaredMethod
649 $xml = $dom->__toString();
650 }
651 $vals['parsetree'] = $xml;
652 } else {
653 $vals['badcontentformatforparsetree'] = true;
654 $this->addWarning(
655 [
656 'apierror-parsetree-notwikitext-title',
657 wfEscapeWikiText( $title->getPrefixedText() ),
658 $content->getModel()
659 ],
660 'parsetree-notwikitext'
661 );
662 }
663 }
664
665 if ( $this->fld_content ) {
666 $text = null;
667
668 if ( $this->expandTemplates && !$this->parseContent ) {
669 if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
671 '@phan-var WikitextContent $content';
672 $text = $content->getText();
673
674 $text = $this->parserFactory->create()->preprocess(
675 $text,
676 $title,
677 ParserOptions::newFromContext( $this->getContext() )
678 );
679 } else {
680 $this->addWarning( [
681 'apierror-templateexpansion-notwikitext',
682 wfEscapeWikiText( $title->getPrefixedText() ),
683 $content->getModel()
684 ] );
685 $vals['badcontentformat'] = true;
686 $text = false;
687 }
688 }
689 if ( $this->parseContent ) {
690 $popts = ParserOptions::newFromContext( $this->getContext() );
691 $po = $this->contentRenderer->getParserOutput(
692 $content,
693 $title,
694 $revision,
695 $popts
696 );
697 // TODO T371004 move runOutputPipeline out of $parserOutput
698 $text = $po->runOutputPipeline( $popts, [] )->getContentHolderText();
699 }
700
701 if ( $text === null ) {
702 $format = $this->contentFormat ?: $content->getDefaultFormat();
703 $model = $content->getModel();
704
705 if ( !$content->isSupportedFormat( $format ) ) {
706 $name = wfEscapeWikiText( $title->getPrefixedText() );
707 $this->addWarning( [ 'apierror-badformat', $this->contentFormat, $model, $name ] );
708 $vals['badcontentformat'] = true;
709 $text = false;
710 } else {
711 $text = $content->serialize( $format );
712 // always include format and model.
713 // Format is needed to deserialize, model is needed to interpret.
714 $vals['contentformat'] = $format;
715 $vals['contentmodel'] = $model;
716 }
717 }
718
719 if ( $text !== false ) {
720 ApiResult::setContentValue( $vals, 'content', $text );
721 }
722 }
723
724 if ( $content && ( $this->diffto !== null || $this->difftotext !== null ) ) {
725 if ( $this->numUncachedDiffs < $this->getConfig()->get( MainConfigNames::APIMaxUncachedDiffs ) ) {
726 $vals['diff'] = [];
727 $context = new DerivativeContext( $this->getContext() );
728 $context->setTitle( $title );
729 $handler = $content->getContentHandler();
730
731 if ( $this->difftotext !== null ) {
732 $model = $title->getContentModel();
733
734 if ( $this->contentFormat
735 && !$this->contentHandlerFactory->getContentHandler( $model )
736 ->isSupportedFormat( $this->contentFormat )
737 ) {
738 $name = wfEscapeWikiText( $title->getPrefixedText() );
739 $this->addWarning( [ 'apierror-badformat', $this->contentFormat, $model, $name ] );
740 $vals['diff']['badcontentformat'] = true;
741 $engine = null;
742 } else {
743 $difftocontent = $this->contentHandlerFactory->getContentHandler( $model )
744 ->unserializeContent( $this->difftotext, $this->contentFormat );
745
746 if ( $this->difftotextpst ) {
747 $popts = ParserOptions::newFromContext( $this->getContext() );
748 $difftocontent = $this->contentTransformer->preSaveTransform(
749 $difftocontent,
750 $title,
751 $this->getUserForPreview(),
752 $popts
753 );
754 }
755
756 $engine = $handler->createDifferenceEngine( $context );
757 $engine->setContent( $content, $difftocontent );
758 }
759 } else {
760 $engine = $handler->createDifferenceEngine( $context, $revision->getId(), $this->diffto );
761 $vals['diff']['from'] = $engine->getOldid();
762 $vals['diff']['to'] = $engine->getNewid();
763 }
764 if ( $engine ) {
765 $difftext = $engine->getDiffBody();
766 ApiResult::setContentValue( $vals['diff'], 'body', $difftext );
767 if ( !$engine->wasCacheHit() ) {
768 $this->numUncachedDiffs++;
769 }
770 foreach ( $engine->getRevisionLoadErrors() as $msg ) {
771 $this->addWarning( $msg );
772 }
773 }
774 } else {
775 $vals['diff']['notcached'] = true;
776 }
777 }
778
779 return $vals;
780 }
781
782 private function getUserForPreview(): UserIdentity {
783 $user = $this->getUser();
784 if ( $this->tempUserCreator->shouldAutoCreate( $user, 'edit' ) ) {
785 return $this->userFactory->newUnsavedTempUser(
786 $this->tempUserCreator->getStashedName( $this->getRequest()->getSession() )
787 );
788 }
789 return $user;
790 }
791
798 public function getCacheMode( $params ) {
799 if ( $this->userCanSeeRevDel() ) {
800 return 'private';
801 }
802
803 return 'public';
804 }
805
810 public function getAllowedParams() {
811 $slotRoles = $this->slotRoleRegistry->getKnownRoles();
812 sort( $slotRoles, SORT_STRING );
813 $smallLimit = $this->getMain()->canApiHighLimits() ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_SML1;
814
815 return [
816 'prop' => [
817 ParamValidator::PARAM_ISMULTI => true,
818 ParamValidator::PARAM_DEFAULT => 'ids|timestamp|flags|comment|user',
819 ParamValidator::PARAM_TYPE => [
820 'ids',
821 'flags',
822 'timestamp',
823 'user',
824 'userid',
825 'size',
826 'slotsize',
827 'sha1',
828 'slotsha1',
829 'contentmodel',
830 'comment',
831 'parsedcomment',
832 'content',
833 'tags',
834 'roles',
835 'parsetree',
836 ],
837 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-prop',
838 ApiBase::PARAM_HELP_MSG_PER_VALUE => [
839 'ids' => 'apihelp-query+revisions+base-paramvalue-prop-ids',
840 'flags' => 'apihelp-query+revisions+base-paramvalue-prop-flags',
841 'timestamp' => 'apihelp-query+revisions+base-paramvalue-prop-timestamp',
842 'user' => 'apihelp-query+revisions+base-paramvalue-prop-user',
843 'userid' => 'apihelp-query+revisions+base-paramvalue-prop-userid',
844 'size' => 'apihelp-query+revisions+base-paramvalue-prop-size',
845 'slotsize' => 'apihelp-query+revisions+base-paramvalue-prop-slotsize',
846 'sha1' => 'apihelp-query+revisions+base-paramvalue-prop-sha1',
847 'slotsha1' => 'apihelp-query+revisions+base-paramvalue-prop-slotsha1',
848 'contentmodel' => 'apihelp-query+revisions+base-paramvalue-prop-contentmodel',
849 'comment' => 'apihelp-query+revisions+base-paramvalue-prop-comment',
850 'parsedcomment' => 'apihelp-query+revisions+base-paramvalue-prop-parsedcomment',
851 'content' => [ 'apihelp-query+revisions+base-paramvalue-prop-content', $smallLimit ],
852 'tags' => 'apihelp-query+revisions+base-paramvalue-prop-tags',
853 'roles' => 'apihelp-query+revisions+base-paramvalue-prop-roles',
854 'parsetree' => [ 'apihelp-query+revisions+base-paramvalue-prop-parsetree',
855 CONTENT_MODEL_WIKITEXT, $smallLimit ],
856 ],
857 EnumDef::PARAM_DEPRECATED_VALUES => [
858 'parsetree' => true,
859 ],
860 ],
861 'slots' => [
862 ParamValidator::PARAM_TYPE => $slotRoles,
863 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-slots',
864 ParamValidator::PARAM_ISMULTI => true,
865 ParamValidator::PARAM_ALL => true,
866 ],
867 'contentformat-{slot}' => [
868 ApiBase::PARAM_TEMPLATE_VARS => [ 'slot' => 'slots' ],
869 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-contentformat-slot',
870 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
871 ],
872 'limit' => [
873 ParamValidator::PARAM_TYPE => 'limit',
874 IntegerDef::PARAM_MIN => 1,
875 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
876 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2,
877 ApiBase::PARAM_HELP_MSG => [ 'apihelp-query+revisions+base-param-limit',
878 $smallLimit, self::LIMIT_PARSE ],
879 ],
880 'expandtemplates' => [
881 ParamValidator::PARAM_DEFAULT => false,
882 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-expandtemplates',
883 ParamValidator::PARAM_DEPRECATED => true,
884 ],
885 'generatexml' => [
886 ParamValidator::PARAM_DEFAULT => false,
887 ParamValidator::PARAM_DEPRECATED => true,
888 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-generatexml',
889 ],
890 'parse' => [
891 ParamValidator::PARAM_DEFAULT => false,
892 ApiBase::PARAM_HELP_MSG => [ 'apihelp-query+revisions+base-param-parse', self::LIMIT_PARSE ],
893 ParamValidator::PARAM_DEPRECATED => true,
894 ],
895 'section' => [
896 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-section',
897 ],
898 'diffto' => [
899 ApiBase::PARAM_HELP_MSG => [ 'apihelp-query+revisions+base-param-diffto', $smallLimit ],
900 ParamValidator::PARAM_DEPRECATED => true,
901 ],
902 'difftotext' => [
903 ApiBase::PARAM_HELP_MSG => [ 'apihelp-query+revisions+base-param-difftotext', $smallLimit ],
904 ParamValidator::PARAM_DEPRECATED => true,
905 ],
906 'difftotextpst' => [
907 ParamValidator::PARAM_DEFAULT => false,
908 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-difftotextpst',
909 ParamValidator::PARAM_DEPRECATED => true,
910 ],
911 'contentformat' => [
912 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
913 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-contentformat',
914 ParamValidator::PARAM_DEPRECATED => true,
915 ],
916 ];
917 }
918}
919
921class_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:236
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1507
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition ApiBase.php:552
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:543
getMain()
Get the main module.
Definition ApiBase.php:561
getResult()
Get the result object.
Definition ApiBase.php:682
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition ApiBase.php:1425
const LIMIT_SML2
Slow query, apihighlimits limit.
Definition ApiBase.php:238
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:234
addDeprecation( $msg, $feature, $data=[])
Add a deprecation warning for this module.
Definition ApiBase.php:1439
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:232
This class contains a list of pages that the client has requested.
getParent()
Get the parent of this module.to override 1.25 ApiBase|null
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:135
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 give 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.