From 7762a0cab6b48bc542fa85a05aabb9ba43e8f417 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Mon, 20 Jun 2016 11:58:53 -0400 Subject: [PATCH] Add 'ApiMakeParserOptions' hook This allows extensions (e.g. TemplateSandbox in I77a9aa5a) to better interact with the ApiParse and ApiExpandTemplates modules. Change-Id: I72d5cf8e0b86e4250af1459219dc3b42d7adbbb8 --- RELEASE-NOTES-1.28 | 4 ++++ docs/hooks.txt | 9 +++++++++ includes/api/ApiExpandTemplates.php | 5 +++++ includes/api/ApiParse.php | 20 ++++++++++++-------- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/RELEASE-NOTES-1.28 b/RELEASE-NOTES-1.28 index 360f6d5bfb..6beeac9344 100644 --- a/RELEASE-NOTES-1.28 +++ b/RELEASE-NOTES-1.28 @@ -20,6 +20,8 @@ production. === New features in 1.28 === * User::isBot() method for checking if an account is a bot role account. * Added a new hook, 'UserIsBot', to aid in determining if a user is a bot. +* Added a new hook, 'ApiMakeParserOptions', to allow extensions to better + interact with API parsing. === External library changes in 1.28 === @@ -35,6 +37,8 @@ production. === Action API changes in 1.28 === === Action API internal changes in 1.28 === +* Added a new hook, 'ApiMakeParserOptions', to allow extensions to better + interact with ApiParse and ApiExpandTemplates. === Languages updated in 1.28 === diff --git a/docs/hooks.txt b/docs/hooks.txt index 44f2551985..39cae7391e 100644 --- a/docs/hooks.txt +++ b/docs/hooks.txt @@ -443,6 +443,15 @@ an exception is thrown during API action execution. $apiMain: Calling ApiMain instance. $e: Exception object. +'ApiMakeParserOptions': Called from ApiParse and ApiExpandTemplates to allow +extensions to adjust the ParserOptions before parsing. +$options: ParserOptions object +$title: Title to be parsed +$params: Parameter array for the API module +$module: API module (which is also a ContextSource) +&$reset: Set to a ScopedCallback used to reset any hooks after the parse is done. +&$suppressCache: Set true if cache should be suppressed. + 'ApiOpenSearchSuggest': Called when constructing the OpenSearch results. Hooks can alter or append to the array. &$results: array with integer keys to associative arrays. Keys in associative diff --git a/includes/api/ApiExpandTemplates.php b/includes/api/ApiExpandTemplates.php index 286fe887c3..48e7698147 100644 --- a/includes/api/ApiExpandTemplates.php +++ b/includes/api/ApiExpandTemplates.php @@ -77,6 +77,11 @@ class ApiExpandTemplates extends ApiBase { $options->setRemoveComments( false ); } + $reset = null; + $suppressCache = false; + Hooks::run( 'ApiMakeParserOptions', + [ $options, $title_obj, $params, $this, &$reset, &$suppressCache ] ); + $retval = []; if ( isset( $prop['parsetree'] ) || $params['generatexml'] ) { diff --git a/includes/api/ApiParse.php b/includes/api/ApiParse.php index fe418e3b0b..f96acf3121 100644 --- a/includes/api/ApiParse.php +++ b/includes/api/ApiParse.php @@ -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'] ) ); @@ -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'] || @@ -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 ) { @@ -470,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'] ); @@ -483,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 ]; } /** -- 2.20.1