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