Call quote_ident on raw schema names, per bug 11952.
[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
70 $pageSet = $this->getPageSet();
71 $titles = $pageSet->getGoodTitles();
72 $result = $this->getResult();
73
74 $pageRestrictions = $pageSet->getCustomField('page_restrictions');
75 $pageIsRedir = $pageSet->getCustomField('page_is_redirect');
76 $pageIsNew = $pageSet->getCustomField('page_is_new');
77 $pageCounter = $pageSet->getCustomField('page_counter');
78 $pageTouched = $pageSet->getCustomField('page_touched');
79 $pageLatest = $pageSet->getCustomField('page_latest');
80 $pageLength = $pageSet->getCustomField('page_len');
81
82 if ($fld_protection && count($titles) > 0) {
83 $this->addTables('page_restrictions');
84 $this->addFields(array('pr_page', 'pr_type', 'pr_level', 'pr_expiry'));
85 $this->addWhereFld('pr_page', array_keys($titles));
86
87 $db = $this->getDB();
88 $res = $this->select(__METHOD__);
89 while($row = $db->fetchObject($res)) {
90 $protections[$row->pr_page][] = array(
91 'type' => $row->pr_type,
92 'level' => $row->pr_level,
93 'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 )
94 );
95 }
96 $db->freeResult($res);
97 }
98
99 foreach ( $titles as $pageid => $title ) {
100 $pageInfo = array (
101 'touched' => wfTimestamp(TS_ISO_8601, $pageTouched[$pageid]),
102 'lastrevid' => intval($pageLatest[$pageid]),
103 'counter' => intval($pageCounter[$pageid]),
104 'length' => intval($pageLength[$pageid]),
105 );
106
107 if ($pageIsRedir[$pageid])
108 $pageInfo['redirect'] = '';
109
110 if ($pageIsNew[$pageid])
111 $pageInfo['new'] = '';
112
113 if (!is_null($token)) {
114 // Currently all tokens are generated the same way, but it might change
115 if ($tok_edit)
116 $pageInfo['edittoken'] = $wgUser->editToken();
117 if ($tok_delete)
118 $pageInfo['deletetoken'] = $wgUser->editToken();
119 if ($tok_protect)
120 $pageInfo['protecttoken'] = $wgUser->editToken();
121 if ($tok_move)
122 $pageInfo['movetoken'] = $wgUser->editToken();
123 }
124
125 if($fld_protection) {
126 if (isset($protections[$pageid])) {
127 $pageInfo['protection'] = $protections[$pageid];
128 $result->setIndexedTagName($pageInfo['protection'], 'pr');
129 } else {
130 # Also check old restrictions
131 if( $pageRestrictions[$pageid] ) {
132 foreach( explode( ':', trim( $pageRestrictions[$pageid] ) ) as $restrict ) {
133 $temp = explode( '=', trim( $restrict ) );
134 if(count($temp) == 1) {
135 // old old format should be treated as edit/move restriction
136 $restriction = trim( $temp[0] );
137 $pageInfo['protection'][] = array(
138 'type' => 'edit',
139 'level' => $restriction,
140 'expiry' => 'infinity',
141 );
142 $pageInfo['protection'][] = array(
143 'type' => 'move',
144 'level' => $restriction,
145 'expiry' => 'infinity',
146 );
147 } else {
148 $restriction = trim( $temp[1] );
149 $pageInfo['protection'][] = array(
150 'type' => $temp[0],
151 'level' => $restriction,
152 'expiry' => 'infinity',
153 );
154 }
155 }
156 $result->setIndexedTagName($pageInfo['protection'], 'pr');
157 } else {
158 $pageInfo['protection'] = array();
159 }
160 }
161 }
162
163 $result->addValue(array (
164 'query',
165 'pages'
166 ), $pageid, $pageInfo);
167 }
168
169 // Get edit tokens for missing titles if requested
170 // Delete, protect and move tokens are N/A for missing titles anyway
171 if($tok_edit)
172 {
173 $missing = $pageSet->getMissingTitles();
174 $res = &$result->getData();
175 foreach($missing as $pageid => $title)
176 $res['query']['pages'][$pageid]['edittoken'] = $wgUser->editToken();
177 }
178 }
179
180 protected function getAllowedParams() {
181 return array (
182 'prop' => array (
183 ApiBase :: PARAM_DFLT => NULL,
184 ApiBase :: PARAM_ISMULTI => true,
185 ApiBase :: PARAM_TYPE => array (
186 'protection'
187 )),
188 'token' => array (
189 ApiBase :: PARAM_DFLT => NULL,
190 ApiBase :: PARAM_ISMULTI => true,
191 ApiBase :: PARAM_TYPE => array (
192 'edit',
193 'delete',
194 'protect',
195 'move',
196 )),
197 );
198 }
199
200 protected function getParamDescription() {
201 return array (
202 'prop' => array (
203 'Which additional properties to get:',
204 ' "protection" - List the protection level of each page'
205 ),
206 'token' => 'Request a token to perform a data-modifying action on a page',
207 );
208 }
209
210
211 protected function getDescription() {
212 return 'Get basic page information such as namespace, title, last touched date, ...';
213 }
214
215 protected function getExamples() {
216 return array (
217 'api.php?action=query&prop=info&titles=Main%20Page',
218 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
219 );
220 }
221
222 public function getVersion() {
223 return __CLASS__ . ': $Id$';
224 }
225 }
226