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
28 class ApiParse
extends ApiBase
{
29 private $section, $text, $pstText = null;
31 public function __construct( $main, $action ) {
32 parent
::__construct( $main, $action );
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' );
40 $params = $this->extractRequestParams();
41 $text = $params['text'];
42 $title = $params['title'];
43 $page = $params['page'];
44 $pageid = $params['pageid'];
45 $oldid = $params['oldid'];
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' );
50 $prop = array_flip( $params['prop'] );
52 if ( isset( $params['section'] ) ) {
53 $this->section
= $params['section'];
55 $this->section
= false;
58 // The parser needs $wgTitle to be set, apparently the
59 // $title parameter in Parser::parse isn't enough *sigh*
60 // TODO: Does this still need $wgTitle?
61 global $wgParser, $wgTitle, $wgLang;
63 // Currently unnecessary, code to act as a safeguard against any change in current behaviour of uselang breaks
65 if ( isset( $params['uselang'] ) && $params['uselang'] != $wgLang->getCode() ) {
66 $oldLang = $wgLang; // Backup wgLang
67 $wgLang = Language
::factory( $params['uselang'] );
70 $popts = ParserOptions
::newFromContext( $this->getContext() );
71 $popts->setTidy( true );
72 $popts->enableLimitReport( !$params['disablepp'] );
77 $result = $this->getResult();
79 if ( !is_null( $oldid ) ||
!is_null( $pageid ) ||
!is_null( $page ) ) {
80 if ( !is_null( $oldid ) ) {
81 // Don't use the parser cache
82 $rev = Revision
::newFromID( $oldid );
84 $this->dieUsage( "There is no revision ID $oldid", 'missingrev' );
86 if ( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
87 $this->dieUsage( "You don't have permission to view deleted revisions", 'permissiondenied' );
90 $titleObj = $rev->getTitle();
94 // If for some reason the "oldid" is actually the current revision, it may be cached
95 if ( $titleObj->getLatestRevID() === intval( $oldid ) ) {
96 // May get from/save to parser cache
97 $p_result = $this->getParsedSectionOrText( $titleObj, $popts, $pageid,
98 isset( $prop['wikitext'] ) ) ;
99 } else { // This is an old revision, so get the text differently
100 $this->text
= $rev->getText( Revision
::FOR_THIS_USER
, $this->getUser() );
102 if ( $this->section
!== false ) {
103 $this->text
= $this->getSectionText( $this->text
, 'r' . $rev->getId() );
106 // Should we save old revision parses to the parser cache?
107 $p_result = $wgParser->parse( $this->text
, $titleObj, $popts );
109 } else { // Not $oldid, but $pageid or $page
110 if ( $params['redirects'] ) {
115 if ( !is_null ( $pageid ) ) {
116 $reqParams['pageids'] = $pageid;
118 $reqParams['titles'] = $page;
120 $req = new FauxRequest( $reqParams );
121 $main = new ApiMain( $req );
123 $data = $main->getResultData();
124 $redirValues = isset( $data['query']['redirects'] )
125 ?
$data['query']['redirects']
128 foreach ( (array)$redirValues as $r ) {
131 $titleObj = Title
::newFromText( $to );
133 if ( !is_null ( $pageid ) ) {
134 $reqParams['pageids'] = $pageid;
135 $titleObj = Title
::newFromID( $pageid );
138 $titleObj = Title
::newFromText( $to );
141 if ( !is_null ( $pageid ) ) {
143 // Still throw nosuchpageid error if pageid was provided
144 $this->dieUsageMsg( array( 'nosuchpageid', $pageid ) );
146 } elseif ( !$titleObj ||
!$titleObj->exists() ) {
147 $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
149 $wgTitle = $titleObj;
151 if ( isset( $prop['revid'] ) ) {
152 $oldid = $titleObj->getLatestRevID();
155 // Potentially cached
156 $p_result = $this->getParsedSectionOrText( $titleObj, $popts, $pageid,
157 isset( $prop['wikitext'] ) ) ;
159 } else { // Not $oldid, $pageid, $page. Hence based on $text
162 $titleObj = Title
::newFromText( $title );
164 $this->dieUsageMsg( array( 'invalidtitle', $title ) );
166 $wgTitle = $titleObj;
168 if ( $this->section
!== false ) {
169 $this->text
= $this->getSectionText( $this->text
, $titleObj->getText() );
172 if ( $params['pst'] ||
$params['onlypst'] ) {
173 $this->pstText
= $wgParser->preSaveTransform( $this->text
, $titleObj, $this->getUser(), $popts );
175 if ( $params['onlypst'] ) {
176 // Build a result and bail out
177 $result_array = array();
178 $result_array['text'] = array();
179 $result->setContent( $result_array['text'], $this->pstText
);
180 if ( isset( $prop['wikitext'] ) ) {
181 $result_array['wikitext'] = array();
182 $result->setContent( $result_array['wikitext'], $this->text
);
184 $result->addValue( null, $this->getModuleName(), $result_array );
187 // Not cached (save or load)
188 $p_result = $wgParser->parse( $params['pst'] ?
$this->pstText
: $this->text
, $titleObj, $popts );
191 $result_array = array();
193 $result_array['title'] = $titleObj->getPrefixedText();
195 if ( !is_null( $oldid ) ) {
196 $result_array['revid'] = intval( $oldid );
199 if ( $params['redirects'] && !is_null( $redirValues ) ) {
200 $result_array['redirects'] = $redirValues;
203 if ( isset( $prop['text'] ) ) {
204 $result_array['text'] = array();
205 $result->setContent( $result_array['text'], $p_result->getText() );
208 if ( !is_null( $params['summary'] ) ) {
209 $result_array['parsedsummary'] = array();
210 $result->setContent( $result_array['parsedsummary'], Linker
::formatComment( $params['summary'], $titleObj ) );
213 if ( isset( $prop['langlinks'] ) ) {
214 $result_array['langlinks'] = $this->formatLangLinks( $p_result->getLanguageLinks() );
216 if ( isset( $prop['languageshtml'] ) ) {
217 $languagesHtml = $this->languagesHtml( $p_result->getLanguageLinks() );
218 $result_array['languageshtml'] = array();
219 $result->setContent( $result_array['languageshtml'], $languagesHtml );
221 if ( isset( $prop['categories'] ) ) {
222 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
224 if ( isset( $prop['categorieshtml'] ) ) {
225 $categoriesHtml = $this->categoriesHtml( $p_result->getCategories() );
226 $result_array['categorieshtml'] = array();
227 $result->setContent( $result_array['categorieshtml'], $categoriesHtml );
229 if ( isset( $prop['links'] ) ) {
230 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
232 if ( isset( $prop['templates'] ) ) {
233 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
235 if ( isset( $prop['images'] ) ) {
236 $result_array['images'] = array_keys( $p_result->getImages() );
238 if ( isset( $prop['externallinks'] ) ) {
239 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
241 if ( isset( $prop['sections'] ) ) {
242 $result_array['sections'] = $p_result->getSections();
245 if ( isset( $prop['displaytitle'] ) ) {
246 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
247 $p_result->getDisplayTitle() :
248 $titleObj->getPrefixedText();
251 if ( isset( $prop['headitems'] ) ||
isset( $prop['headhtml'] ) ) {
252 $context = $this->getContext();
253 $context->setTitle( $titleObj );
254 $context->getOutput()->addParserOutputNoText( $p_result );
256 if ( isset( $prop['headitems'] ) ) {
257 $headItems = $this->formatHeadItems( $p_result->getHeadItems() );
259 $css = $this->formatCss( $context->getOutput()->buildCssLinksArray() );
261 $scripts = array( $context->getOutput()->getHeadScripts() );
263 $result_array['headitems'] = array_merge( $headItems, $css, $scripts );
266 if ( isset( $prop['headhtml'] ) ) {
267 $result_array['headhtml'] = array();
268 $result->setContent( $result_array['headhtml'], $context->getOutput()->headElement( $context->getSkin() ) );
272 if ( isset( $prop['iwlinks'] ) ) {
273 $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
276 if ( isset( $prop['wikitext'] ) ) {
277 $result_array['wikitext'] = array();
278 $result->setContent( $result_array['wikitext'], $this->text
);
279 if ( !is_null( $this->pstText
) ) {
280 $result_array['psttext'] = array();
281 $result->setContent( $result_array['psttext'], $this->pstText
);
285 $result_mapping = array(
288 'categories' => 'cl',
292 'externallinks' => 'el',
297 $this->setIndexedTagNames( $result_array, $result_mapping );
298 $result->addValue( null, $this->getModuleName(), $result_array );
300 if ( !is_null( $oldLang ) ) {
301 $wgLang = $oldLang; // Reset $wgLang to $oldLang
306 * @param $titleObj Title
307 * @param $popts ParserOptions
309 * @param $getWikitext Bool
310 * @return ParserOutput
312 private function getParsedSectionOrText( $titleObj, $popts, $pageId = null, $getWikitext = false ) {
315 $page = WikiPage
::factory( $titleObj );
317 if ( $this->section
!== false ) {
318 $this->text
= $this->getSectionText( $page->getRawText(), !is_null( $pageId )
319 ?
'page id ' . $pageId : $titleObj->getText() );
321 // Not cached (save or load)
322 return $wgParser->parse( $this->text
, $titleObj, $popts );
324 // Try the parser cache first
325 // getParserOutput will save to Parser cache if able
326 $pout = $page->getParserOutput( $popts );
327 if ( $getWikitext ) {
328 $this->text
= $page->getRawText();
334 private function getSectionText( $text, $what ) {
336 // Not cached (save or load)
337 $text = $wgParser->getSection( $text, $this->section
, false );
338 if ( $text === false ) {
339 $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
344 private function formatLangLinks( $links ) {
346 foreach ( $links as $link ) {
348 $bits = explode( ':', $link, 2 );
349 $title = Title
::newFromText( $link );
351 $entry['lang'] = $bits[0];
353 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
);
355 $this->getResult()->setContent( $entry, $bits[1] );
361 private function formatCategoryLinks( $links ) {
363 foreach ( $links as $link => $sortkey ) {
365 $entry['sortkey'] = $sortkey;
366 $this->getResult()->setContent( $entry, $link );
372 private function categoriesHtml( $categories ) {
373 $context = $this->getContext();
374 $context->getOutput()->addCategoryLinks( $categories );
375 return $context->getSkin()->getCategories();
379 * @deprecated since 1.18 No modern skin generates language links this way, please use language links
380 * data to generate your own HTML.
381 * @param $languages array
384 private function languagesHtml( $languages ) {
385 global $wgContLang, $wgHideInterlanguageLinks;
387 if ( $wgHideInterlanguageLinks ||
count( $languages ) == 0 ) {
391 $s = htmlspecialchars( wfMsg( 'otherlanguages' ) . wfMsg( 'colon-separator' ) );
394 foreach ( $languages as $l ) {
395 $nt = Title
::newFromText( $l );
396 $text = $wgContLang->getLanguageName( $nt->getInterwiki() );
398 $langs[] = Html
::element( 'a',
399 array( 'href' => $nt->getFullURL(), 'title' => $nt->getText(), 'class' => "external" ),
400 $text == '' ?
$l : $text );
403 $s .= implode( htmlspecialchars( wfMsgExt( 'pipe-separator', 'escapenoentities' ) ), $langs );
405 if ( $wgContLang->isRTL() ) {
406 $s = Html
::rawElement( 'span', array( 'dir' => "LTR" ), $s );
412 private function formatLinks( $links ) {
414 foreach ( $links as $ns => $nslinks ) {
415 foreach ( $nslinks as $title => $id ) {
418 $this->getResult()->setContent( $entry, Title
::makeTitle( $ns, $title )->getFullText() );
420 $entry['exists'] = '';
428 private function formatIWLinks( $iw ) {
430 foreach ( $iw as $prefix => $titles ) {
431 foreach ( array_keys( $titles ) as $title ) {
433 $entry['prefix'] = $prefix;
435 $title = Title
::newFromText( "{$prefix}:{$title}" );
437 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
);
440 $this->getResult()->setContent( $entry, $title->getFullText() );
447 private function formatHeadItems( $headItems ) {
449 foreach ( $headItems as $tag => $content ) {
451 $entry['tag'] = $tag;
452 $this->getResult()->setContent( $entry, $content );
458 private function formatCss( $css ) {
460 foreach ( $css as $file => $link ) {
462 $entry['file'] = $file;
463 $this->getResult()->setContent( $entry, $link );
469 private function setIndexedTagNames( &$array, $mapping ) {
470 foreach ( $mapping as $key => $name ) {
471 if ( isset( $array[$key] ) ) {
472 $this->getResult()->setIndexedTagName( $array[$key], $name );
477 public function getAllowedParams() {
480 ApiBase
::PARAM_DFLT
=> 'API',
486 ApiBase
::PARAM_TYPE
=> 'integer',
488 'redirects' => false,
490 ApiBase
::PARAM_TYPE
=> 'integer',
493 ApiBase
::PARAM_DFLT
=> 'text|langlinks|categories|links|templates|images|externallinks|sections|revid|displaytitle',
494 ApiBase
::PARAM_ISMULTI
=> true,
495 ApiBase
::PARAM_TYPE
=> array(
518 'disablepp' => false,
522 public function getParamDescription() {
523 $p = $this->getModulePrefix();
525 'text' => 'Wikitext to parse',
526 'summary' => 'Summary to parse',
527 'redirects' => "If the {$p}page or the {$p}pageid parameter is set to a redirect, resolve it",
528 'title' => 'Title of page the text belongs to',
529 'page' => "Parse the content of this page. Cannot be used together with {$p}text and {$p}title",
530 'pageid' => "Parse the content of this page. Overrides {$p}page",
531 'oldid' => "Parse the content of this revision. Overrides {$p}page and {$p}pageid",
533 'Which pieces of information to get',
534 ' text - Gives the parsed text of the wikitext',
535 ' langlinks - Gives the language links in the parsed wikitext',
536 ' categories - Gives the categories in the parsed wikitext',
537 ' categorieshtml - Gives the HTML version of the categories',
538 ' languageshtml - Gives the HTML version of the language links',
539 ' links - Gives the internal links in the parsed wikitext',
540 ' templates - Gives the templates in the parsed wikitext',
541 ' images - Gives the images in the parsed wikitext',
542 ' externallinks - Gives the external links in the parsed wikitext',
543 ' sections - Gives the sections in the parsed wikitext',
544 ' revid - Adds the revision ID of the parsed page',
545 ' displaytitle - Adds the title of the parsed wikitext',
546 ' headitems - Gives items to put in the <head> of the page',
547 ' headhtml - Gives parsed <head> of the page',
548 ' iwlinks - Gives interwiki links in the parsed wikitext',
549 ' wikitext - Gives the original wikitext that was parsed',
552 'Do a pre-save transform on the input before parsing it',
553 'Ignored if page, pageid or oldid is used'
556 'Do a pre-save transform (PST) on the input, but don\'t parse it',
557 'Returns the same wikitext, after a PST has been applied. Ignored if page, pageid or oldid is used'
559 'uselang' => 'Which language to parse the request in',
560 'section' => 'Only retrieve the content of this section number',
561 'disablepp' => 'Disable the PP Report from the parser output',
565 public function getDescription() {
566 return 'Parses wikitext and returns parser output';
569 public function getPossibleErrors() {
570 return array_merge( parent
::getPossibleErrors(), array(
571 array( 'code' => 'params', 'info' => 'The page parameter cannot be used together with the text and title parameters' ),
572 array( 'code' => 'missingrev', 'info' => 'There is no revision ID oldid' ),
573 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revisions' ),
574 array( 'code' => 'missingtitle', 'info' => 'The page you specified doesn\'t exist' ),
575 array( 'code' => 'nosuchsection', 'info' => 'There is no section sectionnumber in page' ),
576 array( 'nosuchpageid' ),
577 array( 'invalidtitle', 'title' ),
581 public function getExamples() {
583 'api.php?action=parse&text={{Project:Sandbox}}'
587 public function getHelpUrls() {
588 return 'http://www.mediawiki.org/wiki/API:Parsing_wikitext#parse';
591 public function getVersion() {
592 return __CLASS__
. ': $Id$';