3 * Created on Dec 01, 2007
5 * Copyright © 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
25 if ( !defined( 'MEDIAWIKI' ) ) {
26 // Eclipse helper - will be ignored in production
27 require_once( "ApiBase.php" );
33 class ApiParse
extends ApiBase
{
34 private $section, $text, $pstText = null;
36 public function __construct( $main, $action ) {
37 parent
::__construct( $main, $action );
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' );
45 $params = $this->extractRequestParams();
46 $text = $params['text'];
47 $title = $params['title'];
48 $page = $params['page'];
49 $pageid = $params['pageid'];
50 $oldid = $params['oldid'];
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' );
55 $prop = array_flip( $params['prop'] );
57 if ( isset( $params['section'] ) ) {
58 $this->section
= $params['section'];
60 $this->section
= false;
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;
67 // Currently unnecessary, code to act as a safeguard against any change in current behaviour of uselang breaks
69 if ( isset( $params['uselang'] ) && $params['uselang'] != $wgLang->getCode() ) {
70 $oldLang = $wgLang; // Backup wgLang
71 $wgLang = Language
::factory( $params['uselang'] );
74 $popts = new ParserOptions();
75 $popts->setTidy( true );
76 $popts->enableLimitReport( !$params['disablepp'] );
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 );
85 $this->dieUsage( "There is no revision ID $oldid", 'missingrev' );
87 if ( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
88 $this->dieUsage( "You don't have permission to view deleted revisions", 'permissiondenied' );
91 $titleObj = $rev->getTitle();
95 // If for some reason the "oldid" is actually the current revision, it may be cached
96 if ( $titleObj->getLatestRevID() === intval( $oldid ) ) {
97 $articleObj = new Article( $titleObj, 0 );
99 $p_result = $this->getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageid,
100 isset( $prop['wikitext'] ) ) ;
101 } else { // This is an old revision, so get the text differently
102 $this->text
= $rev->getText( Revision
::FOR_THIS_USER
);
104 $wgTitle = $titleObj;
106 if ( $this->section
!== false ) {
107 $this->text
= $this->getSectionText( $this->text
, 'r' . $rev->getId() );
110 $p_result = $wgParser->parse( $this->text
, $titleObj, $popts );
112 } else { // Not $oldid
113 if ( !is_null ( $pageid ) ) {
114 $titleObj = Title
::newFromID( $pageid );
117 $this->dieUsageMsg( array( 'nosuchpageid', $pageid ) );
120 if ( $params['redirects'] ) {
121 $req = new FauxRequest( array(
126 $main = new ApiMain( $req );
128 $data = $main->getResultData();
129 $redirValues = isset( $data['query']['redirects'] )
130 ?
$data['query']['redirects'] : array();
132 foreach ( (array)$redirValues as $r ) {
138 $titleObj = Title
::newFromText( $to );
139 if ( !$titleObj ||
!$titleObj->exists() ) {
140 $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
143 $wgTitle = $titleObj;
145 $articleObj = new Article( $titleObj, 0 );
146 if ( isset( $prop['revid'] ) ) {
147 $oldid = $articleObj->getRevIdFetched();
150 $p_result = $this->getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageid,
151 isset( $prop['wikitext'] ) ) ;
153 } else { // Not $oldid, $pageid, $page. Hence based on $text
156 $titleObj = Title
::newFromText( $title );
158 $this->dieUsageMsg( array( 'invalidtitle', $title ) );
160 $wgTitle = $titleObj;
162 if ( $this->section
!== false ) {
163 $this->text
= $this->getSectionText( $this->text
, $titleObj->getText() );
166 if ( $params['pst'] ||
$params['onlypst'] ) {
167 $this->pstText
= $wgParser->preSaveTransform( $this->text
, $titleObj, $wgUser, $popts );
169 if ( $params['onlypst'] ) {
170 // Build a result and bail out
171 $result_array['text'] = array();
172 $this->getResult()->setContent( $result_array['text'], $this->pstText
);
173 if ( isset( $prop['wikitext'] ) ) {
174 $result_array['wikitext'] = array();
175 $this->getResult()->setContent( $result_array['wikitext'], $this->text
);
177 $this->getResult()->addValue( null, $this->getModuleName(), $result_array );
180 $p_result = $wgParser->parse( $params['pst'] ?
$this->pstText
: $this->text
, $titleObj, $popts );
184 $result = $this->getResult();
185 $result_array = array();
187 $result_array['title'] = $titleObj->getPrefixedText();
189 if ( !is_null( $oldid ) ) {
190 $result_array['revid'] = intval( $oldid );
193 if ( $params['redirects'] && !is_null( $redirValues ) ) {
194 $result_array['redirects'] = $redirValues;
197 if ( isset( $prop['text'] ) ) {
198 $result_array['text'] = array();
199 $result->setContent( $result_array['text'], $p_result->getText() );
202 if ( !is_null( $params['summary'] ) ) {
203 $result_array['parsedsummary'] = array();
204 $result->setContent( $result_array['parsedsummary'], $wgUser->getSkin()->formatComment( $params['summary'], $titleObj ) );
207 if ( isset( $prop['langlinks'] ) ) {
208 $result_array['langlinks'] = $this->formatLangLinks( $p_result->getLanguageLinks() );
210 if ( isset( $prop['languageshtml'] ) ) {
211 $languagesHtml = $this->languagesHtml( $p_result->getLanguageLinks() );
212 $result_array['languageshtml'] = array();
213 $result->setContent( $result_array['languageshtml'], $languagesHtml );
215 if ( isset( $prop['categories'] ) ) {
216 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
218 if ( isset( $prop['categorieshtml'] ) ) {
219 $categoriesHtml = $this->categoriesHtml( $p_result->getCategories() );
220 $result_array['categorieshtml'] = array();
221 $result->setContent( $result_array['categorieshtml'], $categoriesHtml );
223 if ( isset( $prop['links'] ) ) {
224 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
226 if ( isset( $prop['templates'] ) ) {
227 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
229 if ( isset( $prop['images'] ) ) {
230 $result_array['images'] = array_keys( $p_result->getImages() );
232 if ( isset( $prop['externallinks'] ) ) {
233 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
235 if ( isset( $prop['sections'] ) ) {
236 $result_array['sections'] = $p_result->getSections();
239 if ( isset( $prop['displaytitle'] ) ) {
240 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
241 $p_result->getDisplayTitle() :
242 $titleObj->getPrefixedText();
245 if ( isset( $prop['headitems'] ) ||
isset( $prop['headhtml'] ) ) {
246 $context = new RequestContext
;
247 $context->getOutput()->addParserOutputNoText( $p_result );
249 if ( isset( $prop['headitems'] ) ) {
250 $headItems = $this->formatHeadItems( $p_result->getHeadItems() );
252 $context->getSkin()->setupUserCss( $context->getOutput() );
253 $css = $this->formatCss( $context->getOutput()->buildCssLinksArray() );
255 $scripts = array( $context->getOutput()->getHeadScripts( $context->getSkin() ) );
257 $result_array['headitems'] = array_merge( $headItems, $css, $scripts );
260 if ( isset( $prop['headhtml'] ) ) {
261 $result_array['headhtml'] = array();
262 $result->setContent( $result_array['headhtml'], $context->getOutput()->headElement( $context->getSkin() ) );
266 if ( isset( $prop['iwlinks'] ) ) {
267 $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
270 if ( isset( $prop['wikitext'] ) ) {
271 $result_array['wikitext'] = array();
272 $result->setContent( $result_array['wikitext'], $this->text
);
273 if ( !is_null( $this->pstText
) ) {
274 $result_array['psttext'] = array();
275 $result->setContent( $result_array['psttext'], $this->pstText
);
279 $result_mapping = array(
282 'categories' => 'cl',
286 'externallinks' => 'el',
291 $this->setIndexedTagNames( $result_array, $result_mapping );
292 $result->addValue( null, $this->getModuleName(), $result_array );
294 if ( !is_null( $oldLang ) ) {
295 $wgLang = $oldLang; // Reset $wgLang to $oldLang
300 * @param $articleObj Article
301 * @param $titleObj Title
302 * @param $popts ParserOptions
304 * @param $getWikitext Bool
305 * @return ParserOutput
307 private function getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageId = null, $getWikitext = false ) {
308 if ( $this->section
!== false ) {
311 $this->text
= $this->getSectionText( $articleObj->getRawText(), !is_null ( $pageId )
312 ?
'page id ' . $pageId : $titleObj->getText() );
314 return $wgParser->parse( $this->text
, $titleObj, $popts );
316 // Try the parser cache first
317 $pout = $articleObj->getParserOutput();
318 if ( $getWikitext ) {
319 $rev = Revision
::newFromTitle( $titleObj );
321 $this->text
= $rev->getText();
328 private function getSectionText( $text, $what ) {
330 $text = $wgParser->getSection( $text, $this->section
, false );
331 if ( $text === false ) {
332 $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
337 private function formatLangLinks( $links ) {
339 foreach ( $links as $link ) {
341 $bits = explode( ':', $link, 2 );
342 $title = Title
::newFromText( $link );
344 $entry['lang'] = $bits[0];
346 $entry['url'] = $title->getFullURL();
348 $this->getResult()->setContent( $entry, $bits[1] );
354 private function formatCategoryLinks( $links ) {
356 foreach ( $links as $link => $sortkey ) {
358 $entry['sortkey'] = $sortkey;
359 $this->getResult()->setContent( $entry, $link );
365 private function categoriesHtml( $categories ) {
366 global $wgOut, $wgUser;
367 $wgOut->addCategoryLinks( $categories );
368 return $wgUser->getSkin()->getCategories();
372 * @deprecated since 1.18 No modern skin generates language links this way, please use language links
373 * data to generate your own HTML.
375 private function languagesHtml( $languages ) {
376 global $wgContLang, $wgHideInterlanguageLinks;
378 if ( $wgHideInterlanguageLinks ||
count( $languages ) == 0 ) {
382 $s = htmlspecialchars( wfMsg( 'otherlanguages' ) . wfMsg( 'colon-separator' ) );
385 foreach ( $languages as $l ) {
386 $nt = Title
::newFromText( $l );
387 $text = $wgContLang->getLanguageName( $nt->getInterwiki() );
389 $langs[] = Html
::element( 'a',
390 array( 'href' => $nt->getFullURL(), 'title' => $nt->getText(), 'class' => "external" ),
391 $text == '' ?
$l : $text );
394 $s .= implode( htmlspecialchars( wfMsgExt( 'pipe-separator', 'escapenoentities' ) ), $langs );
396 if ( $wgContLang->isRTL() ) {
397 $s = Html
::rawElement( 'span', array( 'dir' => "LTR" ), $s );
403 private function formatLinks( $links ) {
405 foreach ( $links as $ns => $nslinks ) {
406 foreach ( $nslinks as $title => $id ) {
409 $this->getResult()->setContent( $entry, Title
::makeTitle( $ns, $title )->getFullText() );
411 $entry['exists'] = '';
419 private function formatIWLinks( $iw ) {
421 foreach ( $iw as $prefix => $titles ) {
422 foreach ( array_keys( $titles ) as $title ) {
424 $entry['prefix'] = $prefix;
426 $title = Title
::newFromText( "{$prefix}:{$title}" );
428 $entry['url'] = $title->getFullURL();
431 $this->getResult()->setContent( $entry, $title->getFullText() );
438 private function formatHeadItems( $headItems ) {
440 foreach ( $headItems as $tag => $content ) {
442 $entry['tag'] = $tag;
443 $this->getResult()->setContent( $entry, $content );
449 private function formatCss( $css ) {
451 foreach ( $css as $file => $link ) {
453 $entry['file'] = $file;
454 $this->getResult()->setContent( $entry, $link );
460 private function setIndexedTagNames( &$array, $mapping ) {
461 foreach ( $mapping as $key => $name ) {
462 if ( isset( $array[$key] ) ) {
463 $this->getResult()->setIndexedTagName( $array[$key], $name );
468 public function getAllowedParams() {
471 ApiBase
::PARAM_DFLT
=> 'API',
477 ApiBase
::PARAM_TYPE
=> 'integer',
479 'redirects' => false,
481 ApiBase
::PARAM_TYPE
=> 'integer',
484 ApiBase
::PARAM_DFLT
=> 'text|langlinks|categories|links|templates|images|externallinks|sections|revid|displaytitle',
485 ApiBase
::PARAM_ISMULTI
=> true,
486 ApiBase
::PARAM_TYPE
=> array(
509 'disablepp' => false,
513 public function getParamDescription() {
514 $p = $this->getModulePrefix();
516 'text' => 'Wikitext to parse',
517 'summary' => 'Summary to parse',
518 'redirects' => "If the {$p}page parameter is set to a redirect, resolve it",
519 'title' => 'Title of page the text belongs to',
520 'page' => "Parse the content of this page. Cannot be used together with {$p}text and {$p}title",
521 'pageid' => "Parse the content of this page. Overrides {$p}page",
522 'oldid' => "Parse the content of this revision. Overrides {$p}page and {$p}pageid",
524 'Which pieces of information to get',
525 ' text - Gives the parsed text of the wikitext',
526 ' langlinks - Gives the language links in the parsed wikitext',
527 ' categories - Gives the categories in the parsed wikitext',
528 ' categorieshtml - Gives the HTML version of the categories',
529 ' languageshtml - Gives the HTML version of the language links',
530 ' links - Gives the internal links in the parsed wikitext',
531 ' templates - Gives the templates in the parsed wikitext',
532 ' images - Gives the images in the parsed wikitext',
533 ' externallinks - Gives the external links in the parsed wikitext',
534 ' sections - Gives the sections in the parsed wikitext',
535 ' revid - Adds the revision ID of the parsed page',
536 ' displaytitle - Adds the title of the parsed wikitext',
537 ' headitems - Gives items to put in the <head> of the page',
538 ' headhtml - Gives parsed <head> of the page',
539 ' iwlinks - Gives interwiki links in the parsed wikitext',
540 ' wikitext - Gives the original wikitext that was parsed',
543 'Do a pre-save transform on the input before parsing it',
544 'Ignored if page, pageid or oldid is used'
547 'Do a pre-save transform (PST) on the input, but don\'t parse it',
548 'Returns the same wikitext, after a PST has been applied. Ignored if page, pageid or oldid is used'
550 'uselang' => 'Which language to parse the request in',
551 'section' => 'Only retrieve the content of this section number',
552 'disablepp' => 'Disable the PP Report from the parser output',
556 public function getDescription() {
557 return 'Parses wikitext and returns parser output';
560 public function getPossibleErrors() {
561 return array_merge( parent
::getPossibleErrors(), array(
562 array( 'code' => 'params', 'info' => 'The page parameter cannot be used together with the text and title parameters' ),
563 array( 'code' => 'missingrev', 'info' => 'There is no revision ID oldid' ),
564 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revisions' ),
565 array( 'code' => 'missingtitle', 'info' => 'The page you specified doesn\'t exist' ),
566 array( 'code' => 'nosuchsection', 'info' => 'There is no section sectionnumber in page' ),
567 array( 'nosuchpageid' ),
568 array( 'invalidtitle', 'title' ),
572 protected function getExamples() {
574 'api.php?action=parse&text={{Project:Sandbox}}'
578 public function getVersion() {
579 return __CLASS__
. ': $Id$';