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