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