Return comment stuffs
[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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiBase.php" );
30 }
31
32 /**
33 * API module that functions as a shortcut to the wikitext preprocessor. Expands
34 * any templates in a provided string, and returns the result of this expansion
35 * to the caller.
36 *
37 * @ingroup API
38 */
39 class ApiExpandTemplates extends ApiBase {
40
41 public function __construct( $main, $action ) {
42 parent::__construct( $main, $action );
43 }
44
45 public function execute() {
46 // Cache may vary on $wgUser because ParserOptions gets data from it
47 $this->getMain()->setCacheMode( 'anon-public-user-private' );
48
49 // Get parameters
50 $params = $this->extractRequestParams();
51
52 // Create title for parser
53 $title_obj = Title::newFromText( $params['title'] );
54 if ( !$title_obj ) {
55 $title_obj = Title::newFromText( 'API' ); // default
56 }
57
58 $result = $this->getResult();
59
60 // Parse text
61 global $wgParser;
62 $options = new ParserOptions();
63
64 if ( $params['includecomments'] ) {
65 $options->setRemoveComments( false );
66 }
67
68 if ( $params['generatexml'] ) {
69 $wgParser->startExternalParse( $title_obj, $options, OT_PREPROCESS );
70 $dom = $wgParser->preprocessToDom( $params['text'] );
71 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
72 $xml = $dom->saveXML();
73 } else {
74 $xml = $dom->__toString();
75 }
76 $xml_result = array();
77 $result->setContent( $xml_result, $xml );
78 $result->addValue( null, 'parsetree', $xml_result );
79 }
80 $retval = $wgParser->preprocess( $params['text'], $title_obj, $options );
81
82 // Return result
83 $retval_array = array();
84 $result->setContent( $retval_array, $retval );
85 $result->addValue( null, $this->getModuleName(), $retval_array );
86 }
87
88 public function getAllowedParams() {
89 return array(
90 'title' => array(
91 ApiBase::PARAM_DFLT => 'API',
92 ),
93 'text' => null,
94 'generatexml' => false,
95 'includecomments' => false,
96 );
97 }
98
99 public function getParamDescription() {
100 return array(
101 'text' => 'Wikitext to convert',
102 'title' => 'Title of page',
103 'generatexml' => 'Generate XML parse tree',
104 'includecomments' => 'Whether to include HTML comments in the output',
105 );
106 }
107
108 public function getDescription() {
109 return 'Expands all templates in wikitext';
110 }
111
112 protected function getExamples() {
113 return array(
114 'api.php?action=expandtemplates&text={{Project:Sandbox}}'
115 );
116 }
117
118 public function getVersion() {
119 return __CLASS__ . ': $Id$';
120 }
121 }