]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiParse.php
MediaWiki 1.15.5
[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                 // 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;
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                                 $text = $rev->getText( Revision::FOR_THIS_USER );
73                                 $titleObj = $rev->getTitle();
74                                 $wgTitle = $titleObj;
75                                 $p_result = $wgParser->parse($text, $titleObj, $popts);
76                         }
77                         else
78                         {
79                                 if($params['redirects'])
80                                 {
81                                         $req = new FauxRequest(array(
82                                                 'action' => 'query',
83                                                 'redirects' => '',
84                                                 'titles' => $page
85                                         ));
86                                         $main = new ApiMain($req);
87                                         $main->execute();
88                                         $data = $main->getResultData();
89                                         $redirValues = @$data['query']['redirects'];
90                                         $to = $page;
91                                         foreach((array)$redirValues as $r)
92                                                 $to = $r['to'];
93                                 }
94                                 else
95                                         $to = $page; 
96                                 $titleObj = Title::newFromText($to);
97                                 if(!$titleObj)
98                                         $this->dieUsage("The page you specified doesn't exist", 'missingtitle');
99
100                                 $articleObj = new Article($titleObj);
101                                 if(isset($prop['revid']))
102                                         $oldid = $articleObj->getRevIdFetched();
103                                 // Try the parser cache first
104                                 $pcache = ParserCache::singleton();
105                                 $p_result = $pcache->get($articleObj, $wgUser);
106                                 if(!$p_result)
107                                 {
108                                         $p_result = $wgParser->parse($articleObj->getContent(), $titleObj, $popts);
109                                         global $wgUseParserCache;
110                                         if($wgUseParserCache)
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                 if(!is_null($oldid))
162                         $result_array['revid'] = intval($oldid);
163
164                 $result_mapping = array(
165                         'redirects' => 'r',
166                         'langlinks' => 'll',
167                         'categories' => 'cl',
168                         'links' => 'pl',
169                         'templates' => 'tl',
170                         'images' => 'img',
171                         'externallinks' => 'el',
172                         'sections' => 's',
173                 );
174                 $this->setIndexedTagNames( $result_array, $result_mapping );
175                 $result->addValue( null, $this->getModuleName(), $result_array );
176         }
177
178         private function formatLangLinks( $links ) {
179                 $result = array();
180                 foreach( $links as $link ) {
181                         $entry = array();
182                         $bits = split( ':', $link, 2 );
183                         $entry['lang'] = $bits[0];
184                         $this->getResult()->setContent( $entry, $bits[1] );
185                         $result[] = $entry;
186                 }
187                 return $result;
188         }
189
190         private function formatCategoryLinks( $links ) {
191                 $result = array();
192                 foreach( $links as $link => $sortkey ) {
193                         $entry = array();
194                         $entry['sortkey'] = $sortkey;
195                         $this->getResult()->setContent( $entry, $link );
196                         $result[] = $entry;
197                 }
198                 return $result;
199         }
200
201         private function formatLinks( $links ) {
202                 $result = array();
203                 foreach( $links as $ns => $nslinks ) {
204                         foreach( $nslinks as $title => $id ) {
205                                 $entry = array();
206                                 $entry['ns'] = $ns;
207                                 $this->getResult()->setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() );
208                                 if( $id != 0 )
209                                         $entry['exists'] = '';
210                                 $result[] = $entry;
211                         }
212                 }
213                 return $result;
214         }
215
216         private function setIndexedTagNames( &$array, $mapping ) {
217                 foreach( $mapping as $key => $name ) {
218                         if( isset( $array[$key] ) )
219                                 $this->getResult()->setIndexedTagName( $array[$key], $name );
220                 }
221         }
222
223         public function getAllowedParams() {
224                 return array (
225                         'title' => array(
226                                 ApiBase :: PARAM_DFLT => 'API',
227                         ),
228                         'text' => null,
229                         'page' => null,
230                         'redirects' => false,
231                         'oldid' => null,
232                         'prop' => array(
233                                 ApiBase :: PARAM_DFLT => 'text|langlinks|categories|links|templates|images|externallinks|sections|revid|displaytitle',
234                                 ApiBase :: PARAM_ISMULTI => true,
235                                 ApiBase :: PARAM_TYPE => array(
236                                         'text',
237                                         'langlinks',
238                                         'categories',
239                                         'links',
240                                         'templates',
241                                         'images',
242                                         'externallinks',
243                                         'sections',
244                                         'revid',
245                                         'displaytitle',
246                                 )
247                         ),
248                         'pst' => false,
249                         'onlypst' => false,
250                 );
251         }
252
253         public function getParamDescription() {
254                 return array (
255                         'text' => 'Wikitext to parse',
256                         'redirects' => 'If the page parameter is set to a redirect, resolve it',
257                         'title' => 'Title of page the text belongs to',
258                         'page' => 'Parse the content of this page. Cannot be used together with text and title',
259                         'oldid' => 'Parse the content of this revision. Overrides page',
260                         'prop' => array('Which pieces of information to get.',
261                                         'NOTE: Section tree is only generated if there are more than 4 sections, or if the __TOC__ keyword is present'
262                         ),
263                         'pst' => array( 'Do a pre-save transform on the input before parsing it.',
264                                         'Ignored if page or oldid is used.'
265                         ),
266                         'onlypst' => array('Do a PST on the input, but don\'t parse it.',
267                                         'Returns PSTed wikitext. Ignored if page or oldid is used.'
268                         ),
269                 );
270         }
271
272         public function getDescription() {
273                 return 'This module parses wikitext and returns parser output';
274         }
275
276         protected function getExamples() {
277                 return array (
278                         'api.php?action=parse&text={{Project:Sandbox}}'
279                 );
280         }
281
282         public function getVersion() {
283                 return __CLASS__ . ': $Id: ApiParse.php 69986 2010-07-27 03:57:39Z tstarling $';
284         }
285 }