Add 'ApiMakeParserOptions' hook
[lhc/web/wiklou.git] / includes / api / ApiParse.php
index 5754c23..f96acf3 100644 (file)
@@ -72,7 +72,7 @@ class ApiParse extends ApiBase {
                        $this->section = $params['section'];
                        if ( !preg_match( '/^((T-)?\d+|new)$/', $this->section ) ) {
                                $this->dieUsage(
-                                       "The section parameter must be a valid section id or 'new'", "invalidsection"
+                                       'The section parameter must be a valid section id or "new"', 'invalidsection'
                                );
                        }
                } else {
@@ -109,13 +109,13 @@ class ApiParse extends ApiBase {
                                $titleObj = $rev->getTitle();
                                $wgTitle = $titleObj;
                                $pageObj = WikiPage::factory( $titleObj );
-                               $popts = $this->makeParserOptions( $pageObj, $params );
+                               list( $popts, $reset, $suppressCache ) = $this->makeParserOptions( $pageObj, $params );
 
                                // If for some reason the "oldid" is actually the current revision, it may be cached
                                // Deliberately comparing $pageObj->getLatest() with $rev->getId(), rather than
                                // checking $rev->isCurrent(), because $pageObj is what actually ends up being used,
                                // and if its ->getLatest() is outdated, $rev->isCurrent() won't tell us that.
-                               if ( $rev->getId() == $pageObj->getLatest() ) {
+                               if ( !$suppressCache && $rev->getId() == $pageObj->getLatest() ) {
                                        // May get from/save to parser cache
                                        $p_result = $this->getParsedContent( $pageObj, $popts,
                                                $pageid, isset( $prop['wikitext'] ) );
@@ -131,9 +131,9 @@ class ApiParse extends ApiBase {
                                }
                        } else { // Not $oldid, but $pageid or $page
                                if ( $params['redirects'] ) {
-                                       $reqParams = array(
+                                       $reqParams = [
                                                'redirects' => '',
-                                       );
+                                       ];
                                        if ( !is_null( $pageid ) ) {
                                                $reqParams['pageids'] = $pageid;
                                        } else { // $page
@@ -149,11 +149,11 @@ class ApiParse extends ApiBase {
                                        foreach ( $pageSet->getRedirectTitles() as $title ) {
                                                $to = $title->getFullText();
                                        }
-                                       $pageParams = array( 'title' => $to );
+                                       $pageParams = [ 'title' => $to ];
                                } elseif ( !is_null( $pageid ) ) {
-                                       $pageParams = array( 'pageid' => $pageid );
+                                       $pageParams = [ 'pageid' => $pageid ];
                                } else { // $page
-                                       $pageParams = array( 'title' => $page );
+                                       $pageParams = [ 'title' => $page ];
                                }
 
                                $pageObj = $this->getTitleOrPageId( $pageParams, 'fromdb' );
@@ -167,12 +167,12 @@ class ApiParse extends ApiBase {
                                        $oldid = $pageObj->getLatest();
                                }
 
-                               $popts = $this->makeParserOptions( $pageObj, $params );
+                               list( $popts, $reset, $suppressCache ) = $this->makeParserOptions( $pageObj, $params );
 
                                // Don't pollute the parser cache when setting options that aren't
                                // in ParserOptions::optionsHash()
                                /// @todo: This should be handled closer to the actual cache instead of here, see T110269
-                               $suppressCache =
+                               $suppressCache = $suppressCache ||
                                        $params['disablepp'] ||
                                        $params['disablelimitreport'] ||
                                        $params['preview'] ||
@@ -191,7 +191,7 @@ class ApiParse extends ApiBase {
                } else { // Not $oldid, $pageid, $page. Hence based on $text
                        $titleObj = Title::newFromText( $title );
                        if ( !$titleObj || $titleObj->isExternal() ) {
-                               $this->dieUsageMsg( array( 'invalidtitle', $title ) );
+                               $this->dieUsageMsg( [ 'invalidtitle', $title ] );
                        }
                        $wgTitle = $titleObj;
                        if ( $titleObj->canExist() ) {
@@ -202,7 +202,7 @@ class ApiParse extends ApiBase {
                                $pageObj = $article->getPage();
                        }
 
-                       $popts = $this->makeParserOptions( $pageObj, $params );
+                       list( $popts, $reset ) = $this->makeParserOptions( $pageObj, $params );
                        $textProvided = !is_null( $text );
 
                        if ( !$textProvided ) {
@@ -245,7 +245,7 @@ class ApiParse extends ApiBase {
                        }
                        if ( $params['onlypst'] ) {
                                // Build a result and bail out
-                               $result_array = array();
+                               $result_array = [];
                                $result_array['text'] = $this->pstContent->serialize( $format );
                                $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'text';
                                if ( isset( $prop['wikitext'] ) ) {
@@ -272,10 +272,10 @@ class ApiParse extends ApiBase {
                        }
                }
 
-               $result_array = array();
+               $result_array = [];
 
                $result_array['title'] = $titleObj->getPrefixedText();
-               $result_array['pageid'] = $pageid ? $pageid : $pageObj->getId();
+               $result_array['pageid'] = $pageid ?: $pageObj->getId();
 
                if ( !is_null( $oldid ) ) {
                        $result_array['revid'] = intval( $oldid );
@@ -307,8 +307,8 @@ class ApiParse extends ApiBase {
                        if ( $params['effectivelanglinks'] ) {
                                // Link flags are ignored for now, but may in the future be
                                // included in the result.
-                               $linkFlags = array();
-                               Hooks::run( 'LanguageLinks', array( $titleObj, &$langlinks, &$linkFlags ) );
+                               $linkFlags = [];
+                               Hooks::run( 'LanguageLinks', [ $titleObj, &$langlinks, &$linkFlags ] );
                        }
                } else {
                        $langlinks = false;
@@ -341,8 +341,7 @@ class ApiParse extends ApiBase {
                }
 
                if ( isset( $prop['displaytitle'] ) ) {
-                       $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
-                               $p_result->getDisplayTitle() :
+                       $result_array['displaytitle'] = $p_result->getDisplayTitle() ?:
                                $titleObj->getPrefixedText();
                }
 
@@ -356,7 +355,7 @@ class ApiParse extends ApiBase {
 
                                $css = $this->formatCss( $context->getOutput()->buildCssLinksArray() );
 
-                               $scripts = array( $context->getOutput()->getHeadScripts() );
+                               $scripts = [ $context->getOutput()->getHeadScripts() ];
 
                                $result_array['headitems'] = array_merge( $headItems, $css, $scripts );
                        }
@@ -372,7 +371,7 @@ class ApiParse extends ApiBase {
                        $result_array['modulescripts'] = array_values( array_unique( $p_result->getModuleScripts() ) );
                        $result_array['modulestyles'] = array_values( array_unique( $p_result->getModuleStyles() ) );
                        // To be removed in 1.27
-                       $result_array['modulemessages'] = array();
+                       $result_array['modulemessages'] = [];
                        $this->setWarning( 'modulemessages is deprecated since MediaWiki 1.26' );
                }
 
@@ -390,9 +389,9 @@ class ApiParse extends ApiBase {
 
                if ( isset( $prop['modules'] ) &&
                        !isset( $prop['jsconfigvars'] ) && !isset( $prop['encodedjsconfigvars'] ) ) {
-                       $this->setWarning( "Property 'modules' was set but not 'jsconfigvars' " .
-                               "or 'encodedjsconfigvars'. Configuration variables are necessary " .
-                               "for proper module usage." );
+                       $this->setWarning( 'Property "modules" was set but not "jsconfigvars" ' .
+                               'or "encodedjsconfigvars". Configuration variables are necessary ' .
+                               'for proper module usage.' );
                }
 
                if ( isset( $prop['indicators'] ) ) {
@@ -428,12 +427,12 @@ class ApiParse extends ApiBase {
 
                if ( isset( $prop['parsetree'] ) || $params['generatexml'] ) {
                        if ( $this->content->getModel() != CONTENT_MODEL_WIKITEXT ) {
-                               $this->dieUsage( "parsetree is only supported for wikitext content", "notwikitext" );
+                               $this->dieUsage( 'parsetree is only supported for wikitext content', 'notwikitext' );
                        }
 
                        $wgParser->startExternalParse( $titleObj, $popts, Parser::OT_PREPROCESS );
                        $dom = $wgParser->preprocessToDom( $this->content->getNativeData() );
-                       if ( is_callable( array( $dom, 'saveXML' ) ) ) {
+                       if ( is_callable( [ $dom, 'saveXML' ] ) ) {
                                $xml = $dom->saveXML();
                        } else {
                                $xml = $dom->__toString();
@@ -442,7 +441,7 @@ class ApiParse extends ApiBase {
                        $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'parsetree';
                }
 
-               $result_mapping = array(
+               $result_mapping = [
                        'redirects' => 'r',
                        'langlinks' => 'll',
                        'categories' => 'cl',
@@ -460,7 +459,7 @@ class ApiParse extends ApiBase {
                        'modulemessages' => 'm',
                        'properties' => 'pp',
                        'limitreportdata' => 'lr',
-               );
+               ];
                $this->setIndexedTagNames( $result_array, $result_mapping );
                $result->addValue( null, $this->getModuleName(), $result_array );
        }
@@ -471,10 +470,9 @@ class ApiParse extends ApiBase {
         * @param WikiPage $pageObj
         * @param array $params
         *
-        * @return ParserOptions
+        * @return array [ ParserOptions, ScopedCallback, bool $suppressCache ]
         */
        protected function makeParserOptions( WikiPage $pageObj, array $params ) {
-
                $popts = $pageObj->makeParserOptions( $this->getContext() );
                $popts->enableLimitReport( !$params['disablepp'] && !$params['disablelimitreport'] );
                $popts->setIsPreview( $params['preview'] || $params['sectionpreview'] );
@@ -484,7 +482,12 @@ class ApiParse extends ApiBase {
                        $popts->setTidy( false );
                }
 
-               return $popts;
+               $reset = null;
+               $suppressCache = false;
+               Hooks::run( 'ApiMakeParserOptions',
+                       [ $popts, $pageObj->getTitle(), $params, $this, &$reset, &$suppressCache ] );
+
+               return [ $popts, $reset, $suppressCache ];
        }
 
        /**
@@ -545,10 +548,10 @@ class ApiParse extends ApiBase {
                // Not cached (save or load)
                $section = $content->getSection( $this->section );
                if ( $section === false ) {
-                       $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
+                       $this->dieUsage( "There is no section {$this->section} in $what", 'nosuchsection' );
                }
                if ( $section === null ) {
-                       $this->dieUsage( "Sections are not supported by " . $what, 'nosuchsection' );
+                       $this->dieUsage( "Sections are not supported by $what", 'nosuchsection' );
                        $section = false;
                }
 
@@ -581,9 +584,9 @@ class ApiParse extends ApiBase {
        }
 
        private function formatLangLinks( $links ) {
-               $result = array();
+               $result = [];
                foreach ( $links as $link ) {
-                       $entry = array();
+                       $entry = [];
                        $bits = explode( ':', $link, 2 );
                        $title = Title::newFromText( $link );
 
@@ -607,7 +610,7 @@ class ApiParse extends ApiBase {
        }
 
        private function formatCategoryLinks( $links ) {
-               $result = array();
+               $result = [];
 
                if ( !$links ) {
                        return $result;
@@ -615,24 +618,24 @@ class ApiParse extends ApiBase {
 
                // Fetch hiddencat property
                $lb = new LinkBatch;
-               $lb->setArray( array( NS_CATEGORY => $links ) );
+               $lb->setArray( [ NS_CATEGORY => $links ] );
                $db = $this->getDB();
-               $res = $db->select( array( 'page', 'page_props' ),
-                       array( 'page_title', 'pp_propname' ),
+               $res = $db->select( [ 'page', 'page_props' ],
+                       [ 'page_title', 'pp_propname' ],
                        $lb->constructSet( 'page', $db ),
                        __METHOD__,
-                       array(),
-                       array( 'page_props' => array(
-                               'LEFT JOIN', array( 'pp_propname' => 'hiddencat', 'pp_page = page_id' )
-                       ) )
+                       [],
+                       [ 'page_props' => [
+                               'LEFT JOIN', [ 'pp_propname' => 'hiddencat', 'pp_page = page_id' ]
+                       ] ]
                );
-               $hiddencats = array();
+               $hiddencats = [];
                foreach ( $res as $row ) {
                        $hiddencats[$row->page_title] = isset( $row->pp_propname );
                }
 
                foreach ( $links as $link => $sortkey ) {
-                       $entry = array();
+                       $entry = [];
                        $entry['sortkey'] = $sortkey;
                        // array keys will cast numeric category names to ints, so cast back to string
                        ApiResult::setContentValue( $entry, 'category', (string)$link );
@@ -655,10 +658,10 @@ class ApiParse extends ApiBase {
        }
 
        private function formatLinks( $links ) {
-               $result = array();
+               $result = [];
                foreach ( $links as $ns => $nslinks ) {
                        foreach ( $nslinks as $title => $id ) {
-                               $entry = array();
+                               $entry = [];
                                $entry['ns'] = $ns;
                                ApiResult::setContentValue( $entry, 'title', Title::makeTitle( $ns, $title )->getFullText() );
                                $entry['exists'] = $id != 0;
@@ -670,10 +673,10 @@ class ApiParse extends ApiBase {
        }
 
        private function formatIWLinks( $iw ) {
-               $result = array();
+               $result = [];
                foreach ( $iw as $prefix => $titles ) {
                        foreach ( array_keys( $titles ) as $title ) {
-                               $entry = array();
+                               $entry = [];
                                $entry['prefix'] = $prefix;
 
                                $title = Title::newFromText( "{$prefix}:{$title}" );
@@ -690,9 +693,9 @@ class ApiParse extends ApiBase {
        }
 
        private function formatHeadItems( $headItems ) {
-               $result = array();
+               $result = [];
                foreach ( $headItems as $tag => $content ) {
-                       $entry = array();
+                       $entry = [];
                        $entry['tag'] = $tag;
                        ApiResult::setContentValue( $entry, 'content', $content );
                        $result[] = $entry;
@@ -702,9 +705,9 @@ class ApiParse extends ApiBase {
        }
 
        private function formatCss( $css ) {
-               $result = array();
+               $result = [];
                foreach ( $css as $file => $link ) {
-                       $entry = array();
+                       $entry = [];
                        $entry['file'] = $file;
                        ApiResult::setContentValue( $entry, 'link', $link );
                        $result[] = $entry;
@@ -714,13 +717,13 @@ class ApiParse extends ApiBase {
        }
 
        private function formatLimitReportData( $limitReportData ) {
-               $result = array();
+               $result = [];
 
                foreach ( $limitReportData as $name => $value ) {
-                       $entry = array();
+                       $entry = [];
                        $entry['name'] = $name;
                        if ( !is_array( $value ) ) {
-                               $value = array( $value );
+                               $value = [ $value ];
                        }
                        ApiResult::setIndexedTagNameRecursive( $value, 'param' );
                        $entry = array_merge( $entry, $value );
@@ -739,25 +742,25 @@ class ApiParse extends ApiBase {
        }
 
        public function getAllowedParams() {
-               return array(
+               return [
                        'title' => null,
-                       'text' => array(
+                       'text' => [
                                ApiBase::PARAM_TYPE => 'text',
-                       ),
+                       ],
                        'summary' => null,
                        'page' => null,
-                       'pageid' => array(
+                       'pageid' => [
                                ApiBase::PARAM_TYPE => 'integer',
-                       ),
+                       ],
                        'redirects' => false,
-                       'oldid' => array(
+                       'oldid' => [
                                ApiBase::PARAM_TYPE => 'integer',
-                       ),
-                       'prop' => array(
+                       ],
+                       'prop' => [
                                ApiBase::PARAM_DFLT => 'text|langlinks|categories|links|templates|' .
                                        'images|externallinks|sections|revid|displaytitle|iwlinks|properties',
                                ApiBase::PARAM_ISMULTI => true,
-                               ApiBase::PARAM_TYPE => array(
+                               ApiBase::PARAM_TYPE => [
                                        'text',
                                        'langlinks',
                                        'categories',
@@ -781,46 +784,46 @@ class ApiParse extends ApiBase {
                                        'limitreportdata',
                                        'limitreporthtml',
                                        'parsetree',
-                               ),
-                               ApiBase::PARAM_HELP_MSG_PER_VALUE => array(
-                                       'parsetree' => array( 'apihelp-parse-paramvalue-prop-parsetree', CONTENT_MODEL_WIKITEXT ),
-                               ),
-                       ),
+                               ],
+                               ApiBase::PARAM_HELP_MSG_PER_VALUE => [
+                                       'parsetree' => [ 'apihelp-parse-paramvalue-prop-parsetree', CONTENT_MODEL_WIKITEXT ],
+                               ],
+                       ],
                        'pst' => false,
                        'onlypst' => false,
                        'effectivelanglinks' => false,
                        'section' => null,
-                       'sectiontitle' => array(
+                       'sectiontitle' => [
                                ApiBase::PARAM_TYPE => 'string',
-                       ),
-                       'disablepp' => array(
+                       ],
+                       'disablepp' => [
                                ApiBase::PARAM_DFLT => false,
                                ApiBase::PARAM_DEPRECATED => true,
-                       ),
+                       ],
                        'disablelimitreport' => false,
                        'disableeditsection' => false,
                        'disabletidy' => false,
-                       'generatexml' => array(
+                       'generatexml' => [
                                ApiBase::PARAM_DFLT => false,
-                               ApiBase::PARAM_HELP_MSG => array(
+                               ApiBase::PARAM_HELP_MSG => [
                                        'apihelp-parse-param-generatexml', CONTENT_MODEL_WIKITEXT
-                               ),
+                               ],
                                ApiBase::PARAM_DEPRECATED => true,
-                       ),
+                       ],
                        'preview' => false,
                        'sectionpreview' => false,
                        'disabletoc' => false,
-                       'contentformat' => array(
+                       'contentformat' => [
                                ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
-                       ),
-                       'contentmodel' => array(
+                       ],
+                       'contentmodel' => [
                                ApiBase::PARAM_TYPE => ContentHandler::getContentModels(),
-                       )
-               );
+                       ]
+               ];
        }
 
        protected function getExamplesMessages() {
-               return array(
+               return [
                        'action=parse&page=Project:Sandbox'
                                => 'apihelp-parse-example-page',
                        'action=parse&text={{Project:Sandbox}}&contentmodel=wikitext'
@@ -829,7 +832,7 @@ class ApiParse extends ApiBase {
                                => 'apihelp-parse-example-texttitle',
                        'action=parse&summary=Some+[[link]]&prop='
                                => 'apihelp-parse-example-summary',
-               );
+               ];
        }
 
        public function getHelpUrls() {