Merge "Add support for PHP7 random_bytes in favor of mcrypt_create_iv"
[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, $difftotextpst, $expandTemplates, $generateXML,
35 $section, $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 $this->difftotextpst = $params['difftotextpst'];
65 } elseif ( !is_null( $params['diffto'] ) ) {
66 if ( $params['diffto'] == 'cur' ) {
67 $params['diffto'] = 0;
68 }
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' );
74 }
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'] ] );
82 }
83 if ( !$difftoRev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
84 $this->addWarning( [ 'apiwarn-difftohidden', $difftoRev->getId() ] );
85 $params['diffto'] = null;
86 }
87 }
88 $this->diffto = $params['diffto'];
89 }
90
91 $prop = array_flip( $params['prop'] );
92
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'] );
106
107 if ( !empty( $params['contentformat'] ) ) {
108 $this->contentFormat = $params['contentformat'];
109 }
110
111 $this->limit = $params['limit'];
112
113 $this->fetchContent = $this->fld_content || !is_null( $this->diffto )
114 || !is_null( $this->difftotext ) || $this->fld_parsetree;
115
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;
126 }
127 }
128 if ( isset( $params['section'] ) ) {
129 $this->section = $params['section'];
130 } else {
131 $this->section = false;
132 }
133 }
134
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 );
141 }
142 }
143
144 if ( is_null( $this->limit ) ) {
145 $this->limit = 10;
146 }
147 $this->validateLimit( 'limit', $this->limit, 1, $userMax, $botMax );
148 }
149
150 /**
151 * Extract information from the Revision
152 *
153 * @param Revision $revision
154 * @param object $row Should have a field 'ts_tags' if $this->fld_tags is set
155 * @return array
156 */
157 protected function extractRevisionInfo( Revision $revision, $row ) {
158 $title = $revision->getTitle();
159 $user = $this->getUser();
160 $vals = [];
161 $anyHidden = false;
162
163 if ( $this->fld_ids ) {
164 $vals['revid'] = intval( $revision->getId() );
165 if ( !is_null( $revision->getParentId() ) ) {
166 $vals['parentid'] = intval( $revision->getParentId() );
167 }
168 }
169
170 if ( $this->fld_flags ) {
171 $vals['minor'] = $revision->isMinor();
172 }
173
174 if ( $this->fld_user || $this->fld_userid ) {
175 if ( $revision->isDeleted( Revision::DELETED_USER ) ) {
176 $vals['userhidden'] = true;
177 $anyHidden = true;
178 }
179 if ( $revision->userCan( Revision::DELETED_USER, $user ) ) {
180 if ( $this->fld_user ) {
181 $vals['user'] = $revision->getUserText( Revision::RAW );
182 }
183 $userid = $revision->getUser( Revision::RAW );
184 if ( !$userid ) {
185 $vals['anon'] = true;
186 }
187
188 if ( $this->fld_userid ) {
189 $vals['userid'] = $userid;
190 }
191 }
192 }
193
194 if ( $this->fld_timestamp ) {
195 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $revision->getTimestamp() );
196 }
197
198 if ( $this->fld_size ) {
199 if ( !is_null( $revision->getSize() ) ) {
200 $vals['size'] = intval( $revision->getSize() );
201 } else {
202 $vals['size'] = 0;
203 }
204 }
205
206 if ( $this->fld_sha1 ) {
207 if ( $revision->isDeleted( Revision::DELETED_TEXT ) ) {
208 $vals['sha1hidden'] = true;
209 $anyHidden = true;
210 }
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'] = '';
216 }
217 }
218 }
219
220 if ( $this->fld_contentmodel ) {
221 $vals['contentmodel'] = $revision->getContentModel();
222 }
223
224 if ( $this->fld_comment || $this->fld_parsedcomment ) {
225 if ( $revision->isDeleted( Revision::DELETED_COMMENT ) ) {
226 $vals['commenthidden'] = true;
227 $anyHidden = true;
228 }
229 if ( $revision->userCan( Revision::DELETED_COMMENT, $user ) ) {
230 $comment = $revision->getComment( Revision::RAW );
231
232 if ( $this->fld_comment ) {
233 $vals['comment'] = $comment;
234 }
235
236 if ( $this->fld_parsedcomment ) {
237 $vals['parsedcomment'] = Linker::formatComment( $comment, $title );
238 }
239 }
240 }
241
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'] = [];
249 }
250 }
251
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(
263 [
264 'apierror-nosuchsection-what',
265 wfEscapeWikiText( $this->section ),
266 $this->msg( 'revid', $revision->getId() )
267 ],
268 'nosuchsection'
269 );
270 }
271 }
272 if ( $revision->isDeleted( Revision::DELETED_TEXT ) ) {
273 $vals['texthidden'] = true;
274 $anyHidden = true;
275 } elseif ( !$content ) {
276 $vals['textmissing'] = true;
277 }
278 }
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
283
284 $wgParser->startExternalParse(
285 $title,
286 ParserOptions::newFromContext( $this->getContext() ),
287 Parser::OT_PREPROCESS
288 );
289 $dom = $wgParser->preprocessToDom( $t );
290 if ( is_callable( [ $dom, 'saveXML' ] ) ) {
291 $xml = $dom->saveXML();
292 } else {
293 $xml = $dom->__toString();
294 }
295 $vals['parsetree'] = $xml;
296 } else {
297 $vals['badcontentformatforparsetree'] = true;
298 $this->addWarning(
299 [
300 'apierror-parsetree-notwikitext-title',
301 wfEscapeWikiText( $title->getPrefixedText() ),
302 $content->getModel()
303 ],
304 'parsetree-notwikitext'
305 );
306 }
307 }
308 }
309
310 if ( $this->fld_content && $content ) {
311 $text = null;
312
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();
317
318 $text = $wgParser->preprocess(
319 $text,
320 $title,
321 ParserOptions::newFromContext( $this->getContext() )
322 );
323 } else {
324 $this->addWarning( [
325 'apierror-templateexpansion-notwikitext',
326 wfEscapeWikiText( $title->getPrefixedText() ),
327 $content->getModel()
328 ] );
329 $vals['badcontentformat'] = true;
330 $text = false;
331 }
332 }
333 if ( $this->parseContent ) {
334 $po = $content->getParserOutput(
335 $title,
336 $revision->getId(),
337 ParserOptions::newFromContext( $this->getContext() )
338 );
339 $text = $po->getText();
340 }
341
342 if ( $text === null ) {
343 $format = $this->contentFormat ?: $content->getDefaultFormat();
344 $model = $content->getModel();
345
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;
357 }
358 }
359
360 if ( $text !== false ) {
361 ApiResult::setContentValue( $vals, 'content', $text );
362 }
363 }
364
365 if ( $content && ( !is_null( $this->diffto ) || !is_null( $this->difftotext ) ) ) {
366 static $n = 0; // Number of uncached diffs we've had
367
368 if ( $n < $this->getConfig()->get( 'APIMaxUncachedDiffs' ) ) {
369 $vals['diff'] = [];
370 $context = new DerivativeContext( $this->getContext() );
371 $context->setTitle( $title );
372 $handler = $revision->getContentHandler();
373
374 if ( !is_null( $this->difftotext ) ) {
375 $model = $title->getContentModel();
376
377 if ( $this->contentFormat
378 && !ContentHandler::getForModelID( $model )->isSupportedFormat( $this->contentFormat )
379 ) {
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
390 );
391
392 if ( $this->difftotextpst ) {
393 $popts = ParserOptions::newFromContext( $this->getContext() );
394 $difftocontent = $difftocontent->preSaveTransform( $title, $user, $popts );
395 }
396
397 $engine = $handler->createDifferenceEngine( $context );
398 $engine->setContent( $content, $difftocontent );
399 }
400 } else {
401 $engine = $handler->createDifferenceEngine( $context, $revision->getId(), $this->diffto );
402 $vals['diff']['from'] = $engine->getOldid();
403 $vals['diff']['to'] = $engine->getNewid();
404 }
405 if ( $engine ) {
406 $difftext = $engine->getDiffBody();
407 ApiResult::setContentValue( $vals['diff'], 'body', $difftext );
408 if ( !$engine->wasCacheHit() ) {
409 $n++;
410 }
411 }
412 } else {
413 $vals['diff']['notcached'] = true;
414 }
415 }
416
417 if ( $anyHidden && $revision->isDeleted( Revision::DELETED_RESTRICTED ) ) {
418 $vals['suppressed'] = true;
419 }
420
421 return $vals;
422 }
423
424 public function getCacheMode( $params ) {
425 if ( $this->userCanSeeRevDel() ) {
426 return 'private';
427 }
428
429 return 'public';
430 }
431
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',
451 ],
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 ],
468 ],
469 ],
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',
476 ],
477 'expandtemplates' => [
478 ApiBase::PARAM_DFLT => false,
479 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-expandtemplates',
480 ],
481 'generatexml' => [
482 ApiBase::PARAM_DFLT => false,
483 ApiBase::PARAM_DEPRECATED => true,
484 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-generatexml',
485 ],
486 'parse' => [
487 ApiBase::PARAM_DFLT => false,
488 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-parse',
489 ],
490 'section' => [
491 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-section',
492 ],
493 'diffto' => [
494 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-diffto',
495 ],
496 'difftotext' => [
497 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-difftotext',
498 ],
499 'difftotextpst' => [
500 ApiBase::PARAM_DFLT => false,
501 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-difftotextpst',
502 ],
503 'contentformat' => [
504 ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
505 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-contentformat',
506 ],
507 ];
508 }
509
510 }