00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if (!defined('MEDIAWIKI')) {
00027
00028 require_once ('ApiQueryBase.php');
00029 }
00030
00036 class ApiQueryContributions extends ApiQueryBase {
00037
00038 public function __construct($query, $moduleName) {
00039 parent :: __construct($query, $moduleName, 'uc');
00040 }
00041
00042 private $params, $username;
00043 private $fld_ids = false, $fld_title = false, $fld_timestamp = false,
00044 $fld_comment = false, $fld_flags = false,
00045 $fld_patrolled = false;
00046
00047 public function execute() {
00048
00049
00050 $this->params = $this->extractRequestParams();
00051
00052 $prop = array_flip($this->params['prop']);
00053 $this->fld_ids = isset($prop['ids']);
00054 $this->fld_title = isset($prop['title']);
00055 $this->fld_comment = isset($prop['comment']);
00056 $this->fld_flags = isset($prop['flags']);
00057 $this->fld_timestamp = isset($prop['timestamp']);
00058 $this->fld_patrolled = isset($prop['patrolled']);
00059
00060
00061 $this->selectNamedDB('contributions', DB_SLAVE, 'contributions');
00062 $db = $this->getDB();
00063
00064 if(isset($this->params['userprefix']))
00065 {
00066 $this->prefixMode = true;
00067 $this->multiUserMode = true;
00068 $this->userprefix = $this->params['userprefix'];
00069 }
00070 else
00071 {
00072 $this->usernames = array();
00073 if(!is_array($this->params['user']))
00074 $this->params['user'] = array($this->params['user']);
00075 foreach($this->params['user'] as $u)
00076 $this->prepareUsername($u);
00077 $this->prefixMode = false;
00078 $this->multiUserMode = (count($this->params['user']) > 1);
00079 }
00080 $this->prepareQuery();
00081
00082
00083 $res = $this->select( __METHOD__ );
00084
00085
00086 $count = 0;
00087 $limit = $this->params['limit'];
00088
00089
00090 while ( $row = $db->fetchObject( $res ) ) {
00091 if (++ $count > $limit) {
00092
00093 if($this->multiUserMode)
00094 $this->setContinueEnumParameter('continue', $this->continueStr($row));
00095 else
00096 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->rev_timestamp));
00097 break;
00098 }
00099
00100 $vals = $this->extractRowInfo($row);
00101 $fit = $this->getResult()->addValue(array('query', $this->getModuleName()), null, $vals);
00102 if(!$fit)
00103 {
00104 if($this->multiUserMode)
00105 $this->setContinueEnumParameter('continue', $this->continueStr($row));
00106 else
00107 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->rev_timestamp));
00108 break;
00109 }
00110 }
00111
00112
00113 $db->freeResult($res);
00114
00115 $this->getResult()->setIndexedTagName_internal(array('query', $this->getModuleName()), 'item');
00116 }
00117
00122 private function prepareUsername($user) {
00123 if( $user ) {
00124 $name = User::isIP( $user )
00125 ? $user
00126 : User::getCanonicalName( $user, 'valid' );
00127 if( $name === false ) {
00128 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
00129 } else {
00130 $this->usernames[] = $name;
00131 }
00132 } else {
00133 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
00134 }
00135 }
00136
00140 private function prepareQuery() {
00141
00142
00143
00144 $tables = array('page', 'revision');
00145 $this->addWhere('page_id=rev_page');
00146
00147
00148 if($this->multiUserMode && !is_null($this->params['continue']))
00149 {
00150 $continue = explode('|', $this->params['continue']);
00151 if(count($continue) != 2)
00152 $this->dieUsage("Invalid continue param. You should pass the original " .
00153 "value returned by the previous query", "_badcontinue");
00154 $encUser = $this->getDB()->strencode($continue[0]);
00155 $encTS = wfTimestamp(TS_MW, $continue[1]);
00156 $op = ($this->params['dir'] == 'older' ? '<' : '>');
00157 $this->addWhere("rev_user_text $op '$encUser' OR " .
00158 "(rev_user_text = '$encUser' AND " .
00159 "rev_timestamp $op= '$encTS')");
00160 }
00161
00162 $this->addWhereFld('rev_deleted', 0);
00163
00164 if($this->prefixMode)
00165 $this->addWhere("rev_user_text LIKE '" . $this->getDB()->escapeLike($this->userprefix) . "%'");
00166 else
00167 $this->addWhereFld('rev_user_text', $this->usernames);
00168
00169
00170
00171 if($this->multiUserMode)
00172 $this->addWhereRange('rev_user_text', $this->params['dir'], null, null);
00173 $this->addWhereRange('rev_timestamp',
00174 $this->params['dir'], $this->params['start'], $this->params['end'] );
00175 $this->addWhereFld('page_namespace', $this->params['namespace']);
00176
00177 $show = $this->params['show'];
00178 if (!is_null($show)) {
00179 $show = array_flip($show);
00180 if ((isset($show['minor']) && isset($show['!minor']))
00181 || (isset($show['patrolled']) && isset($show['!patrolled'])))
00182 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
00183
00184 $this->addWhereIf('rev_minor_edit = 0', isset($show['!minor']));
00185 $this->addWhereIf('rev_minor_edit != 0', isset($show['minor']));
00186 $this->addWhereIf('rc_patrolled = 0', isset($show['!patrolled']));
00187 $this->addWhereIf('rc_patrolled != 0', isset($show['patrolled']));
00188 }
00189 $this->addOption('LIMIT', $this->params['limit'] + 1);
00190 $index['revision'] = 'usertext_timestamp';
00191
00192
00193
00194
00195 $this->addFields(array(
00196 'rev_timestamp',
00197 'page_namespace',
00198 'page_title',
00199 'rev_user_text',
00200 ));
00201
00202 if(isset($show['patrolled']) || isset($show['!patrolled']) ||
00203 $this->fld_patrolled)
00204 {
00205 global $wgUser;
00206 if(!$wgUser->useRCPatrol() && !$wgUser->useNPPatrol())
00207 $this->dieUsage("You need the patrol right to request the patrolled flag", 'permissiondenied');
00208
00209
00210
00211 $index['recentchanges'] = 'rc_user_text';
00212 if(isset($show['patrolled']) || isset($show['!patrolled']))
00213 {
00214
00215
00216 $tables = array('revision', 'recentchanges', 'page');
00217 $this->addOption('STRAIGHT_JOIN');
00218 $this->addWhere('rc_user_text=rev_user_text');
00219 $this->addWhere('rc_timestamp=rev_timestamp');
00220 $this->addWhere('rc_this_oldid=rev_id');
00221 }
00222 else
00223 {
00224 $tables[] = 'recentchanges';
00225 $this->addJoinConds(array('recentchanges' => array(
00226 'LEFT JOIN', array(
00227 'rc_user_text=rev_user_text',
00228 'rc_timestamp=rev_timestamp',
00229 'rc_this_oldid=rev_id'))));
00230 }
00231 }
00232
00233 $this->addTables($tables);
00234 $this->addOption('USE INDEX', $index);
00235 $this->addFieldsIf('rev_page', $this->fld_ids);
00236 $this->addFieldsIf('rev_id', $this->fld_ids || $this->fld_flags);
00237 $this->addFieldsIf('page_latest', $this->fld_flags);
00238
00239 $this->addFieldsIf('rev_comment', $this->fld_comment);
00240 $this->addFieldsIf('rev_minor_edit', $this->fld_flags);
00241 $this->addFieldsIf('rev_parent_id', $this->fld_flags);
00242 $this->addFieldsIf('rc_patrolled', $this->fld_patrolled);
00243 }
00244
00248 private function extractRowInfo($row) {
00249
00250 $vals = array();
00251
00252 $vals['user'] = $row->rev_user_text;
00253 if ($this->fld_ids) {
00254 $vals['pageid'] = intval($row->rev_page);
00255 $vals['revid'] = intval($row->rev_id);
00256
00257 }
00258
00259 if ($this->fld_title)
00260 ApiQueryBase :: addTitleInfo($vals,
00261 Title :: makeTitle($row->page_namespace, $row->page_title));
00262
00263 if ($this->fld_timestamp)
00264 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rev_timestamp);
00265
00266 if ($this->fld_flags) {
00267 if ($row->rev_parent_id == 0)
00268 $vals['new'] = '';
00269 if ($row->rev_minor_edit)
00270 $vals['minor'] = '';
00271 if ($row->page_latest == $row->rev_id)
00272 $vals['top'] = '';
00273 }
00274
00275 if ($this->fld_comment && isset($row->rev_comment))
00276 $vals['comment'] = $row->rev_comment;
00277
00278 if ($this->fld_patrolled && $row->rc_patrolled)
00279 $vals['patrolled'] = '';
00280
00281 return $vals;
00282 }
00283
00284 private function continueStr($row)
00285 {
00286 return $row->rev_user_text . '|' .
00287 wfTimestamp(TS_ISO_8601, $row->rev_timestamp);
00288 }
00289
00290 public function getAllowedParams() {
00291 return array (
00292 'limit' => array (
00293 ApiBase :: PARAM_DFLT => 10,
00294 ApiBase :: PARAM_TYPE => 'limit',
00295 ApiBase :: PARAM_MIN => 1,
00296 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00297 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00298 ),
00299 'start' => array (
00300 ApiBase :: PARAM_TYPE => 'timestamp'
00301 ),
00302 'end' => array (
00303 ApiBase :: PARAM_TYPE => 'timestamp'
00304 ),
00305 'continue' => null,
00306 'user' => array (
00307 ApiBase :: PARAM_ISMULTI => true
00308 ),
00309 'userprefix' => null,
00310 'dir' => array (
00311 ApiBase :: PARAM_DFLT => 'older',
00312 ApiBase :: PARAM_TYPE => array (
00313 'newer',
00314 'older'
00315 )
00316 ),
00317 'namespace' => array (
00318 ApiBase :: PARAM_ISMULTI => true,
00319 ApiBase :: PARAM_TYPE => 'namespace'
00320 ),
00321 'prop' => array (
00322 ApiBase :: PARAM_ISMULTI => true,
00323 ApiBase :: PARAM_DFLT => 'ids|title|timestamp|flags|comment',
00324 ApiBase :: PARAM_TYPE => array (
00325 'ids',
00326 'title',
00327 'timestamp',
00328 'comment',
00329 'flags',
00330 'patrolled',
00331 )
00332 ),
00333 'show' => array (
00334 ApiBase :: PARAM_ISMULTI => true,
00335 ApiBase :: PARAM_TYPE => array (
00336 'minor',
00337 '!minor',
00338 'patrolled',
00339 '!patrolled',
00340 )
00341 ),
00342 );
00343 }
00344
00345 public function getParamDescription() {
00346 return array (
00347 'limit' => 'The maximum number of contributions to return.',
00348 'start' => 'The start timestamp to return from.',
00349 'end' => 'The end timestamp to return to.',
00350 'continue' => 'When more results are available, use this to continue.',
00351 'user' => 'The user to retrieve contributions for.',
00352 'userprefix' => 'Retrieve contibutions for all users whose names begin with this value. Overrides ucuser.',
00353 'dir' => 'The direction to search (older or newer).',
00354 'namespace' => 'Only list contributions in these namespaces',
00355 'prop' => 'Include additional pieces of information',
00356 'show' => array('Show only items that meet this criteria, e.g. non minor edits only: show=!minor',
00357 'NOTE: if show=patrolled or show=!patrolled is set, revisions older than $wgRCMaxAge won\'t be shown',),
00358 );
00359 }
00360
00361 public function getDescription() {
00362 return 'Get all edits by a user';
00363 }
00364
00365 protected function getExamples() {
00366 return array (
00367 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
00368 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
00369 );
00370 }
00371
00372 public function getVersion() {
00373 return __CLASS__ . ': $Id: ApiQueryUserContributions.php 47037 2009-02-09 14:07:18Z catrope $';
00374 }
00375 }