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