Followup to bryans comment for r62328
[lhc/web/wiklou.git] / includes / api / ApiExpandTemplates.php
index df99ba1..5876645 100644 (file)
  * http://www.gnu.org/copyleft/gpl.html
  */
 
-if (!defined('MEDIAWIKI')) {
+if ( !defined( 'MEDIAWIKI' ) ) {
        // Eclipse helper - will be ignored in production
-       require_once ("ApiBase.php");
+       require_once ( "ApiBase.php" );
 }
 
 /**
- * @addtogroup API
+ * API module that functions as a shortcut to the wikitext preprocessor. Expands
+ * any templates in a provided string, and returns the result of this expansion
+ * to the caller.
+ *
+ * @ingroup API
  */
 class ApiExpandTemplates extends ApiBase {
 
-       public function __construct($main, $action) {
-               parent :: __construct($main, $action);
+       public function __construct( $main, $action ) {
+               parent :: __construct( $main, $action );
        }
 
        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']);
-               if(!$title_obj)
-                       $title_obj = Title :: newFromText("API");       //  Default title is "API". For example, ExpandTemplates uses "ExpendTemplates" for it
+               // Create title for parser
+               $title_obj = Title :: newFromText( $params['title'] );
+               if ( !$title_obj )
+                       $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';
        }
 
@@ -90,4 +109,3 @@ class ApiExpandTemplates extends ApiBase {
                return __CLASS__ . ': $Id$';
        }
 }
-