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