fixing long lines
[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 /**
26 * @ingroup API
27 */
28 class ApiParse extends ApiBase {
29 private $section, $text, $pstText = null;
30
31 public function __construct( $main, $action ) {
32 parent::__construct( $main, $action );
33 }
34
35 public function execute() {
36 // The data is hot but user-dependent, like page views, so we set vary cookies
37 $this->getMain()->setCacheMode( 'anon-public-user-private' );
38
39 // Get parameters
40 $params = $this->extractRequestParams();
41 $text = $params['text'];
42 $title = $params['title'];
43 $page = $params['page'];
44 $pageid = $params['pageid'];
45 $oldid = $params['oldid'];
46
47 if ( !is_null( $page ) && ( !is_null( $text ) || $title != 'API' ) ) {
48 $this->dieUsage( 'The page parameter cannot be used together with the text and title parameters', 'params' );
49 }
50
51 $prop = array_flip( $params['prop'] );
52
53 if ( isset( $params['section'] ) ) {
54 $this->section = $params['section'];
55 } else {
56 $this->section = false;
57 }
58
59 // The parser needs $wgTitle to be set, apparently the
60 // $title parameter in Parser::parse isn't enough *sigh*
61 // TODO: Does this still need $wgTitle?
62 global $wgParser, $wgTitle;
63
64 // Currently unnecessary, code to act as a safeguard against any change in current behaviour of uselang breaks
65 $oldLang = null;
66 if ( isset( $params['uselang'] ) && $params['uselang'] != $this->getContext()->getLanguage()->getCode() ) {
67 $oldLang = $this->getContext()->getLanguage(); // Backup language
68 $this->getContext()->setLanguage( Language::factory( $params['uselang'] ) );
69 }
70
71 $popts = ParserOptions::newFromContext( $this->getContext() );
72 $popts->setTidy( true );
73 $popts->enableLimitReport( !$params['disablepp'] );
74
75 $redirValues = null;
76
77 // Return result
78 $result = $this->getResult();
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, $this->getUser() ) ) {
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 // May get from/save to parser cache
98 $p_result = $this->getParsedSectionOrText( $titleObj, $popts, $pageid,
99 isset( $prop['wikitext'] ) ) ;
100 } else { // This is an old revision, so get the text differently
101 $this->text = $rev->getText( Revision::FOR_THIS_USER, $this->getUser() );
102
103 if ( $this->section !== false ) {
104 $this->text = $this->getSectionText( $this->text, 'r' . $rev->getId() );
105 }
106
107 // Should we save old revision parses to the parser cache?
108 $p_result = $wgParser->parse( $this->text, $titleObj, $popts );
109 }
110 } else { // Not $oldid, but $pageid or $page
111 if ( $params['redirects'] ) {
112 $reqParams = array(
113 'action' => 'query',
114 'redirects' => '',
115 );
116 if ( !is_null ( $pageid ) ) {
117 $reqParams['pageids'] = $pageid;
118 } else { // $page
119 $reqParams['titles'] = $page;
120 }
121 $req = new FauxRequest( $reqParams );
122 $main = new ApiMain( $req );
123 $main->execute();
124 $data = $main->getResultData();
125 $redirValues = isset( $data['query']['redirects'] )
126 ? $data['query']['redirects']
127 : array();
128 $to = $page;
129 foreach ( (array)$redirValues as $r ) {
130 $to = $r['to'];
131 }
132 $titleObj = Title::newFromText( $to );
133 } else {
134 if ( !is_null ( $pageid ) ) {
135 $reqParams['pageids'] = $pageid;
136 $titleObj = Title::newFromID( $pageid );
137 } else { // $page
138 $to = $page;
139 $titleObj = Title::newFromText( $to );
140 }
141 }
142 if ( !is_null ( $pageid ) ) {
143 if ( !$titleObj ) {
144 // Still throw nosuchpageid error if pageid was provided
145 $this->dieUsageMsg( array( 'nosuchpageid', $pageid ) );
146 }
147 } elseif ( !$titleObj || !$titleObj->exists() ) {
148 $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
149 }
150 $wgTitle = $titleObj;
151
152 if ( isset( $prop['revid'] ) ) {
153 $oldid = $titleObj->getLatestRevID();
154 }
155
156 // Potentially cached
157 $p_result = $this->getParsedSectionOrText( $titleObj, $popts, $pageid,
158 isset( $prop['wikitext'] ) ) ;
159 }
160 } else { // Not $oldid, $pageid, $page. Hence based on $text
161
162 if ( is_null( $text ) ) {
163 $this->dieUsage( 'The text parameter should be passed with the title parameter. Should you be using the "page" parameter instead?', 'params' );
164 }
165 $this->text = $text;
166 $titleObj = Title::newFromText( $title );
167 if ( !$titleObj ) {
168 $this->dieUsageMsg( array( 'invalidtitle', $title ) );
169 }
170 $wgTitle = $titleObj;
171
172 if ( $this->section !== false ) {
173 $this->text = $this->getSectionText( $this->text, $titleObj->getText() );
174 }
175
176 if ( $params['pst'] || $params['onlypst'] ) {
177 $this->pstText = $wgParser->preSaveTransform( $this->text, $titleObj, $this->getUser(), $popts );
178 }
179 if ( $params['onlypst'] ) {
180 // Build a result and bail out
181 $result_array = array();
182 $result_array['text'] = array();
183 $result->setContent( $result_array['text'], $this->pstText );
184 if ( isset( $prop['wikitext'] ) ) {
185 $result_array['wikitext'] = array();
186 $result->setContent( $result_array['wikitext'], $this->text );
187 }
188 $result->addValue( null, $this->getModuleName(), $result_array );
189 return;
190 }
191 // Not cached (save or load)
192 #FIXME: use Content object!
193 $p_result = $wgParser->parse( $params['pst'] ? $this->pstText : $this->text, $titleObj, $popts );
194 }
195
196 $result_array = array();
197
198 $result_array['title'] = $titleObj->getPrefixedText();
199
200 if ( !is_null( $oldid ) ) {
201 $result_array['revid'] = intval( $oldid );
202 }
203
204 if ( $params['redirects'] && !is_null( $redirValues ) ) {
205 $result_array['redirects'] = $redirValues;
206 }
207
208 if ( isset( $prop['text'] ) ) {
209 $result_array['text'] = array();
210 $result->setContent( $result_array['text'], $p_result->getText() );
211 }
212
213 if ( !is_null( $params['summary'] ) ) {
214 $result_array['parsedsummary'] = array();
215 $result->setContent( $result_array['parsedsummary'], Linker::formatComment( $params['summary'], $titleObj ) );
216 }
217
218 if ( isset( $prop['langlinks'] ) ) {
219 $result_array['langlinks'] = $this->formatLangLinks( $p_result->getLanguageLinks() );
220 }
221 if ( isset( $prop['languageshtml'] ) ) {
222 $languagesHtml = $this->languagesHtml( $p_result->getLanguageLinks() );
223 $result_array['languageshtml'] = array();
224 $result->setContent( $result_array['languageshtml'], $languagesHtml );
225 }
226 if ( isset( $prop['categories'] ) ) {
227 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
228 }
229 if ( isset( $prop['categorieshtml'] ) ) {
230 $categoriesHtml = $this->categoriesHtml( $p_result->getCategories() );
231 $result_array['categorieshtml'] = array();
232 $result->setContent( $result_array['categorieshtml'], $categoriesHtml );
233 }
234 if ( isset( $prop['links'] ) ) {
235 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
236 }
237 if ( isset( $prop['templates'] ) ) {
238 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
239 }
240 if ( isset( $prop['images'] ) ) {
241 $result_array['images'] = array_keys( $p_result->getImages() );
242 }
243 if ( isset( $prop['externallinks'] ) ) {
244 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
245 }
246 if ( isset( $prop['sections'] ) ) {
247 $result_array['sections'] = $p_result->getSections();
248 }
249
250 if ( isset( $prop['displaytitle'] ) ) {
251 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
252 $p_result->getDisplayTitle() :
253 $titleObj->getPrefixedText();
254 }
255
256 if ( isset( $prop['headitems'] ) || isset( $prop['headhtml'] ) ) {
257 $context = $this->getContext();
258 $context->setTitle( $titleObj );
259 $context->getOutput()->addParserOutputNoText( $p_result );
260
261 if ( isset( $prop['headitems'] ) ) {
262 $headItems = $this->formatHeadItems( $p_result->getHeadItems() );
263
264 $css = $this->formatCss( $context->getOutput()->buildCssLinksArray() );
265
266 $scripts = array( $context->getOutput()->getHeadScripts() );
267
268 $result_array['headitems'] = array_merge( $headItems, $css, $scripts );
269 }
270
271 if ( isset( $prop['headhtml'] ) ) {
272 $result_array['headhtml'] = array();
273 $result->setContent( $result_array['headhtml'], $context->getOutput()->headElement( $context->getSkin() ) );
274 }
275 }
276
277 if ( isset( $prop['iwlinks'] ) ) {
278 $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
279 }
280
281 if ( isset( $prop['wikitext'] ) ) {
282 $result_array['wikitext'] = array();
283 $result->setContent( $result_array['wikitext'], $this->text );
284 if ( !is_null( $this->pstText ) ) {
285 $result_array['psttext'] = array();
286 $result->setContent( $result_array['psttext'], $this->pstText );
287 }
288 }
289 if ( isset( $prop['properties'] ) ) {
290 $result_array['properties'] = $this->formatProperties( $p_result->getProperties() );
291 }
292
293 if ( $params['generatexml'] ) {
294 $wgParser->startExternalParse( $titleObj, $popts, OT_PREPROCESS );
295 $dom = $wgParser->preprocessToDom( $this->text );
296 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
297 $xml = $dom->saveXML();
298 } else {
299 $xml = $dom->__toString();
300 }
301 $result_array['parsetree'] = array();
302 $result->setContent( $result_array['parsetree'], $xml );
303 }
304
305 $result_mapping = array(
306 'redirects' => 'r',
307 'langlinks' => 'll',
308 'categories' => 'cl',
309 'links' => 'pl',
310 'templates' => 'tl',
311 'images' => 'img',
312 'externallinks' => 'el',
313 'iwlinks' => 'iw',
314 'sections' => 's',
315 'headitems' => 'hi',
316 'properties' => 'pp',
317 );
318 $this->setIndexedTagNames( $result_array, $result_mapping );
319 $result->addValue( null, $this->getModuleName(), $result_array );
320
321 if ( !is_null( $oldLang ) ) {
322 $this->getContext()->setLanguage( $oldLang ); // Reset language to $oldLang
323 }
324 }
325
326 /**
327 * @param $titleObj Title
328 * @param $popts ParserOptions
329 * @param $pageId Int
330 * @param $getWikitext Bool
331 * @return ParserOutput
332 */
333 private function getParsedSectionOrText( $titleObj, $popts, $pageId = null, $getWikitext = false ) {
334 global $wgParser;
335
336 $page = WikiPage::factory( $titleObj );
337
338 if ( $this->section !== false ) { #FIXME: get section Content, get parser output, ...
339 $this->text = $this->getSectionText( $page->getRawText(), !is_null( $pageId )
340 ? 'page id ' . $pageId : $titleObj->getText() ); #FIXME: get section...
341
342 // Not cached (save or load)
343 return $wgParser->parse( $this->text, $titleObj, $popts );
344 } else {
345 // Try the parser cache first
346 // getParserOutput will save to Parser cache if able
347 $pout = $page->getParserOutput( $popts );
348 if ( !$pout ) {
349 $this->dieUsage( "There is no revision ID {$page->getLatest()}", 'missingrev' );
350 }
351 if ( $getWikitext ) {
352 $this->content = $page->getContent( Revision::RAW ); #FIXME: use $this->content everywhere
353 $this->text = ContentHandler::getContentText( $this->content ); #FIXME: serialize, get format from params; or use object structure in result?
354 }
355 return $pout;
356 }
357 }
358
359 private function getSectionText( $text, $what ) { #FIXME: replace with Content::getSection
360 global $wgParser;
361 // Not cached (save or load)
362 $text = $wgParser->getSection( $text, $this->section, false );
363 if ( $text === false ) {
364 $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
365 }
366 return $text;
367 }
368
369 private function formatLangLinks( $links ) {
370 $result = array();
371 foreach ( $links as $link ) {
372 $entry = array();
373 $bits = explode( ':', $link, 2 );
374 $title = Title::newFromText( $link );
375
376 $entry['lang'] = $bits[0];
377 if ( $title ) {
378 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
379 }
380 $this->getResult()->setContent( $entry, $bits[1] );
381 $result[] = $entry;
382 }
383 return $result;
384 }
385
386 private function formatCategoryLinks( $links ) {
387 $result = array();
388 foreach ( $links as $link => $sortkey ) {
389 $entry = array();
390 $entry['sortkey'] = $sortkey;
391 $this->getResult()->setContent( $entry, $link );
392 $result[] = $entry;
393 }
394 return $result;
395 }
396
397 private function categoriesHtml( $categories ) {
398 $context = $this->getContext();
399 $context->getOutput()->addCategoryLinks( $categories );
400 return $context->getSkin()->getCategories();
401 }
402
403 /**
404 * @deprecated since 1.18 No modern skin generates language links this way, please use language links
405 * data to generate your own HTML.
406 * @param $languages array
407 * @return string
408 */
409 private function languagesHtml( $languages ) {
410 wfDeprecated( __METHOD__, '1.18' );
411
412 global $wgContLang, $wgHideInterlanguageLinks;
413
414 if ( $wgHideInterlanguageLinks || count( $languages ) == 0 ) {
415 return '';
416 }
417
418 $s = htmlspecialchars( wfMessage( 'otherlanguages' )->text() . wfMessage( 'colon-separator' )->text() );
419
420 $langs = array();
421 foreach ( $languages as $l ) {
422 $nt = Title::newFromText( $l );
423 $text = Language::fetchLanguageName( $nt->getInterwiki() );
424
425 $langs[] = Html::element( 'a',
426 array( 'href' => $nt->getFullURL(), 'title' => $nt->getText(), 'class' => "external" ),
427 $text == '' ? $l : $text );
428 }
429
430 $s .= implode( htmlspecialchars( wfMsgExt( 'pipe-separator', 'escapenoentities' ) ), $langs );
431
432 if ( $wgContLang->isRTL() ) {
433 $s = Html::rawElement( 'span', array( 'dir' => "LTR" ), $s );
434 }
435
436 return $s;
437 }
438
439 private function formatLinks( $links ) {
440 $result = array();
441 foreach ( $links as $ns => $nslinks ) {
442 foreach ( $nslinks as $title => $id ) {
443 $entry = array();
444 $entry['ns'] = $ns;
445 $this->getResult()->setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() );
446 if ( $id != 0 ) {
447 $entry['exists'] = '';
448 }
449 $result[] = $entry;
450 }
451 }
452 return $result;
453 }
454
455 private function formatIWLinks( $iw ) {
456 $result = array();
457 foreach ( $iw as $prefix => $titles ) {
458 foreach ( array_keys( $titles ) as $title ) {
459 $entry = array();
460 $entry['prefix'] = $prefix;
461
462 $title = Title::newFromText( "{$prefix}:{$title}" );
463 if ( $title ) {
464 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
465 }
466
467 $this->getResult()->setContent( $entry, $title->getFullText() );
468 $result[] = $entry;
469 }
470 }
471 return $result;
472 }
473
474 private function formatHeadItems( $headItems ) {
475 $result = array();
476 foreach ( $headItems as $tag => $content ) {
477 $entry = array();
478 $entry['tag'] = $tag;
479 $this->getResult()->setContent( $entry, $content );
480 $result[] = $entry;
481 }
482 return $result;
483 }
484
485 private function formatProperties( $properties ) {
486 $result = array();
487 foreach ( $properties as $name => $value ) {
488 $entry = array();
489 $entry['name'] = $name;
490 $this->getResult()->setContent( $entry, $value );
491 $result[] = $entry;
492 }
493 return $result;
494 }
495
496 private function formatCss( $css ) {
497 $result = array();
498 foreach ( $css as $file => $link ) {
499 $entry = array();
500 $entry['file'] = $file;
501 $this->getResult()->setContent( $entry, $link );
502 $result[] = $entry;
503 }
504 return $result;
505 }
506
507 private function setIndexedTagNames( &$array, $mapping ) {
508 foreach ( $mapping as $key => $name ) {
509 if ( isset( $array[$key] ) ) {
510 $this->getResult()->setIndexedTagName( $array[$key], $name );
511 }
512 }
513 }
514
515 public function getAllowedParams() {
516 return array(
517 'title' => array(
518 ApiBase::PARAM_DFLT => 'API',
519 ),
520 'text' => null,
521 'summary' => null,
522 'page' => null,
523 'pageid' => array(
524 ApiBase::PARAM_TYPE => 'integer',
525 ),
526 'redirects' => false,
527 'oldid' => array(
528 ApiBase::PARAM_TYPE => 'integer',
529 ),
530 'prop' => array(
531 ApiBase::PARAM_DFLT => 'text|langlinks|categories|links|templates|images|externallinks|sections|revid|displaytitle|iwlinks|properties',
532 ApiBase::PARAM_ISMULTI => true,
533 ApiBase::PARAM_TYPE => array(
534 'text',
535 'langlinks',
536 'languageshtml',
537 'categories',
538 'categorieshtml',
539 'links',
540 'templates',
541 'images',
542 'externallinks',
543 'sections',
544 'revid',
545 'displaytitle',
546 'headitems',
547 'headhtml',
548 'iwlinks',
549 'wikitext',
550 'properties',
551 )
552 ),
553 'pst' => false,
554 'onlypst' => false,
555 'uselang' => null,
556 'section' => null,
557 'disablepp' => false,
558 'generatexml' => false,
559 );
560 }
561
562 public function getParamDescription() {
563 $p = $this->getModulePrefix();
564 return array(
565 'text' => 'Wikitext to parse',
566 'summary' => 'Summary to parse',
567 'redirects' => "If the {$p}page or the {$p}pageid parameter is set to a redirect, resolve it",
568 'title' => 'Title of page the text belongs to',
569 'page' => "Parse the content of this page. Cannot be used together with {$p}text and {$p}title",
570 'pageid' => "Parse the content of this page. Overrides {$p}page",
571 'oldid' => "Parse the content of this revision. Overrides {$p}page and {$p}pageid",
572 'prop' => array(
573 'Which pieces of information to get',
574 ' text - Gives the parsed text of the wikitext',
575 ' langlinks - Gives the language links in the parsed wikitext',
576 ' categories - Gives the categories in the parsed wikitext',
577 ' categorieshtml - Gives the HTML version of the categories',
578 ' languageshtml - Gives the HTML version of the language links',
579 ' links - Gives the internal links in the parsed wikitext',
580 ' templates - Gives the templates in the parsed wikitext',
581 ' images - Gives the images in the parsed wikitext',
582 ' externallinks - Gives the external links in the parsed wikitext',
583 ' sections - Gives the sections in the parsed wikitext',
584 ' revid - Adds the revision ID of the parsed page',
585 ' displaytitle - Adds the title of the parsed wikitext',
586 ' headitems - Gives items to put in the <head> of the page',
587 ' headhtml - Gives parsed <head> of the page',
588 ' iwlinks - Gives interwiki links in the parsed wikitext',
589 ' wikitext - Gives the original wikitext that was parsed',
590 ' properties - Gives various properties defined in the parsed wikitext',
591 ),
592 'pst' => array(
593 'Do a pre-save transform on the input before parsing it',
594 'Ignored if page, pageid or oldid is used'
595 ),
596 'onlypst' => array(
597 'Do a pre-save transform (PST) on the input, but don\'t parse it',
598 'Returns the same wikitext, after a PST has been applied. Ignored if page, pageid or oldid is used'
599 ),
600 'uselang' => 'Which language to parse the request in',
601 'section' => 'Only retrieve the content of this section number',
602 'disablepp' => 'Disable the PP Report from the parser output',
603 'generatexml' => 'Generate XML parse tree',
604 );
605 }
606
607 public function getDescription() {
608 return array(
609 'Parses wikitext and returns parser output',
610 'See the various prop-Modules of action=query to get information from the current version of a page',
611 );
612 }
613
614 public function getPossibleErrors() {
615 return array_merge( parent::getPossibleErrors(), array(
616 array( 'code' => 'params', 'info' => 'The page parameter cannot be used together with the text and title parameters' ),
617 array( 'code' => 'params', 'info' => 'The text parameter should be passed with the title parameter. Should you be using the "page" parameter instead?' ),
618 array( 'code' => 'missingrev', 'info' => 'There is no revision ID oldid' ),
619 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revisions' ),
620 array( 'code' => 'missingtitle', 'info' => 'The page you specified doesn\'t exist' ),
621 array( 'code' => 'nosuchsection', 'info' => 'There is no section sectionnumber in page' ),
622 array( 'nosuchpageid' ),
623 array( 'invalidtitle', 'title' ),
624 ) );
625 }
626
627 public function getExamples() {
628 return array(
629 'api.php?action=parse&text={{Project:Sandbox}}'
630 );
631 }
632
633 public function getHelpUrls() {
634 return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#parse';
635 }
636
637 public function getVersion() {
638 return __CLASS__ . ': $Id$';
639 }
640 }