* Separating UI code and DB code in Article::rollback()
[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 global $wgUser;
53
54 $params = $this->extractRequestParams();
55 $fld_protection = false;
56 if(!is_null($params['prop'])) {
57 $prop = array_flip($params['prop']);
58 $fld_protection = isset($prop['protection']);
59 $fld_lastrevby = isset($prop['lastrevby']);
60 }
61 if(!is_null($params['tokens']))
62 $params['tokens'] = array_flip($params['tokens']);
63
64 $pageSet = $this->getPageSet();
65 $titles = $pageSet->getGoodTitles();
66 $result = $this->getResult();
67
68 $pageIsRedir = $pageSet->getCustomField('page_is_redirect');
69 $pageIsNew = $pageSet->getCustomField('page_is_new');
70 $pageCounter = $pageSet->getCustomField('page_counter');
71 $pageTouched = $pageSet->getCustomField('page_touched');
72 $pageLatest = $pageSet->getCustomField('page_latest');
73 $pageLength = $pageSet->getCustomField('page_len');
74
75 if ($fld_protection) {
76 $this->addTables('page_restrictions');
77 $this->addFields(array('pr_page', 'pr_type', 'pr_level', 'pr_expiry'));
78 $this->addWhereFld('pr_page', array_keys($titles));
79
80 $db = $this->getDB();
81 $res = $this->select(__METHOD__);
82 while($row = $db->fetchObject($res)) {
83 $protections[$row->pr_page][] = array(
84 'type' => $row->pr_type,
85 'level' => $row->pr_level,
86 'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 )
87 );
88 }
89 $db->freeResult($res);
90 }
91
92 foreach ( $titles as $pageid => $title ) {
93 $pageInfo = array (
94 'touched' => wfTimestamp(TS_ISO_8601, $pageTouched[$pageid]),
95 'lastrevid' => intval($pageLatest[$pageid]),
96 'counter' => intval($pageCounter[$pageid]),
97 'length' => intval($pageLength[$pageid])
98 );
99 if(isset($params['tokens']) || $fld_lastrevby)
100 {
101 $lastrev = Revision::newFromId($pageInfo['lastrevid']);
102 $pageInfo['lastrevby'] = $lastrev->getUserText();
103 }
104
105 if ($pageIsRedir[$pageid])
106 $pageInfo['redirect'] = '';
107
108 if ($pageIsNew[$pageid])
109 $pageInfo['new'] = '';
110
111 if($fld_protection) {
112 if (isset($protections[$pageid])) {
113 $pageInfo['protection'] = $protections[$pageid];
114 $result->setIndexedTagName($pageInfo['protection'], 'pr');
115 } else {
116 $pageInfo['protection'] = array();
117 }
118 }
119
120 $tokenArr = array();
121 foreach($params['tokens'] as $token => $unused)
122 switch($token)
123 {
124 case 'rollback':
125 $tokenArr[$token] = $wgUser->editToken(array($title->getPrefixedText(), $pageInfo['lastrevby']));
126 break;
127 case 'edit':
128 case 'move':
129 case 'delete':
130 case 'undelete':
131 case 'protect':
132 case 'unprotect':
133 if($wgUser->isAnon())
134 $tokenArr[$token] = EDIT_TOKEN_SUFFIX;
135 else
136 $tokenArr[$token] = $wgUser->editToken();
137 // default: can't happen, ignore it if it does happen in some weird way
138 }
139 if(count($tokenArr) > 0)
140 {
141 $pageInfo['tokens'] = $tokenArr;
142 $result->setIndexedTagName($pageInfo['tokens'], 't');
143 }
144
145 $result->addValue(array (
146 'query',
147 'pages'
148 ), $pageid, $pageInfo);
149 }
150 }
151
152 protected function getAllowedParams() {
153 return array (
154 'prop' => array (
155 ApiBase :: PARAM_DFLT => NULL,
156 ApiBase :: PARAM_ISMULTI => true,
157 ApiBase :: PARAM_TYPE => array (
158 'protection',
159 'lastrevby'
160 )),
161 'tokens' => array(
162 ApiBase :: PARAM_DFLT => NULL,
163 ApiBase :: PARAM_ISMULTI => true,
164 ApiBase :: PARAM_TYPE => array(
165 'edit',
166 'move',
167 'delete',
168 'undelete',
169 'rollback',
170 'protect',
171 'unprotect'
172 ))
173 );
174 }
175
176 protected function getParamDescription() {
177 return array (
178 'prop' => array (
179 'Which additional properties to get:',
180 ' "protection" - List the protection level of each page',
181 ' "lastrevby" - The name of the user who made the last edit. You may need this for action=rollback.'
182 ),
183 'tokens' => 'Which tokens to get.'
184 );
185 }
186
187
188 protected function getDescription() {
189 return 'Get basic page information such as namespace, title, last touched date, ...';
190 }
191
192 protected function getExamples() {
193 return array (
194 'api.php?action=query&prop=info&titles=Main%20Page',
195 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page',
196 'api.php?action=query&prop=info&intokens=edit|rollback&titles=Main%20Page'
197 );
198 }
199
200 public function getVersion() {
201 return __CLASS__ . ': $Id$';
202 }
203 }
204