(bug 11632) API: Breaking change: Specify the type of a change in the recentchanges...
[lhc/web/wiklou.git] / includes / api / ApiQueryUserContributions.php
1 <?php
2
3 /*
4 * Created on Oct 16, 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 * This query action adds a list of a specified user's contributions to the output.
33 *
34 * @addtogroup API
35 */
36 class ApiQueryContributions extends ApiQueryBase {
37
38 public function __construct($query, $moduleName) {
39 parent :: __construct($query, $moduleName, 'uc');
40 }
41
42 private $params, $username;
43 private $fld_ids = false, $fld_title = false, $fld_timestamp = false,
44 $fld_comment = false, $fld_flags = false;
45
46 public function execute() {
47
48 // Parse some parameters
49 $this->params = $this->extractRequestParams();
50
51 $prop = array_flip($this->params['prop']);
52 $this->fld_ids = isset($prop['ids']);
53 $this->fld_title = isset($prop['title']);
54 $this->fld_comment = isset($prop['comment']);
55 $this->fld_flags = isset($prop['flags']);
56 $this->fld_timestamp = isset($prop['timestamp']);
57
58 // TODO: if the query is going only against the revision table, should this be done?
59 $this->selectNamedDB('contributions', DB_SLAVE, 'contributions');
60 $db = $this->getDB();
61
62 // Prepare query
63 $this->prepareUsername();
64 $this->prepareQuery();
65
66 //Do the actual query.
67 $res = $this->select( __METHOD__ );
68
69 //Initialise some variables
70 $data = array ();
71 $count = 0;
72 $limit = $this->params['limit'];
73
74 //Fetch each row
75 while ( $row = $db->fetchObject( $res ) ) {
76 if (++ $count > $limit) {
77 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
78 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->rev_timestamp));
79 break;
80 }
81
82 $vals = $this->extractRowInfo($row);
83 if ($vals)
84 $data[] = $vals;
85 }
86
87 //Free the database record so the connection can get on with other stuff
88 $db->freeResult($res);
89
90 //And send the whole shebang out as output.
91 $this->getResult()->setIndexedTagName($data, 'item');
92 $this->getResult()->addValue('query', $this->getModuleName(), $data);
93 }
94
95 /**
96 * Validate the 'user' parameter and set the value to compare
97 * against `revision`.`rev_user_text`
98 */
99 private function prepareUsername() {
100 $user = $this->params['user'];
101 if( $user ) {
102 $name = User::isIP( $user )
103 ? $user
104 : User::getCanonicalName( $user, 'valid' );
105 if( $name === false ) {
106 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
107 } else {
108 $this->username = $name;
109 }
110 } else {
111 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
112 }
113 }
114
115 /**
116 * Prepares the query and returns the limit of rows requested
117 */
118 private function prepareQuery() {
119
120 //We're after the revision table, and the corresponding page row for
121 //anything we retrieve.
122 list ($tbl_page, $tbl_revision) = $this->getDB()->tableNamesN('page', 'revision');
123 $this->addTables("$tbl_revision LEFT OUTER JOIN $tbl_page ON page_id=rev_page");
124
125 $this->addWhereFld('rev_deleted', 0);
126
127 // We only want pages by the specified user.
128 $this->addWhereFld( 'rev_user_text', $this->username );
129
130 // ... and in the specified timeframe.
131 $this->addWhereRange('rev_timestamp',
132 $this->params['dir'], $this->params['start'], $this->params['end'] );
133
134 $this->addWhereFld('page_namespace', $this->params['namespace']);
135
136 $show = $this->params['show'];
137 if (!is_null($show)) {
138 $show = array_flip($show);
139 if (isset ($show['minor']) && isset ($show['!minor']))
140 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
141
142 $this->addWhereIf('rev_minor_edit = 0', isset ($show['!minor']));
143 $this->addWhereIf('rev_minor_edit != 0', isset ($show['minor']));
144 }
145
146 $this->addOption('LIMIT', $this->params['limit'] + 1);
147
148 // Mandatory fields: timestamp allows request continuation
149 // ns+title checks if the user has access rights for this page
150 $this->addFields(array(
151 'rev_timestamp',
152 'page_namespace',
153 'page_title',
154 ));
155
156 $this->addFieldsIf('rev_page', $this->fld_ids);
157 $this->addFieldsIf('rev_id', $this->fld_ids);
158 // $this->addFieldsIf('rev_text_id', $this->fld_ids); // Should this field be exposed?
159 $this->addFieldsIf('rev_comment', $this->fld_comment);
160 $this->addFieldsIf('rev_minor_edit', $this->fld_flags);
161
162 // These fields depend only work if the page table is joined
163 $this->addFieldsIf('page_is_new', $this->fld_flags);
164 }
165
166 /**
167 * Extract fields from the database row and append them to a result array
168 */
169 private function extractRowInfo($row) {
170
171 $vals = array();
172
173 if ($this->fld_ids) {
174 $vals['pageid'] = intval($row->rev_page);
175 $vals['revid'] = intval($row->rev_id);
176 // $vals['textid'] = intval($row->rev_text_id); // todo: Should this field be exposed?
177 }
178
179 if ($this->fld_title)
180 ApiQueryBase :: addTitleInfo($vals,
181 Title :: makeTitle($row->page_namespace, $row->page_title));
182
183 if ($this->fld_timestamp)
184 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rev_timestamp);
185
186 if ($this->fld_flags) {
187 if ($row->page_is_new)
188 $vals['new'] = '';
189 if ($row->rev_minor_edit)
190 $vals['minor'] = '';
191 }
192
193 if ($this->fld_comment && !empty ($row->rev_comment))
194 $vals['comment'] = $row->rev_comment;
195
196 return $vals;
197 }
198
199 protected function getAllowedParams() {
200 return array (
201 'limit' => array (
202 ApiBase :: PARAM_DFLT => 10,
203 ApiBase :: PARAM_TYPE => 'limit',
204 ApiBase :: PARAM_MIN => 1,
205 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
206 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
207 ),
208 'start' => array (
209 ApiBase :: PARAM_TYPE => 'timestamp'
210 ),
211 'end' => array (
212 ApiBase :: PARAM_TYPE => 'timestamp'
213 ),
214 'user' => array (
215 ApiBase :: PARAM_TYPE => 'user'
216 ),
217 'dir' => array (
218 ApiBase :: PARAM_DFLT => 'older',
219 ApiBase :: PARAM_TYPE => array (
220 'newer',
221 'older'
222 )
223 ),
224 'namespace' => array (
225 ApiBase :: PARAM_ISMULTI => true,
226 ApiBase :: PARAM_TYPE => 'namespace'
227 ),
228 'prop' => array (
229 ApiBase :: PARAM_ISMULTI => true,
230 ApiBase :: PARAM_DFLT => 'ids|title|timestamp|flags|comment',
231 ApiBase :: PARAM_TYPE => array (
232 'ids',
233 'title',
234 'timestamp',
235 'comment',
236 'flags'
237 )
238 ),
239 'show' => array (
240 ApiBase :: PARAM_ISMULTI => true,
241 ApiBase :: PARAM_TYPE => array (
242 'minor',
243 '!minor',
244 )
245 ),
246 );
247 }
248
249 protected function getParamDescription() {
250 return array (
251 'limit' => 'The maximum number of contributions to return.',
252 'start' => 'The start timestamp to return from.',
253 'end' => 'The end timestamp to return to.',
254 'user' => 'The user to retrieve contributions for.',
255 'dir' => 'The direction to search (older or newer).',
256 'namespace' => 'Only list contributions in these namespaces',
257 'prop' => 'Include additional pieces of information',
258 'show' => 'Show only items that meet this criteria, e.g. non minor edits only: show=!minor',
259 );
260 }
261
262 protected function getDescription() {
263 return 'Get all edits by a user';
264 }
265
266 protected function getExamples() {
267 return array (
268 'api.php?action=query&list=usercontribs&ucuser=YurikBot'
269 );
270 }
271
272 public function getVersion() {
273 return __CLASS__ . ': $Id$';
274 }
275 }
276