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