Merge "Add prebodyhtml template variable"
[lhc/web/wiklou.git] / includes / api / ApiExpandTemplates.php
1 <?php
2 /**
3 *
4 *
5 * Created on Oct 05, 2007
6 *
7 * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
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.
31 *
32 * @ingroup API
33 */
34 class ApiExpandTemplates extends ApiBase {
35
36 public function execute() {
37 // Cache may vary on $wgUser because ParserOptions gets data from it
38 $this->getMain()->setCacheMode( 'anon-public-user-private' );
39
40 // Get parameters
41 $params = $this->extractRequestParams();
42
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'] ) );
47 }
48
49 $result = $this->getResult();
50
51 // Parse text
52 global $wgParser;
53 $options = ParserOptions::newFromContext( $this->getContext() );
54
55 if ( $params['includecomments'] ) {
56 $options->setRemoveComments( false );
57 }
58
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();
66 }
67 $xml_result = array();
68 ApiResult::setContent( $xml_result, $xml );
69 $result->addValue( null, 'parsetree', $xml_result );
70 }
71 $retval = $wgParser->preprocess( $params['text'], $title_obj, $options );
72
73 // Return result
74 $retval_array = array();
75 ApiResult::setContent( $retval_array, $retval );
76 $result->addValue( null, $this->getModuleName(), $retval_array );
77 }
78
79 public function getAllowedParams() {
80 return array(
81 'title' => array(
82 ApiBase::PARAM_DFLT => 'API',
83 ),
84 'text' => array(
85 ApiBase::PARAM_TYPE => 'string',
86 ApiBase::PARAM_REQUIRED => true,
87 ),
88 'generatexml' => false,
89 'includecomments' => false,
90 );
91 }
92
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',
99 );
100 }
101
102 public function getResultProperties() {
103 return array(
104 '' => array(
105 '*' => 'string'
106 )
107 );
108 }
109
110 public function getDescription() {
111 return 'Expands all templates in wikitext.';
112 }
113
114 public function getPossibleErrors() {
115 return array_merge( parent::getPossibleErrors(), array(
116 array( 'invalidtitle', 'title' ),
117 ) );
118 }
119
120 public function getExamples() {
121 return array(
122 'api.php?action=expandtemplates&text={{Project:Sandbox}}'
123 );
124 }
125
126 public function getHelpUrls() {
127 return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#expandtemplates';
128 }
129 }