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