Part 1 of bug 18427 - "add comment= parameter to action=parse, spitting out <parsedco...
[lhc/web/wiklou.git] / includes / api / ApiParse.php
1 <?php
2
3 /*
4 * Created on Dec 01, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once ( "ApiBase.php" );
29 }
30
31 /**
32 * @ingroup API
33 */
34 class ApiParse extends ApiBase {
35
36 public function __construct( $main, $action ) {
37 parent :: __construct( $main, $action );
38 }
39
40 public function execute() {
41 // Get parameters
42 $params = $this->extractRequestParams();
43 $text = $params['text'];
44 $title = $params['title'];
45 $page = $params['page'];
46 $oldid = $params['oldid'];
47 if ( !is_null( $page ) && ( !is_null( $text ) || $title != "API" ) )
48 $this->dieUsage( "The page parameter cannot be used together with the text and title parameters", 'params' );
49 $prop = array_flip( $params['prop'] );
50 $revid = false;
51
52 // The parser needs $wgTitle to be set, apparently the
53 // $title parameter in Parser::parse isn't enough *sigh*
54 global $wgParser, $wgUser, $wgTitle, $wgEnableParserCache;
55 $popts = new ParserOptions();
56 $popts->setTidy( true );
57 $popts->enableLimitReport();
58 $redirValues = null;
59 if ( !is_null( $oldid ) || !is_null( $page ) )
60 {
61 if ( !is_null( $oldid ) )
62 {
63 // Don't use the parser cache
64 $rev = Revision::newFromID( $oldid );
65 if ( !$rev )
66 $this->dieUsage( "There is no revision ID $oldid", 'missingrev' );
67 if ( !$rev->userCan( Revision::DELETED_TEXT ) )
68 $this->dieUsage( "You don't have permission to view deleted revisions", 'permissiondenied' );
69
70 $text = $rev->getText( Revision::FOR_THIS_USER );
71 $titleObj = $rev->getTitle();
72 $wgTitle = $titleObj;
73 $p_result = $wgParser->parse( $text, $titleObj, $popts );
74 }
75 else
76 {
77 if ( $params['redirects'] )
78 {
79 $req = new FauxRequest( array(
80 'action' => 'query',
81 'redirects' => '',
82 'titles' => $page
83 ) );
84 $main = new ApiMain( $req );
85 $main->execute();
86 $data = $main->getResultData();
87 $redirValues = @$data['query']['redirects'];
88 $to = $page;
89 foreach ( (array)$redirValues as $r )
90 $to = $r['to'];
91 }
92 else
93 $to = $page;
94 $titleObj = Title::newFromText( $to );
95 if ( !$titleObj )
96 $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
97
98 $articleObj = new Article( $titleObj );
99 if ( isset( $prop['revid'] ) )
100 $oldid = $articleObj->getRevIdFetched();
101 // Try the parser cache first
102 $p_result = false;
103 $pcache = ParserCache::singleton();
104 if ( $wgEnableParserCache )
105 $p_result = $pcache->get( $articleObj, $wgUser );
106 if ( !$p_result )
107 {
108 $p_result = $wgParser->parse( $articleObj->getContent(), $titleObj, $popts );
109
110 if ( $wgEnableParserCache )
111 $pcache->save( $p_result, $articleObj, $popts );
112 }
113 }
114 }
115 else
116 {
117 $titleObj = Title::newFromText( $title );
118 if ( !$titleObj )
119 $titleObj = Title::newFromText( "API" );
120 $wgTitle = $titleObj;
121 if ( $params['pst'] || $params['onlypst'] )
122 $text = $wgParser->preSaveTransform( $text, $titleObj, $wgUser, $popts );
123 if ( $params['onlypst'] )
124 {
125 // Build a result and bail out
126 $result_array['text'] = array();
127 $this->getResult()->setContent( $result_array['text'], $text );
128 $this->getResult()->addValue( null, $this->getModuleName(), $result_array );
129 return;
130 }
131 $p_result = $wgParser->parse( $text, $titleObj, $popts );
132 }
133
134 // Return result
135 $result = $this->getResult();
136 $result_array = array();
137 if ( $params['redirects'] && !is_null( $redirValues ) )
138 $result_array['redirects'] = $redirValues;
139
140 if ( isset( $prop['text'] ) ) {
141 $result_array['text'] = array();
142 $result->setContent( $result_array['text'], $p_result->getText() );
143 }
144
145 if ( !is_null( $params['summary'] ) ) {
146 $result_array['parsedsummary'] = array();
147 $result->setContent( $result_array['parsedsummary'], $wgUser->getSkin()->formatComment( $params['summary'], $titleObj ) );
148 }
149
150 if ( isset( $prop['langlinks'] ) )
151 $result_array['langlinks'] = $this->formatLangLinks( $p_result->getLanguageLinks() );
152 if ( isset( $prop['categories'] ) )
153 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
154 if ( isset( $prop['links'] ) )
155 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
156 if ( isset( $prop['templates'] ) )
157 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
158 if ( isset( $prop['images'] ) )
159 $result_array['images'] = array_keys( $p_result->getImages() );
160 if ( isset( $prop['externallinks'] ) )
161 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
162 if ( isset( $prop['sections'] ) )
163 $result_array['sections'] = $p_result->getSections();
164 if ( isset( $prop['displaytitle'] ) )
165 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
166 $p_result->getDisplayTitle() :
167 $titleObj->getPrefixedText();
168
169 if ( isset( $prop['headitems'] ) )
170 $result_array['headitems'] = $this->formatHeadItems( $p_result->getHeadItems() );
171
172 if ( !is_null( $oldid ) )
173 $result_array['revid'] = intval( $oldid );
174
175 $result_mapping = array(
176 'redirects' => 'r',
177 'langlinks' => 'll',
178 'categories' => 'cl',
179 'links' => 'pl',
180 'templates' => 'tl',
181 'images' => 'img',
182 'externallinks' => 'el',
183 'sections' => 's',
184 'headitems' => 'hi'
185 );
186 $this->setIndexedTagNames( $result_array, $result_mapping );
187 $result->addValue( null, $this->getModuleName(), $result_array );
188 }
189
190 private function formatLangLinks( $links ) {
191 $result = array();
192 foreach ( $links as $link ) {
193 $entry = array();
194 $bits = explode( ':', $link, 2 );
195 $entry['lang'] = $bits[0];
196 $this->getResult()->setContent( $entry, $bits[1] );
197 $result[] = $entry;
198 }
199 return $result;
200 }
201
202 private function formatCategoryLinks( $links ) {
203 $result = array();
204 foreach ( $links as $link => $sortkey ) {
205 $entry = array();
206 $entry['sortkey'] = $sortkey;
207 $this->getResult()->setContent( $entry, $link );
208 $result[] = $entry;
209 }
210 return $result;
211 }
212
213 private function formatLinks( $links ) {
214 $result = array();
215 foreach ( $links as $ns => $nslinks ) {
216 foreach ( $nslinks as $title => $id ) {
217 $entry = array();
218 $entry['ns'] = $ns;
219 $this->getResult()->setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() );
220 if ( $id != 0 )
221 $entry['exists'] = '';
222 $result[] = $entry;
223 }
224 }
225 return $result;
226 }
227
228 private function formatHeadItems( $headItems ) {
229 $result = array();
230 foreach ( $headItems as $tag => $content ) {
231 $entry = array();
232 $entry['tag'] = $tag;
233 $this->getResult()->setContent( $entry, $content );
234 $result[] = $entry;
235 }
236 return $result;
237 }
238
239 private function setIndexedTagNames( &$array, $mapping ) {
240 foreach ( $mapping as $key => $name ) {
241 if ( isset( $array[$key] ) )
242 $this->getResult()->setIndexedTagName( $array[$key], $name );
243 }
244 }
245
246 public function getAllowedParams() {
247 return array (
248 'title' => array(
249 ApiBase :: PARAM_DFLT => 'API',
250 ),
251 'text' => null,
252 'summary' => null,
253 'page' => null,
254 'redirects' => false,
255 'oldid' => null,
256 'prop' => array(
257 ApiBase :: PARAM_DFLT => 'text|langlinks|categories|links|templates|images|externallinks|sections|revid|displaytitle',
258 ApiBase :: PARAM_ISMULTI => true,
259 ApiBase :: PARAM_TYPE => array(
260 'text',
261 'langlinks',
262 'categories',
263 'links',
264 'templates',
265 'images',
266 'externallinks',
267 'sections',
268 'revid',
269 'displaytitle',
270 'headitems'
271 )
272 ),
273 'pst' => false,
274 'onlypst' => false,
275 );
276 }
277
278 public function getParamDescription() {
279 return array (
280 'text' => 'Wikitext to parse',
281 'summary' => 'Summary to parse',
282 'redirects' => 'If the page parameter is set to a redirect, resolve it',
283 'title' => 'Title of page the text belongs to',
284 'page' => 'Parse the content of this page. Cannot be used together with text and title',
285 'oldid' => 'Parse the content of this revision. Overrides page',
286 'prop' => array( 'Which pieces of information to get.',
287 'NOTE: Section tree is only generated if there are more than 4 sections, or if the __TOC__ keyword is present'
288 ),
289 'pst' => array( 'Do a pre-save transform on the input before parsing it.',
290 'Ignored if page or oldid is used.'
291 ),
292 'onlypst' => array( 'Do a PST on the input, but don\'t parse it.',
293 'Returns PSTed wikitext. Ignored if page or oldid is used.'
294 ),
295 );
296 }
297
298 public function getDescription() {
299 return 'This module parses wikitext and returns parser output';
300 }
301
302 protected function getExamples() {
303 return array (
304 'api.php?action=parse&text={{Project:Sandbox}}'
305 );
306 }
307
308 public function getVersion() {
309 return __CLASS__ . ': $Id$';
310 }
311 }