Part of bug 23254 - Add interwiki links to parse output
[lhc/web/wiklou.git] / includes / api / ApiParse.php
1 <?php
2
3 /**
4 * Created on Dec 01, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( "ApiBase.php" );
29 }
30
31 /**
32 * @ingroup API
33 */
34 class ApiParse extends ApiBase {
35
36 public function __construct( $main, $action ) {
37 parent::__construct( $main, $action );
38 }
39
40 public function execute() {
41 // Get parameters
42 $params = $this->extractRequestParams();
43 $text = $params['text'];
44 $title = $params['title'];
45 $page = $params['page'];
46 $pageid = $params['pageid'];
47 $oldid = $params['oldid'];
48
49 if ( !is_null( $page ) && ( !is_null( $text ) || $title != 'API' ) ) {
50 $this->dieUsage( 'The page parameter cannot be used together with the text and title parameters', 'params' );
51 }
52 $prop = array_flip( $params['prop'] );
53 $revid = false;
54
55 if ( isset( $params['section'] ) ) {
56 $this->section = $params['section'];
57 } else {
58 $this->section = false;
59 }
60
61 // The parser needs $wgTitle to be set, apparently the
62 // $title parameter in Parser::parse isn't enough *sigh*
63 global $wgParser, $wgUser, $wgTitle, $wgEnableParserCache, $wgLang;
64
65 // Currently unncessary, code to act as a safeguard against any change in current behaviour of uselang breaks
66 $oldLang = null;
67 if ( isset( $params['uselang'] ) && $params['uselang'] != $wgLang->getCode() ) {
68 $oldLang = $wgLang; // Backup wgLang
69 $wgLang = Language::factory( $params['uselang'] );
70 }
71
72 $popts = new ParserOptions();
73 $popts->setTidy( true );
74 $popts->enableLimitReport();
75 $redirValues = null;
76 if ( !is_null( $oldid ) || !is_null( $pageid ) || !is_null( $page ) ) {
77 if ( !is_null( $oldid ) ) {
78 // Don't use the parser cache
79 $rev = Revision::newFromID( $oldid );
80 if ( !$rev ) {
81 $this->dieUsage( "There is no revision ID $oldid", 'missingrev' );
82 }
83 if ( !$rev->userCan( Revision::DELETED_TEXT ) ) {
84 $this->dieUsage( "You don't have permission to view deleted revisions", 'permissiondenied' );
85 }
86
87 $text = $rev->getText( Revision::FOR_THIS_USER );
88 $titleObj = $rev->getTitle();
89 $wgTitle = $titleObj;
90
91 if ( $this->section !== false ) {
92 $text = $this->getSectionText( $text, 'r' . $rev );
93 }
94
95 $p_result = $wgParser->parse( $text, $titleObj, $popts );
96 } else {
97 if ( !is_null ( $pageid ) ) {
98 $titleObj = Title::newFromID( $pageid );
99
100 if ( !$titleObj ) {
101 $this->dieUsageMsg( array( 'nosuchpageid', $pageid ) );
102 }
103 } else {
104 if ( $params['redirects'] ) {
105 $req = new FauxRequest( array(
106 'action' => 'query',
107 'redirects' => '',
108 'titles' => $page
109 ) );
110 $main = new ApiMain( $req );
111 $main->execute();
112 $data = $main->getResultData();
113 $redirValues = @$data['query']['redirects'];
114 $to = $page;
115 foreach ( (array)$redirValues as $r ) {
116 $to = $r['to'];
117 }
118 } else {
119 $to = $page;
120 }
121 $titleObj = Title::newFromText( $to );
122 if ( !$titleObj ) {
123 $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
124 }
125 }
126 $wgTitle = $titleObj;
127
128 $articleObj = new Article( $titleObj );
129 if ( isset( $prop['revid'] ) ) {
130 $oldid = $articleObj->getRevIdFetched();
131 }
132
133 if ( $this->section !== false ) {
134 $text = $this->getSectionText( $text, !is_null ( $pageid ) ? 'page id ' . $pageid : $titleObj->getText() );
135 $p_result = $wgParser->parse( $text, $titleObj, $popts );
136 } else {
137 // Try the parser cache first
138 $p_result = false;
139 $pcache = ParserCache::singleton();
140 if ( $wgEnableParserCache ) {
141 $p_result = $pcache->get( $articleObj, $wgUser );
142 }
143 if ( !$p_result ) {
144 $p_result = $wgParser->parse( $articleObj->getContent(), $titleObj, $popts );
145
146 if ( $wgEnableParserCache ) {
147 $pcache->save( $p_result, $articleObj, $popts );
148 }
149 }
150 }
151 }
152 } else {
153 $titleObj = Title::newFromText( $title );
154 if ( !$titleObj ) {
155 $titleObj = Title::newFromText( 'API' );
156 }
157 $wgTitle = $titleObj;
158
159 if ( $this->section !== false ) {
160 $text = $this->getSectionText( $text, $titleObj->getText() );
161 }
162
163 if ( $params['pst'] || $params['onlypst'] ) {
164 $text = $wgParser->preSaveTransform( $text, $titleObj, $wgUser, $popts );
165 }
166 if ( $params['onlypst'] ) {
167 // Build a result and bail out
168 $result_array['text'] = array();
169 $this->getResult()->setContent( $result_array['text'], $text );
170 $this->getResult()->addValue( null, $this->getModuleName(), $result_array );
171 return;
172 }
173 $p_result = $wgParser->parse( $text, $titleObj, $popts );
174 }
175
176 // Return result
177 $result = $this->getResult();
178 $result_array = array();
179 if ( $params['redirects'] && !is_null( $redirValues ) ) {
180 $result_array['redirects'] = $redirValues;
181 }
182
183 if ( isset( $prop['text'] ) ) {
184 $result_array['text'] = array();
185 $result->setContent( $result_array['text'], $p_result->getText() );
186 }
187
188 if ( !is_null( $params['summary'] ) ) {
189 $result_array['parsedsummary'] = array();
190 $result->setContent( $result_array['parsedsummary'], $wgUser->getSkin()->formatComment( $params['summary'], $titleObj ) );
191 }
192
193 if ( isset( $prop['langlinks'] ) ) {
194 $result_array['langlinks'] = $this->formatLangLinks( $p_result->getLanguageLinks() );
195 }
196 if ( isset( $prop['categories'] ) ) {
197 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
198 }
199 if ( isset( $prop['links'] ) ) {
200 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
201 }
202 if ( isset( $prop['templates'] ) ) {
203 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
204 }
205 if ( isset( $prop['images'] ) ) {
206 $result_array['images'] = array_keys( $p_result->getImages() );
207 }
208 if ( isset( $prop['externallinks'] ) ) {
209 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
210 }
211 if ( isset( $prop['sections'] ) ) {
212 $result_array['sections'] = $p_result->getSections();
213 }
214
215 if ( isset( $prop['displaytitle'] ) ) {
216 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
217 $p_result->getDisplayTitle() :
218 $titleObj->getPrefixedText();
219 }
220
221 if ( isset( $prop['headitems'] ) || isset( $prop['headhtml'] ) ) {
222 $out = new OutputPage;
223 $out->addParserOutputNoText( $p_result );
224 $userSkin = $wgUser->getSkin();
225 }
226
227 if ( isset( $prop['headitems'] ) ) {
228 $headItems = $this->formatHeadItems( $p_result->getHeadItems() );
229
230 $userSkin->setupUserCss( $out );
231 $css = $this->formatCss( $out->buildCssLinksArray() );
232
233 $scripts = array( $out->getHeadScripts( $userSkin ) );
234
235 $result_array['headitems'] = array_merge( $headItems , $css, $scripts );
236 }
237
238 if ( isset( $prop['headhtml'] ) ) {
239 $result_array['headhtml'] = array();
240 $result->setContent( $result_array['headhtml'], $out->headElement( $userSkin ) );
241 }
242
243 if ( isset( $prop['iwlinks'] ) ) {
244 $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
245 }
246
247 if ( !is_null( $oldid ) ) {
248 $result_array['revid'] = intval( $oldid );
249 }
250
251 $result_mapping = array(
252 'redirects' => 'r',
253 'langlinks' => 'll',
254 'categories' => 'cl',
255 'links' => 'pl',
256 'templates' => 'tl',
257 'images' => 'img',
258 'externallinks' => 'el',
259 'iwlinks' => 'iw',
260 'sections' => 's',
261 'headitems' => 'hi',
262 );
263 $this->setIndexedTagNames( $result_array, $result_mapping );
264 $result->addValue( null, $this->getModuleName(), $result_array );
265
266 if ( !is_null( $oldLang ) ) {
267 $wgLang = $oldLang; // Reset $wgLang to $oldLang
268 }
269 }
270
271 private function getSectionText( $text, $what ) {
272 global $wgParser;
273 $text = $wgParser->getSection( $text, $this->section, false );
274 if ( $text === false ) {
275 $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
276 }
277 return $text;
278 }
279
280 private function formatLangLinks( $links ) {
281 $result = array();
282 foreach ( $links as $link ) {
283 $entry = array();
284 $bits = explode( ':', $link, 2 );
285 $entry['lang'] = $bits[0];
286 $this->getResult()->setContent( $entry, $bits[1] );
287 $result[] = $entry;
288 }
289 return $result;
290 }
291
292 private function formatCategoryLinks( $links ) {
293 $result = array();
294 foreach ( $links as $link => $sortkey ) {
295 $entry = array();
296 $entry['sortkey'] = $sortkey;
297 $this->getResult()->setContent( $entry, $link );
298 $result[] = $entry;
299 }
300 return $result;
301 }
302
303 private function formatLinks( $links ) {
304 $result = array();
305 foreach ( $links as $ns => $nslinks ) {
306 foreach ( $nslinks as $title => $id ) {
307 $entry = array();
308 $entry['ns'] = $ns;
309 $this->getResult()->setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() );
310 if ( $id != 0 ) {
311 $entry['exists'] = '';
312 }
313 $result[] = $entry;
314 }
315 }
316 return $result;
317 }
318
319 private function formatIWLinks( $iw ) {
320 $result = array();
321 foreach ( $iw as $prefix => $titles ) {
322 foreach ( $titles as $title => $id ) {
323 $entry = array();
324 $entry['prefix'] = $prefix;
325 $this->getResult()->setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() );
326 $result[] = $entry;
327 }
328 }
329 return $result;
330 }
331
332 private function formatHeadItems( $headItems ) {
333 $result = array();
334 foreach ( $headItems as $tag => $content ) {
335 $entry = array();
336 $entry['tag'] = $tag;
337 $this->getResult()->setContent( $entry, $content );
338 $result[] = $entry;
339 }
340 return $result;
341 }
342
343 private function formatCss( $css ) {
344 $result = array();
345 foreach ( $css as $file => $link ) {
346 $entry = array();
347 $entry['file'] = $file;
348 $this->getResult()->setContent( $entry, $link );
349 $result[] = $entry;
350 }
351 return $result;
352 }
353
354 private function setIndexedTagNames( &$array, $mapping ) {
355 foreach ( $mapping as $key => $name ) {
356 if ( isset( $array[$key] ) ) {
357 $this->getResult()->setIndexedTagName( $array[$key], $name );
358 }
359 }
360 }
361
362 public function getAllowedParams() {
363 return array(
364 'title' => array(
365 ApiBase::PARAM_DFLT => 'API',
366 ),
367 'text' => null,
368 'summary' => null,
369 'page' => null,
370 'pageid' => null,
371 'redirects' => false,
372 'oldid' => null,
373 'prop' => array(
374 ApiBase::PARAM_DFLT => 'text|langlinks|categories|links|templates|images|externallinks|sections|revid|displaytitle',
375 ApiBase::PARAM_ISMULTI => true,
376 ApiBase::PARAM_TYPE => array(
377 'text',
378 'langlinks',
379 'categories',
380 'links',
381 'templates',
382 'images',
383 'externallinks',
384 'sections',
385 'revid',
386 'displaytitle',
387 'headitems',
388 'headhtml',
389 'iwlinks',
390 )
391 ),
392 'pst' => false,
393 'onlypst' => false,
394 'uselang' => null,
395 'section' => null,
396 );
397 }
398
399 public function getParamDescription() {
400 $p = $this->getModulePrefix();
401 return array(
402 'text' => 'Wikitext to parse',
403 'summary' => 'Summary to parse',
404 'redirects' => "If the {$p}page parameter is set to a redirect, resolve it",
405 'title' => 'Title of page the text belongs to',
406 'page' => "Parse the content of this page. Cannot be used together with {$p}text and {$p}title",
407 'pageid' => "Parse the content of this page. Overrides {$p}page",
408 'oldid' => "Parse the content of this revision. Overrides {$p}page and {$p}pageid",
409 'prop' => array( 'Which pieces of information to get',
410 'NOTE: Section tree is only generated if there are more than 4 sections, or if the __TOC__ keyword is present'
411 ),
412 'pst' => array( 'Do a pre-save transform on the input before parsing it',
413 'Ignored if page, pageid or oldid is used'
414 ),
415 'onlypst' => array( 'Do a pre-save transform (PST) on the input, but don\'t parse it',
416 'Returns the same wikitext, after a PST has been applied. Ignored if page, pageid or oldid is used'
417 ),
418 'uselang' => 'Which language to parse the request in',
419 'section' => 'Only retrieve the content of this section number',
420 );
421 }
422
423 public function getDescription() {
424 return 'This module parses wikitext and returns parser output';
425 }
426
427 public function getPossibleErrors() {
428 return array_merge( parent::getPossibleErrors(), array(
429 array( 'code' => 'params', 'info' => 'The page parameter cannot be used together with the text and title parameters' ),
430 array( 'code' => 'missingrev', 'info' => 'There is no revision ID oldid' ),
431 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revisions' ),
432 array( 'code' => 'missingtitle', 'info' => 'The page you specified doesn\'t exist' ),
433 array( 'code' => 'nosuchsection', 'info' => 'There is no section sectionnumber in page'),
434 array( 'nosuchpageid' ),
435 ) );
436 }
437
438 protected function getExamples() {
439 return array(
440 'api.php?action=parse&text={{Project:Sandbox}}'
441 );
442 }
443
444 public function getVersion() {
445 return __CLASS__ . ': $Id$';
446 }
447 }