]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/api/ApiQueryRevisionsBase.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.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 ( $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"
115                         );
116                 }
117
118                 if ( !empty( $params['contentformat'] ) ) {
119                         $this->contentFormat = $params['contentformat'];
120                 }
121
122                 $this->limit = $params['limit'];
123
124                 $this->fetchContent = $this->fld_content || !is_null( $this->diffto )
125                         || !is_null( $this->difftotext ) || $this->fld_parsetree;
126
127                 $smallLimit = false;
128                 if ( $this->fetchContent ) {
129                         $smallLimit = true;
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 ) ) {
136                                         $this->limit = 1;
137                                 }
138                         }
139                         if ( isset( $params['section'] ) ) {
140                                 $this->section = $params['section'];
141                         } else {
142                                 $this->section = false;
143                         }
144                 }
145
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 );
152                         }
153                 }
154
155                 if ( is_null( $this->limit ) ) {
156                         $this->limit = 10;
157                 }
158                 $this->validateLimit( 'limit', $this->limit, 1, $userMax, $botMax );
159         }
160
161         /**
162          * Extract information from the Revision
163          *
164          * @param Revision $revision
165          * @param object $row Should have a field 'ts_tags' if $this->fld_tags is set
166          * @return array
167          */
168         protected function extractRevisionInfo( Revision $revision, $row ) {
169                 $title = $revision->getTitle();
170                 $user = $this->getUser();
171                 $vals = [];
172                 $anyHidden = false;
173
174                 if ( $this->fld_ids ) {
175                         $vals['revid'] = intval( $revision->getId() );
176                         if ( !is_null( $revision->getParentId() ) ) {
177                                 $vals['parentid'] = intval( $revision->getParentId() );
178                         }
179                 }
180
181                 if ( $this->fld_flags ) {
182                         $vals['minor'] = $revision->isMinor();
183                 }
184
185                 if ( $this->fld_user || $this->fld_userid ) {
186                         if ( $revision->isDeleted( Revision::DELETED_USER ) ) {
187                                 $vals['userhidden'] = true;
188                                 $anyHidden = true;
189                         }
190                         if ( $revision->userCan( Revision::DELETED_USER, $user ) ) {
191                                 if ( $this->fld_user ) {
192                                         $vals['user'] = $revision->getUserText( Revision::RAW );
193                                 }
194                                 $userid = $revision->getUser( Revision::RAW );
195                                 if ( !$userid ) {
196                                         $vals['anon'] = true;
197                                 }
198
199                                 if ( $this->fld_userid ) {
200                                         $vals['userid'] = $userid;
201                                 }
202                         }
203                 }
204
205                 if ( $this->fld_timestamp ) {
206                         $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $revision->getTimestamp() );
207                 }
208
209                 if ( $this->fld_size ) {
210                         if ( !is_null( $revision->getSize() ) ) {
211                                 $vals['size'] = intval( $revision->getSize() );
212                         } else {
213                                 $vals['size'] = 0;
214                         }
215                 }
216
217                 if ( $this->fld_sha1 ) {
218                         if ( $revision->isDeleted( Revision::DELETED_TEXT ) ) {
219                                 $vals['sha1hidden'] = true;
220                                 $anyHidden = true;
221                         }
222                         if ( $revision->userCan( Revision::DELETED_TEXT, $user ) ) {
223                                 if ( $revision->getSha1() != '' ) {
224                                         $vals['sha1'] = Wikimedia\base_convert( $revision->getSha1(), 36, 16, 40 );
225                                 } else {
226                                         $vals['sha1'] = '';
227                                 }
228                         }
229                 }
230
231                 if ( $this->fld_contentmodel ) {
232                         $vals['contentmodel'] = $revision->getContentModel();
233                 }
234
235                 if ( $this->fld_comment || $this->fld_parsedcomment ) {
236                         if ( $revision->isDeleted( Revision::DELETED_COMMENT ) ) {
237                                 $vals['commenthidden'] = true;
238                                 $anyHidden = true;
239                         }
240                         if ( $revision->userCan( Revision::DELETED_COMMENT, $user ) ) {
241                                 $comment = $revision->getComment( Revision::RAW );
242
243                                 if ( $this->fld_comment ) {
244                                         $vals['comment'] = $comment;
245                                 }
246
247                                 if ( $this->fld_parsedcomment ) {
248                                         $vals['parsedcomment'] = Linker::formatComment( $comment, $title );
249                                 }
250                         }
251                 }
252
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;
258                         } else {
259                                 $vals['tags'] = [];
260                         }
261                 }
262
263                 $content = null;
264                 global $wgParser;
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 );
272                                 if ( !$content ) {
273                                         $this->dieWithError(
274                                                 [
275                                                         'apierror-nosuchsection-what',
276                                                         wfEscapeWikiText( $this->section ),
277                                                         $this->msg( 'revid', $revision->getId() )
278                                                 ],
279                                                 'nosuchsection'
280                                         );
281                                 }
282                         }
283                         if ( $revision->isDeleted( Revision::DELETED_TEXT ) ) {
284                                 $vals['texthidden'] = true;
285                                 $anyHidden = true;
286                         } elseif ( !$content ) {
287                                 $vals['textmissing'] = true;
288                         }
289                 }
290                 if ( $this->fld_parsetree || ( $this->fld_content && $this->generateXML ) ) {
291                         if ( $content ) {
292                                 if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
293                                         $t = $content->getNativeData(); # note: don't set $text
294
295                                         $wgParser->startExternalParse(
296                                                 $title,
297                                                 ParserOptions::newFromContext( $this->getContext() ),
298                                                 Parser::OT_PREPROCESS
299                                         );
300                                         $dom = $wgParser->preprocessToDom( $t );
301                                         if ( is_callable( [ $dom, 'saveXML' ] ) ) {
302                                                 $xml = $dom->saveXML();
303                                         } else {
304                                                 $xml = $dom->__toString();
305                                         }
306                                         $vals['parsetree'] = $xml;
307                                 } else {
308                                         $vals['badcontentformatforparsetree'] = true;
309                                         $this->addWarning(
310                                                 [
311                                                         'apierror-parsetree-notwikitext-title',
312                                                         wfEscapeWikiText( $title->getPrefixedText() ),
313                                                         $content->getModel()
314                                                 ],
315                                                 'parsetree-notwikitext'
316                                         );
317                                 }
318                         }
319                 }
320
321                 if ( $this->fld_content && $content ) {
322                         $text = null;
323
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();
328
329                                         $text = $wgParser->preprocess(
330                                                 $text,
331                                                 $title,
332                                                 ParserOptions::newFromContext( $this->getContext() )
333                                         );
334                                 } else {
335                                         $this->addWarning( [
336                                                 'apierror-templateexpansion-notwikitext',
337                                                 wfEscapeWikiText( $title->getPrefixedText() ),
338                                                 $content->getModel()
339                                         ] );
340                                         $vals['badcontentformat'] = true;
341                                         $text = false;
342                                 }
343                         }
344                         if ( $this->parseContent ) {
345                                 $po = $content->getParserOutput(
346                                         $title,
347                                         $revision->getId(),
348                                         ParserOptions::newFromContext( $this->getContext() )
349                                 );
350                                 $text = $po->getText();
351                         }
352
353                         if ( $text === null ) {
354                                 $format = $this->contentFormat ?: $content->getDefaultFormat();
355                                 $model = $content->getModel();
356
357                                 if ( !$content->isSupportedFormat( $format ) ) {
358                                         $name = wfEscapeWikiText( $title->getPrefixedText() );
359                                         $this->addWarning( [ 'apierror-badformat', $this->contentFormat, $model, $name ] );
360                                         $vals['badcontentformat'] = true;
361                                         $text = false;
362                                 } else {
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;
368                                 }
369                         }
370
371                         if ( $text !== false ) {
372                                 ApiResult::setContentValue( $vals, 'content', $text );
373                         }
374                 }
375
376                 if ( $content && ( !is_null( $this->diffto ) || !is_null( $this->difftotext ) ) ) {
377                         static $n = 0; // Number of uncached diffs we've had
378
379                         if ( $n < $this->getConfig()->get( 'APIMaxUncachedDiffs' ) ) {
380                                 $vals['diff'] = [];
381                                 $context = new DerivativeContext( $this->getContext() );
382                                 $context->setTitle( $title );
383                                 $handler = $revision->getContentHandler();
384
385                                 if ( !is_null( $this->difftotext ) ) {
386                                         $model = $title->getContentModel();
387
388                                         if ( $this->contentFormat
389                                                 && !ContentHandler::getForModelID( $model )->isSupportedFormat( $this->contentFormat )
390                                         ) {
391                                                 $name = wfEscapeWikiText( $title->getPrefixedText() );
392                                                 $this->addWarning( [ 'apierror-badformat', $this->contentFormat, $model, $name ] );
393                                                 $vals['diff']['badcontentformat'] = true;
394                                                 $engine = null;
395                                         } else {
396                                                 $difftocontent = ContentHandler::makeContent(
397                                                         $this->difftotext,
398                                                         $title,
399                                                         $model,
400                                                         $this->contentFormat
401                                                 );
402
403                                                 if ( $this->difftotextpst ) {
404                                                         $popts = ParserOptions::newFromContext( $this->getContext() );
405                                                         $difftocontent = $difftocontent->preSaveTransform( $title, $user, $popts );
406                                                 }
407
408                                                 $engine = $handler->createDifferenceEngine( $context );
409                                                 $engine->setContent( $content, $difftocontent );
410                                         }
411                                 } else {
412                                         $engine = $handler->createDifferenceEngine( $context, $revision->getId(), $this->diffto );
413                                         $vals['diff']['from'] = $engine->getOldid();
414                                         $vals['diff']['to'] = $engine->getNewid();
415                                 }
416                                 if ( $engine ) {
417                                         $difftext = $engine->getDiffBody();
418                                         ApiResult::setContentValue( $vals['diff'], 'body', $difftext );
419                                         if ( !$engine->wasCacheHit() ) {
420                                                 $n++;
421                                         }
422                                 }
423                         } else {
424                                 $vals['diff']['notcached'] = true;
425                         }
426                 }
427
428                 if ( $anyHidden && $revision->isDeleted( Revision::DELETED_RESTRICTED ) ) {
429                         $vals['suppressed'] = true;
430                 }
431
432                 return $vals;
433         }
434
435         public function getCacheMode( $params ) {
436                 if ( $this->userCanSeeRevDel() ) {
437                         return 'private';
438                 }
439
440                 return 'public';
441         }
442
443         public function getAllowedParams() {
444                 return [
445                         'prop' => [
446                                 ApiBase::PARAM_ISMULTI => true,
447                                 ApiBase::PARAM_DFLT => 'ids|timestamp|flags|comment|user',
448                                 ApiBase::PARAM_TYPE => [
449                                         'ids',
450                                         'flags',
451                                         'timestamp',
452                                         'user',
453                                         'userid',
454                                         'size',
455                                         'sha1',
456                                         'contentmodel',
457                                         'comment',
458                                         'parsedcomment',
459                                         'content',
460                                         'tags',
461                                         'parsetree',
462                                 ],
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 ],
479                                 ],
480                         ],
481                         'limit' => [
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',
487                         ],
488                         'expandtemplates' => [
489                                 ApiBase::PARAM_DFLT => false,
490                                 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-expandtemplates',
491                                 ApiBase::PARAM_DEPRECATED => true,
492                         ],
493                         'generatexml' => [
494                                 ApiBase::PARAM_DFLT => false,
495                                 ApiBase::PARAM_DEPRECATED => true,
496                                 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-generatexml',
497                         ],
498                         'parse' => [
499                                 ApiBase::PARAM_DFLT => false,
500                                 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-parse',
501                                 ApiBase::PARAM_DEPRECATED => true,
502                         ],
503                         'section' => [
504                                 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-section',
505                         ],
506                         'diffto' => [
507                                 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-diffto',
508                                 ApiBase::PARAM_DEPRECATED => true,
509                         ],
510                         'difftotext' => [
511                                 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-difftotext',
512                                 ApiBase::PARAM_DEPRECATED => true,
513                         ],
514                         'difftotextpst' => [
515                                 ApiBase::PARAM_DFLT => false,
516                                 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-difftotextpst',
517                                 ApiBase::PARAM_DEPRECATED => true,
518                         ],
519                         'contentformat' => [
520                                 ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
521                                 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-contentformat',
522                         ],
523                 ];
524         }
525
526 }