(bug 12608) (in continuation of r29719) Unifying the spelling of getDBkey() in the...
[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 }
62 if(!is_null($params['token'])) {
63 $token = $params['token'];
64 $tok_edit = $this->getTokenFlag($token, 'edit');
65 $tok_delete = $this->getTokenFlag($token, 'delete');
66 $tok_protect = $this->getTokenFlag($token, 'protect');
67 $tok_move = $this->getTokenFlag($token, 'move');
68 }
69 else
70 // Fix E_NOTICEs about unset variables
71 $token = $tok_edit = $tok_delete = $tok_protect = $tok_move = null;
72
73 $pageSet = $this->getPageSet();
74 $titles = $pageSet->getGoodTitles();
75 $missing = $pageSet->getMissingTitles();
76 $result = $this->getResult();
77
78 $pageRestrictions = $pageSet->getCustomField('page_restrictions');
79 $pageIsRedir = $pageSet->getCustomField('page_is_redirect');
80 $pageIsNew = $pageSet->getCustomField('page_is_new');
81 $pageCounter = $pageSet->getCustomField('page_counter');
82 $pageTouched = $pageSet->getCustomField('page_touched');
83 $pageLatest = $pageSet->getCustomField('page_latest');
84 $pageLength = $pageSet->getCustomField('page_len');
85
86 $db = $this->getDB();
87 if ($fld_protection && !empty($titles)) {
88 $this->addTables('page_restrictions');
89 $this->addFields(array('pr_page', 'pr_type', 'pr_level', 'pr_expiry'));
90 $this->addWhereFld('pr_page', array_keys($titles));
91
92 $res = $this->select(__METHOD__);
93 while($row = $db->fetchObject($res)) {
94 $protections[$row->pr_page][] = array(
95 'type' => $row->pr_type,
96 'level' => $row->pr_level,
97 'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 )
98 );
99 }
100 $db->freeResult($res);
101 }
102 // We don't need to check for pt stuff if there are no nonexistent titles
103 if($fld_protection && !empty($missing))
104 {
105 $this->resetQueryParams();
106 // Construct a custom WHERE clause that matches all titles in $missing
107 $lb = new LinkBatch($missing);
108 $this->addTables('protected_titles');
109 $this->addFields(array('pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry'));
110 $this->addWhere($lb->constructSet('pt', $db));
111 $res = $this->select(__METHOD__);
112 $prottitles = array();
113 while($row = $db->fetchObject($res)) {
114 $prottitles[$row->pt_namespace][$row->pt_title] = array(
115 'type' => 'create',
116 'level' => $row->pt_create_perm,
117 'expiry' => Block::decodeExpiry($row->pt_expiry, TS_ISO_8601)
118 );
119 }
120 $db->freeResult($res);
121 }
122
123 foreach ( $titles as $pageid => $title ) {
124 $pageInfo = array (
125 'touched' => wfTimestamp(TS_ISO_8601, $pageTouched[$pageid]),
126 'lastrevid' => intval($pageLatest[$pageid]),
127 'counter' => intval($pageCounter[$pageid]),
128 'length' => intval($pageLength[$pageid]),
129 );
130
131 if ($pageIsRedir[$pageid])
132 $pageInfo['redirect'] = '';
133
134 if ($pageIsNew[$pageid])
135 $pageInfo['new'] = '';
136
137 if (!is_null($token)) {
138 // Currently all tokens are generated the same way, but it might change
139 if ($tok_edit)
140 $pageInfo['edittoken'] = $wgUser->editToken();
141 if ($tok_delete)
142 $pageInfo['deletetoken'] = $wgUser->editToken();
143 if ($tok_protect)
144 $pageInfo['protecttoken'] = $wgUser->editToken();
145 if ($tok_move)
146 $pageInfo['movetoken'] = $wgUser->editToken();
147 }
148
149 if($fld_protection) {
150 if (isset($protections[$pageid])) {
151 $pageInfo['protection'] = $protections[$pageid];
152 $result->setIndexedTagName($pageInfo['protection'], 'pr');
153 } else {
154 # Also check old restrictions
155 if( $pageRestrictions[$pageid] ) {
156 foreach( explode( ':', trim( $pageRestrictions[$pageid] ) ) as $restrict ) {
157 $temp = explode( '=', trim( $restrict ) );
158 if(count($temp) == 1) {
159 // old old format should be treated as edit/move restriction
160 $restriction = trim( $temp[0] );
161 $pageInfo['protection'][] = array(
162 'type' => 'edit',
163 'level' => $restriction,
164 'expiry' => 'infinity',
165 );
166 $pageInfo['protection'][] = array(
167 'type' => 'move',
168 'level' => $restriction,
169 'expiry' => 'infinity',
170 );
171 } else {
172 $restriction = trim( $temp[1] );
173 $pageInfo['protection'][] = array(
174 'type' => $temp[0],
175 'level' => $restriction,
176 'expiry' => 'infinity',
177 );
178 }
179 }
180 $result->setIndexedTagName($pageInfo['protection'], 'pr');
181 } else {
182 $pageInfo['protection'] = array();
183 }
184 }
185 }
186
187 $result->addValue(array (
188 'query',
189 'pages'
190 ), $pageid, $pageInfo);
191 }
192
193 // Get edit/protect tokens and protection data for missing titles if requested
194 // Delete and move tokens are N/A for missing titles anyway
195 if($tok_edit || $tok_protect || $fld_protection)
196 {
197 $res = &$result->getData();
198 foreach($missing as $pageid => $title) {
199 if($tok_edit)
200 $res['query']['pages'][$pageid]['edittoken'] = $wgUser->editToken();
201 if($tok_protect)
202 $res['query']['pages'][$pageid]['protecttoken'] = $wgUser->editToken();
203 if($fld_protection)
204 {
205 // Apparently the XML formatting code doesn't like array(null)
206 // This is painful to fix, so we'll just work around it
207 if(isset($prottitles[$title->getNamespace()][$title->getDBkey()]))
208 $res['query']['pages'][$pageid]['protection'][] = $prottitles[$title->getNamespace()][$title->getDBkey()];
209 else
210 $res['query']['pages'][$pageid]['protection'] = array();
211 $result->setIndexedTagName($res['query']['pages'][$pageid]['protection'], 'pr');
212 }
213 }
214 }
215 }
216
217 protected function getAllowedParams() {
218 return array (
219 'prop' => array (
220 ApiBase :: PARAM_DFLT => NULL,
221 ApiBase :: PARAM_ISMULTI => true,
222 ApiBase :: PARAM_TYPE => array (
223 'protection'
224 )),
225 'token' => array (
226 ApiBase :: PARAM_DFLT => NULL,
227 ApiBase :: PARAM_ISMULTI => true,
228 ApiBase :: PARAM_TYPE => array (
229 'edit',
230 'delete',
231 'protect',
232 'move',
233 )),
234 );
235 }
236
237 protected function getParamDescription() {
238 return array (
239 'prop' => array (
240 'Which additional properties to get:',
241 ' "protection" - List the protection level of each page'
242 ),
243 'token' => 'Request a token to perform a data-modifying action on a page',
244 );
245 }
246
247
248 protected function getDescription() {
249 return 'Get basic page information such as namespace, title, last touched date, ...';
250 }
251
252 protected function getExamples() {
253 return array (
254 'api.php?action=query&prop=info&titles=Main%20Page',
255 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
256 );
257 }
258
259 public function getVersion() {
260 return __CLASS__ . ': $Id$';
261 }
262 }
263