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