5618f972106b713298e970f2ec56b7994f815fe1
[lhc/web/wiklou.git] / includes / api / ApiParse.php
1 <?php
2
3 /**
4 * Created on Dec 01, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
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.
14 *
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.
19 *
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
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( "ApiBase.php" );
29 }
30
31 /**
32 * @ingroup API
33 */
34 class ApiParse extends ApiBase {
35
36 public function __construct( $main, $action ) {
37 parent::__construct( $main, $action );
38 }
39
40 public function execute() {
41 // Get parameters
42 $params = $this->extractRequestParams();
43 $text = $params['text'];
44 $title = $params['title'];
45 $page = $params['page'];
46 $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' );
49 }
50 $prop = array_flip( $params['prop'] );
51 $revid = false;
52
53 // The parser needs $wgTitle to be set, apparently the
54 // $title parameter in Parser::parse isn't enough *sigh*
55 global $wgParser, $wgUser, $wgTitle, $wgEnableParserCache, $wgLang;
56
57 // Current unncessary, code to act as a safeguard against any change in current behaviour of uselang breaks
58 $oldLang = null;
59 if ( isset( $params['uselang'] ) && $params['uselang'] != $wgLang->getCode() ) {
60 $oldLang = $wgLang; // Backup wgLang
61 $wgLang = Language::factory( $params['uselang'] );
62 }
63
64 $popts = new ParserOptions();
65 $popts->setTidy( true );
66 $popts->enableLimitReport();
67 $redirValues = null;
68 if ( !is_null( $oldid ) || !is_null( $page ) ) {
69 if ( !is_null( $oldid ) ) {
70 // Don't use the parser cache
71 $rev = Revision::newFromID( $oldid );
72 if ( !$rev ) {
73 $this->dieUsage( "There is no revision ID $oldid", 'missingrev' );
74 }
75 if ( !$rev->userCan( Revision::DELETED_TEXT ) ) {
76 $this->dieUsage( "You don't have permission to view deleted revisions", 'permissiondenied' );
77 }
78
79 $text = $rev->getText( Revision::FOR_THIS_USER );
80 $titleObj = $rev->getTitle();
81 $wgTitle = $titleObj;
82 $p_result = $wgParser->parse( $text, $titleObj, $popts );
83 } else {
84 if ( $params['redirects'] ) {
85 $req = new FauxRequest( array(
86 'action' => 'query',
87 'redirects' => '',
88 'titles' => $page
89 ) );
90 $main = new ApiMain( $req );
91 $main->execute();
92 $data = $main->getResultData();
93 $redirValues = @$data['query']['redirects'];
94 $to = $page;
95 foreach ( (array)$redirValues as $r ) {
96 $to = $r['to'];
97 }
98 } else {
99 $to = $page;
100 }
101 $titleObj = Title::newFromText( $to );
102 if ( !$titleObj ) {
103 $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
104 }
105
106 $articleObj = new Article( $titleObj );
107 if ( isset( $prop['revid'] ) ) {
108 $oldid = $articleObj->getRevIdFetched();
109 }
110 // Try the parser cache first
111 $p_result = false;
112 $pcache = ParserCache::singleton();
113 if ( $wgEnableParserCache ) {
114 $p_result = $pcache->get( $articleObj, $wgUser );
115 }
116 if ( !$p_result ) {
117 $p_result = $wgParser->parse( $articleObj->getContent(), $titleObj, $popts );
118
119 if ( $wgEnableParserCache ) {
120 $pcache->save( $p_result, $articleObj, $popts );
121 }
122 }
123 }
124 } else {
125 $titleObj = Title::newFromText( $title );
126 if ( !$titleObj ) {
127 $titleObj = Title::newFromText( 'API' );
128 }
129 $wgTitle = $titleObj;
130 if ( $params['pst'] || $params['onlypst'] ) {
131 $text = $wgParser->preSaveTransform( $text, $titleObj, $wgUser, $popts );
132 }
133 if ( $params['onlypst'] ) {
134 // Build a result and bail out
135 $result_array['text'] = array();
136 $this->getResult()->setContent( $result_array['text'], $text );
137 $this->getResult()->addValue( null, $this->getModuleName(), $result_array );
138 return;
139 }
140 $p_result = $wgParser->parse( $text, $titleObj, $popts );
141 }
142
143 // Return result
144 $result = $this->getResult();
145 $result_array = array();
146 if ( $params['redirects'] && !is_null( $redirValues ) ) {
147 $result_array['redirects'] = $redirValues;
148 }
149
150 if ( isset( $prop['text'] ) ) {
151 $result_array['text'] = array();
152 $result->setContent( $result_array['text'], $p_result->getText() );
153 }
154
155 if ( !is_null( $params['summary'] ) ) {
156 $result_array['parsedsummary'] = array();
157 $result->setContent( $result_array['parsedsummary'], $wgUser->getSkin()->formatComment( $params['summary'], $titleObj ) );
158 }
159
160 if ( isset( $prop['langlinks'] ) ) {
161 $result_array['langlinks'] = $this->formatLangLinks( $p_result->getLanguageLinks() );
162 }
163 if ( isset( $prop['categories'] ) ) {
164 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
165 }
166 if ( isset( $prop['links'] ) ) {
167 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
168 }
169 if ( isset( $prop['templates'] ) ) {
170 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
171 }
172 if ( isset( $prop['images'] ) ) {
173 $result_array['images'] = array_keys( $p_result->getImages() );
174 }
175 if ( isset( $prop['externallinks'] ) ) {
176 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
177 }
178 if ( isset( $prop['sections'] ) ) {
179 $result_array['sections'] = $p_result->getSections();
180 }
181 if ( isset( $prop['displaytitle'] ) ) {
182 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
183 $p_result->getDisplayTitle() :
184 $titleObj->getPrefixedText();
185 }
186
187 if ( isset( $prop['headitems'] ) ) {
188 $result_array['headitems'] = $this->formatHeadItems( $p_result->getHeadItems() );
189 }
190
191 if ( isset( $prop['headhtml'] ) ) {
192 $out = new OutputPage;
193 $out->addParserOutputNoText( $p_result );
194 $result_array['headhtml'] = array();
195 $result->setContent( $result_array['headhtml'], $out->headElement( $wgUser->getSkin() ) );
196 }
197
198 if ( !is_null( $oldid ) ) {
199 $result_array['revid'] = intval( $oldid );
200 }
201
202 $result_mapping = array(
203 'redirects' => 'r',
204 'langlinks' => 'll',
205 'categories' => 'cl',
206 'links' => 'pl',
207 'templates' => 'tl',
208 'images' => 'img',
209 'externallinks' => 'el',
210 'sections' => 's',
211 'headitems' => 'hi'
212 );
213 $this->setIndexedTagNames( $result_array, $result_mapping );
214 $result->addValue( null, $this->getModuleName(), $result_array );
215
216 if ( !is_null( $oldLang ) ) {
217 $wgLang = $oldLang; // Reset $wgLang to $oldLang
218 }
219 }
220
221 private function formatLangLinks( $links ) {
222 $result = array();
223 foreach ( $links as $link ) {
224 $entry = array();
225 $bits = explode( ':', $link, 2 );
226 $entry['lang'] = $bits[0];
227 $this->getResult()->setContent( $entry, $bits[1] );
228 $result[] = $entry;
229 }
230 return $result;
231 }
232
233 private function formatCategoryLinks( $links ) {
234 $result = array();
235 foreach ( $links as $link => $sortkey ) {
236 $entry = array();
237 $entry['sortkey'] = $sortkey;
238 $this->getResult()->setContent( $entry, $link );
239 $result[] = $entry;
240 }
241 return $result;
242 }
243
244 private function formatLinks( $links ) {
245 $result = array();
246 foreach ( $links as $ns => $nslinks ) {
247 foreach ( $nslinks as $title => $id ) {
248 $entry = array();
249 $entry['ns'] = $ns;
250 $this->getResult()->setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() );
251 if ( $id != 0 ) {
252 $entry['exists'] = '';
253 }
254 $result[] = $entry;
255 }
256 }
257 return $result;
258 }
259
260 private function formatHeadItems( $headItems ) {
261 $result = array();
262 foreach ( $headItems as $tag => $content ) {
263 $entry = array();
264 $entry['tag'] = $tag;
265 $this->getResult()->setContent( $entry, $content );
266 $result[] = $entry;
267 }
268 return $result;
269 }
270
271 private function setIndexedTagNames( &$array, $mapping ) {
272 foreach ( $mapping as $key => $name ) {
273 if ( isset( $array[$key] ) ) {
274 $this->getResult()->setIndexedTagName( $array[$key], $name );
275 }
276 }
277 }
278
279 public function getAllowedParams() {
280 return array(
281 'title' => array(
282 ApiBase::PARAM_DFLT => 'API',
283 ),
284 'text' => null,
285 'summary' => null,
286 'page' => null,
287 'redirects' => false,
288 'oldid' => null,
289 'prop' => array(
290 ApiBase::PARAM_DFLT => 'text|langlinks|categories|links|templates|images|externallinks|sections|revid|displaytitle',
291 ApiBase::PARAM_ISMULTI => true,
292 ApiBase::PARAM_TYPE => array(
293 'text',
294 'langlinks',
295 'categories',
296 'links',
297 'templates',
298 'images',
299 'externallinks',
300 'sections',
301 'revid',
302 'displaytitle',
303 'headitems',
304 'headhtml'
305 )
306 ),
307 'pst' => false,
308 'onlypst' => false,
309 );
310 }
311
312 public function getParamDescription() {
313 return array(
314 'text' => 'Wikitext to parse.',
315 'summary' => 'Summary to parse.',
316 'redirects' => 'If the page parameter is set to a redirect, resolve it.',
317 'title' => 'Title of page the text belongs to.',
318 'page' => 'Parse the content of this page. Cannot be used together with text and title.',
319 'oldid' => 'Parse the content of this revision. Overrides page.',
320 'prop' => array( 'Which pieces of information to get.',
321 'NOTE: Section tree is only generated if there are more than 4 sections, or if the __TOC__ keyword is present.'
322 ),
323 'pst' => array( 'Do a pre-save transform on the input before parsing it.',
324 'Ignored if page or oldid is used.'
325 ),
326 'onlypst' => array( 'Do a PST on the input, but don\'t parse it.',
327 'Returns PSTed wikitext. Ignored if page or oldid is used.'
328 ),
329 'uselang' => 'Which language to parse the request in.'
330 );
331 }
332
333 public function getDescription() {
334 return 'This module parses wikitext and returns parser output';
335 }
336
337 public function getPossibleErrors() {
338 return array_merge( parent::getPossibleErrors(), array(
339 array( 'code' => 'params', 'info' => 'The page parameter cannot be used together with the text and title parameters' ),
340 array( 'code' => 'missingrev', 'info' => 'There is no revision ID oldid' ),
341 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revisions' ),
342 array( 'code' => 'missingtitle', 'info' => 'The page you specified doesn\'t exist' ),
343 ) );
344 }
345
346 protected function getExamples() {
347 return array(
348 'api.php?action=parse&text={{Project:Sandbox}}'
349 );
350 }
351
352 public function getVersion() {
353 return __CLASS__ . ': $Id$';
354 }
355 }