MediaWiki  REL1_19
ApiExpandTemplates.php
Go to the documentation of this file.
00001 <?php
00034 class ApiExpandTemplates extends ApiBase {
00035 
00036         public function __construct( $main, $action ) {
00037                 parent::__construct( $main, $action );
00038         }
00039 
00040         public function execute() {
00041                 // Cache may vary on $wgUser because ParserOptions gets data from it
00042                 $this->getMain()->setCacheMode( 'anon-public-user-private' );
00043 
00044                 // Get parameters
00045                 $params = $this->extractRequestParams();
00046 
00047                 // Create title for parser
00048                 $title_obj = Title::newFromText( $params['title'] );
00049                 if ( !$title_obj ) {
00050                         $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
00051                 }
00052 
00053                 $result = $this->getResult();
00054 
00055                 // Parse text
00056                 global $wgParser;
00057                 $options = ParserOptions::newFromContext( $this->getContext() );
00058 
00059                 if ( $params['includecomments'] ) {
00060                         $options->setRemoveComments( false );
00061                 }
00062 
00063                 if ( $params['generatexml'] ) {
00064                         $wgParser->startExternalParse( $title_obj, $options, OT_PREPROCESS );
00065                         $dom = $wgParser->preprocessToDom( $params['text'] );
00066                         if ( is_callable( array( $dom, 'saveXML' ) ) ) {
00067                                 $xml = $dom->saveXML();
00068                         } else {
00069                                 $xml = $dom->__toString();
00070                         }
00071                         $xml_result = array();
00072                         $result->setContent( $xml_result, $xml );
00073                         $result->addValue( null, 'parsetree', $xml_result );
00074                 }
00075                 $retval = $wgParser->preprocess( $params['text'], $title_obj, $options );
00076 
00077                 // Return result
00078                 $retval_array = array();
00079                 $result->setContent( $retval_array, $retval );
00080                 $result->addValue( null, $this->getModuleName(), $retval_array );
00081         }
00082 
00083         public function getAllowedParams() {
00084                 return array(
00085                         'title' => array(
00086                                 ApiBase::PARAM_DFLT => 'API',
00087                         ),
00088                         'text' => array(
00089                                 ApiBase::PARAM_TYPE => 'string',
00090                                 ApiBase::PARAM_REQUIRED => true,
00091                         ),
00092                         'generatexml' => false,
00093                         'includecomments' => false,
00094                 );
00095         }
00096 
00097         public function getParamDescription() {
00098                 return array(
00099                         'text' => 'Wikitext to convert',
00100                         'title' => 'Title of page',
00101                         'generatexml' => 'Generate XML parse tree',
00102                         'includecomments' => 'Whether to include HTML comments in the output',
00103                 );
00104         }
00105 
00106         public function getDescription() {
00107                 return 'Expands all templates in wikitext';
00108         }
00109 
00110         public function getPossibleErrors() {
00111                 return array_merge( parent::getPossibleErrors(), array(
00112                         array( 'invalidtitle', 'title' ),
00113                 ) );
00114         }
00115 
00116         public function getExamples() {
00117                 return array(
00118                         'api.php?action=expandtemplates&text={{Project:Sandbox}}'
00119                 );
00120         }
00121 
00122         public function getHelpUrls() {
00123                 return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#expandtemplates';
00124         }
00125 
00126         public function getVersion() {
00127                 return __CLASS__ . ': $Id$';
00128         }
00129 }