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