Merge "Revert "Log the reason why revision->getContent() returns null""
[lhc/web/wiklou.git] / includes / api / ApiQueryRevisionsBase.php
index d7fb580..f888434 100644 (file)
@@ -1,9 +1,5 @@
 <?php
 /**
- *
- *
- * Created on Oct 3, 2014 as a split from ApiQueryRevisions
- *
  * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
  *
  * This program is free software; you can redistribute it and/or modify
@@ -70,10 +66,7 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase {
                                && $params['diffto'] != 'prev' && $params['diffto'] != 'next'
                        ) {
                                $p = $this->getModulePrefix();
-                               $this->dieUsage(
-                                       "{$p}diffto must be set to a non-negative number, \"prev\", \"next\" or \"cur\"",
-                                       'diffto'
-                               );
+                               $this->dieWithError( [ 'apierror-baddiffto', $p ], 'diffto' );
                        }
                        // Check whether the revision exists and is readable,
                        // DifferenceEngine returns a rather ambiguous empty
@@ -81,10 +74,10 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase {
                        if ( $params['diffto'] != 0 ) {
                                $difftoRev = Revision::newFromId( $params['diffto'] );
                                if ( !$difftoRev ) {
-                                       $this->dieUsageMsg( array( 'nosuchrevid', $params['diffto'] ) );
+                                       $this->dieWithError( [ 'apierror-nosuchrevid', $params['diffto'] ] );
                                }
                                if ( !$difftoRev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
-                                       $this->setWarning( "Couldn't diff to r{$difftoRev->getId()}: content is hidden" );
+                                       $this->addWarning( [ 'apiwarn-difftohidden', $difftoRev->getId() ] );
                                        $params['diffto'] = null;
                                }
                        }
@@ -107,6 +100,17 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase {
                $this->fld_tags = isset( $prop['tags'] );
                $this->fld_parsetree = isset( $prop['parsetree'] );
 
+               if ( $this->fld_parsetree ) {
+                       $encParam = $this->encodeParamName( 'prop' );
+                       $name = $this->getModuleName();
+                       $parent = $this->getParent();
+                       $parentParam = $parent->encodeParamName( $parent->getModuleManager()->getModuleGroup( $name ) );
+                       $this->addDeprecation(
+                               [ 'apiwarn-deprecation-parameter', "{$encParam}=parsetree" ],
+                               "action=query&{$parentParam}={$name}&{$encParam}=parsetree"
+                       );
+               }
+
                if ( !empty( $params['contentformat'] ) ) {
                        $this->contentFormat = $params['contentformat'];
                }
@@ -160,7 +164,7 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase {
        protected function extractRevisionInfo( Revision $revision, $row ) {
                $title = $revision->getTitle();
                $user = $this->getUser();
-               $vals = array();
+               $vals = [];
                $anyHidden = false;
 
                if ( $this->fld_ids ) {
@@ -248,7 +252,7 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase {
                                ApiResult::setIndexedTagName( $tags, 'tag' );
                                $vals['tags'] = $tags;
                        } else {
-                               $vals['tags'] = array();
+                               $vals['tags'] = [];
                        }
                }
 
@@ -262,8 +266,12 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase {
                        if ( $content && $this->section !== false ) {
                                $content = $content->getSection( $this->section, false );
                                if ( !$content ) {
-                                       $this->dieUsage(
-                                               "There is no section {$this->section} in r" . $revision->getId(),
+                                       $this->dieWithError(
+                                               [
+                                                       'apierror-nosuchsection-what',
+                                                       wfEscapeWikiText( $this->section ),
+                                                       $this->msg( 'revid', $revision->getId() )
+                                               ],
                                                'nosuchsection'
                                        );
                                }
@@ -286,7 +294,7 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase {
                                                Parser::OT_PREPROCESS
                                        );
                                        $dom = $wgParser->preprocessToDom( $t );
-                                       if ( is_callable( array( $dom, 'saveXML' ) ) ) {
+                                       if ( is_callable( [ $dom, 'saveXML' ] ) ) {
                                                $xml = $dom->saveXML();
                                        } else {
                                                $xml = $dom->__toString();
@@ -294,9 +302,14 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase {
                                        $vals['parsetree'] = $xml;
                                } else {
                                        $vals['badcontentformatforparsetree'] = true;
-                                       $this->setWarning( "Conversion to XML is supported for wikitext only, " .
-                                               $title->getPrefixedDBkey() .
-                                               " uses content model " . $content->getModel() );
+                                       $this->addWarning(
+                                               [
+                                                       'apierror-parsetree-notwikitext-title',
+                                                       wfEscapeWikiText( $title->getPrefixedText() ),
+                                                       $content->getModel()
+                                               ],
+                                               'parsetree-notwikitext'
+                                       );
                                }
                        }
                }
@@ -315,9 +328,11 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase {
                                                ParserOptions::newFromContext( $this->getContext() )
                                        );
                                } else {
-                                       $this->setWarning( "Template expansion is supported for wikitext only, " .
-                                               $title->getPrefixedDBkey() .
-                                               " uses content model " . $content->getModel() );
+                                       $this->addWarning( [
+                                               'apierror-templateexpansion-notwikitext',
+                                               wfEscapeWikiText( $title->getPrefixedText() ),
+                                               $content->getModel()
+                                       ] );
                                        $vals['badcontentformat'] = true;
                                        $text = false;
                                }
@@ -332,13 +347,12 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase {
                        }
 
                        if ( $text === null ) {
-                               $format = $this->contentFormat ? $this->contentFormat : $content->getDefaultFormat();
+                               $format = $this->contentFormat ?: $content->getDefaultFormat();
                                $model = $content->getModel();
 
                                if ( !$content->isSupportedFormat( $format ) ) {
-                                       $name = $title->getPrefixedDBkey();
-                                       $this->setWarning( "The requested format {$this->contentFormat} is not " .
-                                               "supported for content model $model used by $name" );
+                                       $name = wfEscapeWikiText( $title->getPrefixedText() );
+                                       $this->addWarning( [ 'apierror-badformat', $this->contentFormat, $model, $name ] );
                                        $vals['badcontentformat'] = true;
                                        $text = false;
                                } else {
@@ -359,7 +373,7 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase {
                        static $n = 0; // Number of uncached diffs we've had
 
                        if ( $n < $this->getConfig()->get( 'APIMaxUncachedDiffs' ) ) {
-                               $vals['diff'] = array();
+                               $vals['diff'] = [];
                                $context = new DerivativeContext( $this->getContext() );
                                $context->setTitle( $title );
                                $handler = $revision->getContentHandler();
@@ -370,9 +384,8 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase {
                                        if ( $this->contentFormat
                                                && !ContentHandler::getForModelID( $model )->isSupportedFormat( $this->contentFormat )
                                        ) {
-                                               $name = $title->getPrefixedDBkey();
-                                               $this->setWarning( "The requested format {$this->contentFormat} is not " .
-                                                       "supported for content model $model used by $name" );
+                                               $name = wfEscapeWikiText( $title->getPrefixedText() );
+                                               $this->addWarning( [ 'apierror-badformat', $this->contentFormat, $model, $name ] );
                                                $vals['diff']['badcontentformat'] = true;
                                                $engine = null;
                                        } else {
@@ -424,11 +437,11 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase {
        }
 
        public function getAllowedParams() {
-               return array(
-                       'prop' => array(
+               return [
+                       'prop' => [
                                ApiBase::PARAM_ISMULTI => true,
                                ApiBase::PARAM_DFLT => 'ids|timestamp|flags|comment|user',
-                               ApiBase::PARAM_TYPE => array(
+                               ApiBase::PARAM_TYPE => [
                                        'ids',
                                        'flags',
                                        'timestamp',
@@ -442,9 +455,9 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase {
                                        'content',
                                        'tags',
                                        'parsetree',
-                               ),
+                               ],
                                ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-prop',
-                               ApiBase::PARAM_HELP_MSG_PER_VALUE => array(
+                               ApiBase::PARAM_HELP_MSG_PER_VALUE => [
                                        'ids' => 'apihelp-query+revisions+base-paramvalue-prop-ids',
                                        'flags' => 'apihelp-query+revisions+base-paramvalue-prop-flags',
                                        'timestamp' => 'apihelp-query+revisions+base-paramvalue-prop-timestamp',
@@ -457,48 +470,53 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase {
                                        'parsedcomment' => 'apihelp-query+revisions+base-paramvalue-prop-parsedcomment',
                                        'content' => 'apihelp-query+revisions+base-paramvalue-prop-content',
                                        'tags' => 'apihelp-query+revisions+base-paramvalue-prop-tags',
-                                       'parsetree' => array( 'apihelp-query+revisions+base-paramvalue-prop-parsetree',
-                                               CONTENT_MODEL_WIKITEXT ),
-                               ),
-                       ),
-                       'limit' => array(
+                                       'parsetree' => [ 'apihelp-query+revisions+base-paramvalue-prop-parsetree',
+                                               CONTENT_MODEL_WIKITEXT ],
+                               ],
+                       ],
+                       'limit' => [
                                ApiBase::PARAM_TYPE => 'limit',
                                ApiBase::PARAM_MIN => 1,
                                ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
                                ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2,
                                ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-limit',
-                       ),
-                       'expandtemplates' => array(
+                       ],
+                       'expandtemplates' => [
                                ApiBase::PARAM_DFLT => false,
                                ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-expandtemplates',
-                       ),
-                       'generatexml' => array(
+                               ApiBase::PARAM_DEPRECATED => true,
+                       ],
+                       'generatexml' => [
                                ApiBase::PARAM_DFLT => false,
                                ApiBase::PARAM_DEPRECATED => true,
                                ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-generatexml',
-                       ),
-                       'parse' => array(
+                       ],
+                       'parse' => [
                                ApiBase::PARAM_DFLT => false,
                                ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-parse',
-                       ),
-                       'section' => array(
+                               ApiBase::PARAM_DEPRECATED => true,
+                       ],
+                       'section' => [
                                ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-section',
-                       ),
-                       'diffto' => array(
+                       ],
+                       'diffto' => [
                                ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-diffto',
-                       ),
-                       'difftotext' => array(
+                               ApiBase::PARAM_DEPRECATED => true,
+                       ],
+                       'difftotext' => [
                                ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-difftotext',
-                       ),
-                       'difftotextpst' => array(
+                               ApiBase::PARAM_DEPRECATED => true,
+                       ],
+                       'difftotextpst' => [
                                ApiBase::PARAM_DFLT => false,
                                ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-difftotextpst',
-                       ),
-                       'contentformat' => array(
+                               ApiBase::PARAM_DEPRECATED => true,
+                       ],
+                       'contentformat' => [
                                ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
                                ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-contentformat',
-                       ),
-               );
+                       ],
+               ];
        }
 
 }