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