* (bug 28897) rvparse doesn’t seem to work with rvsection
[lhc/web/wiklou.git] / includes / api / ApiParse.php
1 <?php
2 /**
3 * Created on Dec 01, 2007
4 *
5 * Copyright © 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 */
24
25 if ( !defined( 'MEDIAWIKI' ) ) {
26 // Eclipse helper - will be ignored in production
27 require_once( "ApiBase.php" );
28 }
29
30 /**
31 * @ingroup API
32 */
33 class ApiParse extends ApiBase {
34 private $section, $text, $pstText = null;
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, $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( !$params['disablepp'] );
77
78 $redirValues = null;
79
80 if ( !is_null( $oldid ) || !is_null( $pageid ) || !is_null( $page ) ) {
81 if ( !is_null( $oldid ) ) {
82 // Don't use the parser cache
83 $rev = Revision::newFromID( $oldid );
84 if ( !$rev ) {
85 $this->dieUsage( "There is no revision ID $oldid", 'missingrev' );
86 }
87 if ( !$rev->userCan( Revision::DELETED_TEXT ) ) {
88 $this->dieUsage( "You don't have permission to view deleted revisions", 'permissiondenied' );
89 }
90
91 $titleObj = $rev->getTitle();
92
93 $wgTitle = $titleObj;
94
95 // If for some reason the "oldid" is actually the current revision, it may be cached
96 if ( $titleObj->getLatestRevID() === intval( $oldid ) ) {
97 $articleObj = new Article( $titleObj, 0 );
98
99 $p_result = $this->getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageid,
100 isset( $prop['wikitext'] ) ) ;
101 } else { // This is an old revision, so get the text differently
102 $this->text = $rev->getText( Revision::FOR_THIS_USER );
103
104 $wgTitle = $titleObj;
105
106 if ( $this->section !== false ) {
107 $this->text = $this->getSectionText( $this->text, 'r' . $rev->getId() );
108 }
109
110 $p_result = $wgParser->parse( $this->text, $titleObj, $popts );
111 }
112 } else { // Not $oldid
113 if ( !is_null ( $pageid ) ) {
114 $titleObj = Title::newFromID( $pageid );
115
116 if ( !$titleObj ) {
117 $this->dieUsageMsg( array( 'nosuchpageid', $pageid ) );
118 }
119 } else { // $page
120 if ( $params['redirects'] ) {
121 $req = new FauxRequest( array(
122 'action' => 'query',
123 'redirects' => '',
124 'titles' => $page
125 ) );
126 $main = new ApiMain( $req );
127 $main->execute();
128 $data = $main->getResultData();
129 $redirValues = isset( $data['query']['redirects'] )
130 ? $data['query']['redirects'] : array();
131 $to = $page;
132 foreach ( (array)$redirValues as $r ) {
133 $to = $r['to'];
134 }
135 } else {
136 $to = $page;
137 }
138 $titleObj = Title::newFromText( $to );
139 if ( !$titleObj || !$titleObj->exists() ) {
140 $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
141 }
142 }
143 $wgTitle = $titleObj;
144
145 $articleObj = new Article( $titleObj, 0 );
146 if ( isset( $prop['revid'] ) ) {
147 $oldid = $articleObj->getRevIdFetched();
148 }
149
150 $p_result = $this->getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageid,
151 isset( $prop['wikitext'] ) ) ;
152 }
153 } else { // Not $oldid, $pageid, $page. Hence based on $text
154
155 $this->text = $text;
156 $titleObj = Title::newFromText( $title );
157 if ( !$titleObj ) {
158 $this->dieUsageMsg( array( 'invalidtitle', $title ) );
159 }
160 $wgTitle = $titleObj;
161
162 if ( $this->section !== false ) {
163 $this->text = $this->getSectionText( $this->text, $titleObj->getText() );
164 }
165
166 if ( $params['pst'] || $params['onlypst'] ) {
167 $this->pstText = $wgParser->preSaveTransform( $this->text, $titleObj, $wgUser, $popts );
168 }
169 if ( $params['onlypst'] ) {
170 // Build a result and bail out
171 $result_array['text'] = array();
172 $this->getResult()->setContent( $result_array['text'], $this->pstText );
173 if ( isset( $prop['wikitext'] ) ) {
174 $result_array['wikitext'] = array();
175 $this->getResult()->setContent( $result_array['wikitext'], $this->text );
176 }
177 $this->getResult()->addValue( null, $this->getModuleName(), $result_array );
178 return;
179 }
180 $p_result = $wgParser->parse( $params['pst'] ? $this->pstText : $this->text, $titleObj, $popts );
181 }
182
183 // Return result
184 $result = $this->getResult();
185 $result_array = array();
186
187 $result_array['title'] = $titleObj->getPrefixedText();
188
189 if ( !is_null( $oldid ) ) {
190 $result_array['revid'] = intval( $oldid );
191 }
192
193 if ( $params['redirects'] && !is_null( $redirValues ) ) {
194 $result_array['redirects'] = $redirValues;
195 }
196
197 if ( isset( $prop['text'] ) ) {
198 $result_array['text'] = array();
199 $result->setContent( $result_array['text'], $p_result->getText() );
200 }
201
202 if ( !is_null( $params['summary'] ) ) {
203 $result_array['parsedsummary'] = array();
204 $result->setContent( $result_array['parsedsummary'], $wgUser->getSkin()->formatComment( $params['summary'], $titleObj ) );
205 }
206
207 if ( isset( $prop['langlinks'] ) ) {
208 $result_array['langlinks'] = $this->formatLangLinks( $p_result->getLanguageLinks() );
209 }
210 if ( isset( $prop['languageshtml'] ) ) {
211 $languagesHtml = $this->languagesHtml( $p_result->getLanguageLinks() );
212 $result_array['languageshtml'] = array();
213 $result->setContent( $result_array['languageshtml'], $languagesHtml );
214 }
215 if ( isset( $prop['categories'] ) ) {
216 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
217 }
218 if ( isset( $prop['categorieshtml'] ) ) {
219 $categoriesHtml = $this->categoriesHtml( $p_result->getCategories() );
220 $result_array['categorieshtml'] = array();
221 $result->setContent( $result_array['categorieshtml'], $categoriesHtml );
222 }
223 if ( isset( $prop['links'] ) ) {
224 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
225 }
226 if ( isset( $prop['templates'] ) ) {
227 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
228 }
229 if ( isset( $prop['images'] ) ) {
230 $result_array['images'] = array_keys( $p_result->getImages() );
231 }
232 if ( isset( $prop['externallinks'] ) ) {
233 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
234 }
235 if ( isset( $prop['sections'] ) ) {
236 $result_array['sections'] = $p_result->getSections();
237 }
238
239 if ( isset( $prop['displaytitle'] ) ) {
240 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
241 $p_result->getDisplayTitle() :
242 $titleObj->getPrefixedText();
243 }
244
245 if ( isset( $prop['headitems'] ) || isset( $prop['headhtml'] ) ) {
246 $context = new RequestContext;
247 $context->getOutput()->addParserOutputNoText( $p_result );
248
249 if ( isset( $prop['headitems'] ) ) {
250 $headItems = $this->formatHeadItems( $p_result->getHeadItems() );
251
252 $context->getSkin()->setupUserCss( $context->getOutput() );
253 $css = $this->formatCss( $context->getOutput()->buildCssLinksArray() );
254
255 $scripts = array( $context->getOutput()->getHeadScripts( $context->getSkin() ) );
256
257 $result_array['headitems'] = array_merge( $headItems, $css, $scripts );
258 }
259
260 if ( isset( $prop['headhtml'] ) ) {
261 $result_array['headhtml'] = array();
262 $result->setContent( $result_array['headhtml'], $context->getOutput()->headElement( $context->getSkin() ) );
263 }
264 }
265
266 if ( isset( $prop['iwlinks'] ) ) {
267 $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
268 }
269
270 if ( isset( $prop['wikitext'] ) ) {
271 $result_array['wikitext'] = array();
272 $result->setContent( $result_array['wikitext'], $this->text );
273 if ( !is_null( $this->pstText ) ) {
274 $result_array['psttext'] = array();
275 $result->setContent( $result_array['psttext'], $this->pstText );
276 }
277 }
278
279 $result_mapping = array(
280 'redirects' => 'r',
281 'langlinks' => 'll',
282 'categories' => 'cl',
283 'links' => 'pl',
284 'templates' => 'tl',
285 'images' => 'img',
286 'externallinks' => 'el',
287 'iwlinks' => 'iw',
288 'sections' => 's',
289 'headitems' => 'hi',
290 );
291 $this->setIndexedTagNames( $result_array, $result_mapping );
292 $result->addValue( null, $this->getModuleName(), $result_array );
293
294 if ( !is_null( $oldLang ) ) {
295 $wgLang = $oldLang; // Reset $wgLang to $oldLang
296 }
297 }
298
299 /**
300 * @param $articleObj Article
301 * @param $titleObj Title
302 * @param $popts ParserOptions
303 * @param $pageId Int
304 * @param $getWikitext Bool
305 * @return ParserOutput
306 */
307 private function getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageId = null, $getWikitext = false ) {
308 if ( $this->section !== false ) {
309 global $wgParser;
310
311 $this->text = $this->getSectionText( $articleObj->getRawText(), !is_null ( $pageId )
312 ? 'page id ' . $pageId : $titleObj->getText() );
313
314 return $wgParser->parse( $this->text, $titleObj, $popts );
315 } else {
316 // Try the parser cache first
317 $pout = $articleObj->getParserOutput();
318 if ( $getWikitext ) {
319 $rev = Revision::newFromTitle( $titleObj );
320 if ( $rev ) {
321 $this->text = $rev->getText();
322 }
323 }
324 return $pout;
325 }
326 }
327
328 private function getSectionText( $text, $what ) {
329 global $wgParser;
330 $text = $wgParser->getSection( $text, $this->section, false );
331 if ( $text === false ) {
332 $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
333 }
334 return $text;
335 }
336
337 private function formatLangLinks( $links ) {
338 $result = array();
339 foreach ( $links as $link ) {
340 $entry = array();
341 $bits = explode( ':', $link, 2 );
342 $title = Title::newFromText( $link );
343
344 $entry['lang'] = $bits[0];
345 if ( $title ) {
346 $entry['url'] = $title->getFullURL();
347 }
348 $this->getResult()->setContent( $entry, $bits[1] );
349 $result[] = $entry;
350 }
351 return $result;
352 }
353
354 private function formatCategoryLinks( $links ) {
355 $result = array();
356 foreach ( $links as $link => $sortkey ) {
357 $entry = array();
358 $entry['sortkey'] = $sortkey;
359 $this->getResult()->setContent( $entry, $link );
360 $result[] = $entry;
361 }
362 return $result;
363 }
364
365 private function categoriesHtml( $categories ) {
366 global $wgOut, $wgUser;
367 $wgOut->addCategoryLinks( $categories );
368 return $wgUser->getSkin()->getCategories();
369 }
370
371 /**
372 * @deprecated since 1.18 No modern skin generates language links this way, please use language links
373 * data to generate your own HTML.
374 */
375 private function languagesHtml( $languages ) {
376 global $wgContLang, $wgHideInterlanguageLinks;
377
378 if ( $wgHideInterlanguageLinks || count( $languages ) == 0 ) {
379 return '';
380 }
381
382 $s = htmlspecialchars( wfMsg( 'otherlanguages' ) . wfMsg( 'colon-separator' ) );
383
384 $langs = array();
385 foreach ( $languages as $l ) {
386 $nt = Title::newFromText( $l );
387 $text = $wgContLang->getLanguageName( $nt->getInterwiki() );
388
389 $langs[] = Html::element( 'a',
390 array( 'href' => $nt->getFullURL(), 'title' => $nt->getText(), 'class' => "external" ),
391 $text == '' ? $l : $text );
392 }
393
394 $s .= implode( htmlspecialchars( wfMsgExt( 'pipe-separator', 'escapenoentities' ) ), $langs );
395
396 if ( $wgContLang->isRTL() ) {
397 $s = Html::rawElement( 'span', array( 'dir' => "LTR" ), $s );
398 }
399
400 return $s;
401 }
402
403 private function formatLinks( $links ) {
404 $result = array();
405 foreach ( $links as $ns => $nslinks ) {
406 foreach ( $nslinks as $title => $id ) {
407 $entry = array();
408 $entry['ns'] = $ns;
409 $this->getResult()->setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() );
410 if ( $id != 0 ) {
411 $entry['exists'] = '';
412 }
413 $result[] = $entry;
414 }
415 }
416 return $result;
417 }
418
419 private function formatIWLinks( $iw ) {
420 $result = array();
421 foreach ( $iw as $prefix => $titles ) {
422 foreach ( array_keys( $titles ) as $title ) {
423 $entry = array();
424 $entry['prefix'] = $prefix;
425
426 $title = Title::newFromText( "{$prefix}:{$title}" );
427 if ( $title ) {
428 $entry['url'] = $title->getFullURL();
429 }
430
431 $this->getResult()->setContent( $entry, $title->getFullText() );
432 $result[] = $entry;
433 }
434 }
435 return $result;
436 }
437
438 private function formatHeadItems( $headItems ) {
439 $result = array();
440 foreach ( $headItems as $tag => $content ) {
441 $entry = array();
442 $entry['tag'] = $tag;
443 $this->getResult()->setContent( $entry, $content );
444 $result[] = $entry;
445 }
446 return $result;
447 }
448
449 private function formatCss( $css ) {
450 $result = array();
451 foreach ( $css as $file => $link ) {
452 $entry = array();
453 $entry['file'] = $file;
454 $this->getResult()->setContent( $entry, $link );
455 $result[] = $entry;
456 }
457 return $result;
458 }
459
460 private function setIndexedTagNames( &$array, $mapping ) {
461 foreach ( $mapping as $key => $name ) {
462 if ( isset( $array[$key] ) ) {
463 $this->getResult()->setIndexedTagName( $array[$key], $name );
464 }
465 }
466 }
467
468 public function getAllowedParams() {
469 return array(
470 'title' => array(
471 ApiBase::PARAM_DFLT => 'API',
472 ),
473 'text' => null,
474 'summary' => null,
475 'page' => null,
476 'pageid' => array(
477 ApiBase::PARAM_TYPE => 'integer',
478 ),
479 'redirects' => false,
480 'oldid' => array(
481 ApiBase::PARAM_TYPE => 'integer',
482 ),
483 'prop' => array(
484 ApiBase::PARAM_DFLT => 'text|langlinks|categories|links|templates|images|externallinks|sections|revid|displaytitle',
485 ApiBase::PARAM_ISMULTI => true,
486 ApiBase::PARAM_TYPE => array(
487 'text',
488 'langlinks',
489 'languageshtml',
490 'categories',
491 'categorieshtml',
492 'links',
493 'templates',
494 'images',
495 'externallinks',
496 'sections',
497 'revid',
498 'displaytitle',
499 'headitems',
500 'headhtml',
501 'iwlinks',
502 'wikitext',
503 )
504 ),
505 'pst' => false,
506 'onlypst' => false,
507 'uselang' => null,
508 'section' => null,
509 'disablepp' => false,
510 );
511 }
512
513 public function getParamDescription() {
514 $p = $this->getModulePrefix();
515 return array(
516 'text' => 'Wikitext to parse',
517 'summary' => 'Summary to parse',
518 'redirects' => "If the {$p}page parameter is set to a redirect, resolve it",
519 'title' => 'Title of page the text belongs to',
520 'page' => "Parse the content of this page. Cannot be used together with {$p}text and {$p}title",
521 'pageid' => "Parse the content of this page. Overrides {$p}page",
522 'oldid' => "Parse the content of this revision. Overrides {$p}page and {$p}pageid",
523 'prop' => array(
524 'Which pieces of information to get',
525 ' text - Gives the parsed text of the wikitext',
526 ' langlinks - Gives the language links in the parsed wikitext',
527 ' categories - Gives the categories in the parsed wikitext',
528 ' categorieshtml - Gives the HTML version of the categories',
529 ' languageshtml - Gives the HTML version of the language links',
530 ' links - Gives the internal links in the parsed wikitext',
531 ' templates - Gives the templates in the parsed wikitext',
532 ' images - Gives the images in the parsed wikitext',
533 ' externallinks - Gives the external links in the parsed wikitext',
534 ' sections - Gives the sections in the parsed wikitext',
535 ' revid - Adds the revision ID of the parsed page',
536 ' displaytitle - Adds the title of the parsed wikitext',
537 ' headitems - Gives items to put in the <head> of the page',
538 ' headhtml - Gives parsed <head> of the page',
539 ' iwlinks - Gives interwiki links in the parsed wikitext',
540 ' wikitext - Gives the original wikitext that was parsed',
541 ),
542 'pst' => array(
543 'Do a pre-save transform on the input before parsing it',
544 'Ignored if page, pageid or oldid is used'
545 ),
546 'onlypst' => array(
547 'Do a pre-save transform (PST) on the input, but don\'t parse it',
548 'Returns the same wikitext, after a PST has been applied. Ignored if page, pageid or oldid is used'
549 ),
550 'uselang' => 'Which language to parse the request in',
551 'section' => 'Only retrieve the content of this section number',
552 'disablepp' => 'Disable the PP Report from the parser output',
553 );
554 }
555
556 public function getDescription() {
557 return 'Parses wikitext and returns parser output';
558 }
559
560 public function getPossibleErrors() {
561 return array_merge( parent::getPossibleErrors(), array(
562 array( 'code' => 'params', 'info' => 'The page parameter cannot be used together with the text and title parameters' ),
563 array( 'code' => 'missingrev', 'info' => 'There is no revision ID oldid' ),
564 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revisions' ),
565 array( 'code' => 'missingtitle', 'info' => 'The page you specified doesn\'t exist' ),
566 array( 'code' => 'nosuchsection', 'info' => 'There is no section sectionnumber in page' ),
567 array( 'nosuchpageid' ),
568 array( 'invalidtitle', 'title' ),
569 ) );
570 }
571
572 protected function getExamples() {
573 return array(
574 'api.php?action=parse&text={{Project:Sandbox}}'
575 );
576 }
577
578 public function getVersion() {
579 return __CLASS__ . ': $Id$';
580 }
581 }