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