(bug 13829) Expose parse tree via action=expandtemplates
authorBryan Tong Minh <btongminh@users.mediawiki.org>
Tue, 13 May 2008 09:03:23 +0000 (09:03 +0000)
committerBryan Tong Minh <btongminh@users.mediawiki.org>
Tue, 13 May 2008 09:03:23 +0000 (09:03 +0000)
RELEASE-NOTES
includes/api/ApiExpandTemplates.php

index f009b67..3e732d7 100644 (file)
@@ -332,6 +332,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
 * (bug 14028) Added language attribute to interwiki map in meta=siteinfo
 * (bug 14022) Added usprop=registration and auprop=blockinfo
 * (bug 14021) Removed titles= support from list=backlinks (has been obsolete for ages)
+* (bug 13829) Expose parse tree via action=expandtemplates
 
 === Languages updated in 1.13 ===
 
index 99f9fa8..c32c4e6 100644 (file)
@@ -43,22 +43,35 @@ class ApiExpandTemplates extends ApiBase {
 
        public function execute() {
                // Get parameters
-               $params = $this->extractRequestParams();
-               $text = $params['text'];
-               $title = $params['title'];
+               extract( $this->extractRequestParams() );
                $retval = '';
 
                //Create title for parser
-               $title_obj = Title :: newFromText($params['title']);
+               $title_obj = Title :: newFromText( $title );
                if(!$title_obj)
-                       $title_obj = Title :: newFromText("API");       //  Default title is "API". For example, ExpandTemplates uses "ExpendTemplates" for it
+                       $title_obj = Title :: newFromText( "API" );     //  Default title is "API". For example, ExpandTemplates uses "ExpendTemplates" for it
+
+               $result = $this->getResult();
 
                // Parse text
                global $wgParser;
-               $retval = $wgParser->preprocess( $text, $title_obj, new ParserOptions() );
+               $options = new ParserOptions();
+               if ( $generatexml )
+               {
+                       $wgParser->startExternalParse( $title_obj, $options, OT_PREPROCESS );
+                       $dom = $wgParser->preprocessToDom( $text );
+                       if ( is_callable( array( $dom, 'saveXML' ) ) ) {
+                               $xml = $dom->saveXML();
+                       } else {
+                               $xml = $dom->__toString();
+                       }
+                       $xml_result = array();
+                       $result->setContent( $xml_result, $xml );
+            $result->addValue( null, 'parsetree', $xml_result);
+               }
+               $retval = $wgParser->preprocess( $text, $title_obj, $options );
 
                // Return result
-               $result = $this->getResult();
                $retval_array = array();
                $result->setContent( $retval_array, $retval );
                $result->addValue( null, $this->getModuleName(), $retval_array );
@@ -69,7 +82,8 @@ class ApiExpandTemplates extends ApiBase {
                        'title' => array(
                                ApiBase :: PARAM_DFLT => 'API',
                        ),
-                       'text' => null
+                       'text' => null,
+                       'generatexml' => false,
                );
        }
 
@@ -77,6 +91,7 @@ class ApiExpandTemplates extends ApiBase {
                return array (
                        'text' => 'Wikitext to convert',
                        'title' => 'Title of page',
+                       'generatexml' => 'Generate XML parse tree',
                );
        }