Merge "Revert "Function for "pretty timestamps" that are human readable and understan...
[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 __construct( $main, $action ) {
37 parent::__construct( $main, $action );
38 }
39
40 public function execute() {
41 // Cache may vary on $wgUser because ParserOptions gets data from it
42 $this->getMain()->setCacheMode( 'anon-public-user-private' );
43
44 // Get parameters
45 $params = $this->extractRequestParams();
46
47 // Create title for parser
48 $title_obj = Title::newFromText( $params['title'] );
49 if ( !$title_obj ) {
50 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
51 }
52
53 $result = $this->getResult();
54
55 // Parse text
56 global $wgParser;
57 $options = ParserOptions::newFromContext( $this->getContext() );
58
59 if ( $params['includecomments'] ) {
60 $options->setRemoveComments( false );
61 }
62
63 if ( $params['generatexml'] ) {
64 $wgParser->startExternalParse( $title_obj, $options, OT_PREPROCESS );
65 $dom = $wgParser->preprocessToDom( $params['text'] );
66 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
67 $xml = $dom->saveXML();
68 } else {
69 $xml = $dom->__toString();
70 }
71 $xml_result = array();
72 $result->setContent( $xml_result, $xml );
73 $result->addValue( null, 'parsetree', $xml_result );
74 }
75 $retval = $wgParser->preprocess( $params['text'], $title_obj, $options );
76
77 // Return result
78 $retval_array = array();
79 $result->setContent( $retval_array, $retval );
80 $result->addValue( null, $this->getModuleName(), $retval_array );
81 }
82
83 public function getAllowedParams() {
84 return array(
85 'title' => array(
86 ApiBase::PARAM_DFLT => 'API',
87 ),
88 'text' => array(
89 ApiBase::PARAM_TYPE => 'string',
90 ApiBase::PARAM_REQUIRED => true,
91 ),
92 'generatexml' => false,
93 'includecomments' => false,
94 );
95 }
96
97 public function getParamDescription() {
98 return array(
99 'text' => 'Wikitext to convert',
100 'title' => 'Title of page',
101 'generatexml' => 'Generate XML parse tree',
102 'includecomments' => 'Whether to include HTML comments in the output',
103 );
104 }
105
106 public function getResultProperties() {
107 return array(
108 '' => array(
109 '*' => 'string'
110 )
111 );
112 }
113
114 public function getDescription() {
115 return 'Expands all templates in wikitext';
116 }
117
118 public function getPossibleErrors() {
119 return array_merge( parent::getPossibleErrors(), array(
120 array( 'invalidtitle', 'title' ),
121 ) );
122 }
123
124 public function getExamples() {
125 return array(
126 'api.php?action=expandtemplates&text={{Project:Sandbox}}'
127 );
128 }
129
130 public function getHelpUrls() {
131 return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#expandtemplates';
132 }
133
134 public function getVersion() {
135 return __CLASS__ . ': $Id$';
136 }
137 }