677f60c6731ce56aecdf45139b1f525f8bce5dfb
4 * Created on Dec 01, 2007
6 * API for MediaWiki 1.8+
8 * Copyright © 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( "ApiBase.php" );
34 class ApiParse
extends ApiBase
{
36 public function __construct( $main, $action ) {
37 parent
::__construct( $main, $action );
40 public function execute() {
42 $params = $this->extractRequestParams();
43 $text = $params['text'];
44 $title = $params['title'];
45 $page = $params['page'];
46 $pageid = $params['pageid'];
47 $oldid = $params['oldid'];
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' );
52 $prop = array_flip( $params['prop'] );
55 if ( isset( $params['section'] ) ) {
56 $this->section
= $params['section'];
58 $this->section
= false;
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;
65 // Currently unncessary, code to act as a safeguard against any change in current behaviour of uselang breaks
67 if ( isset( $params['uselang'] ) && $params['uselang'] != $wgLang->getCode() ) {
68 $oldLang = $wgLang; // Backup wgLang
69 $wgLang = Language
::factory( $params['uselang'] );
72 $popts = new ParserOptions();
73 $popts->setTidy( true );
74 $popts->enableLimitReport();
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 );
81 $this->dieUsage( "There is no revision ID $oldid", 'missingrev' );
83 if ( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
84 $this->dieUsage( "You don't have permission to view deleted revisions", 'permissiondenied' );
87 $text = $rev->getText( Revision
::FOR_THIS_USER
);
88 $titleObj = $rev->getTitle();
91 if ( $this->section
!== false ) {
92 $text = $this->getSectionText( $text, 'r' . $rev );
95 $p_result = $wgParser->parse( $text, $titleObj, $popts );
97 if ( !is_null ( $pageid ) ) {
98 $titleObj = Title
::newFromID( $pageid );
101 $this->dieUsageMsg( array( 'nosuchpageid', $pageid ) );
104 if ( $params['redirects'] ) {
105 $req = new FauxRequest( array(
110 $main = new ApiMain( $req );
112 $data = $main->getResultData();
113 $redirValues = @$data['query']['redirects'];
115 foreach ( (array)$redirValues as $r ) {
121 $titleObj = Title
::newFromText( $to );
123 $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
126 $wgTitle = $titleObj;
128 $articleObj = new Article( $titleObj );
129 if ( isset( $prop['revid'] ) ) {
130 $oldid = $articleObj->getRevIdFetched();
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 );
137 // Try the parser cache first
139 $pcache = ParserCache
::singleton();
140 if ( $wgEnableParserCache ) {
141 $p_result = $pcache->get( $articleObj, $wgUser );
144 $p_result = $wgParser->parse( $articleObj->getContent(), $titleObj, $popts );
146 if ( $wgEnableParserCache ) {
147 $pcache->save( $p_result, $articleObj, $popts );
153 $titleObj = Title
::newFromText( $title );
155 $titleObj = Title
::newFromText( 'API' );
157 $wgTitle = $titleObj;
159 if ( $this->section
!== false ) {
160 $text = $this->getSectionText( $text, $titleObj->getText() );
163 if ( $params['pst'] ||
$params['onlypst'] ) {
164 $text = $wgParser->preSaveTransform( $text, $titleObj, $wgUser, $popts );
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 );
173 $p_result = $wgParser->parse( $text, $titleObj, $popts );
177 $result = $this->getResult();
178 $result_array = array();
179 if ( $params['redirects'] && !is_null( $redirValues ) ) {
180 $result_array['redirects'] = $redirValues;
183 if ( isset( $prop['text'] ) ) {
184 $result_array['text'] = array();
185 $result->setContent( $result_array['text'], $p_result->getText() );
188 if ( !is_null( $params['summary'] ) ) {
189 $result_array['parsedsummary'] = array();
190 $result->setContent( $result_array['parsedsummary'], $wgUser->getSkin()->formatComment( $params['summary'], $titleObj ) );
193 if ( isset( $prop['langlinks'] ) ) {
194 $result_array['langlinks'] = $this->formatLangLinks( $p_result->getLanguageLinks() );
196 if ( isset( $prop['categories'] ) ) {
197 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
199 if ( isset( $prop['links'] ) ) {
200 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
202 if ( isset( $prop['templates'] ) ) {
203 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
205 if ( isset( $prop['images'] ) ) {
206 $result_array['images'] = array_keys( $p_result->getImages() );
208 if ( isset( $prop['externallinks'] ) ) {
209 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
211 if ( isset( $prop['sections'] ) ) {
212 $result_array['sections'] = $p_result->getSections();
215 if ( isset( $prop['displaytitle'] ) ) {
216 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
217 $p_result->getDisplayTitle() :
218 $titleObj->getPrefixedText();
221 if ( isset( $prop['headitems'] ) ||
isset( $prop['headhtml'] ) ) {
222 $out = new OutputPage
;
223 $out->addParserOutputNoText( $p_result );
224 $userSkin = $wgUser->getSkin();
227 if ( isset( $prop['headitems'] ) ) {
228 $headItems = $this->formatHeadItems( $p_result->getHeadItems() );
230 $userSkin->setupUserCss( $out );
231 $css = $this->formatCss( $out->buildCssLinksArray() );
233 $scripts = array( $out->getHeadScripts( $userSkin ) );
235 $result_array['headitems'] = array_merge( $headItems , $css, $scripts );
238 if ( isset( $prop['headhtml'] ) ) {
239 $result_array['headhtml'] = array();
240 $result->setContent( $result_array['headhtml'], $out->headElement( $userSkin ) );
243 if ( isset( $prop['iwlinks'] ) ) {
244 $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
247 if ( !is_null( $oldid ) ) {
248 $result_array['revid'] = intval( $oldid );
251 $result_mapping = array(
254 'categories' => 'cl',
258 'externallinks' => 'el',
263 $this->setIndexedTagNames( $result_array, $result_mapping );
264 $result->addValue( null, $this->getModuleName(), $result_array );
266 if ( !is_null( $oldLang ) ) {
267 $wgLang = $oldLang; // Reset $wgLang to $oldLang
271 private function getSectionText( $text, $what ) {
273 $text = $wgParser->getSection( $text, $this->section
, false );
274 if ( $text === false ) {
275 $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
280 private function formatLangLinks( $links ) {
282 foreach ( $links as $link ) {
284 $bits = explode( ':', $link, 2 );
285 $entry['lang'] = $bits[0];
286 $this->getResult()->setContent( $entry, $bits[1] );
292 private function formatCategoryLinks( $links ) {
294 foreach ( $links as $link => $sortkey ) {
296 $entry['sortkey'] = $sortkey;
297 $this->getResult()->setContent( $entry, $link );
303 private function formatLinks( $links ) {
305 foreach ( $links as $ns => $nslinks ) {
306 foreach ( $nslinks as $title => $id ) {
309 $this->getResult()->setContent( $entry, Title
::makeTitle( $ns, $title )->getFullText() );
311 $entry['exists'] = '';
319 private function formatIWLinks( $iw ) {
321 foreach ( $iw as $prefix => $titles ) {
322 foreach ( $titles as $title => $id ) {
324 $entry['prefix'] = $prefix;
326 $title = Title
::newFromText( "{$prefix}:{$title}" );
328 $entry['url'] = $title->getFullURL();
331 $this->getResult()->setContent( $entry, $title->getFullText() );
338 private function formatHeadItems( $headItems ) {
340 foreach ( $headItems as $tag => $content ) {
342 $entry['tag'] = $tag;
343 $this->getResult()->setContent( $entry, $content );
349 private function formatCss( $css ) {
351 foreach ( $css as $file => $link ) {
353 $entry['file'] = $file;
354 $this->getResult()->setContent( $entry, $link );
360 private function setIndexedTagNames( &$array, $mapping ) {
361 foreach ( $mapping as $key => $name ) {
362 if ( isset( $array[$key] ) ) {
363 $this->getResult()->setIndexedTagName( $array[$key], $name );
368 public function getAllowedParams() {
371 ApiBase
::PARAM_DFLT
=> 'API',
377 'redirects' => false,
380 ApiBase
::PARAM_DFLT
=> 'text|langlinks|categories|links|templates|images|externallinks|sections|revid|displaytitle',
381 ApiBase
::PARAM_ISMULTI
=> true,
382 ApiBase
::PARAM_TYPE
=> array(
405 public function getParamDescription() {
406 $p = $this->getModulePrefix();
408 'text' => 'Wikitext to parse',
409 'summary' => 'Summary to parse',
410 'redirects' => "If the {$p}page parameter is set to a redirect, resolve it",
411 'title' => 'Title of page the text belongs to',
412 'page' => "Parse the content of this page. Cannot be used together with {$p}text and {$p}title",
413 'pageid' => "Parse the content of this page. Overrides {$p}page",
414 'oldid' => "Parse the content of this revision. Overrides {$p}page and {$p}pageid",
415 'prop' => array( 'Which pieces of information to get',
416 'NOTE: Section tree is only generated if there are more than 4 sections, or if the __TOC__ keyword is present'
418 'pst' => array( 'Do a pre-save transform on the input before parsing it',
419 'Ignored if page, pageid or oldid is used'
421 'onlypst' => array( 'Do a pre-save transform (PST) on the input, but don\'t parse it',
422 'Returns the same wikitext, after a PST has been applied. Ignored if page, pageid or oldid is used'
424 'uselang' => 'Which language to parse the request in',
425 'section' => 'Only retrieve the content of this section number',
429 public function getDescription() {
430 return 'This module parses wikitext and returns parser output';
433 public function getPossibleErrors() {
434 return array_merge( parent
::getPossibleErrors(), array(
435 array( 'code' => 'params', 'info' => 'The page parameter cannot be used together with the text and title parameters' ),
436 array( 'code' => 'missingrev', 'info' => 'There is no revision ID oldid' ),
437 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revisions' ),
438 array( 'code' => 'missingtitle', 'info' => 'The page you specified doesn\'t exist' ),
439 array( 'code' => 'nosuchsection', 'info' => 'There is no section sectionnumber in page'),
440 array( 'nosuchpageid' ),
444 protected function getExamples() {
446 'api.php?action=parse&text={{Project:Sandbox}}'
450 public function getVersion() {
451 return __CLASS__
. ': $Id$';