Removed more functions marked for removal in 1.19: wfParseCIDR(), wfRFC822Phrase...
[mediawiki.git] / includes / api / ApiExpandTemplates.php
blobdd35aaa8915a913fd3a806b80bc079fe40dd26e1
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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiBase.php" );
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.
37 * @ingroup API
39 class ApiExpandTemplates extends ApiBase {
41 public function __construct( $main, $action ) {
42 parent::__construct( $main, $action );
45 public function execute() {
46 // Cache may vary on $wgUser because ParserOptions gets data from it
47 $this->getMain()->setCacheMode( 'anon-public-user-private' );
49 // Get parameters
50 $params = $this->extractRequestParams();
52 // Create title for parser
53 $title_obj = Title::newFromText( $params['title'] );
54 if ( !$title_obj ) {
55 $title_obj = Title::newFromText( 'API' ); // default
58 $result = $this->getResult();
60 // Parse text
61 global $wgParser;
62 $options = new ParserOptions();
64 if ( $params['includecomments'] ) {
65 $options->setRemoveComments( false );
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();
76 $xml_result = array();
77 $result->setContent( $xml_result, $xml );
78 $result->addValue( null, 'parsetree', $xml_result );
80 $retval = $wgParser->preprocess( $params['text'], $title_obj, $options );
82 // Return result
83 $retval_array = array();
84 $result->setContent( $retval_array, $retval );
85 $result->addValue( null, $this->getModuleName(), $retval_array );
88 public function getAllowedParams() {
89 return array(
90 'title' => array(
91 ApiBase::PARAM_DFLT => 'API',
93 'text' => null,
94 'generatexml' => false,
95 'includecomments' => false,
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',
108 public function getDescription() {
109 return 'Expands all templates in wikitext';
112 protected function getExamples() {
113 return array(
114 'api.php?action=expandtemplates&text={{Project:Sandbox}}'
118 public function getVersion() {
119 return __CLASS__ . ': $Id$';