(bug 10898) API does not return an edit token for non-existent pages
[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_is_redirect');
44 $pageSet->requestField('page_is_new');
45 $pageSet->requestField('page_counter');
46 $pageSet->requestField('page_touched');
47 $pageSet->requestField('page_latest');
48 $pageSet->requestField('page_len');
49 }
50
51 public function execute() {
52
53 global $wgUser;
54
55 $params = $this->extractRequestParams();
56 $fld_protection = false;
57 if(!is_null($params['prop'])) {
58 $prop = array_flip($params['prop']);
59 $fld_protection = isset($prop['protection']);
60 }
61 if(!is_null($params['token'])) {
62 $token = $params['token'];
63 $tok_edit = $this->getTokenFlag($token, 'edit');
64 $tok_delete = $this->getTokenFlag($token, 'delete');
65 $tok_protect = $this->getTokenFlag($token, 'protect');
66 $tok_move = $this->getTokenFlag($token, 'move');
67 }
68
69 $pageSet = $this->getPageSet();
70 $titles = $pageSet->getGoodTitles();
71 $result = $this->getResult();
72
73 $pageIsRedir = $pageSet->getCustomField('page_is_redirect');
74 $pageIsNew = $pageSet->getCustomField('page_is_new');
75 $pageCounter = $pageSet->getCustomField('page_counter');
76 $pageTouched = $pageSet->getCustomField('page_touched');
77 $pageLatest = $pageSet->getCustomField('page_latest');
78 $pageLength = $pageSet->getCustomField('page_len');
79
80 if ($fld_protection && count($titles) > 0) {
81 $this->addTables('page_restrictions');
82 $this->addFields(array('pr_page', 'pr_type', 'pr_level', 'pr_expiry'));
83 $this->addWhereFld('pr_page', array_keys($titles));
84
85 $db = $this->getDB();
86 $res = $this->select(__METHOD__);
87 while($row = $db->fetchObject($res)) {
88 $protections[$row->pr_page][] = array(
89 'type' => $row->pr_type,
90 'level' => $row->pr_level,
91 'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 )
92 );
93 }
94 $db->freeResult($res);
95 }
96
97 foreach ( $titles as $pageid => $title ) {
98 $pageInfo = array (
99 'touched' => wfTimestamp(TS_ISO_8601, $pageTouched[$pageid]),
100 'lastrevid' => intval($pageLatest[$pageid]),
101 'counter' => intval($pageCounter[$pageid]),
102 'length' => intval($pageLength[$pageid]),
103 );
104
105 if ($pageIsRedir[$pageid])
106 $pageInfo['redirect'] = '';
107
108 if ($pageIsNew[$pageid])
109 $pageInfo['new'] = '';
110
111 if (!is_null($token)) {
112 // Currently all tokens are generated the same way, but it might change
113 if ($tok_edit)
114 $pageInfo['edittoken'] = $wgUser->editToken();
115 if ($tok_delete)
116 $pageInfo['deletetoken'] = $wgUser->editToken();
117 if ($tok_protect)
118 $pageInfo['protecttoken'] = $wgUser->editToken();
119 if ($tok_move)
120 $pageInfo['movetoken'] = $wgUser->editToken();
121 }
122
123 if($fld_protection) {
124 if (isset($protections[$pageid])) {
125 $pageInfo['protection'] = $protections[$pageid];
126 $result->setIndexedTagName($pageInfo['protection'], 'pr');
127 } else {
128 $pageInfo['protection'] = array();
129 }
130 }
131
132 $result->addValue(array (
133 'query',
134 'pages'
135 ), $pageid, $pageInfo);
136 }
137
138 // Get edit tokens for missing titles if requested
139 // Delete, protect and move tokens are N/A for missing titles anyway
140 if($tok_edit)
141 {
142 $missing = $pageSet->getMissingTitles();
143 $res = $result->getData();
144 foreach($missing as $pageid => $title)
145 $res['query']['pages'][$pageid]['edittoken'] = $wgUser->editToken();
146 }
147 }
148
149 protected function getAllowedParams() {
150 return array (
151 'prop' => array (
152 ApiBase :: PARAM_DFLT => NULL,
153 ApiBase :: PARAM_ISMULTI => true,
154 ApiBase :: PARAM_TYPE => array (
155 'protection'
156 )),
157 'token' => array (
158 ApiBase :: PARAM_DFLT => NULL,
159 ApiBase :: PARAM_ISMULTI => true,
160 ApiBase :: PARAM_TYPE => array (
161 'edit',
162 'delete',
163 'protect',
164 'move',
165 )),
166 );
167 }
168
169 protected function getParamDescription() {
170 return array (
171 'prop' => array (
172 'Which additional properties to get:',
173 ' "protection" - List the protection level of each page'
174 ),
175 'token' => 'Request a token to perform a data-modifying action on a page',
176 );
177 }
178
179
180 protected function getDescription() {
181 return 'Get basic page information such as namespace, title, last touched date, ...';
182 }
183
184 protected function getExamples() {
185 return array (
186 'api.php?action=query&prop=info&titles=Main%20Page',
187 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
188 );
189 }
190
191 public function getVersion() {
192 return __CLASS__ . ': $Id$';
193 }
194 }
195