API: Added redirect resolution to action=parse. Based on a patch by Robert Chin
[lhc/web/wiklou.git] / includes / api / ApiParse.php
index 7c1637b..92857ed 100644 (file)
@@ -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',