(bug 25832) query=allimages now outputs ns/title as well
[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['generatexml'] ) {
65 $wgParser->startExternalParse( $title_obj, $options, OT_PREPROCESS );
66 $dom = $wgParser->preprocessToDom( $params['text'] );
67 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
68 $xml = $dom->saveXML();
69 } else {
70 $xml = $dom->__toString();
71 }
72 $xml_result = array();
73 $result->setContent( $xml_result, $xml );
74 $result->addValue( null, 'parsetree', $xml_result );
75 }
76 $retval = $wgParser->preprocess( $params['text'], $title_obj, $options );
77
78 // Return result
79 $retval_array = array();
80 $result->setContent( $retval_array, $retval );
81 $result->addValue( null, $this->getModuleName(), $retval_array );
82 }
83
84 public function getAllowedParams() {
85 return array(
86 'title' => array(
87 ApiBase::PARAM_DFLT => 'API',
88 ),
89 'text' => null,
90 'generatexml' => false,
91 );
92 }
93
94 public function getParamDescription() {
95 return array(
96 'text' => 'Wikitext to convert',
97 'title' => 'Title of page',
98 'generatexml' => 'Generate XML parse tree',
99 );
100 }
101
102 public function getDescription() {
103 return 'This module expand all templates in wikitext';
104 }
105
106 protected function getExamples() {
107 return array(
108 'api.php?action=expandtemplates&text={{Project:Sandbox}}'
109 );
110 }
111
112 public function getVersion() {
113 return __CLASS__ . ': $Id$';
114 }
115 }