5 * Created on Oct 3, 2014 as a split from ApiQueryRevisions
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
28 * A base class for functions common to producing a list of revisions.
32 abstract class ApiQueryRevisionsBase
extends ApiQueryGeneratorBase
{
34 protected $limit, $diffto, $difftotext, $difftotextpst, $expandTemplates, $generateXML,
35 $section, $parseContent, $fetchContent, $contentFormat, $setParsedLimit = true;
37 protected $fld_ids = false, $fld_flags = false, $fld_timestamp = false,
38 $fld_size = false, $fld_sha1 = false, $fld_comment = false,
39 $fld_parsedcomment = false, $fld_user = false, $fld_userid = false,
40 $fld_content = false, $fld_tags = false, $fld_contentmodel = false, $fld_parsetree = false;
42 public function execute() {
46 public function executeGenerator( $resultPageSet ) {
47 $this->run( $resultPageSet );
51 * @param ApiPageSet $resultPageSet
54 abstract protected function run( ApiPageSet
$resultPageSet = null );
57 * Parse the parameters into the various instance fields.
59 * @param array $params
61 protected function parseParameters( $params ) {
62 if ( !is_null( $params['difftotext'] ) ) {
63 $this->difftotext
= $params['difftotext'];
64 $this->difftotextpst
= $params['difftotextpst'];
65 } elseif ( !is_null( $params['diffto'] ) ) {
66 if ( $params['diffto'] == 'cur' ) {
67 $params['diffto'] = 0;
69 if ( ( !ctype_digit( $params['diffto'] ) ||
$params['diffto'] < 0 )
70 && $params['diffto'] != 'prev' && $params['diffto'] != 'next'
72 $p = $this->getModulePrefix();
73 $this->dieWithError( [ 'apierror-baddiffto', $p ], 'diffto' );
75 // Check whether the revision exists and is readable,
76 // DifferenceEngine returns a rather ambiguous empty
77 // string if that's not the case
78 if ( $params['diffto'] != 0 ) {
79 $difftoRev = Revision
::newFromId( $params['diffto'] );
81 $this->dieWithError( [ 'apierror-nosuchrevid', $params['diffto'] ] );
83 if ( !$difftoRev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
84 $this->addWarning( [ 'apiwarn-difftohidden', $difftoRev->getId() ] );
85 $params['diffto'] = null;
88 $this->diffto
= $params['diffto'];
91 $prop = array_flip( $params['prop'] );
93 $this->fld_ids
= isset( $prop['ids'] );
94 $this->fld_flags
= isset( $prop['flags'] );
95 $this->fld_timestamp
= isset( $prop['timestamp'] );
96 $this->fld_comment
= isset( $prop['comment'] );
97 $this->fld_parsedcomment
= isset( $prop['parsedcomment'] );
98 $this->fld_size
= isset( $prop['size'] );
99 $this->fld_sha1
= isset( $prop['sha1'] );
100 $this->fld_content
= isset( $prop['content'] );
101 $this->fld_contentmodel
= isset( $prop['contentmodel'] );
102 $this->fld_userid
= isset( $prop['userid'] );
103 $this->fld_user
= isset( $prop['user'] );
104 $this->fld_tags
= isset( $prop['tags'] );
105 $this->fld_parsetree
= isset( $prop['parsetree'] );
107 if ( $this->fld_parsetree
) {
108 $encParam = $this->encodeParamName( 'prop' );
109 $name = $this->getModuleName();
110 $parent = $this->getParent();
111 $parentParam = $parent->encodeParamName( $parent->getModuleManager()->getModuleGroup( $name ) );
112 $this->addDeprecation(
113 [ 'apiwarn-deprecation-parameter', "{$encParam}=parsetree" ],
114 "action=query&{$parentParam}={$name}&{$encParam}=parsetree"
118 if ( !empty( $params['contentformat'] ) ) {
119 $this->contentFormat
= $params['contentformat'];
122 $this->limit
= $params['limit'];
124 $this->fetchContent
= $this->fld_content ||
!is_null( $this->diffto
)
125 ||
!is_null( $this->difftotext
) ||
$this->fld_parsetree
;
128 if ( $this->fetchContent
) {
130 $this->expandTemplates
= $params['expandtemplates'];
131 $this->generateXML
= $params['generatexml'];
132 $this->parseContent
= $params['parse'];
133 if ( $this->parseContent
) {
134 // Must manually initialize unset limit
135 if ( is_null( $this->limit
) ) {
139 if ( isset( $params['section'] ) ) {
140 $this->section
= $params['section'];
142 $this->section
= false;
146 $userMax = $this->parseContent ?
1 : ( $smallLimit ? ApiBase
::LIMIT_SML1
: ApiBase
::LIMIT_BIG1
);
147 $botMax = $this->parseContent ?
1 : ( $smallLimit ? ApiBase
::LIMIT_SML2
: ApiBase
::LIMIT_BIG2
);
148 if ( $this->limit
== 'max' ) {
149 $this->limit
= $this->getMain()->canApiHighLimits() ?
$botMax : $userMax;
150 if ( $this->setParsedLimit
) {
151 $this->getResult()->addParsedLimit( $this->getModuleName(), $this->limit
);
155 if ( is_null( $this->limit
) ) {
158 $this->validateLimit( 'limit', $this->limit
, 1, $userMax, $botMax );
162 * Extract information from the Revision
164 * @param Revision $revision
165 * @param object $row Should have a field 'ts_tags' if $this->fld_tags is set
168 protected function extractRevisionInfo( Revision
$revision, $row ) {
169 $title = $revision->getTitle();
170 $user = $this->getUser();
174 if ( $this->fld_ids
) {
175 $vals['revid'] = intval( $revision->getId() );
176 if ( !is_null( $revision->getParentId() ) ) {
177 $vals['parentid'] = intval( $revision->getParentId() );
181 if ( $this->fld_flags
) {
182 $vals['minor'] = $revision->isMinor();
185 if ( $this->fld_user ||
$this->fld_userid
) {
186 if ( $revision->isDeleted( Revision
::DELETED_USER
) ) {
187 $vals['userhidden'] = true;
190 if ( $revision->userCan( Revision
::DELETED_USER
, $user ) ) {
191 if ( $this->fld_user
) {
192 $vals['user'] = $revision->getUserText( Revision
::RAW
);
194 $userid = $revision->getUser( Revision
::RAW
);
196 $vals['anon'] = true;
199 if ( $this->fld_userid
) {
200 $vals['userid'] = $userid;
205 if ( $this->fld_timestamp
) {
206 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $revision->getTimestamp() );
209 if ( $this->fld_size
) {
210 if ( !is_null( $revision->getSize() ) ) {
211 $vals['size'] = intval( $revision->getSize() );
217 if ( $this->fld_sha1
) {
218 if ( $revision->isDeleted( Revision
::DELETED_TEXT
) ) {
219 $vals['sha1hidden'] = true;
222 if ( $revision->userCan( Revision
::DELETED_TEXT
, $user ) ) {
223 if ( $revision->getSha1() != '' ) {
224 $vals['sha1'] = Wikimedia\base_convert
( $revision->getSha1(), 36, 16, 40 );
231 if ( $this->fld_contentmodel
) {
232 $vals['contentmodel'] = $revision->getContentModel();
235 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
236 if ( $revision->isDeleted( Revision
::DELETED_COMMENT
) ) {
237 $vals['commenthidden'] = true;
240 if ( $revision->userCan( Revision
::DELETED_COMMENT
, $user ) ) {
241 $comment = $revision->getComment( Revision
::RAW
);
243 if ( $this->fld_comment
) {
244 $vals['comment'] = $comment;
247 if ( $this->fld_parsedcomment
) {
248 $vals['parsedcomment'] = Linker
::formatComment( $comment, $title );
253 if ( $this->fld_tags
) {
254 if ( $row->ts_tags
) {
255 $tags = explode( ',', $row->ts_tags
);
256 ApiResult
::setIndexedTagName( $tags, 'tag' );
257 $vals['tags'] = $tags;
265 if ( $this->fetchContent
) {
266 $content = $revision->getContent( Revision
::FOR_THIS_USER
, $this->getUser() );
267 // Expand templates after getting section content because
268 // template-added sections don't count and Parser::preprocess()
269 // will have less input
270 if ( $content && $this->section
!== false ) {
271 $content = $content->getSection( $this->section
, false );
275 'apierror-nosuchsection-what',
276 wfEscapeWikiText( $this->section
),
277 $this->msg( 'revid', $revision->getId() )
283 if ( $revision->isDeleted( Revision
::DELETED_TEXT
) ) {
284 $vals['texthidden'] = true;
286 } elseif ( !$content ) {
287 $vals['textmissing'] = true;
290 if ( $this->fld_parsetree ||
( $this->fld_content
&& $this->generateXML
) ) {
292 if ( $content->getModel() === CONTENT_MODEL_WIKITEXT
) {
293 $t = $content->getNativeData(); # note: don't set $text
295 $wgParser->startExternalParse(
297 ParserOptions
::newFromContext( $this->getContext() ),
298 Parser
::OT_PREPROCESS
300 $dom = $wgParser->preprocessToDom( $t );
301 if ( is_callable( [ $dom, 'saveXML' ] ) ) {
302 $xml = $dom->saveXML();
304 $xml = $dom->__toString();
306 $vals['parsetree'] = $xml;
308 $vals['badcontentformatforparsetree'] = true;
311 'apierror-parsetree-notwikitext-title',
312 wfEscapeWikiText( $title->getPrefixedText() ),
315 'parsetree-notwikitext'
321 if ( $this->fld_content
&& $content ) {
324 if ( $this->expandTemplates
&& !$this->parseContent
) {
325 # XXX: implement template expansion for all content types in ContentHandler?
326 if ( $content->getModel() === CONTENT_MODEL_WIKITEXT
) {
327 $text = $content->getNativeData();
329 $text = $wgParser->preprocess(
332 ParserOptions
::newFromContext( $this->getContext() )
336 'apierror-templateexpansion-notwikitext',
337 wfEscapeWikiText( $title->getPrefixedText() ),
340 $vals['badcontentformat'] = true;
344 if ( $this->parseContent
) {
345 $po = $content->getParserOutput(
348 ParserOptions
::newFromContext( $this->getContext() )
350 $text = $po->getText();
353 if ( $text === null ) {
354 $format = $this->contentFormat ?
: $content->getDefaultFormat();
355 $model = $content->getModel();
357 if ( !$content->isSupportedFormat( $format ) ) {
358 $name = wfEscapeWikiText( $title->getPrefixedText() );
359 $this->addWarning( [ 'apierror-badformat', $this->contentFormat
, $model, $name ] );
360 $vals['badcontentformat'] = true;
363 $text = $content->serialize( $format );
364 // always include format and model.
365 // Format is needed to deserialize, model is needed to interpret.
366 $vals['contentformat'] = $format;
367 $vals['contentmodel'] = $model;
371 if ( $text !== false ) {
372 ApiResult
::setContentValue( $vals, 'content', $text );
376 if ( $content && ( !is_null( $this->diffto
) ||
!is_null( $this->difftotext
) ) ) {
377 static $n = 0; // Number of uncached diffs we've had
379 if ( $n < $this->getConfig()->get( 'APIMaxUncachedDiffs' ) ) {
381 $context = new DerivativeContext( $this->getContext() );
382 $context->setTitle( $title );
383 $handler = $revision->getContentHandler();
385 if ( !is_null( $this->difftotext
) ) {
386 $model = $title->getContentModel();
388 if ( $this->contentFormat
389 && !ContentHandler
::getForModelID( $model )->isSupportedFormat( $this->contentFormat
)
391 $name = wfEscapeWikiText( $title->getPrefixedText() );
392 $this->addWarning( [ 'apierror-badformat', $this->contentFormat
, $model, $name ] );
393 $vals['diff']['badcontentformat'] = true;
396 $difftocontent = ContentHandler
::makeContent(
403 if ( $this->difftotextpst
) {
404 $popts = ParserOptions
::newFromContext( $this->getContext() );
405 $difftocontent = $difftocontent->preSaveTransform( $title, $user, $popts );
408 $engine = $handler->createDifferenceEngine( $context );
409 $engine->setContent( $content, $difftocontent );
412 $engine = $handler->createDifferenceEngine( $context, $revision->getId(), $this->diffto
);
413 $vals['diff']['from'] = $engine->getOldid();
414 $vals['diff']['to'] = $engine->getNewid();
417 $difftext = $engine->getDiffBody();
418 ApiResult
::setContentValue( $vals['diff'], 'body', $difftext );
419 if ( !$engine->wasCacheHit() ) {
424 $vals['diff']['notcached'] = true;
428 if ( $anyHidden && $revision->isDeleted( Revision
::DELETED_RESTRICTED
) ) {
429 $vals['suppressed'] = true;
435 public function getCacheMode( $params ) {
436 if ( $this->userCanSeeRevDel() ) {
443 public function getAllowedParams() {
446 ApiBase
::PARAM_ISMULTI
=> true,
447 ApiBase
::PARAM_DFLT
=> 'ids|timestamp|flags|comment|user',
448 ApiBase
::PARAM_TYPE
=> [
463 ApiBase
::PARAM_HELP_MSG
=> 'apihelp-query+revisions+base-param-prop',
464 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [
465 'ids' => 'apihelp-query+revisions+base-paramvalue-prop-ids',
466 'flags' => 'apihelp-query+revisions+base-paramvalue-prop-flags',
467 'timestamp' => 'apihelp-query+revisions+base-paramvalue-prop-timestamp',
468 'user' => 'apihelp-query+revisions+base-paramvalue-prop-user',
469 'userid' => 'apihelp-query+revisions+base-paramvalue-prop-userid',
470 'size' => 'apihelp-query+revisions+base-paramvalue-prop-size',
471 'sha1' => 'apihelp-query+revisions+base-paramvalue-prop-sha1',
472 'contentmodel' => 'apihelp-query+revisions+base-paramvalue-prop-contentmodel',
473 'comment' => 'apihelp-query+revisions+base-paramvalue-prop-comment',
474 'parsedcomment' => 'apihelp-query+revisions+base-paramvalue-prop-parsedcomment',
475 'content' => 'apihelp-query+revisions+base-paramvalue-prop-content',
476 'tags' => 'apihelp-query+revisions+base-paramvalue-prop-tags',
477 'parsetree' => [ 'apihelp-query+revisions+base-paramvalue-prop-parsetree',
478 CONTENT_MODEL_WIKITEXT
],
482 ApiBase
::PARAM_TYPE
=> 'limit',
483 ApiBase
::PARAM_MIN
=> 1,
484 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
485 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
,
486 ApiBase
::PARAM_HELP_MSG
=> 'apihelp-query+revisions+base-param-limit',
488 'expandtemplates' => [
489 ApiBase
::PARAM_DFLT
=> false,
490 ApiBase
::PARAM_HELP_MSG
=> 'apihelp-query+revisions+base-param-expandtemplates',
491 ApiBase
::PARAM_DEPRECATED
=> true,
494 ApiBase
::PARAM_DFLT
=> false,
495 ApiBase
::PARAM_DEPRECATED
=> true,
496 ApiBase
::PARAM_HELP_MSG
=> 'apihelp-query+revisions+base-param-generatexml',
499 ApiBase
::PARAM_DFLT
=> false,
500 ApiBase
::PARAM_HELP_MSG
=> 'apihelp-query+revisions+base-param-parse',
501 ApiBase
::PARAM_DEPRECATED
=> true,
504 ApiBase
::PARAM_HELP_MSG
=> 'apihelp-query+revisions+base-param-section',
507 ApiBase
::PARAM_HELP_MSG
=> 'apihelp-query+revisions+base-param-diffto',
508 ApiBase
::PARAM_DEPRECATED
=> true,
511 ApiBase
::PARAM_HELP_MSG
=> 'apihelp-query+revisions+base-param-difftotext',
512 ApiBase
::PARAM_DEPRECATED
=> true,
515 ApiBase
::PARAM_DFLT
=> false,
516 ApiBase
::PARAM_HELP_MSG
=> 'apihelp-query+revisions+base-param-difftotextpst',
517 ApiBase
::PARAM_DEPRECATED
=> true,
520 ApiBase
::PARAM_TYPE
=> ContentHandler
::getAllContentFormats(),
521 ApiBase
::PARAM_HELP_MSG
=> 'apihelp-query+revisions+base-param-contentformat',