5 * Created on Oct 05, 2007
7 * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
28 * API module that functions as a shortcut to the wikitext preprocessor. Expands
29 * any templates in a provided string, and returns the result of this expansion
34 class ApiExpandTemplates
extends ApiBase
{
36 public function execute() {
37 // Cache may vary on $wgUser because ParserOptions gets data from it
38 $this->getMain()->setCacheMode( 'anon-public-user-private' );
41 $params = $this->extractRequestParams();
42 $this->requireMaxOneParameter( $params, 'prop', 'generatexml' );
44 if ( $params['prop'] === null ) {
45 $this->setWarning( 'Because no values have been specified for the prop parameter, a legacy format has been used for the output.'
46 . ' This format is deprecated, and in the future, a default value will be set for the prop parameter, causing the new format to always be used.' );
49 $prop = array_flip( $params['prop'] );
52 // Create title for parser
53 $title_obj = Title
::newFromText( $params['title'] );
54 if ( !$title_obj ||
$title_obj->isExternal() ) {
55 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
58 $result = $this->getResult();
62 $options = ParserOptions
::newFromContext( $this->getContext() );
64 if ( $params['includecomments'] ) {
65 $options->setRemoveComments( false );
70 if ( isset( $prop['parsetree'] ) ||
$params['generatexml'] ) {
71 $wgParser->startExternalParse( $title_obj, $options, OT_PREPROCESS
);
72 $dom = $wgParser->preprocessToDom( $params['text'] );
73 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
74 $xml = $dom->saveXML();
76 $xml = $dom->__toString();
78 if ( isset( $prop['parsetree'] ) ) {
79 unset( $prop['parsetree'] );
80 $retval['parsetree'] = $xml;
83 $xml_result = array();
84 ApiResult
::setContent( $xml_result, $xml );
85 $result->addValue( null, 'parsetree', $xml_result );
89 // if they didn't want any output except (probably) the parse tree,
90 // then don't bother actually fully expanding it
91 if ( $prop ||
$params['prop'] === null ) {
92 $wgParser->startExternalParse( $title_obj, $options, OT_PREPROCESS
);
93 $frame = $wgParser->getPreprocessor()->newFrame();
94 $wikitext = $wgParser->preprocess( $params['text'], $title_obj, $options, null, $frame );
95 if ( $params['prop'] === null ) {
97 ApiResult
::setContent( $retval, $wikitext );
99 if ( isset( $prop['categories'] ) ) {
100 $categories = $wgParser->getOutput()->getCategories();
101 if ( !empty( $categories ) ) {
102 $categories_result = array();
103 foreach ( $categories as $category => $sortkey ) {
105 $entry['sortkey'] = $sortkey;
106 ApiResult
::setContent( $entry, $category );
107 $categories_result[] = $entry;
109 $result->setIndexedTagName( $categories_result, 'category' );
110 $retval['categories'] = $categories_result;
113 if ( isset( $prop['volatile'] ) && $frame->isVolatile() ) {
114 $retval['volatile'] = '';
116 if ( isset( $prop['ttl'] ) && $frame->getTTL() !== null ) {
117 $retval['ttl'] = $frame->getTTL();
119 if ( isset( $prop['wikitext'] ) ) {
120 $retval['wikitext'] = $wikitext;
124 $result->setSubelements( $retval, array( 'wikitext', 'parsetree' ) );
125 $result->addValue( null, $this->getModuleName(), $retval );
128 public function getAllowedParams() {
131 ApiBase
::PARAM_DFLT
=> 'API',
134 ApiBase
::PARAM_TYPE
=> 'string',
135 ApiBase
::PARAM_REQUIRED
=> true,
138 ApiBase
::PARAM_TYPE
=> array(
145 ApiBase
::PARAM_ISMULTI
=> true,
147 'includecomments' => false,
148 'generatexml' => array(
149 ApiBase
::PARAM_TYPE
=> 'boolean',
150 ApiBase
::PARAM_DEPRECATED
=> true,
155 public function getParamDescription() {
157 'text' => 'Wikitext to convert',
158 'title' => 'Title of page',
160 'Which pieces of information to get',
161 ' wikitext - The expanded wikitext',
162 ' categories - Any categories present in the input that are not represented in the wikitext output',
163 ' volatile - Whether the output is volatile and should not be reused elsewhere within the page',
164 ' ttl - The maximum time after which caches of the result should be invalidated',
165 ' parsetree - The XML parse tree of the input',
166 'Note that if no values are selected, the result will contain the wikitext,',
167 'but the output will be in a deprecated format.',
169 'includecomments' => 'Whether to include HTML comments in the output',
170 'generatexml' => 'Generate XML parse tree (replaced by prop=parsetree)',
174 public function getResultProperties() {
177 'wikitext' => 'string',
179 'categories' => array(
180 'categories' => array(
181 ApiBase
::PROP_TYPE
=> 'array',
182 ApiBase
::PROP_NULLABLE
=> true,
187 ApiBase
::PROP_TYPE
=> 'boolean',
188 ApiBase
::PROP_NULLABLE
=> true,
193 ApiBase
::PROP_TYPE
=> 'integer',
194 ApiBase
::PROP_NULLABLE
=> true,
197 'parsetree' => array(
198 'parsetree' => 'string',
203 public function getDescription() {
204 return 'Expands all templates in wikitext.';
207 public function getPossibleErrors() {
208 return array_merge( parent
::getPossibleErrors(), array(
209 array( 'invalidtitle', 'title' ),
213 public function getExamples() {
215 'api.php?action=expandtemplates&text={{Project:Sandbox}}'
219 public function getHelpUrls() {
220 return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#expandtemplates';