Move remaining LoadBalancer classes to Rdbms
[mediawiki.git] / includes / api / ApiQueryRevisionsBase.php
blob696ec878670c77203eaad7d0ce1a226b91bd3d28
1 <?php
2 /**
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
24 * @file
27 /**
28 * A base class for functions common to producing a list of revisions.
30 * @ingroup API
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() {
43 $this->run();
46 public function executeGenerator( $resultPageSet ) {
47 $this->run( $resultPageSet );
50 /**
51 * @param ApiPageSet $resultPageSet
52 * @return void
54 abstract protected function run( ApiPageSet $resultPageSet = null );
56 /**
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'
71 ) {
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'] );
80 if ( !$difftoRev ) {
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 ( !empty( $params['contentformat'] ) ) {
108 $this->contentFormat = $params['contentformat'];
111 $this->limit = $params['limit'];
113 $this->fetchContent = $this->fld_content || !is_null( $this->diffto )
114 || !is_null( $this->difftotext ) || $this->fld_parsetree;
116 $smallLimit = false;
117 if ( $this->fetchContent ) {
118 $smallLimit = true;
119 $this->expandTemplates = $params['expandtemplates'];
120 $this->generateXML = $params['generatexml'];
121 $this->parseContent = $params['parse'];
122 if ( $this->parseContent ) {
123 // Must manually initialize unset limit
124 if ( is_null( $this->limit ) ) {
125 $this->limit = 1;
128 if ( isset( $params['section'] ) ) {
129 $this->section = $params['section'];
130 } else {
131 $this->section = false;
135 $userMax = $this->parseContent ? 1 : ( $smallLimit ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1 );
136 $botMax = $this->parseContent ? 1 : ( $smallLimit ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2 );
137 if ( $this->limit == 'max' ) {
138 $this->limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
139 if ( $this->setParsedLimit ) {
140 $this->getResult()->addParsedLimit( $this->getModuleName(), $this->limit );
144 if ( is_null( $this->limit ) ) {
145 $this->limit = 10;
147 $this->validateLimit( 'limit', $this->limit, 1, $userMax, $botMax );
151 * Extract information from the Revision
153 * @param Revision $revision
154 * @param object $row Should have a field 'ts_tags' if $this->fld_tags is set
155 * @return array
157 protected function extractRevisionInfo( Revision $revision, $row ) {
158 $title = $revision->getTitle();
159 $user = $this->getUser();
160 $vals = [];
161 $anyHidden = false;
163 if ( $this->fld_ids ) {
164 $vals['revid'] = intval( $revision->getId() );
165 if ( !is_null( $revision->getParentId() ) ) {
166 $vals['parentid'] = intval( $revision->getParentId() );
170 if ( $this->fld_flags ) {
171 $vals['minor'] = $revision->isMinor();
174 if ( $this->fld_user || $this->fld_userid ) {
175 if ( $revision->isDeleted( Revision::DELETED_USER ) ) {
176 $vals['userhidden'] = true;
177 $anyHidden = true;
179 if ( $revision->userCan( Revision::DELETED_USER, $user ) ) {
180 if ( $this->fld_user ) {
181 $vals['user'] = $revision->getUserText( Revision::RAW );
183 $userid = $revision->getUser( Revision::RAW );
184 if ( !$userid ) {
185 $vals['anon'] = true;
188 if ( $this->fld_userid ) {
189 $vals['userid'] = $userid;
194 if ( $this->fld_timestamp ) {
195 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $revision->getTimestamp() );
198 if ( $this->fld_size ) {
199 if ( !is_null( $revision->getSize() ) ) {
200 $vals['size'] = intval( $revision->getSize() );
201 } else {
202 $vals['size'] = 0;
206 if ( $this->fld_sha1 ) {
207 if ( $revision->isDeleted( Revision::DELETED_TEXT ) ) {
208 $vals['sha1hidden'] = true;
209 $anyHidden = true;
211 if ( $revision->userCan( Revision::DELETED_TEXT, $user ) ) {
212 if ( $revision->getSha1() != '' ) {
213 $vals['sha1'] = Wikimedia\base_convert( $revision->getSha1(), 36, 16, 40 );
214 } else {
215 $vals['sha1'] = '';
220 if ( $this->fld_contentmodel ) {
221 $vals['contentmodel'] = $revision->getContentModel();
224 if ( $this->fld_comment || $this->fld_parsedcomment ) {
225 if ( $revision->isDeleted( Revision::DELETED_COMMENT ) ) {
226 $vals['commenthidden'] = true;
227 $anyHidden = true;
229 if ( $revision->userCan( Revision::DELETED_COMMENT, $user ) ) {
230 $comment = $revision->getComment( Revision::RAW );
232 if ( $this->fld_comment ) {
233 $vals['comment'] = $comment;
236 if ( $this->fld_parsedcomment ) {
237 $vals['parsedcomment'] = Linker::formatComment( $comment, $title );
242 if ( $this->fld_tags ) {
243 if ( $row->ts_tags ) {
244 $tags = explode( ',', $row->ts_tags );
245 ApiResult::setIndexedTagName( $tags, 'tag' );
246 $vals['tags'] = $tags;
247 } else {
248 $vals['tags'] = [];
252 $content = null;
253 global $wgParser;
254 if ( $this->fetchContent ) {
255 $content = $revision->getContent( Revision::FOR_THIS_USER, $this->getUser() );
256 // Expand templates after getting section content because
257 // template-added sections don't count and Parser::preprocess()
258 // will have less input
259 if ( $content && $this->section !== false ) {
260 $content = $content->getSection( $this->section, false );
261 if ( !$content ) {
262 $this->dieWithError(
264 'apierror-nosuchsection-what',
265 wfEscapeWikiText( $this->section ),
266 $this->msg( 'revid', $revision->getId() )
268 'nosuchsection'
272 if ( $revision->isDeleted( Revision::DELETED_TEXT ) ) {
273 $vals['texthidden'] = true;
274 $anyHidden = true;
275 } elseif ( !$content ) {
276 $vals['textmissing'] = true;
279 if ( $this->fld_parsetree || ( $this->fld_content && $this->generateXML ) ) {
280 if ( $content ) {
281 if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
282 $t = $content->getNativeData(); # note: don't set $text
284 $wgParser->startExternalParse(
285 $title,
286 ParserOptions::newFromContext( $this->getContext() ),
287 Parser::OT_PREPROCESS
289 $dom = $wgParser->preprocessToDom( $t );
290 if ( is_callable( [ $dom, 'saveXML' ] ) ) {
291 $xml = $dom->saveXML();
292 } else {
293 $xml = $dom->__toString();
295 $vals['parsetree'] = $xml;
296 } else {
297 $vals['badcontentformatforparsetree'] = true;
298 $this->addWarning(
300 'apierror-parsetree-notwikitext-title',
301 wfEscapeWikiText( $title->getPrefixedText() ),
302 $content->getModel()
304 'parsetree-notwikitext'
310 if ( $this->fld_content && $content ) {
311 $text = null;
313 if ( $this->expandTemplates && !$this->parseContent ) {
314 # XXX: implement template expansion for all content types in ContentHandler?
315 if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
316 $text = $content->getNativeData();
318 $text = $wgParser->preprocess(
319 $text,
320 $title,
321 ParserOptions::newFromContext( $this->getContext() )
323 } else {
324 $this->addWarning( [
325 'apierror-templateexpansion-notwikitext',
326 wfEscapeWikiText( $title->getPrefixedText() ),
327 $content->getModel()
328 ] );
329 $vals['badcontentformat'] = true;
330 $text = false;
333 if ( $this->parseContent ) {
334 $po = $content->getParserOutput(
335 $title,
336 $revision->getId(),
337 ParserOptions::newFromContext( $this->getContext() )
339 $text = $po->getText();
342 if ( $text === null ) {
343 $format = $this->contentFormat ?: $content->getDefaultFormat();
344 $model = $content->getModel();
346 if ( !$content->isSupportedFormat( $format ) ) {
347 $name = wfEscapeWikiText( $title->getPrefixedText() );
348 $this->addWarning( [ 'apierror-badformat', $this->contentFormat, $model, $name ] );
349 $vals['badcontentformat'] = true;
350 $text = false;
351 } else {
352 $text = $content->serialize( $format );
353 // always include format and model.
354 // Format is needed to deserialize, model is needed to interpret.
355 $vals['contentformat'] = $format;
356 $vals['contentmodel'] = $model;
360 if ( $text !== false ) {
361 ApiResult::setContentValue( $vals, 'content', $text );
365 if ( $content && ( !is_null( $this->diffto ) || !is_null( $this->difftotext ) ) ) {
366 static $n = 0; // Number of uncached diffs we've had
368 if ( $n < $this->getConfig()->get( 'APIMaxUncachedDiffs' ) ) {
369 $vals['diff'] = [];
370 $context = new DerivativeContext( $this->getContext() );
371 $context->setTitle( $title );
372 $handler = $revision->getContentHandler();
374 if ( !is_null( $this->difftotext ) ) {
375 $model = $title->getContentModel();
377 if ( $this->contentFormat
378 && !ContentHandler::getForModelID( $model )->isSupportedFormat( $this->contentFormat )
380 $name = wfEscapeWikiText( $title->getPrefixedText() );
381 $this->addWarning( [ 'apierror-badformat', $this->contentFormat, $model, $name ] );
382 $vals['diff']['badcontentformat'] = true;
383 $engine = null;
384 } else {
385 $difftocontent = ContentHandler::makeContent(
386 $this->difftotext,
387 $title,
388 $model,
389 $this->contentFormat
392 if ( $this->difftotextpst ) {
393 $popts = ParserOptions::newFromContext( $this->getContext() );
394 $difftocontent = $difftocontent->preSaveTransform( $title, $user, $popts );
397 $engine = $handler->createDifferenceEngine( $context );
398 $engine->setContent( $content, $difftocontent );
400 } else {
401 $engine = $handler->createDifferenceEngine( $context, $revision->getId(), $this->diffto );
402 $vals['diff']['from'] = $engine->getOldid();
403 $vals['diff']['to'] = $engine->getNewid();
405 if ( $engine ) {
406 $difftext = $engine->getDiffBody();
407 ApiResult::setContentValue( $vals['diff'], 'body', $difftext );
408 if ( !$engine->wasCacheHit() ) {
409 $n++;
412 } else {
413 $vals['diff']['notcached'] = true;
417 if ( $anyHidden && $revision->isDeleted( Revision::DELETED_RESTRICTED ) ) {
418 $vals['suppressed'] = true;
421 return $vals;
424 public function getCacheMode( $params ) {
425 if ( $this->userCanSeeRevDel() ) {
426 return 'private';
429 return 'public';
432 public function getAllowedParams() {
433 return [
434 'prop' => [
435 ApiBase::PARAM_ISMULTI => true,
436 ApiBase::PARAM_DFLT => 'ids|timestamp|flags|comment|user',
437 ApiBase::PARAM_TYPE => [
438 'ids',
439 'flags',
440 'timestamp',
441 'user',
442 'userid',
443 'size',
444 'sha1',
445 'contentmodel',
446 'comment',
447 'parsedcomment',
448 'content',
449 'tags',
450 'parsetree',
452 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-prop',
453 ApiBase::PARAM_HELP_MSG_PER_VALUE => [
454 'ids' => 'apihelp-query+revisions+base-paramvalue-prop-ids',
455 'flags' => 'apihelp-query+revisions+base-paramvalue-prop-flags',
456 'timestamp' => 'apihelp-query+revisions+base-paramvalue-prop-timestamp',
457 'user' => 'apihelp-query+revisions+base-paramvalue-prop-user',
458 'userid' => 'apihelp-query+revisions+base-paramvalue-prop-userid',
459 'size' => 'apihelp-query+revisions+base-paramvalue-prop-size',
460 'sha1' => 'apihelp-query+revisions+base-paramvalue-prop-sha1',
461 'contentmodel' => 'apihelp-query+revisions+base-paramvalue-prop-contentmodel',
462 'comment' => 'apihelp-query+revisions+base-paramvalue-prop-comment',
463 'parsedcomment' => 'apihelp-query+revisions+base-paramvalue-prop-parsedcomment',
464 'content' => 'apihelp-query+revisions+base-paramvalue-prop-content',
465 'tags' => 'apihelp-query+revisions+base-paramvalue-prop-tags',
466 'parsetree' => [ 'apihelp-query+revisions+base-paramvalue-prop-parsetree',
467 CONTENT_MODEL_WIKITEXT ],
470 'limit' => [
471 ApiBase::PARAM_TYPE => 'limit',
472 ApiBase::PARAM_MIN => 1,
473 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
474 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2,
475 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-limit',
477 'expandtemplates' => [
478 ApiBase::PARAM_DFLT => false,
479 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-expandtemplates',
481 'generatexml' => [
482 ApiBase::PARAM_DFLT => false,
483 ApiBase::PARAM_DEPRECATED => true,
484 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-generatexml',
486 'parse' => [
487 ApiBase::PARAM_DFLT => false,
488 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-parse',
490 'section' => [
491 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-section',
493 'diffto' => [
494 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-diffto',
496 'difftotext' => [
497 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-difftotext',
499 'difftotextpst' => [
500 ApiBase::PARAM_DFLT => false,
501 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-difftotextpst',
503 'contentformat' => [
504 ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
505 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-contentformat',