93a69d2b68761b5e613294f5a8d8ebc539197859
[lhc/web/wiklou.git] / includes / api / ApiQueryInfo.php
1 <?php
2
3 /*
4 * Created on Sep 25, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 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 ('ApiQueryBase.php');
29 }
30
31 /**
32 * A query module to show basic page information.
33 *
34 * @addtogroup API
35 */
36 class ApiQueryInfo extends ApiQueryBase {
37
38 public function __construct($query, $moduleName) {
39 parent :: __construct($query, $moduleName, 'in');
40 }
41
42 public function requestExtraData($pageSet) {
43 $pageSet->requestField('page_restrictions');
44 $pageSet->requestField('page_is_redirect');
45 $pageSet->requestField('page_is_new');
46 $pageSet->requestField('page_counter');
47 $pageSet->requestField('page_touched');
48 $pageSet->requestField('page_latest');
49 $pageSet->requestField('page_len');
50 }
51
52 public function execute() {
53
54 global $wgUser;
55
56 $params = $this->extractRequestParams();
57 $fld_protection = false;
58 if(!is_null($params['prop'])) {
59 $prop = array_flip($params['prop']);
60 $fld_protection = isset($prop['protection']);
61 $fld_talkid = isset($prop['talkid']);
62 $fld_subjectid = isset($prop['subjectid']);
63 }
64 if(!is_null($params['token'])) {
65 $token = $params['token'];
66 $tok_edit = $this->getTokenFlag($token, 'edit');
67 $tok_delete = $this->getTokenFlag($token, 'delete');
68 $tok_protect = $this->getTokenFlag($token, 'protect');
69 $tok_move = $this->getTokenFlag($token, 'move');
70 }
71 else
72 // Fix E_NOTICEs about unset variables
73 $token = $tok_edit = $tok_delete = $tok_protect = $tok_move = null;
74
75 $pageSet = $this->getPageSet();
76 $titles = $pageSet->getGoodTitles();
77 $missing = $pageSet->getMissingTitles();
78 $result = $this->getResult();
79
80 $pageRestrictions = $pageSet->getCustomField('page_restrictions');
81 $pageIsRedir = $pageSet->getCustomField('page_is_redirect');
82 $pageIsNew = $pageSet->getCustomField('page_is_new');
83 $pageCounter = $pageSet->getCustomField('page_counter');
84 $pageTouched = $pageSet->getCustomField('page_touched');
85 $pageLatest = $pageSet->getCustomField('page_latest');
86 $pageLength = $pageSet->getCustomField('page_len');
87
88 $db = $this->getDB();
89 if ($fld_protection && !empty($titles)) {
90 $this->addTables('page_restrictions');
91 $this->addFields(array('pr_page', 'pr_type', 'pr_level', 'pr_expiry', 'pr_cascade'));
92 $this->addWhereFld('pr_page', array_keys($titles));
93
94 $res = $this->select(__METHOD__);
95 while($row = $db->fetchObject($res)) {
96 $a = array(
97 'type' => $row->pr_type,
98 'level' => $row->pr_level,
99 'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 )
100 );
101 if($row->pr_cascade)
102 $a['cascade'] = '';
103 $protections[$row->pr_page][] = $a;
104 }
105 $db->freeResult($res);
106 }
107 // We don't need to check for pt stuff if there are no nonexistent titles
108 if($fld_protection && !empty($missing))
109 {
110 $this->resetQueryParams();
111 // Construct a custom WHERE clause that matches all titles in $missing
112 $lb = new LinkBatch($missing);
113 $this->addTables('protected_titles');
114 $this->addFields(array('pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry'));
115 $this->addWhere($lb->constructSet('pt', $db));
116 $res = $this->select(__METHOD__);
117 $prottitles = array();
118 while($row = $db->fetchObject($res)) {
119 $prottitles[$row->pt_namespace][$row->pt_title] = array(
120 'type' => 'create',
121 'level' => $row->pt_create_perm,
122 'expiry' => Block::decodeExpiry($row->pt_expiry, TS_ISO_8601)
123 );
124 }
125 $db->freeResult($res);
126 }
127
128 // Run the talkid/subjectid query
129 if($fld_talkid || $fld_subjectid)
130 {
131 $talktitles = $subjecttitles =
132 $talkids = $subjectids = array();
133 $everything = array_merge($titles, $missing);
134 foreach($everything as $t)
135 {
136 if(MWNamespace::isTalk($t->getNamespace()))
137 {
138 if($fld_subjectid)
139 $subjecttitles[] = $t->getSubjectPage();
140 }
141 else if($fld_talkid)
142 $talktitles[] = $t->getTalkPage();
143 }
144 if(!empty($talktitles) || !empty($subjecttitles))
145 {
146 // Construct a custom WHERE clause that matches
147 // all titles in $talktitles and $subjecttitles
148 $lb = new LinkBatch(array_merge($talktitles, $subjecttitles));
149 $this->resetQueryParams();
150 $this->addTables('page');
151 $this->addFields(array('page_title', 'page_namespace', 'page_id'));
152 $this->addWhere($lb->constructSet('page', $db));
153 $res = $this->select(__METHOD__);
154 while($row = $db->fetchObject($res))
155 {
156 if(MWNamespace::isTalk($row->page_namespace))
157 $talkids[MWNamespace::getSubject($row->page_namespace)][$row->page_title] = $row->page_id;
158 else
159 $subjectids[MWNamespace::getTalk($row->page_namespace)][$row->page_title] = $row->page_id;
160 }
161 }
162 }
163
164 foreach ( $titles as $pageid => $title ) {
165 $pageInfo = array (
166 'touched' => wfTimestamp(TS_ISO_8601, $pageTouched[$pageid]),
167 'lastrevid' => intval($pageLatest[$pageid]),
168 'counter' => intval($pageCounter[$pageid]),
169 'length' => intval($pageLength[$pageid]),
170 );
171
172 if ($pageIsRedir[$pageid])
173 $pageInfo['redirect'] = '';
174
175 if ($pageIsNew[$pageid])
176 $pageInfo['new'] = '';
177
178 if (!is_null($token)) {
179 // Currently all tokens are generated the same way, but it might change
180 if ($tok_edit)
181 $pageInfo['edittoken'] = $wgUser->editToken();
182 if ($tok_delete)
183 $pageInfo['deletetoken'] = $wgUser->editToken();
184 if ($tok_protect)
185 $pageInfo['protecttoken'] = $wgUser->editToken();
186 if ($tok_move)
187 $pageInfo['movetoken'] = $wgUser->editToken();
188 }
189
190 if($fld_protection) {
191 if (isset($protections[$pageid])) {
192 $pageInfo['protection'] = $protections[$pageid];
193 $result->setIndexedTagName($pageInfo['protection'], 'pr');
194 } else {
195 # Also check old restrictions
196 if( $pageRestrictions[$pageid] ) {
197 foreach( explode( ':', trim( $pageRestrictions[$pageid] ) ) as $restrict ) {
198 $temp = explode( '=', trim( $restrict ) );
199 if(count($temp) == 1) {
200 // old old format should be treated as edit/move restriction
201 $restriction = trim( $temp[0] );
202 $pageInfo['protection'][] = array(
203 'type' => 'edit',
204 'level' => $restriction,
205 'expiry' => 'infinity',
206 );
207 $pageInfo['protection'][] = array(
208 'type' => 'move',
209 'level' => $restriction,
210 'expiry' => 'infinity',
211 );
212 } else {
213 $restriction = trim( $temp[1] );
214 $pageInfo['protection'][] = array(
215 'type' => $temp[0],
216 'level' => $restriction,
217 'expiry' => 'infinity',
218 );
219 }
220 }
221 $result->setIndexedTagName($pageInfo['protection'], 'pr');
222 } else {
223 $pageInfo['protection'] = array();
224 }
225 }
226 }
227 if($fld_talkid && isset($talkids[$title->getNamespace()][$title->getDbKey()]))
228 $pageInfo['talkid'] = $talkids[$title->getNamespace()][$title->getDbKey()];
229 if($fld_subjectid && isset($subjectids[$title->getNamespace()][$title->getDbKey()]))
230 $pageInfo['subjectid'] = $subjectids[$title->getNamespace()][$title->getDbKey()];
231
232 $result->addValue(array (
233 'query',
234 'pages'
235 ), $pageid, $pageInfo);
236 }
237
238 // Get edit/protect tokens and protection data for missing titles if requested
239 // Delete and move tokens are N/A for missing titles anyway
240 if($tok_edit || $tok_protect || $fld_protection)
241 {
242 $res = &$result->getData();
243 foreach($missing as $pageid => $title) {
244 if($tok_edit)
245 $res['query']['pages'][$pageid]['edittoken'] = $wgUser->editToken();
246 if($tok_protect)
247 $res['query']['pages'][$pageid]['protecttoken'] = $wgUser->editToken();
248 if($fld_protection)
249 {
250 // Apparently the XML formatting code doesn't like array(null)
251 // This is painful to fix, so we'll just work around it
252 if(isset($prottitles[$title->getNamespace()][$title->getDBkey()]))
253 $res['query']['pages'][$pageid]['protection'][] = $prottitles[$title->getNamespace()][$title->getDBkey()];
254 else
255 $res['query']['pages'][$pageid]['protection'] = array();
256 $result->setIndexedTagName($res['query']['pages'][$pageid]['protection'], 'pr');
257 }
258 if($fld_talkid && isset($talkids[$title->getNamespace()][$title->getDbKey()]))
259 $res['query']['pages'][$pageid]['talkid'] = $talkids[$title->getNamespace()][$title->getDbKey()];
260 if($fld_subjectid && isset($subjectids[$title->getNamespace()][$title->getDbKey()]))
261 $res['query']['pages'][$pageid]['subjectid'] = $subjectids[$title->getNamespace()][$title->getDbKey()];
262 }
263 }
264 }
265
266 public function getAllowedParams() {
267 return array (
268 'prop' => array (
269 ApiBase :: PARAM_DFLT => NULL,
270 ApiBase :: PARAM_ISMULTI => true,
271 ApiBase :: PARAM_TYPE => array (
272 'protection',
273 'talkid',
274 'subjectid'
275 )),
276 'token' => array (
277 ApiBase :: PARAM_DFLT => NULL,
278 ApiBase :: PARAM_ISMULTI => true,
279 ApiBase :: PARAM_TYPE => array (
280 'edit',
281 'delete',
282 'protect',
283 'move',
284 )),
285 );
286 }
287
288 public function getParamDescription() {
289 return array (
290 'prop' => array (
291 'Which additional properties to get:',
292 ' "protection" - List the protection level of each page',
293 ' "talkid" - The page ID of the talk page for each non-talk page',
294 ' "subjectid" - The page ID of the parent page for each talk page'
295 ),
296 'token' => 'Request a token to perform a data-modifying action on a page',
297 );
298 }
299
300
301 public function getDescription() {
302 return 'Get basic page information such as namespace, title, last touched date, ...';
303 }
304
305 protected function getExamples() {
306 return array (
307 'api.php?action=query&prop=info&titles=Main%20Page',
308 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
309 );
310 }
311
312 public function getVersion() {
313 return __CLASS__ . ': $Id$';
314 }
315 }
316