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