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
{
37 public function __construct( $main, $action ) {
38 parent
::__construct( $main, $action );
41 public function execute() {
42 // The data is hot but user-dependent, like page views, so we set vary cookies
43 $this->getMain()->setCacheMode( 'anon-public-user-private' );
46 $params = $this->extractRequestParams();
47 $text = $params['text'];
48 $title = $params['title'];
49 $page = $params['page'];
50 $pageid = $params['pageid'];
51 $oldid = $params['oldid'];
53 if ( !is_null( $page ) && ( !is_null( $text ) ||
$title != 'API' ) ) {
54 $this->dieUsage( 'The page parameter cannot be used together with the text and title parameters', 'params' );
56 $prop = array_flip( $params['prop'] );
58 if ( isset( $params['section'] ) ) {
59 $this->section
= $params['section'];
61 $this->section
= false;
64 // The parser needs $wgTitle to be set, apparently the
65 // $title parameter in Parser::parse isn't enough *sigh*
66 global $wgParser, $wgUser, $wgTitle, $wgEnableParserCache, $wgLang;
68 // Currently unnecessary, code to act as a safeguard against any change in current behaviour of uselang breaks
70 if ( isset( $params['uselang'] ) && $params['uselang'] != $wgLang->getCode() ) {
71 $oldLang = $wgLang; // Backup wgLang
72 $wgLang = Language
::factory( $params['uselang'] );
75 $popts = new ParserOptions();
76 $popts->setTidy( true );
77 $popts->enableLimitReport( !$params['disablepp'] );
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 $text = $rev->getText( Revision
::FOR_THIS_USER
);
91 $titleObj = $rev->getTitle();
94 if ( $this->section
!== false ) {
95 $text = $this->getSectionText( $text, 'r' . $rev );
98 $p_result = $wgParser->parse( $text, $titleObj, $popts );
100 if ( !is_null ( $pageid ) ) {
101 $titleObj = Title
::newFromID( $pageid );
104 $this->dieUsageMsg( array( 'nosuchpageid', $pageid ) );
107 if ( $params['redirects'] ) {
108 $req = new FauxRequest( array(
113 $main = new ApiMain( $req );
115 $data = $main->getResultData();
116 $redirValues = @$data['query']['redirects'];
118 foreach ( (array)$redirValues as $r ) {
124 $titleObj = Title
::newFromText( $to );
126 $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
129 $wgTitle = $titleObj;
131 $articleObj = new Article( $titleObj );
132 if ( isset( $prop['revid'] ) ) {
133 $oldid = $articleObj->getRevIdFetched();
136 if ( $this->section
!== false ) {
137 $text = $this->getSectionText( $text, !is_null ( $pageid ) ?
'page id ' . $pageid : $titleObj->getText() );
138 $p_result = $wgParser->parse( $text, $titleObj, $popts );
140 // Try the parser cache first
142 $pcache = ParserCache
::singleton();
143 if ( $wgEnableParserCache ) {
144 $p_result = $pcache->get( $articleObj, $popts );
147 $p_result = $wgParser->parse( $articleObj->getContent(), $titleObj, $popts );
149 if ( $wgEnableParserCache ) {
150 $pcache->save( $p_result, $articleObj, $popts );
156 $titleObj = Title
::newFromText( $title );
158 $titleObj = Title
::newFromText( 'API' );
160 $wgTitle = $titleObj;
162 if ( $this->section
!== false ) {
163 $text = $this->getSectionText( $text, $titleObj->getText() );
166 if ( $params['pst'] ||
$params['onlypst'] ) {
167 $text = $wgParser->preSaveTransform( $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'], $text );
173 $this->getResult()->addValue( null, $this->getModuleName(), $result_array );
176 $p_result = $wgParser->parse( $text, $titleObj, $popts );
180 $result = $this->getResult();
181 $result_array = array();
182 if ( $params['redirects'] && !is_null( $redirValues ) ) {
183 $result_array['redirects'] = $redirValues;
186 if ( isset( $prop['text'] ) ) {
187 $result_array['text'] = array();
188 $result->setContent( $result_array['text'], $p_result->getText() );
191 if ( !is_null( $params['summary'] ) ) {
192 $result_array['parsedsummary'] = array();
193 $result->setContent( $result_array['parsedsummary'], $wgUser->getSkin()->formatComment( $params['summary'], $titleObj ) );
196 if ( isset( $prop['langlinks'] ) ) {
197 $result_array['langlinks'] = $this->formatLangLinks( $p_result->getLanguageLinks() );
199 if ( isset( $prop['categories'] ) ) {
200 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
202 if ( isset( $prop['links'] ) ) {
203 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
205 if ( isset( $prop['templates'] ) ) {
206 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
208 if ( isset( $prop['images'] ) ) {
209 $result_array['images'] = array_keys( $p_result->getImages() );
211 if ( isset( $prop['externallinks'] ) ) {
212 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
214 if ( isset( $prop['sections'] ) ) {
215 $result_array['sections'] = $p_result->getSections();
218 if ( isset( $prop['displaytitle'] ) ) {
219 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
220 $p_result->getDisplayTitle() :
221 $titleObj->getPrefixedText();
224 if ( isset( $prop['headitems'] ) ||
isset( $prop['headhtml'] ) ) {
225 $out = new OutputPage
;
226 $out->addParserOutputNoText( $p_result );
227 $userSkin = $wgUser->getSkin();
230 if ( isset( $prop['headitems'] ) ) {
231 $headItems = $this->formatHeadItems( $p_result->getHeadItems() );
233 $userSkin->setupUserCss( $out );
234 $css = $this->formatCss( $out->buildCssLinksArray() );
236 $scripts = array( $out->getHeadScripts( $userSkin ) );
238 $result_array['headitems'] = array_merge( $headItems , $css, $scripts );
241 if ( isset( $prop['headhtml'] ) ) {
242 $result_array['headhtml'] = array();
243 $result->setContent( $result_array['headhtml'], $out->headElement( $userSkin ) );
246 if ( isset( $prop['iwlinks'] ) ) {
247 $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
250 if ( !is_null( $oldid ) ) {
251 $result_array['revid'] = intval( $oldid );
254 $result_mapping = array(
257 'categories' => 'cl',
261 'externallinks' => 'el',
266 $this->setIndexedTagNames( $result_array, $result_mapping );
267 $result->addValue( null, $this->getModuleName(), $result_array );
269 if ( !is_null( $oldLang ) ) {
270 $wgLang = $oldLang; // Reset $wgLang to $oldLang
274 private function getSectionText( $text, $what ) {
276 $text = $wgParser->getSection( $text, $this->section
, false );
277 if ( $text === false ) {
278 $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
283 private function formatLangLinks( $links ) {
285 foreach ( $links as $link ) {
287 $bits = explode( ':', $link, 2 );
288 $entry['lang'] = $bits[0];
289 $this->getResult()->setContent( $entry, $bits[1] );
295 private function formatCategoryLinks( $links ) {
297 foreach ( $links as $link => $sortkey ) {
299 $entry['sortkey'] = $sortkey;
300 $this->getResult()->setContent( $entry, $link );
306 private function formatLinks( $links ) {
308 foreach ( $links as $ns => $nslinks ) {
309 foreach ( $nslinks as $title => $id ) {
312 $this->getResult()->setContent( $entry, Title
::makeTitle( $ns, $title )->getFullText() );
314 $entry['exists'] = '';
322 private function formatIWLinks( $iw ) {
324 foreach ( $iw as $prefix => $titles ) {
325 foreach ( $titles as $title => $id ) {
327 $entry['prefix'] = $prefix;
329 $title = Title
::newFromText( "{$prefix}:{$title}" );
331 $entry['url'] = $title->getFullURL();
334 $this->getResult()->setContent( $entry, $title->getFullText() );
341 private function formatHeadItems( $headItems ) {
343 foreach ( $headItems as $tag => $content ) {
345 $entry['tag'] = $tag;
346 $this->getResult()->setContent( $entry, $content );
352 private function formatCss( $css ) {
354 foreach ( $css as $file => $link ) {
356 $entry['file'] = $file;
357 $this->getResult()->setContent( $entry, $link );
363 private function setIndexedTagNames( &$array, $mapping ) {
364 foreach ( $mapping as $key => $name ) {
365 if ( isset( $array[$key] ) ) {
366 $this->getResult()->setIndexedTagName( $array[$key], $name );
371 public function getAllowedParams() {
374 ApiBase
::PARAM_DFLT
=> 'API',
380 'redirects' => false,
383 ApiBase
::PARAM_DFLT
=> 'text|langlinks|categories|links|templates|images|externallinks|sections|revid|displaytitle',
384 ApiBase
::PARAM_ISMULTI
=> true,
385 ApiBase
::PARAM_TYPE
=> array(
405 'disablepp' => false,
409 public function getParamDescription() {
410 $p = $this->getModulePrefix();
412 'text' => 'Wikitext to parse',
413 'summary' => 'Summary to parse',
414 'redirects' => "If the {$p}page parameter is set to a redirect, resolve it",
415 'title' => 'Title of page the text belongs to',
416 'page' => "Parse the content of this page. Cannot be used together with {$p}text and {$p}title",
417 'pageid' => "Parse the content of this page. Overrides {$p}page",
418 'oldid' => "Parse the content of this revision. Overrides {$p}page and {$p}pageid",
420 'Which pieces of information to get',
421 ' text - Gives the parsed text of the wikitext',
422 ' langlinks - Gives the langlinks the parsed wikitext',
423 ' categories - Gives the categories of the parsed wikitext',
424 ' links - Gives the internal links in the parsed wikitext',
425 ' templates - Gives the templates in the parsed wikitext',
426 ' images - Gives the images in the parsed wikitext',
427 ' externallinks - Gives the external links in the parsed wikitext',
428 ' sections - Gives the sections in the parsed wikitext',
429 ' revid - Adds the revision id of the parsed page',
430 ' displaytitle - Adds the title of the parsed wikitext',
431 ' headitems - Gives items to put in the <head> of the page',
432 ' headhtml - Gives parsed <head> of the page',
433 ' iwlinks - Gives interwiki links in the parsed wikitext',
434 'NOTE: Section tree is only generated if there are more than 4 sections, or if the __TOC__ keyword is present'
437 'Do a pre-save transform on the input before parsing it',
438 'Ignored if page, pageid or oldid is used'
441 'Do a pre-save transform (PST) on the input, but don\'t parse it',
442 'Returns the same wikitext, after a PST has been applied. Ignored if page, pageid or oldid is used'
444 'uselang' => 'Which language to parse the request in',
445 'section' => 'Only retrieve the content of this section number',
446 'disablepp' => 'Disable the PP Report from the parser output',
450 public function getDescription() {
451 return 'This module parses wikitext and returns parser output';
454 public function getPossibleErrors() {
455 return array_merge( parent
::getPossibleErrors(), array(
456 array( 'code' => 'params', 'info' => 'The page parameter cannot be used together with the text and title parameters' ),
457 array( 'code' => 'missingrev', 'info' => 'There is no revision ID oldid' ),
458 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revisions' ),
459 array( 'code' => 'missingtitle', 'info' => 'The page you specified doesn\'t exist' ),
460 array( 'code' => 'nosuchsection', 'info' => 'There is no section sectionnumber in page' ),
461 array( 'nosuchpageid' ),
465 protected function getExamples() {
467 'api.php?action=parse&text={{Project:Sandbox}}'
471 public function getVersion() {
472 return __CLASS__
. ': $Id$';