Normalise comment usage (# --> //)
[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 (C) 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 $prop = array_flip( $params['prop'] );
50 $revid = false;
51
52 // The parser needs $wgTitle to be set, apparently the
53 // $title parameter in Parser::parse isn't enough *sigh*
54 global $wgParser, $wgUser, $wgTitle, $wgEnableParserCache;
55 $popts = new ParserOptions();
56 $popts->setTidy( true );
57 $popts->enableLimitReport();
58 $redirValues = null;
59 if ( !is_null( $oldid ) || !is_null( $page ) )
60 {
61 if ( !is_null( $oldid ) )
62 {
63 // Don't use the parser cache
64 $rev = Revision::newFromID( $oldid );
65 if ( !$rev )
66 $this->dieUsage( "There is no revision ID $oldid", 'missingrev' );
67 if ( !$rev->userCan( Revision::DELETED_TEXT ) )
68 $this->dieUsage( "You don't have permission to view deleted revisions", 'permissiondenied' );
69
70 $text = $rev->getText( Revision::FOR_THIS_USER );
71 $titleObj = $rev->getTitle();
72 $wgTitle = $titleObj;
73 $p_result = $wgParser->parse( $text, $titleObj, $popts );
74 }
75 else
76 {
77 if ( $params['redirects'] )
78 {
79 $req = new FauxRequest( array(
80 'action' => 'query',
81 'redirects' => '',
82 'titles' => $page
83 ) );
84 $main = new ApiMain( $req );
85 $main->execute();
86 $data = $main->getResultData();
87 $redirValues = @$data['query']['redirects'];
88 $to = $page;
89 foreach ( (array)$redirValues as $r )
90 $to = $r['to'];
91 }
92 else
93 $to = $page;
94 $titleObj = Title::newFromText( $to );
95 if ( !$titleObj )
96 $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
97
98 $articleObj = new Article( $titleObj );
99 if ( isset( $prop['revid'] ) )
100 $oldid = $articleObj->getRevIdFetched();
101 // Try the parser cache first
102 $p_result = false;
103 $pcache = ParserCache::singleton();
104 if ( $wgEnableParserCache )
105 $p_result = $pcache->get( $articleObj, $wgUser );
106 if ( !$p_result )
107 {
108 $p_result = $wgParser->parse( $articleObj->getContent(), $titleObj, $popts );
109
110 if ( $wgEnableParserCache )
111 $pcache->save( $p_result, $articleObj, $popts );
112 }
113 }
114 }
115 else
116 {
117 $titleObj = Title::newFromText( $title );
118 if ( !$titleObj )
119 $titleObj = Title::newFromText( "API" );
120 $wgTitle = $titleObj;
121 if ( $params['pst'] || $params['onlypst'] )
122 $text = $wgParser->preSaveTransform( $text, $titleObj, $wgUser, $popts );
123 if ( $params['onlypst'] )
124 {
125 // Build a result and bail out
126 $result_array['text'] = array();
127 $this->getResult()->setContent( $result_array['text'], $text );
128 $this->getResult()->addValue( null, $this->getModuleName(), $result_array );
129 return;
130 }
131 $p_result = $wgParser->parse( $text, $titleObj, $popts );
132 }
133
134 // Return result
135 $result = $this->getResult();
136 $result_array = array();
137 if ( $params['redirects'] && !is_null( $redirValues ) )
138 $result_array['redirects'] = $redirValues;
139 if ( isset( $prop['text'] ) ) {
140 $result_array['text'] = array();
141 $result->setContent( $result_array['text'], $p_result->getText() );
142 }
143 if ( isset( $prop['langlinks'] ) )
144 $result_array['langlinks'] = $this->formatLangLinks( $p_result->getLanguageLinks() );
145 if ( isset( $prop['categories'] ) )
146 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
147 if ( isset( $prop['links'] ) )
148 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
149 if ( isset( $prop['templates'] ) )
150 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
151 if ( isset( $prop['images'] ) )
152 $result_array['images'] = array_keys( $p_result->getImages() );
153 if ( isset( $prop['externallinks'] ) )
154 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
155 if ( isset( $prop['sections'] ) )
156 $result_array['sections'] = $p_result->getSections();
157 if ( isset( $prop['displaytitle'] ) )
158 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
159 $p_result->getDisplayTitle() :
160 $titleObj->getPrefixedText();
161
162 if ( isset( $prop['headitems'] ) )
163 $result_array['headitems'] = $this->formatHeadItems( $p_result->getHeadItems() );
164
165 if ( !is_null( $oldid ) )
166 $result_array['revid'] = intval( $oldid );
167
168 $result_mapping = array(
169 'redirects' => 'r',
170 'langlinks' => 'll',
171 'categories' => 'cl',
172 'links' => 'pl',
173 'templates' => 'tl',
174 'images' => 'img',
175 'externallinks' => 'el',
176 'sections' => 's',
177 'headitems' => 'hi'
178 );
179 $this->setIndexedTagNames( $result_array, $result_mapping );
180 $result->addValue( null, $this->getModuleName(), $result_array );
181 }
182
183 private function formatLangLinks( $links ) {
184 $result = array();
185 foreach ( $links as $link ) {
186 $entry = array();
187 $bits = explode( ':', $link, 2 );
188 $entry['lang'] = $bits[0];
189 $this->getResult()->setContent( $entry, $bits[1] );
190 $result[] = $entry;
191 }
192 return $result;
193 }
194
195 private function formatCategoryLinks( $links ) {
196 $result = array();
197 foreach ( $links as $link => $sortkey ) {
198 $entry = array();
199 $entry['sortkey'] = $sortkey;
200 $this->getResult()->setContent( $entry, $link );
201 $result[] = $entry;
202 }
203 return $result;
204 }
205
206 private function formatLinks( $links ) {
207 $result = array();
208 foreach ( $links as $ns => $nslinks ) {
209 foreach ( $nslinks as $title => $id ) {
210 $entry = array();
211 $entry['ns'] = $ns;
212 $this->getResult()->setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() );
213 if ( $id != 0 )
214 $entry['exists'] = '';
215 $result[] = $entry;
216 }
217 }
218 return $result;
219 }
220
221 private function formatHeadItems( $headItems ) {
222 $result = array();
223 foreach ( $headItems as $tag => $content ) {
224 $entry = array();
225 $entry['tag'] = $tag;
226 $this->getResult()->setContent( $entry, $content );
227 $result[] = $entry;
228 }
229 return $result;
230 }
231
232 private function setIndexedTagNames( &$array, $mapping ) {
233 foreach ( $mapping as $key => $name ) {
234 if ( isset( $array[$key] ) )
235 $this->getResult()->setIndexedTagName( $array[$key], $name );
236 }
237 }
238
239 public function getAllowedParams() {
240 return array (
241 'title' => array(
242 ApiBase :: PARAM_DFLT => 'API',
243 ),
244 'text' => null,
245 'page' => null,
246 'redirects' => false,
247 'oldid' => null,
248 'prop' => array(
249 ApiBase :: PARAM_DFLT => 'text|langlinks|categories|links|templates|images|externallinks|sections|revid|displaytitle',
250 ApiBase :: PARAM_ISMULTI => true,
251 ApiBase :: PARAM_TYPE => array(
252 'text',
253 'langlinks',
254 'categories',
255 'links',
256 'templates',
257 'images',
258 'externallinks',
259 'sections',
260 'revid',
261 'displaytitle',
262 'headitems'
263 )
264 ),
265 'pst' => false,
266 'onlypst' => false,
267 );
268 }
269
270 public function getParamDescription() {
271 return array (
272 'text' => 'Wikitext to parse',
273 'redirects' => 'If the page parameter is set to a redirect, resolve it',
274 'title' => 'Title of page the text belongs to',
275 'page' => 'Parse the content of this page. Cannot be used together with text and title',
276 'oldid' => 'Parse the content of this revision. Overrides page',
277 'prop' => array( 'Which pieces of information to get.',
278 'NOTE: Section tree is only generated if there are more than 4 sections, or if the __TOC__ keyword is present'
279 ),
280 'pst' => array( 'Do a pre-save transform on the input before parsing it.',
281 'Ignored if page or oldid is used.'
282 ),
283 'onlypst' => array( 'Do a PST on the input, but don\'t parse it.',
284 'Returns PSTed wikitext. Ignored if page or oldid is used.'
285 ),
286 );
287 }
288
289 public function getDescription() {
290 return 'This module parses wikitext and returns parser output';
291 }
292
293 protected function getExamples() {
294 return array (
295 'api.php?action=parse&text={{Project:Sandbox}}'
296 );
297 }
298
299 public function getVersion() {
300 return __CLASS__ . ': $Id$';
301 }
302 }