Merge "Typo fix"
[mediawiki.git] / includes / api / ApiExpandTemplates.php
blobd5c789c35f0b09d0aa1cdac790710bb7bd6206f7
1 <?php
2 /**
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
24 * @file
27 /**
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
30 * to the caller.
32 * @ingroup API
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' );
40 // Get parameters
41 $params = $this->extractRequestParams();
43 // Create title for parser
44 $title_obj = Title::newFromText( $params['title'] );
45 if ( !$title_obj || $title_obj->isExternal() ) {
46 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
49 $result = $this->getResult();
51 // Parse text
52 global $wgParser;
53 $options = ParserOptions::newFromContext( $this->getContext() );
55 if ( $params['includecomments'] ) {
56 $options->setRemoveComments( false );
59 if ( $params['generatexml'] ) {
60 $wgParser->startExternalParse( $title_obj, $options, OT_PREPROCESS );
61 $dom = $wgParser->preprocessToDom( $params['text'] );
62 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
63 $xml = $dom->saveXML();
64 } else {
65 $xml = $dom->__toString();
67 $xml_result = array();
68 ApiResult::setContent( $xml_result, $xml );
69 $result->addValue( null, 'parsetree', $xml_result );
71 $retval = $wgParser->preprocess( $params['text'], $title_obj, $options );
73 // Return result
74 $retval_array = array();
75 ApiResult::setContent( $retval_array, $retval );
76 $result->addValue( null, $this->getModuleName(), $retval_array );
79 public function getAllowedParams() {
80 return array(
81 'title' => array(
82 ApiBase::PARAM_DFLT => 'API',
84 'text' => array(
85 ApiBase::PARAM_TYPE => 'string',
86 ApiBase::PARAM_REQUIRED => true,
88 'generatexml' => false,
89 'includecomments' => false,
93 public function getParamDescription() {
94 return array(
95 'text' => 'Wikitext to convert',
96 'title' => 'Title of page',
97 'generatexml' => 'Generate XML parse tree',
98 'includecomments' => 'Whether to include HTML comments in the output',
102 public function getResultProperties() {
103 return array(
104 '' => array(
105 '*' => 'string'
110 public function getDescription() {
111 return 'Expands all templates in wikitext';
114 public function getPossibleErrors() {
115 return array_merge( parent::getPossibleErrors(), array(
116 array( 'invalidtitle', 'title' ),
117 ) );
120 public function getExamples() {
121 return array(
122 'api.php?action=expandtemplates&text={{Project:Sandbox}}'
126 public function getHelpUrls() {
127 return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#expandtemplates';