API: Make output containing private or user-specific data uncacheable for logged...
[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 © 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 $pageid = $params['pageid'];
47 $oldid = $params['oldid'];
48
49 if ( !is_null( $page ) && ( !is_null( $text ) || $title != 'API' ) ) {
50 $this->dieUsage( 'The page parameter cannot be used together with the text and title parameters', 'params' );
51 }
52 $prop = array_flip( $params['prop'] );
53 $revid = false;
54
55 if ( isset( $params['section'] ) ) {
56 $this->section = $params['section'];
57 } else {
58 $this->section = false;
59 }
60
61 // The parser needs $wgTitle to be set, apparently the
62 // $title parameter in Parser::parse isn't enough *sigh*
63 global $wgParser, $wgUser, $wgTitle, $wgEnableParserCache, $wgLang;
64
65 // Currently unnecessary, code to act as a safeguard against any change in current behaviour of uselang breaks
66 $oldLang = null;
67 if ( isset( $params['uselang'] ) && $params['uselang'] != $wgLang->getCode() ) {
68 $oldLang = $wgLang; // Backup wgLang
69 $wgLang = Language::factory( $params['uselang'] );
70 }
71
72 $popts = new ParserOptions();
73 $popts->setTidy( true );
74 $popts->enableLimitReport();
75 $redirValues = null;
76 if ( !is_null( $oldid ) || !is_null( $pageid ) || !is_null( $page ) ) {
77 if ( !is_null( $oldid ) ) {
78 // Don't use the parser cache
79 $rev = Revision::newFromID( $oldid );
80 if ( !$rev ) {
81 $this->dieUsage( "There is no revision ID $oldid", 'missingrev' );
82 }
83 if ( !$rev->userCan( Revision::DELETED_TEXT ) ) {
84 $this->dieUsage( "You don't have permission to view deleted revisions", 'permissiondenied' );
85 }
86
87 $text = $rev->getText( Revision::FOR_THIS_USER );
88 $titleObj = $rev->getTitle();
89 $wgTitle = $titleObj;
90
91 if ( $this->section !== false ) {
92 $text = $this->getSectionText( $text, 'r' . $rev );
93 }
94
95 $p_result = $wgParser->parse( $text, $titleObj, $popts );
96 } else {
97 if ( !is_null ( $pageid ) ) {
98 $titleObj = Title::newFromID( $pageid );
99
100 if ( !$titleObj ) {
101 $this->dieUsageMsg( array( 'nosuchpageid', $pageid ) );
102 }
103 } else {
104 if ( $params['redirects'] ) {
105 $req = new FauxRequest( array(
106 'action' => 'query',
107 'redirects' => '',
108 'titles' => $page
109 ) );
110 $main = new ApiMain( $req );
111 $main->execute();
112 $data = $main->getResultData();
113 $redirValues = @$data['query']['redirects'];
114 $to = $page;
115 foreach ( (array)$redirValues as $r ) {
116 $to = $r['to'];
117 }
118 } else {
119 $to = $page;
120 }
121 $titleObj = Title::newFromText( $to );
122 if ( !$titleObj ) {
123 $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
124 }
125 }
126 $wgTitle = $titleObj;
127
128 $articleObj = new Article( $titleObj );
129 if ( isset( $prop['revid'] ) ) {
130 $oldid = $articleObj->getRevIdFetched();
131 }
132
133 if ( $this->section !== false ) {
134 $text = $this->getSectionText( $text, !is_null ( $pageid ) ? 'page id ' . $pageid : $titleObj->getText() );
135 $p_result = $wgParser->parse( $text, $titleObj, $popts );
136 } else {
137 // Try the parser cache first
138 $p_result = false;
139 $pcache = ParserCache::singleton();
140 if ( $wgEnableParserCache ) {
141 $p_result = $pcache->get( $articleObj, $popts );
142 }
143 if ( !$p_result ) {
144 $p_result = $wgParser->parse( $articleObj->getContent(), $titleObj, $popts );
145
146 if ( $wgEnableParserCache ) {
147 $pcache->save( $p_result, $articleObj, $popts );
148 }
149 }
150 }
151 }
152 } else {
153 $titleObj = Title::newFromText( $title );
154 if ( !$titleObj ) {
155 $titleObj = Title::newFromText( 'API' );
156 }
157 $wgTitle = $titleObj;
158
159 if ( $this->section !== false ) {
160 $text = $this->getSectionText( $text, $titleObj->getText() );
161 }
162
163 if ( $params['pst'] || $params['onlypst'] ) {
164 $text = $wgParser->preSaveTransform( $text, $titleObj, $wgUser, $popts );
165 $this->getMain()->setVaryCookie();
166 }
167 if ( $params['onlypst'] ) {
168 // Build a result and bail out
169 $result_array['text'] = array();
170 $this->getResult()->setContent( $result_array['text'], $text );
171 $this->getResult()->addValue( null, $this->getModuleName(), $result_array );
172 return;
173 }
174 $p_result = $wgParser->parse( $text, $titleObj, $popts );
175 }
176
177 // Return result
178 $result = $this->getResult();
179 $result_array = array();
180 if ( $params['redirects'] && !is_null( $redirValues ) ) {
181 $result_array['redirects'] = $redirValues;
182 }
183
184 if ( isset( $prop['text'] ) ) {
185 $result_array['text'] = array();
186 $result->setContent( $result_array['text'], $p_result->getText() );
187 }
188
189 if ( !is_null( $params['summary'] ) ) {
190 $result_array['parsedsummary'] = array();
191 $result->setContent( $result_array['parsedsummary'], $wgUser->getSkin()->formatComment( $params['summary'], $titleObj ) );
192 }
193
194 if ( isset( $prop['langlinks'] ) ) {
195 $result_array['langlinks'] = $this->formatLangLinks( $p_result->getLanguageLinks() );
196 }
197 if ( isset( $prop['categories'] ) ) {
198 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
199 }
200 if ( isset( $prop['links'] ) ) {
201 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
202 }
203 if ( isset( $prop['templates'] ) ) {
204 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
205 }
206 if ( isset( $prop['images'] ) ) {
207 $result_array['images'] = array_keys( $p_result->getImages() );
208 }
209 if ( isset( $prop['externallinks'] ) ) {
210 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
211 }
212 if ( isset( $prop['sections'] ) ) {
213 $result_array['sections'] = $p_result->getSections();
214 }
215
216 if ( isset( $prop['displaytitle'] ) ) {
217 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
218 $p_result->getDisplayTitle() :
219 $titleObj->getPrefixedText();
220 }
221
222 if ( isset( $prop['headitems'] ) || isset( $prop['headhtml'] ) ) {
223 $out = new OutputPage;
224 $out->addParserOutputNoText( $p_result );
225 $userSkin = $wgUser->getSkin();
226 }
227
228 if ( isset( $prop['headitems'] ) ) {
229 $headItems = $this->formatHeadItems( $p_result->getHeadItems() );
230
231 $userSkin->setupUserCss( $out );
232 $css = $this->formatCss( $out->buildCssLinksArray() );
233
234 $scripts = array( $out->getHeadScripts( $userSkin ) );
235
236 $result_array['headitems'] = array_merge( $headItems , $css, $scripts );
237 }
238
239 if ( isset( $prop['headhtml'] ) ) {
240 $result_array['headhtml'] = array();
241 $result->setContent( $result_array['headhtml'], $out->headElement( $userSkin ) );
242 }
243
244 if ( isset( $prop['iwlinks'] ) ) {
245 $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
246 }
247
248 if ( !is_null( $oldid ) ) {
249 $result_array['revid'] = intval( $oldid );
250 }
251
252 $result_mapping = array(
253 'redirects' => 'r',
254 'langlinks' => 'll',
255 'categories' => 'cl',
256 'links' => 'pl',
257 'templates' => 'tl',
258 'images' => 'img',
259 'externallinks' => 'el',
260 'iwlinks' => 'iw',
261 'sections' => 's',
262 'headitems' => 'hi',
263 );
264 $this->setIndexedTagNames( $result_array, $result_mapping );
265 $result->addValue( null, $this->getModuleName(), $result_array );
266
267 if ( !is_null( $oldLang ) ) {
268 $wgLang = $oldLang; // Reset $wgLang to $oldLang
269 }
270 }
271
272 private function getSectionText( $text, $what ) {
273 global $wgParser;
274 $text = $wgParser->getSection( $text, $this->section, false );
275 if ( $text === false ) {
276 $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
277 }
278 return $text;
279 }
280
281 private function formatLangLinks( $links ) {
282 $result = array();
283 foreach ( $links as $link ) {
284 $entry = array();
285 $bits = explode( ':', $link, 2 );
286 $entry['lang'] = $bits[0];
287 $this->getResult()->setContent( $entry, $bits[1] );
288 $result[] = $entry;
289 }
290 return $result;
291 }
292
293 private function formatCategoryLinks( $links ) {
294 $result = array();
295 foreach ( $links as $link => $sortkey ) {
296 $entry = array();
297 $entry['sortkey'] = $sortkey;
298 $this->getResult()->setContent( $entry, $link );
299 $result[] = $entry;
300 }
301 return $result;
302 }
303
304 private function formatLinks( $links ) {
305 $result = array();
306 foreach ( $links as $ns => $nslinks ) {
307 foreach ( $nslinks as $title => $id ) {
308 $entry = array();
309 $entry['ns'] = $ns;
310 $this->getResult()->setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() );
311 if ( $id != 0 ) {
312 $entry['exists'] = '';
313 }
314 $result[] = $entry;
315 }
316 }
317 return $result;
318 }
319
320 private function formatIWLinks( $iw ) {
321 $result = array();
322 foreach ( $iw as $prefix => $titles ) {
323 foreach ( $titles as $title => $id ) {
324 $entry = array();
325 $entry['prefix'] = $prefix;
326
327 $title = Title::newFromText( "{$prefix}:{$title}" );
328 if ( $title ) {
329 $entry['url'] = $title->getFullURL();
330 }
331
332 $this->getResult()->setContent( $entry, $title->getFullText() );
333 $result[] = $entry;
334 }
335 }
336 return $result;
337 }
338
339 private function formatHeadItems( $headItems ) {
340 $result = array();
341 foreach ( $headItems as $tag => $content ) {
342 $entry = array();
343 $entry['tag'] = $tag;
344 $this->getResult()->setContent( $entry, $content );
345 $result[] = $entry;
346 }
347 return $result;
348 }
349
350 private function formatCss( $css ) {
351 $result = array();
352 foreach ( $css as $file => $link ) {
353 $entry = array();
354 $entry['file'] = $file;
355 $this->getResult()->setContent( $entry, $link );
356 $result[] = $entry;
357 }
358 return $result;
359 }
360
361 private function setIndexedTagNames( &$array, $mapping ) {
362 foreach ( $mapping as $key => $name ) {
363 if ( isset( $array[$key] ) ) {
364 $this->getResult()->setIndexedTagName( $array[$key], $name );
365 }
366 }
367 }
368
369 public function getAllowedParams() {
370 return array(
371 'title' => array(
372 ApiBase::PARAM_DFLT => 'API',
373 ),
374 'text' => null,
375 'summary' => null,
376 'page' => null,
377 'pageid' => null,
378 'redirects' => false,
379 'oldid' => null,
380 'prop' => array(
381 ApiBase::PARAM_DFLT => 'text|langlinks|categories|links|templates|images|externallinks|sections|revid|displaytitle',
382 ApiBase::PARAM_ISMULTI => true,
383 ApiBase::PARAM_TYPE => array(
384 'text',
385 'langlinks',
386 'categories',
387 'links',
388 'templates',
389 'images',
390 'externallinks',
391 'sections',
392 'revid',
393 'displaytitle',
394 'headitems',
395 'headhtml',
396 'iwlinks',
397 )
398 ),
399 'pst' => false,
400 'onlypst' => false,
401 'uselang' => null,
402 'section' => null,
403 );
404 }
405
406 public function getParamDescription() {
407 $p = $this->getModulePrefix();
408 return array(
409 'text' => 'Wikitext to parse',
410 'summary' => 'Summary to parse',
411 'redirects' => "If the {$p}page parameter is set to a redirect, resolve it",
412 'title' => 'Title of page the text belongs to',
413 'page' => "Parse the content of this page. Cannot be used together with {$p}text and {$p}title",
414 'pageid' => "Parse the content of this page. Overrides {$p}page",
415 'oldid' => "Parse the content of this revision. Overrides {$p}page and {$p}pageid",
416 'prop' => array(
417 'Which pieces of information to get',
418 ' text - Gives the parsed text of the wikitext',
419 ' langlinks - Gives the langlinks the parsed wikitext',
420 ' categories - Gives the categories of the parsed wikitext',
421 ' links - Gives the internal links in the parsed wikitext',
422 ' templates - Gives the templates in the parsed wikitext',
423 ' images - Gives the images in the parsed wikitext',
424 ' externallinks - Gives the external links in the parsed wikitext',
425 ' sections - Gives the sections in the parsed wikitext',
426 ' revid - Adds the revision id of the parsed page',
427 ' displaytitle - Adds the title of the parsed wikitext',
428 ' headitems - Gives items to put in the <head> of the page',
429 ' headhtml - Gives parsed <head> of the page',
430 ' iwlinks - Gives interwiki links in the parsed wikitext',
431 'NOTE: Section tree is only generated if there are more than 4 sections, or if the __TOC__ keyword is present'
432 ),
433 'pst' => array(
434 'Do a pre-save transform on the input before parsing it',
435 'Ignored if page, pageid or oldid is used'
436 ),
437 'onlypst' => array(
438 'Do a pre-save transform (PST) on the input, but don\'t parse it',
439 'Returns the same wikitext, after a PST has been applied. Ignored if page, pageid or oldid is used'
440 ),
441 'uselang' => 'Which language to parse the request in',
442 'section' => 'Only retrieve the content of this section number',
443 );
444 }
445
446 public function getDescription() {
447 return 'This module parses wikitext and returns parser output';
448 }
449
450 public function getPossibleErrors() {
451 return array_merge( parent::getPossibleErrors(), array(
452 array( 'code' => 'params', 'info' => 'The page parameter cannot be used together with the text and title parameters' ),
453 array( 'code' => 'missingrev', 'info' => 'There is no revision ID oldid' ),
454 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revisions' ),
455 array( 'code' => 'missingtitle', 'info' => 'The page you specified doesn\'t exist' ),
456 array( 'code' => 'nosuchsection', 'info' => 'There is no section sectionnumber in page' ),
457 array( 'nosuchpageid' ),
458 ) );
459 }
460
461 protected function getExamples() {
462 return array(
463 'api.php?action=parse&text={{Project:Sandbox}}'
464 );
465 }
466
467 public function getVersion() {
468 return __CLASS__ . ': $Id$';
469 }
470 }