]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiParse.php
MediaWiki 1.15.3
[autoinstalls/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                 // 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;
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                                 $pcache = ParserCache::singleton();
102                                 $p_result = $pcache->get($articleObj, $wgUser);
103                                 if(!$p_result)
104                                 {
105                                         $p_result = $wgParser->parse($articleObj->getContent(), $titleObj, $popts);
106                                         global $wgUseParserCache;
107                                         if($wgUseParserCache)
108                                                 $pcache->save($p_result, $articleObj, $popts);
109                                 }
110                         }
111                 }
112                 else
113                 {
114                         $titleObj = Title::newFromText($title);
115                         if(!$titleObj)
116                                 $titleObj = Title::newFromText("API");
117                         $wgTitle = $titleObj;
118                         if($params['pst'] || $params['onlypst'])
119                                 $text = $wgParser->preSaveTransform($text, $titleObj, $wgUser, $popts);
120                         if($params['onlypst'])
121                         {
122                                 // Build a result and bail out
123                                 $result_array['text'] = array();
124                                 $this->getResult()->setContent($result_array['text'], $text);
125                                 $this->getResult()->addValue(null, $this->getModuleName(), $result_array);
126                                 return;
127                         }
128                         $p_result = $wgParser->parse($text, $titleObj, $popts);
129                 }
130
131                 // Return result
132                 $result = $this->getResult();
133                 $result_array = array();
134                 if($params['redirects'] && !is_null($redirValues))
135                         $result_array['redirects'] = $redirValues;
136                 if(isset($prop['text'])) {
137                         $result_array['text'] = array();
138                         $result->setContent($result_array['text'], $p_result->getText());
139                 }
140                 if(isset($prop['langlinks']))
141                         $result_array['langlinks'] = $this->formatLangLinks($p_result->getLanguageLinks());
142                 if(isset($prop['categories']))
143                         $result_array['categories'] = $this->formatCategoryLinks($p_result->getCategories());
144                 if(isset($prop['links']))
145                         $result_array['links'] = $this->formatLinks($p_result->getLinks());
146                 if(isset($prop['templates']))
147                         $result_array['templates'] = $this->formatLinks($p_result->getTemplates());
148                 if(isset($prop['images']))
149                         $result_array['images'] = array_keys($p_result->getImages());
150                 if(isset($prop['externallinks']))
151                         $result_array['externallinks'] = array_keys($p_result->getExternalLinks());
152                 if(isset($prop['sections']))
153                         $result_array['sections'] = $p_result->getSections();
154                 if(isset($prop['displaytitle']))
155                         $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
156                                                         $p_result->getDisplayTitle() :
157                                                         $titleObj->getPrefixedText();
158                 if(!is_null($oldid))
159                         $result_array['revid'] = intval($oldid);
160
161                 $result_mapping = array(
162                         'redirects' => 'r',
163                         'langlinks' => 'll',
164                         'categories' => 'cl',
165                         'links' => 'pl',
166                         'templates' => 'tl',
167                         'images' => 'img',
168                         'externallinks' => 'el',
169                         'sections' => 's',
170                 );
171                 $this->setIndexedTagNames( $result_array, $result_mapping );
172                 $result->addValue( null, $this->getModuleName(), $result_array );
173         }
174
175         private function formatLangLinks( $links ) {
176                 $result = array();
177                 foreach( $links as $link ) {
178                         $entry = array();
179                         $bits = split( ':', $link, 2 );
180                         $entry['lang'] = $bits[0];
181                         $this->getResult()->setContent( $entry, $bits[1] );
182                         $result[] = $entry;
183                 }
184                 return $result;
185         }
186
187         private function formatCategoryLinks( $links ) {
188                 $result = array();
189                 foreach( $links as $link => $sortkey ) {
190                         $entry = array();
191                         $entry['sortkey'] = $sortkey;
192                         $this->getResult()->setContent( $entry, $link );
193                         $result[] = $entry;
194                 }
195                 return $result;
196         }
197
198         private function formatLinks( $links ) {
199                 $result = array();
200                 foreach( $links as $ns => $nslinks ) {
201                         foreach( $nslinks as $title => $id ) {
202                                 $entry = array();
203                                 $entry['ns'] = $ns;
204                                 $this->getResult()->setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() );
205                                 if( $id != 0 )
206                                         $entry['exists'] = '';
207                                 $result[] = $entry;
208                         }
209                 }
210                 return $result;
211         }
212
213         private function setIndexedTagNames( &$array, $mapping ) {
214                 foreach( $mapping as $key => $name ) {
215                         if( isset( $array[$key] ) )
216                                 $this->getResult()->setIndexedTagName( $array[$key], $name );
217                 }
218         }
219
220         public function getAllowedParams() {
221                 return array (
222                         'title' => array(
223                                 ApiBase :: PARAM_DFLT => 'API',
224                         ),
225                         'text' => null,
226                         'page' => null,
227                         'redirects' => false,
228                         'oldid' => null,
229                         'prop' => array(
230                                 ApiBase :: PARAM_DFLT => 'text|langlinks|categories|links|templates|images|externallinks|sections|revid|displaytitle',
231                                 ApiBase :: PARAM_ISMULTI => true,
232                                 ApiBase :: PARAM_TYPE => array(
233                                         'text',
234                                         'langlinks',
235                                         'categories',
236                                         'links',
237                                         'templates',
238                                         'images',
239                                         'externallinks',
240                                         'sections',
241                                         'revid',
242                                         'displaytitle',
243                                 )
244                         ),
245                         'pst' => false,
246                         'onlypst' => false,
247                 );
248         }
249
250         public function getParamDescription() {
251                 return array (
252                         'text' => 'Wikitext to parse',
253                         'redirects' => 'If the page parameter is set to a redirect, resolve it',
254                         'title' => 'Title of page the text belongs to',
255                         'page' => 'Parse the content of this page. Cannot be used together with text and title',
256                         'oldid' => 'Parse the content of this revision. Overrides page',
257                         'prop' => array('Which pieces of information to get.',
258                                         'NOTE: Section tree is only generated if there are more than 4 sections, or if the __TOC__ keyword is present'
259                         ),
260                         'pst' => array( 'Do a pre-save transform on the input before parsing it.',
261                                         'Ignored if page or oldid is used.'
262                         ),
263                         'onlypst' => array('Do a PST on the input, but don\'t parse it.',
264                                         'Returns PSTed wikitext. Ignored if page or oldid is used.'
265                         ),
266                 );
267         }
268
269         public function getDescription() {
270                 return 'This module parses wikitext and returns parser output';
271         }
272
273         protected function getExamples() {
274                 return array (
275                         'api.php?action=parse&text={{Project:Sandbox}}'
276                 );
277         }
278
279         public function getVersion() {
280                 return __CLASS__ . ': $Id: ApiParse.php 48544 2009-03-18 23:27:48Z aboostani $';
281         }
282 }