API: Crusade against extract(). Left one extract() call alone in ApiQueryBacklinks...
[lhc/web/wiklou.git] / includes / api / ApiExpandTemplates.php
index 9d8a7af..1fc1bfe 100644 (file)
@@ -33,7 +33,7 @@ if (!defined('MEDIAWIKI')) {
  * any templates in a provided string, and returns the result of this expansion
  * to the caller.
  *
- * @addtogroup API
+ * @ingroup API
  */
 class ApiExpandTemplates extends ApiBase {
 
@@ -44,43 +44,57 @@ class ApiExpandTemplates extends ApiBase {
        public function execute() {
                // Get parameters
                $params = $this->extractRequestParams();
-               $text = $params['text'];
-               $title = $params['title'];
-               $retval = '';
 
                //Create title for parser
-               $title_obj = Title :: newFromText($params['title']);
+               $title_obj = Title :: newFromText( $params['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
+
+               $result = $this->getResult();
 
                // Parse text
                global $wgParser;
-               $retval = $wgParser->preprocess( $text, $title_obj, new ParserOptions() );
+               $options = new ParserOptions();
+               if ( $params['generatexml'] )
+               {
+                       $wgParser->startExternalParse( $title_obj, $options, OT_PREPROCESS );
+                       $dom = $wgParser->preprocessToDom( $params['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( $params['text'], $title_obj, $options );
 
                // Return result
-               $result = $this->getResult();
                $retval_array = array();
                $result->setContent( $retval_array, $retval );
                $result->addValue( null, $this->getModuleName(), $retval_array );
        }
 
-       protected function getAllowedParams() {
+       public function getAllowedParams() {
                return array (
-                       'title' => array( 
+                       'title' => array(
                                ApiBase :: PARAM_DFLT => 'API',
                        ),
-                       'text' => null
+                       'text' => null,
+                       'generatexml' => false,
                );
        }
 
-       protected function getParamDescription() {
+       public function getParamDescription() {
                return array (
                        'text' => 'Wikitext to convert',
                        'title' => 'Title of page',
+                       'generatexml' => 'Generate XML parse tree',
                );
        }
 
-       protected function getDescription() {
+       public function getDescription() {
                return 'This module expand all templates in wikitext';
        }
 
@@ -94,4 +108,3 @@ class ApiExpandTemplates extends ApiBase {
                return __CLASS__ . ': $Id$';
        }
 }
-