]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/api/ApiQueryUserContributions.php
MediaWiki 1.15.0
[autoinstallsdev/mediawiki.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  * @ingroup 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                         $fld_patrolled = false;
46
47         public function execute() {
48
49                 // Parse some parameters
50                 $this->params = $this->extractRequestParams();
51
52                 $prop = array_flip($this->params['prop']);
53                 $this->fld_ids = isset($prop['ids']);
54                 $this->fld_title = isset($prop['title']);
55                 $this->fld_comment = isset($prop['comment']);
56                 $this->fld_flags = isset($prop['flags']);
57                 $this->fld_timestamp = isset($prop['timestamp']);
58                 $this->fld_patrolled = isset($prop['patrolled']);
59
60                 // TODO: if the query is going only against the revision table, should this be done?
61                 $this->selectNamedDB('contributions', DB_SLAVE, 'contributions');
62                 $db = $this->getDB();
63
64                 if(isset($this->params['userprefix']))
65                 {
66                         $this->prefixMode = true;
67                         $this->multiUserMode = true;
68                         $this->userprefix = $this->params['userprefix'];
69                 }
70                 else
71                 {
72                         $this->usernames = array();
73                         if(!is_array($this->params['user']))
74                                 $this->params['user'] = array($this->params['user']);
75                         foreach($this->params['user'] as $u)
76                                 $this->prepareUsername($u);
77                         $this->prefixMode = false;
78                         $this->multiUserMode = (count($this->params['user']) > 1);
79                 }
80                 $this->prepareQuery();
81
82                 //Do the actual query.
83                 $res = $this->select( __METHOD__ );
84
85                 //Initialise some variables
86                 $count = 0;
87                 $limit = $this->params['limit'];
88
89                 //Fetch each row
90                 while ( $row = $db->fetchObject( $res ) ) {
91                         if (++ $count > $limit) {
92                                 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
93                                 if($this->multiUserMode)
94                                         $this->setContinueEnumParameter('continue', $this->continueStr($row));
95                                 else
96                                         $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->rev_timestamp));
97                                 break;
98                         }
99
100                         $vals = $this->extractRowInfo($row);
101                         $fit = $this->getResult()->addValue(array('query', $this->getModuleName()), null, $vals);
102                         if(!$fit)
103                         {
104                                 if($this->multiUserMode)
105                                         $this->setContinueEnumParameter('continue', $this->continueStr($row));
106                                 else
107                                         $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->rev_timestamp));
108                                 break;
109                         }
110                 }
111
112                 //Free the database record so the connection can get on with other stuff
113                 $db->freeResult($res);
114
115                 $this->getResult()->setIndexedTagName_internal(array('query', $this->getModuleName()), 'item');
116         }
117
118         /**
119          * Validate the 'user' parameter and set the value to compare
120          * against `revision`.`rev_user_text`
121          */
122         private function prepareUsername($user) {
123                 if( $user ) {
124                         $name = User::isIP( $user )
125                                 ? $user
126                                 : User::getCanonicalName( $user, 'valid' );
127                         if( $name === false ) {
128                                 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
129                         } else {
130                                 $this->usernames[] = $name;
131                         }
132                 } else {
133                         $this->dieUsage( 'User parameter may not be empty', 'param_user' );
134                 }
135         }
136
137         /**
138          * Prepares the query and returns the limit of rows requested
139          */
140         private function prepareQuery() {
141                 // We're after the revision table, and the corresponding page
142                 // row for anything we retrieve. We may also need the
143                 // recentchanges row.
144                 $tables = array('page', 'revision'); // Order may change
145                 $this->addWhere('page_id=rev_page');
146
147                 // Handle continue parameter
148                 if($this->multiUserMode && !is_null($this->params['continue']))
149                 {
150                         $continue = explode('|', $this->params['continue']);
151                         if(count($continue) != 2)
152                                 $this->dieUsage("Invalid continue param. You should pass the original " .
153                                         "value returned by the previous query", "_badcontinue");
154                         $encUser = $this->getDB()->strencode($continue[0]);
155                         $encTS = wfTimestamp(TS_MW, $continue[1]);
156                         $op = ($this->params['dir'] == 'older' ? '<' : '>');
157                         $this->addWhere("rev_user_text $op '$encUser' OR " .
158                                         "(rev_user_text = '$encUser' AND " .
159                                         "rev_timestamp $op= '$encTS')");
160                 }
161
162                 $this->addWhereFld('rev_deleted', 0);
163                 // We only want pages by the specified users.
164                 if($this->prefixMode)
165                         $this->addWhere("rev_user_text LIKE '" . $this->getDB()->escapeLike($this->userprefix) . "%'");
166                 else
167                         $this->addWhereFld('rev_user_text', $this->usernames);
168                 // ... and in the specified timeframe.
169                 // Ensure the same sort order for rev_user_text and rev_timestamp
170                 // so our query is indexed
171                 if($this->multiUserMode)
172                         $this->addWhereRange('rev_user_text', $this->params['dir'], null, null);
173                 $this->addWhereRange('rev_timestamp',
174                         $this->params['dir'], $this->params['start'], $this->params['end'] );
175                 $this->addWhereFld('page_namespace', $this->params['namespace']);
176
177                 $show = $this->params['show'];
178                 if (!is_null($show)) {
179                         $show = array_flip($show);
180                         if ((isset($show['minor']) && isset($show['!minor']))
181                                         || (isset($show['patrolled']) && isset($show['!patrolled'])))
182                                 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
183
184                         $this->addWhereIf('rev_minor_edit = 0', isset($show['!minor']));
185                         $this->addWhereIf('rev_minor_edit != 0', isset($show['minor']));
186                         $this->addWhereIf('rc_patrolled = 0', isset($show['!patrolled']));
187                         $this->addWhereIf('rc_patrolled != 0', isset($show['patrolled']));
188                 }
189                 $this->addOption('LIMIT', $this->params['limit'] + 1);
190                 $index['revision'] = 'usertext_timestamp';
191
192                 // Mandatory fields: timestamp allows request continuation
193                 // ns+title checks if the user has access rights for this page
194                 // user_text is necessary if multiple users were specified
195                 $this->addFields(array(
196                         'rev_timestamp',
197                         'page_namespace',
198                         'page_title',
199                         'rev_user_text',
200                 ));
201                 
202                 if(isset($show['patrolled']) || isset($show['!patrolled']) ||
203                                  $this->fld_patrolled)
204                 {
205                         global $wgUser;
206                         if(!$wgUser->useRCPatrol() && !$wgUser->useNPPatrol())
207                                 $this->dieUsage("You need the patrol right to request the patrolled flag", 'permissiondenied');
208                         // Use a redundant join condition on both
209                         // timestamp and ID so we can use the timestamp
210                         // index
211                         $index['recentchanges'] = 'rc_user_text';
212                         if(isset($show['patrolled']) || isset($show['!patrolled']))
213                         {
214                                 // Put the tables in the right order for
215                                 // STRAIGHT_JOIN
216                                 $tables = array('revision', 'recentchanges', 'page');
217                                 $this->addOption('STRAIGHT_JOIN');
218                                 $this->addWhere('rc_user_text=rev_user_text');
219                                 $this->addWhere('rc_timestamp=rev_timestamp');
220                                 $this->addWhere('rc_this_oldid=rev_id');
221                         }
222                         else
223                         {
224                                 $tables[] = 'recentchanges';
225                                 $this->addJoinConds(array('recentchanges' => array(
226                                         'LEFT JOIN', array(
227                                                 'rc_user_text=rev_user_text',
228                                                 'rc_timestamp=rev_timestamp',
229                                                 'rc_this_oldid=rev_id'))));
230                         }
231                 }
232
233                 $this->addTables($tables);
234                 $this->addOption('USE INDEX', $index);
235                 $this->addFieldsIf('rev_page', $this->fld_ids);
236                 $this->addFieldsIf('rev_id', $this->fld_ids || $this->fld_flags);
237                 $this->addFieldsIf('page_latest', $this->fld_flags);
238                 // $this->addFieldsIf('rev_text_id', $this->fld_ids); // Should this field be exposed?
239                 $this->addFieldsIf('rev_comment', $this->fld_comment);
240                 $this->addFieldsIf('rev_minor_edit', $this->fld_flags);
241                 $this->addFieldsIf('rev_parent_id', $this->fld_flags);
242                 $this->addFieldsIf('rc_patrolled', $this->fld_patrolled);
243         }
244
245         /**
246          * Extract fields from the database row and append them to a result array
247          */
248         private function extractRowInfo($row) {
249
250                 $vals = array();
251
252                 $vals['user'] = $row->rev_user_text;
253                 if ($this->fld_ids) {
254                         $vals['pageid'] = intval($row->rev_page);
255                         $vals['revid'] = intval($row->rev_id);
256                         // $vals['textid'] = intval($row->rev_text_id); // todo: Should this field be exposed?
257                 }
258
259                 if ($this->fld_title)
260                         ApiQueryBase :: addTitleInfo($vals,
261                                 Title :: makeTitle($row->page_namespace, $row->page_title));
262
263                 if ($this->fld_timestamp)
264                         $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rev_timestamp);
265
266                 if ($this->fld_flags) {
267                         if ($row->rev_parent_id == 0)
268                                 $vals['new'] = '';
269                         if ($row->rev_minor_edit)
270                                 $vals['minor'] = '';
271                         if ($row->page_latest == $row->rev_id)
272                                 $vals['top'] = '';
273                 }
274
275                 if ($this->fld_comment && isset($row->rev_comment))
276                         $vals['comment'] = $row->rev_comment;
277
278                 if ($this->fld_patrolled && $row->rc_patrolled)
279                         $vals['patrolled'] = '';
280
281                 return $vals;
282         }
283         
284         private function continueStr($row)
285         {
286                 return $row->rev_user_text . '|' .
287                         wfTimestamp(TS_ISO_8601, $row->rev_timestamp);
288         }
289
290         public function getAllowedParams() {
291                 return array (
292                         'limit' => array (
293                                 ApiBase :: PARAM_DFLT => 10,
294                                 ApiBase :: PARAM_TYPE => 'limit',
295                                 ApiBase :: PARAM_MIN => 1,
296                                 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
297                                 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
298                         ),
299                         'start' => array (
300                                 ApiBase :: PARAM_TYPE => 'timestamp'
301                         ),
302                         'end' => array (
303                                 ApiBase :: PARAM_TYPE => 'timestamp'
304                         ),
305                         'continue' => null,
306                         'user' => array (
307                                 ApiBase :: PARAM_ISMULTI => true
308                         ),
309                         'userprefix' => null,
310                         'dir' => array (
311                                 ApiBase :: PARAM_DFLT => 'older',
312                                 ApiBase :: PARAM_TYPE => array (
313                                         'newer',
314                                         'older'
315                                 )
316                         ),
317                         'namespace' => array (
318                                 ApiBase :: PARAM_ISMULTI => true,
319                                 ApiBase :: PARAM_TYPE => 'namespace'
320                         ),
321                         'prop' => array (
322                                 ApiBase :: PARAM_ISMULTI => true,
323                                 ApiBase :: PARAM_DFLT => 'ids|title|timestamp|flags|comment',
324                                 ApiBase :: PARAM_TYPE => array (
325                                         'ids',
326                                         'title',
327                                         'timestamp',
328                                         'comment',
329                                         'flags',
330                                         'patrolled',
331                                 )
332                         ),
333                         'show' => array (
334                                 ApiBase :: PARAM_ISMULTI => true,
335                                 ApiBase :: PARAM_TYPE => array (
336                                         'minor',
337                                         '!minor',
338                                         'patrolled',
339                                         '!patrolled',
340                                 )
341                         ),
342                 );
343         }
344
345         public function getParamDescription() {
346                 return array (
347                         'limit' => 'The maximum number of contributions to return.',
348                         'start' => 'The start timestamp to return from.',
349                         'end' => 'The end timestamp to return to.',
350                         'continue' => 'When more results are available, use this to continue.',
351                         'user' => 'The user to retrieve contributions for.',
352                         'userprefix' => 'Retrieve contibutions for all users whose names begin with this value. Overrides ucuser.',
353                         'dir' => 'The direction to search (older or newer).',
354                         'namespace' => 'Only list contributions in these namespaces',
355                         'prop' => 'Include additional pieces of information',
356                         'show' => array('Show only items that meet this criteria, e.g. non minor edits only: show=!minor',
357                                         'NOTE: if show=patrolled or show=!patrolled is set, revisions older than $wgRCMaxAge won\'t be shown',),
358                 );
359         }
360
361         public function getDescription() {
362                 return 'Get all edits by a user';
363         }
364
365         protected function getExamples() {
366                 return array (
367                         'api.php?action=query&list=usercontribs&ucuser=YurikBot',
368                         'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
369                 );
370         }
371
372         public function getVersion() {
373                 return __CLASS__ . ': $Id: ApiQueryUserContributions.php 47037 2009-02-09 14:07:18Z catrope $';
374         }
375 }