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