ApiBase::PARAM_DFLT => null is the default anyway
[lhc/web/wiklou.git] / includes / api / ApiQueryRevisionsBase.php
1 <?php
2 /**
3 *
4 *
5 * Created on Oct 3, 2014 as a split from ApiQueryRevisions
6 *
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /**
28 * A base class for functions common to producing a list of revisions.
29 *
30 * @ingroup API
31 */
32 abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase {
33
34 protected $limit, $diffto, $difftotext, $expandTemplates, $generateXML, $section,
35 $parseContent, $fetchContent, $contentFormat, $setParsedLimit = true;
36
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;
41
42 public function execute() {
43 $this->run();
44 }
45
46 public function executeGenerator( $resultPageSet ) {
47 $this->run( $resultPageSet );
48 }
49
50 /**
51 * @param ApiPageSet $resultPageSet
52 * @return void
53 */
54 abstract protected function run( ApiPageSet $resultPageSet = null );
55
56 /**
57 * Parse the parameters into the various instance fields.
58 *
59 * @param array $params
60 */
61 protected function parseParameters( $params ) {
62 if ( !is_null( $params['difftotext'] ) ) {
63 $this->difftotext = $params['difftotext'];
64 } elseif ( !is_null( $params['diffto'] ) ) {
65 if ( $params['diffto'] == 'cur' ) {
66 $params['diffto'] = 0;
67 }
68 if ( ( !ctype_digit( $params['diffto'] ) || $params['diffto'] < 0 )
69 && $params['diffto'] != 'prev' && $params['diffto'] != 'next'
70 ) {
71 $p = $this->getModulePrefix();
72 $this->dieUsage(
73 "{$p}diffto must be set to a non-negative number, \"prev\", \"next\" or \"cur\"",
74 'diffto'
75 );
76 }
77 // Check whether the revision exists and is readable,
78 // DifferenceEngine returns a rather ambiguous empty
79 // string if that's not the case
80 if ( $params['diffto'] != 0 ) {
81 $difftoRev = Revision::newFromId( $params['diffto'] );
82 if ( !$difftoRev ) {
83 $this->dieUsageMsg( array( 'nosuchrevid', $params['diffto'] ) );
84 }
85 if ( !$difftoRev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
86 $this->setWarning( "Couldn't diff to r{$difftoRev->getID()}: content is hidden" );
87 $params['diffto'] = null;
88 }
89 }
90 $this->diffto = $params['diffto'];
91 }
92
93 $prop = array_flip( $params['prop'] );
94
95 $this->fld_ids = isset( $prop['ids'] );
96 $this->fld_flags = isset( $prop['flags'] );
97 $this->fld_timestamp = isset( $prop['timestamp'] );
98 $this->fld_comment = isset( $prop['comment'] );
99 $this->fld_parsedcomment = isset( $prop['parsedcomment'] );
100 $this->fld_size = isset( $prop['size'] );
101 $this->fld_sha1 = isset( $prop['sha1'] );
102 $this->fld_content = isset( $prop['content'] );
103 $this->fld_contentmodel = isset( $prop['contentmodel'] );
104 $this->fld_userid = isset( $prop['userid'] );
105 $this->fld_user = isset( $prop['user'] );
106 $this->fld_tags = isset( $prop['tags'] );
107 $this->fld_parsetree = isset( $prop['parsetree'] );
108
109 if ( !empty( $params['contentformat'] ) ) {
110 $this->contentFormat = $params['contentformat'];
111 }
112
113 $this->limit = $params['limit'];
114
115 $this->fetchContent = $this->fld_content || !is_null( $this->diffto )
116 || !is_null( $this->difftotext ) || $this->fld_parsetree;
117
118 $smallLimit = false;
119 if ( $this->fetchContent ) {
120 $smallLimit = true;
121 $this->expandTemplates = $params['expandtemplates'];
122 $this->generateXML = $params['generatexml'];
123 $this->parseContent = $params['parse'];
124 if ( $this->parseContent ) {
125 // Must manually initialize unset limit
126 if ( is_null( $this->limit ) ) {
127 $this->limit = 1;
128 }
129 }
130 if ( isset( $params['section'] ) ) {
131 $this->section = $params['section'];
132 } else {
133 $this->section = false;
134 }
135 }
136
137 $userMax = $this->parseContent ? 1 : ( $smallLimit ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1 );
138 $botMax = $this->parseContent ? 1 : ( $smallLimit ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2 );
139 if ( $this->limit == 'max' ) {
140 $this->limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
141 if ( $this->setParsedLimit ) {
142 $this->getResult()->addParsedLimit( $this->getModuleName(), $this->limit );
143 }
144 }
145
146 if ( is_null( $this->limit ) ) {
147 $this->limit = 10;
148 }
149 $this->validateLimit( 'limit', $this->limit, 1, $userMax, $botMax );
150 }
151
152 /**
153 * Extract information from the Revision
154 *
155 * @param Revision $revision
156 * @param object $row Should have a field 'ts_tags' if $this->fld_tags is set
157 * @return array
158 */
159 protected function extractRevisionInfo( Revision $revision, $row ) {
160 $title = $revision->getTitle();
161 $user = $this->getUser();
162 $vals = array();
163 $anyHidden = false;
164
165 if ( $this->fld_ids ) {
166 $vals['revid'] = intval( $revision->getId() );
167 if ( !is_null( $revision->getParentId() ) ) {
168 $vals['parentid'] = intval( $revision->getParentId() );
169 }
170 }
171
172 if ( $this->fld_flags ) {
173 $vals['minor'] = $revision->isMinor();
174 }
175
176 if ( $this->fld_user || $this->fld_userid ) {
177 if ( $revision->isDeleted( Revision::DELETED_USER ) ) {
178 $vals['userhidden'] = true;
179 $anyHidden = true;
180 }
181 if ( $revision->userCan( Revision::DELETED_USER, $user ) ) {
182 if ( $this->fld_user ) {
183 $vals['user'] = $revision->getUserText( Revision::RAW );
184 }
185 $userid = $revision->getUser( Revision::RAW );
186 if ( !$userid ) {
187 $vals['anon'] = true;
188 }
189
190 if ( $this->fld_userid ) {
191 $vals['userid'] = $userid;
192 }
193 }
194 }
195
196 if ( $this->fld_timestamp ) {
197 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $revision->getTimestamp() );
198 }
199
200 if ( $this->fld_size ) {
201 if ( !is_null( $revision->getSize() ) ) {
202 $vals['size'] = intval( $revision->getSize() );
203 } else {
204 $vals['size'] = 0;
205 }
206 }
207
208 if ( $this->fld_sha1 ) {
209 if ( $revision->isDeleted( Revision::DELETED_TEXT ) ) {
210 $vals['sha1hidden'] = true;
211 $anyHidden = true;
212 }
213 if ( $revision->userCan( Revision::DELETED_TEXT, $user ) ) {
214 if ( $revision->getSha1() != '' ) {
215 $vals['sha1'] = wfBaseConvert( $revision->getSha1(), 36, 16, 40 );
216 } else {
217 $vals['sha1'] = '';
218 }
219 }
220 }
221
222 if ( $this->fld_contentmodel ) {
223 $vals['contentmodel'] = $revision->getContentModel();
224 }
225
226 if ( $this->fld_comment || $this->fld_parsedcomment ) {
227 if ( $revision->isDeleted( Revision::DELETED_COMMENT ) ) {
228 $vals['commenthidden'] = true;
229 $anyHidden = true;
230 }
231 if ( $revision->userCan( Revision::DELETED_COMMENT, $user ) ) {
232 $comment = $revision->getComment( Revision::RAW );
233
234 if ( $this->fld_comment ) {
235 $vals['comment'] = $comment;
236 }
237
238 if ( $this->fld_parsedcomment ) {
239 $vals['parsedcomment'] = Linker::formatComment( $comment, $title );
240 }
241 }
242 }
243
244 if ( $this->fld_tags ) {
245 if ( $row->ts_tags ) {
246 $tags = explode( ',', $row->ts_tags );
247 ApiResult::setIndexedTagName( $tags, 'tag' );
248 $vals['tags'] = $tags;
249 } else {
250 $vals['tags'] = array();
251 }
252 }
253
254 $content = null;
255 global $wgParser;
256 if ( $this->fetchContent ) {
257 $content = $revision->getContent( Revision::FOR_THIS_USER, $this->getUser() );
258 // Expand templates after getting section content because
259 // template-added sections don't count and Parser::preprocess()
260 // will have less input
261 if ( $content && $this->section !== false ) {
262 $content = $content->getSection( $this->section, false );
263 if ( !$content ) {
264 $this->dieUsage(
265 "There is no section {$this->section} in r" . $revision->getId(),
266 'nosuchsection'
267 );
268 }
269 }
270 if ( $revision->isDeleted( Revision::DELETED_TEXT ) ) {
271 $vals['texthidden'] = true;
272 $anyHidden = true;
273 } elseif ( !$content ) {
274 $vals['textmissing'] = true;
275 }
276 }
277 if ( $this->fld_parsetree || ( $this->fld_content && $this->generateXML ) ) {
278 if ( !$this->fld_parsetree ) {
279 $this->logFeatureUsage( 'action=query&prop=revisions+base&generatexml' );
280 }
281 if ( $content ) {
282 if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
283 $t = $content->getNativeData(); # note: don't set $text
284
285 $wgParser->startExternalParse(
286 $title,
287 ParserOptions::newFromContext( $this->getContext() ),
288 Parser::OT_PREPROCESS
289 );
290 $dom = $wgParser->preprocessToDom( $t );
291 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
292 $xml = $dom->saveXML();
293 } else {
294 $xml = $dom->__toString();
295 }
296 $vals['parsetree'] = $xml;
297 } else {
298 $vals['badcontentformatforparsetree'] = true;
299 $this->setWarning( "Conversion to XML is supported for wikitext only, " .
300 $title->getPrefixedDBkey() .
301 " uses content model " . $content->getModel() );
302 }
303 }
304 }
305
306 if ( $this->fld_content && $content ) {
307 $text = null;
308
309 if ( $this->expandTemplates && !$this->parseContent ) {
310 # XXX: implement template expansion for all content types in ContentHandler?
311 if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
312 $text = $content->getNativeData();
313
314 $text = $wgParser->preprocess(
315 $text,
316 $title,
317 ParserOptions::newFromContext( $this->getContext() )
318 );
319 } else {
320 $this->setWarning( "Template expansion is supported for wikitext only, " .
321 $title->getPrefixedDBkey() .
322 " uses content model " . $content->getModel() );
323 $vals['badcontentformat'] = true;
324 $text = false;
325 }
326 }
327 if ( $this->parseContent ) {
328 $po = $content->getParserOutput(
329 $title,
330 $revision->getId(),
331 ParserOptions::newFromContext( $this->getContext() )
332 );
333 $text = $po->getText();
334 }
335
336 if ( $text === null ) {
337 $format = $this->contentFormat ? $this->contentFormat : $content->getDefaultFormat();
338 $model = $content->getModel();
339
340 if ( !$content->isSupportedFormat( $format ) ) {
341 $name = $title->getPrefixedDBkey();
342 $this->setWarning( "The requested format {$this->contentFormat} is not " .
343 "supported for content model $model used by $name" );
344 $vals['badcontentformat'] = true;
345 $text = false;
346 } else {
347 $text = $content->serialize( $format );
348 // always include format and model.
349 // Format is needed to deserialize, model is needed to interpret.
350 $vals['contentformat'] = $format;
351 $vals['contentmodel'] = $model;
352 }
353 }
354
355 if ( $text !== false ) {
356 ApiResult::setContentValue( $vals, 'content', $text );
357 }
358 }
359
360 if ( $content && ( !is_null( $this->diffto ) || !is_null( $this->difftotext ) ) ) {
361 static $n = 0; // Number of uncached diffs we've had
362
363 if ( $n < $this->getConfig()->get( 'APIMaxUncachedDiffs' ) ) {
364 $vals['diff'] = array();
365 $context = new DerivativeContext( $this->getContext() );
366 $context->setTitle( $title );
367 $handler = $revision->getContentHandler();
368
369 if ( !is_null( $this->difftotext ) ) {
370 $model = $title->getContentModel();
371
372 if ( $this->contentFormat
373 && !ContentHandler::getForModelID( $model )->isSupportedFormat( $this->contentFormat )
374 ) {
375 $name = $title->getPrefixedDBkey();
376 $this->setWarning( "The requested format {$this->contentFormat} is not " .
377 "supported for content model $model used by $name" );
378 $vals['diff']['badcontentformat'] = true;
379 $engine = null;
380 } else {
381 $difftocontent = ContentHandler::makeContent(
382 $this->difftotext,
383 $title,
384 $model,
385 $this->contentFormat
386 );
387
388 $engine = $handler->createDifferenceEngine( $context );
389 $engine->setContent( $content, $difftocontent );
390 }
391 } else {
392 $engine = $handler->createDifferenceEngine( $context, $revision->getID(), $this->diffto );
393 $vals['diff']['from'] = $engine->getOldid();
394 $vals['diff']['to'] = $engine->getNewid();
395 }
396 if ( $engine ) {
397 $difftext = $engine->getDiffBody();
398 ApiResult::setContentValue( $vals['diff'], 'body', $difftext );
399 if ( !$engine->wasCacheHit() ) {
400 $n++;
401 }
402 }
403 } else {
404 $vals['diff']['notcached'] = true;
405 }
406 }
407
408 if ( $anyHidden && $revision->isDeleted( Revision::DELETED_RESTRICTED ) ) {
409 $vals['suppressed'] = true;
410 }
411
412 return $vals;
413 }
414
415 public function getCacheMode( $params ) {
416 if ( $this->userCanSeeRevDel() ) {
417 return 'private';
418 }
419
420 return 'public';
421 }
422
423 public function getAllowedParams() {
424 return array(
425 'prop' => array(
426 ApiBase::PARAM_ISMULTI => true,
427 ApiBase::PARAM_DFLT => 'ids|timestamp|flags|comment|user',
428 ApiBase::PARAM_TYPE => array(
429 'ids',
430 'flags',
431 'timestamp',
432 'user',
433 'userid',
434 'size',
435 'sha1',
436 'contentmodel',
437 'comment',
438 'parsedcomment',
439 'content',
440 'tags',
441 'parsetree',
442 ),
443 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-prop',
444 ApiBase::PARAM_HELP_MSG_PER_VALUE => array(
445 'ids' => 'apihelp-query+revisions+base-paramvalue-prop-ids',
446 'flags' => 'apihelp-query+revisions+base-paramvalue-prop-flags',
447 'timestamp' => 'apihelp-query+revisions+base-paramvalue-prop-timestamp',
448 'user' => 'apihelp-query+revisions+base-paramvalue-prop-user',
449 'userid' => 'apihelp-query+revisions+base-paramvalue-prop-userid',
450 'size' => 'apihelp-query+revisions+base-paramvalue-prop-size',
451 'sha1' => 'apihelp-query+revisions+base-paramvalue-prop-sha1',
452 'contentmodel' => 'apihelp-query+revisions+base-paramvalue-prop-contentmodel',
453 'comment' => 'apihelp-query+revisions+base-paramvalue-prop-comment',
454 'parsedcomment' => 'apihelp-query+revisions+base-paramvalue-prop-parsedcomment',
455 'content' => 'apihelp-query+revisions+base-paramvalue-prop-content',
456 'tags' => 'apihelp-query+revisions+base-paramvalue-prop-tags',
457 'parsetree' => array( 'apihelp-query+revisions+base-paramvalue-prop-parsetree',
458 CONTENT_MODEL_WIKITEXT ),
459 ),
460 ),
461 'limit' => array(
462 ApiBase::PARAM_TYPE => 'limit',
463 ApiBase::PARAM_MIN => 1,
464 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
465 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2,
466 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-limit',
467 ),
468 'expandtemplates' => array(
469 ApiBase::PARAM_DFLT => false,
470 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-expandtemplates',
471 ),
472 'generatexml' => array(
473 ApiBase::PARAM_DFLT => false,
474 ApiBase::PARAM_DEPRECATED => true,
475 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-generatexml',
476 ),
477 'parse' => array(
478 ApiBase::PARAM_DFLT => false,
479 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-parse',
480 ),
481 'section' => array(
482 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-section',
483 ),
484 'diffto' => array(
485 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-diffto',
486 ),
487 'difftotext' => array(
488 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-difftotext',
489 ),
490 'contentformat' => array(
491 ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
492 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-contentformat',
493 ),
494 );
495 }
496
497 }