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