From ee3e7075a51f110eb0bf0070799b81ce526a6aa3 Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Fri, 24 Oct 2008 12:28:14 +0000 Subject: [PATCH] API: Added redirect resolution to action=parse. Based on a patch by Robert Chin --- RELEASE-NOTES | 1 + includes/api/ApiParse.php | 30 +++++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 7581604f00..d7895773cb 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -360,6 +360,7 @@ The following extensions are migrated into MediaWiki 1.14: works properly when multiple users are queried or a userprefix is used * (bug 16047) Added activeusers attribute to meta=siteinfo&siprop=statistics output +* Added redirect resolution to action=parse === Languages updated in 1.14 === diff --git a/includes/api/ApiParse.php b/includes/api/ApiParse.php index 7c1637b635..92857edae4 100644 --- a/includes/api/ApiParse.php +++ b/includes/api/ApiParse.php @@ -53,6 +53,7 @@ class ApiParse extends ApiBase { $popts = new ParserOptions(); $popts->setTidy(true); $popts->enableLimitReport(); + $redirValues = null; if(!is_null($oldid) || !is_null($page)) { if(!is_null($oldid)) @@ -69,17 +70,35 @@ class ApiParse extends ApiBase { } else { - $titleObj = Title::newFromText($page); + if($params['redirects']) + { + $req = new FauxRequest(array( + 'action' => 'query', + 'redirects' => '', + 'titles' => $page + )); + $main = new ApiMain($req); + $main->execute(); + $data = $main->getResultData(); + $redirValues = @$data['query']['redirects']; + $to = $page; + foreach((array)$redirValues as $r) + $to = $r['to']; + } + else + $to = $page; + $titleObj = Title::newFromText($to); if(!$titleObj) $this->dieUsage("The page you specified doesn't exist", 'missingtitle'); - // Try the parser cache first $articleObj = new Article($titleObj); if(isset($prop['revid'])) $oldid = $articleObj->getRevIdFetched(); + // Try the parser cache first $pcache = ParserCache::singleton(); $p_result = $pcache->get($articleObj, $wgUser); - if(!$p_result) { + if(!$p_result) + { $p_result = $wgParser->parse($articleObj->getContent(), $titleObj, $popts); global $wgUseParserCache; if($wgUseParserCache) @@ -98,6 +117,8 @@ class ApiParse extends ApiBase { // Return result $result = $this->getResult(); $result_array = array(); + if($params['redirects'] && !is_null($redirValues)) + $result_array['redirects'] = $redirValues; if(isset($prop['text'])) { $result_array['text'] = array(); $result->setContent($result_array['text'], $p_result->getText()); @@ -120,6 +141,7 @@ class ApiParse extends ApiBase { $result_array['revid'] = $oldid; $result_mapping = array( + 'redirects' => 'r', 'langlinks' => 'll', 'categories' => 'cl', 'links' => 'pl', @@ -184,6 +206,7 @@ class ApiParse extends ApiBase { ), 'text' => null, 'page' => null, + 'redirects' => false, 'oldid' => null, 'prop' => array( ApiBase :: PARAM_DFLT => 'text|langlinks|categories|links|templates|images|externallinks|sections|revid', @@ -206,6 +229,7 @@ class ApiParse extends ApiBase { public function getParamDescription() { return array ( 'text' => 'Wikitext to parse', + 'redirects' => 'If the page parameter is set to a redirect, resolve it', 'title' => 'Title of page the text belongs to', 'page' => 'Parse the content of this page. Cannot be used together with text and title', 'oldid' => 'Parse the content of this revision. Overrides page', -- 2.20.1