3 * API for MediaWiki 1.8+
5 * Created on Dec 01, 2007
7 * Copyright © 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiBase.php" );
35 class ApiParse
extends ApiBase
{
39 public function __construct( $main, $action ) {
40 parent
::__construct( $main, $action );
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' );
48 $params = $this->extractRequestParams();
49 $text = $params['text'];
50 $title = $params['title'];
51 $page = $params['page'];
52 $pageid = $params['pageid'];
53 $oldid = $params['oldid'];
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' );
58 $prop = array_flip( $params['prop'] );
60 if ( isset( $params['section'] ) ) {
61 $this->section
= $params['section'];
63 $this->section
= false;
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, $wgEnableParserCache, $wgLang;
70 // Currently unnecessary, code to act as a safeguard against any change in current behaviour of uselang breaks
72 if ( isset( $params['uselang'] ) && $params['uselang'] != $wgLang->getCode() ) {
73 $oldLang = $wgLang; // Backup wgLang
74 $wgLang = Language
::factory( $params['uselang'] );
77 $popts = new ParserOptions();
78 $popts->setTidy( true );
79 $popts->enableLimitReport( !$params['disablepp'] );
81 if ( !is_null( $oldid ) ||
!is_null( $pageid ) ||
!is_null( $page ) ) {
82 if ( !is_null( $oldid ) ) {
83 // Don't use the parser cache
84 $rev = Revision
::newFromID( $oldid );
86 $this->dieUsage( "There is no revision ID $oldid", 'missingrev' );
88 if ( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
89 $this->dieUsage( "You don't have permission to view deleted revisions", 'permissiondenied' );
92 $titleObj = $rev->getTitle();
96 //If for some reason the "oldid" is actually the current revision, it may be cached
97 if ( $titleObj->getLatestRevID() === $oldid ) {
99 $pcache = ParserCache
::singleton();
100 if ( $wgEnableParserCache ) {
101 $p_result = $pcache->get( $titleObj, $popts );
104 $text = $rev->getText( Revision
::FOR_THIS_USER
);
105 $p_result = $wgParser->parse( $text, $titleObj, $popts );
107 if ( $wgEnableParserCache ) {
108 $pcache->save( $p_result, $titleObj, $popts );
112 $text = $rev->getText( Revision
::FOR_THIS_USER
);
114 $wgTitle = $titleObj;
116 if ( $this->section
!== false ) {
117 $text = $this->getSectionText( $text, 'r' . $rev );
120 $p_result = $wgParser->parse( $text, $titleObj, $popts );
123 if ( !is_null ( $pageid ) ) {
124 $titleObj = Title
::newFromID( $pageid );
127 $this->dieUsageMsg( array( 'nosuchpageid', $pageid ) );
130 if ( $params['redirects'] ) {
131 $req = new FauxRequest( array(
136 $main = new ApiMain( $req );
138 $data = $main->getResultData();
139 $redirValues = @$data['query']['redirects'];
141 foreach ( (array)$redirValues as $r ) {
147 $titleObj = Title
::newFromText( $to );
149 $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
152 $wgTitle = $titleObj;
154 $articleObj = new Article( $titleObj );
155 if ( isset( $prop['revid'] ) ) {
156 $oldid = $articleObj->getRevIdFetched();
159 if ( $this->section
!== false ) {
160 $text = $this->getSectionText( $text, !is_null ( $pageid ) ?
'page id ' . $pageid : $titleObj->getText() );
161 $p_result = $wgParser->parse( $text, $titleObj, $popts );
163 // Try the parser cache first
165 $pcache = ParserCache
::singleton();
166 if ( $wgEnableParserCache ) {
167 $p_result = $pcache->get( $articleObj, $popts );
170 $p_result = $wgParser->parse( $articleObj->getContent(), $titleObj, $popts );
172 if ( $wgEnableParserCache ) {
173 $pcache->save( $p_result, $articleObj, $popts );
179 $titleObj = Title
::newFromText( $title );
181 $titleObj = Title
::newFromText( 'API' );
183 $wgTitle = $titleObj;
185 if ( $this->section
!== false ) {
186 $text = $this->getSectionText( $text, $titleObj->getText() );
189 if ( $params['pst'] ||
$params['onlypst'] ) {
190 $text = $wgParser->preSaveTransform( $text, $titleObj, $wgUser, $popts );
192 if ( $params['onlypst'] ) {
193 // Build a result and bail out
194 $result_array['text'] = array();
195 $this->getResult()->setContent( $result_array['text'], $text );
196 $this->getResult()->addValue( null, $this->getModuleName(), $result_array );
199 $p_result = $wgParser->parse( $text, $titleObj, $popts );
203 $result = $this->getResult();
204 $result_array = array();
205 if ( $params['redirects'] && !is_null( $redirValues ) ) {
206 $result_array['redirects'] = $redirValues;
209 if ( isset( $prop['text'] ) ) {
210 $result_array['text'] = array();
211 $result->setContent( $result_array['text'], $p_result->getText() );
214 if ( !is_null( $params['summary'] ) ) {
215 $result_array['parsedsummary'] = array();
216 $result->setContent( $result_array['parsedsummary'], $wgUser->getSkin()->formatComment( $params['summary'], $titleObj ) );
219 if ( isset( $prop['langlinks'] ) ) {
220 $result_array['langlinks'] = $this->formatLangLinks( $p_result->getLanguageLinks() );
222 if ( isset( $prop['categories'] ) ) {
223 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
225 if ( isset( $prop['links'] ) ) {
226 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
228 if ( isset( $prop['templates'] ) ) {
229 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
231 if ( isset( $prop['images'] ) ) {
232 $result_array['images'] = array_keys( $p_result->getImages() );
234 if ( isset( $prop['externallinks'] ) ) {
235 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
237 if ( isset( $prop['sections'] ) ) {
238 $result_array['sections'] = $p_result->getSections();
241 if ( isset( $prop['displaytitle'] ) ) {
242 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
243 $p_result->getDisplayTitle() :
244 $titleObj->getPrefixedText();
247 if ( isset( $prop['headitems'] ) ||
isset( $prop['headhtml'] ) ) {
248 $out = new OutputPage
;
249 $out->addParserOutputNoText( $p_result );
250 $userSkin = $wgUser->getSkin();
253 if ( isset( $prop['headitems'] ) ) {
254 $headItems = $this->formatHeadItems( $p_result->getHeadItems() );
256 $userSkin->setupUserCss( $out );
257 $css = $this->formatCss( $out->buildCssLinksArray() );
259 $scripts = array( $out->getHeadScripts( $userSkin ) );
261 $result_array['headitems'] = array_merge( $headItems, $css, $scripts );
264 if ( isset( $prop['headhtml'] ) ) {
265 $result_array['headhtml'] = array();
266 $result->setContent( $result_array['headhtml'], $out->headElement( $userSkin ) );
269 if ( isset( $prop['iwlinks'] ) ) {
270 $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
273 if ( !is_null( $oldid ) ) {
274 $result_array['revid'] = intval( $oldid );
277 $result_mapping = array(
280 'categories' => 'cl',
284 'externallinks' => 'el',
289 $this->setIndexedTagNames( $result_array, $result_mapping );
290 $result->addValue( null, $this->getModuleName(), $result_array );
292 if ( !is_null( $oldLang ) ) {
293 $wgLang = $oldLang; // Reset $wgLang to $oldLang
297 private function getSectionText( $text, $what ) {
299 $text = $wgParser->getSection( $text, $this->section
, false );
300 if ( $text === false ) {
301 $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
306 private function formatLangLinks( $links ) {
308 foreach ( $links as $link ) {
310 $bits = explode( ':', $link, 2 );
311 $entry['lang'] = $bits[0];
312 $this->getResult()->setContent( $entry, $bits[1] );
318 private function formatCategoryLinks( $links ) {
320 foreach ( $links as $link => $sortkey ) {
322 $entry['sortkey'] = $sortkey;
323 $this->getResult()->setContent( $entry, $link );
329 private function formatLinks( $links ) {
331 foreach ( $links as $ns => $nslinks ) {
332 foreach ( $nslinks as $title => $id ) {
335 $this->getResult()->setContent( $entry, Title
::makeTitle( $ns, $title )->getFullText() );
337 $entry['exists'] = '';
345 private function formatIWLinks( $iw ) {
347 foreach ( $iw as $prefix => $titles ) {
348 foreach ( array_keys( $titles ) as $title ) {
350 $entry['prefix'] = $prefix;
352 $title = Title
::newFromText( "{$prefix}:{$title}" );
354 $entry['url'] = $title->getFullURL();
357 $this->getResult()->setContent( $entry, $title->getFullText() );
364 private function formatHeadItems( $headItems ) {
366 foreach ( $headItems as $tag => $content ) {
368 $entry['tag'] = $tag;
369 $this->getResult()->setContent( $entry, $content );
375 private function formatCss( $css ) {
377 foreach ( $css as $file => $link ) {
379 $entry['file'] = $file;
380 $this->getResult()->setContent( $entry, $link );
386 private function setIndexedTagNames( &$array, $mapping ) {
387 foreach ( $mapping as $key => $name ) {
388 if ( isset( $array[$key] ) ) {
389 $this->getResult()->setIndexedTagName( $array[$key], $name );
394 public function getAllowedParams() {
397 ApiBase
::PARAM_DFLT
=> 'API',
403 'redirects' => false,
406 ApiBase
::PARAM_DFLT
=> 'text|langlinks|categories|links|templates|images|externallinks|sections|revid|displaytitle',
407 ApiBase
::PARAM_ISMULTI
=> true,
408 ApiBase
::PARAM_TYPE
=> array(
428 'disablepp' => false,
432 public function getParamDescription() {
433 $p = $this->getModulePrefix();
435 'text' => 'Wikitext to parse',
436 'summary' => 'Summary to parse',
437 'redirects' => "If the {$p}page parameter is set to a redirect, resolve it",
438 'title' => 'Title of page the text belongs to',
439 'page' => "Parse the content of this page. Cannot be used together with {$p}text and {$p}title",
440 'pageid' => "Parse the content of this page. Overrides {$p}page",
441 'oldid' => "Parse the content of this revision. Overrides {$p}page and {$p}pageid",
443 'Which pieces of information to get',
444 ' text - Gives the parsed text of the wikitext',
445 ' langlinks - Gives the langlinks the parsed wikitext',
446 ' categories - Gives the categories of the parsed wikitext',
447 ' links - Gives the internal links in the parsed wikitext',
448 ' templates - Gives the templates in the parsed wikitext',
449 ' images - Gives the images in the parsed wikitext',
450 ' externallinks - Gives the external links in the parsed wikitext',
451 ' sections - Gives the sections in the parsed wikitext',
452 ' revid - Adds the revision id of the parsed page',
453 ' displaytitle - Adds the title of the parsed wikitext',
454 ' headitems - Gives items to put in the <head> of the page',
455 ' headhtml - Gives parsed <head> of the page',
456 ' iwlinks - Gives interwiki links in the parsed wikitext',
457 'NOTE: Section tree is only generated if there are more than 4 sections, or if the __TOC__ keyword is present'
460 'Do a pre-save transform on the input before parsing it',
461 'Ignored if page, pageid or oldid is used'
464 'Do a pre-save transform (PST) on the input, but don\'t parse it',
465 'Returns the same wikitext, after a PST has been applied. Ignored if page, pageid or oldid is used'
467 'uselang' => 'Which language to parse the request in',
468 'section' => 'Only retrieve the content of this section number',
469 'disablepp' => 'Disable the PP Report from the parser output',
473 public function getDescription() {
474 return 'This module parses wikitext and returns parser output';
477 public function getPossibleErrors() {
478 return array_merge( parent
::getPossibleErrors(), array(
479 array( 'code' => 'params', 'info' => 'The page parameter cannot be used together with the text and title parameters' ),
480 array( 'code' => 'missingrev', 'info' => 'There is no revision ID oldid' ),
481 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revisions' ),
482 array( 'code' => 'missingtitle', 'info' => 'The page you specified doesn\'t exist' ),
483 array( 'code' => 'nosuchsection', 'info' => 'There is no section sectionnumber in page' ),
484 array( 'nosuchpageid' ),
488 protected function getExamples() {
490 'api.php?action=parse&text={{Project:Sandbox}}'
494 public function getVersion() {
495 return __CLASS__
. ': $Id$';