1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
|
<?php
/**
*
*
* Created on Oct 16, 2006
*
* Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
/**
* This query action adds a list of a specified user's contributions to the output.
*
* @ingroup API
*/
class ApiQueryContributions extends ApiQueryBase {
public function __construct( ApiQuery $query, $moduleName ) {
parent::__construct( $query, $moduleName, 'uc' );
}
private $params, $prefixMode, $userprefix, $multiUserMode, $usernames, $parentLens;
private $fld_ids = false, $fld_title = false, $fld_timestamp = false,
$fld_comment = false, $fld_parsedcomment = false, $fld_flags = false,
$fld_patrolled = false, $fld_tags = false, $fld_size = false, $fld_sizediff = false;
public function execute() {
// Parse some parameters
$this->params = $this->extractRequestParams();
$prop = array_flip( $this->params['prop'] );
$this->fld_ids = isset( $prop['ids'] );
$this->fld_title = isset( $prop['title'] );
$this->fld_comment = isset( $prop['comment'] );
$this->fld_parsedcomment = isset( $prop['parsedcomment'] );
$this->fld_size = isset( $prop['size'] );
$this->fld_sizediff = isset( $prop['sizediff'] );
$this->fld_flags = isset( $prop['flags'] );
$this->fld_timestamp = isset( $prop['timestamp'] );
$this->fld_patrolled = isset( $prop['patrolled'] );
$this->fld_tags = isset( $prop['tags'] );
// Most of this code will use the 'contributions' group DB, which can map to slaves
// with extra user based indexes or partioning by user. The additional metadata
// queries should use a regular slave since the lookup pattern is not all by user.
$dbSecondary = $this->getDB(); // any random slave
// TODO: if the query is going only against the revision table, should this be done?
$this->selectNamedDB( 'contributions', DB_SLAVE, 'contributions' );
if ( isset( $this->params['userprefix'] ) ) {
$this->prefixMode = true;
$this->multiUserMode = true;
$this->userprefix = $this->params['userprefix'];
} else {
$this->usernames = array();
if ( !is_array( $this->params['user'] ) ) {
$this->params['user'] = array( $this->params['user'] );
}
if ( !count( $this->params['user'] ) ) {
$this->dieUsage( 'User parameter may not be empty.', 'param_user' );
}
foreach ( $this->params['user'] as $u ) {
$this->prepareUsername( $u );
}
$this->prefixMode = false;
$this->multiUserMode = ( count( $this->params['user'] ) > 1 );
}
$this->prepareQuery();
// Do the actual query.
$res = $this->select( __METHOD__ );
if ( $this->fld_sizediff ) {
$revIds = array();
foreach ( $res as $row ) {
if ( $row->rev_parent_id ) {
$revIds[] = $row->rev_parent_id;
}
}
$this->parentLens = Revision::getParentLengths( $dbSecondary, $revIds );
$res->rewind(); // reset
}
// Initialise some variables
$count = 0;
$limit = $this->params['limit'];
// Fetch each row
foreach ( $res as $row ) {
if ( ++$count > $limit ) {
// We've reached the one extra which shows that there are
// additional pages to be had. Stop here...
$this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
break;
}
$vals = $this->extractRowInfo( $row );
$fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
if ( !$fit ) {
$this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
break;
}
}
$this->getResult()->setIndexedTagName_internal(
array( 'query', $this->getModuleName() ),
'item'
);
}
/**
* Validate the 'user' parameter and set the value to compare
* against `revision`.`rev_user_text`
*
* @param string $user
*/
private function prepareUsername( $user ) {
if ( !is_null( $user ) && $user !== '' ) {
$name = User::isIP( $user )
? $user
: User::getCanonicalName( $user, 'valid' );
if ( $name === false ) {
$this->dieUsage( "User name {$user} is not valid", 'param_user' );
} else {
$this->usernames[] = $name;
}
} else {
$this->dieUsage( 'User parameter may not be empty', 'param_user' );
}
}
/**
* Prepares the query and returns the limit of rows requested
*/
private function prepareQuery() {
// We're after the revision table, and the corresponding page
// row for anything we retrieve. We may also need the
// recentchanges row and/or tag summary row.
$user = $this->getUser();
$tables = array( 'page', 'revision' ); // Order may change
$this->addWhere( 'page_id=rev_page' );
// Handle continue parameter
if ( !is_null( $this->params['continue'] ) ) {
$continue = explode( '|', $this->params['continue'] );
$db = $this->getDB();
if ( $this->multiUserMode ) {
$this->dieContinueUsageIf( count( $continue ) != 3 );
$encUser = $db->addQuotes( array_shift( $continue ) );
} else {
$this->dieContinueUsageIf( count( $continue ) != 2 );
}
$encTS = $db->addQuotes( $db->timestamp( $continue[0] ) );
$encId = (int)$continue[1];
$this->dieContinueUsageIf( $encId != $continue[1] );
$op = ( $this->params['dir'] == 'older' ? '<' : '>' );
if ( $this->multiUserMode ) {
$this->addWhere(
"rev_user_text $op $encUser OR " .
"(rev_user_text = $encUser AND " .
"(rev_timestamp $op $encTS OR " .
"(rev_timestamp = $encTS AND " .
"rev_id $op= $encId)))"
);
} else {
$this->addWhere(
"rev_timestamp $op $encTS OR " .
"(rev_timestamp = $encTS AND " .
"rev_id $op= $encId)"
);
}
}
// Don't include any revisions where we're not supposed to be able to
// see the username.
if ( !$user->isAllowed( 'deletedhistory' ) ) {
$bitmask = Revision::DELETED_USER;
} elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
$bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
} else {
$bitmask = 0;
}
if ( $bitmask ) {
$this->addWhere( $this->getDB()->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
}
// We only want pages by the specified users.
if ( $this->prefixMode ) {
$this->addWhere( 'rev_user_text' .
$this->getDB()->buildLike( $this->userprefix, $this->getDB()->anyString() ) );
} else {
$this->addWhereFld( 'rev_user_text', $this->usernames );
}
// ... and in the specified timeframe.
// Ensure the same sort order for rev_user_text and rev_timestamp
// so our query is indexed
if ( $this->multiUserMode ) {
$this->addWhereRange( 'rev_user_text', $this->params['dir'], null, null );
}
$this->addTimestampWhereRange( 'rev_timestamp',
$this->params['dir'], $this->params['start'], $this->params['end'] );
// Include in ORDER BY for uniqueness
$this->addWhereRange( 'rev_id', $this->params['dir'], null, null );
$this->addWhereFld( 'page_namespace', $this->params['namespace'] );
$show = $this->params['show'];
if ( $this->params['toponly'] ) { // deprecated/old param
$this->logFeatureUsage( 'list=usercontribs&uctoponly' );
$show[] = 'top';
}
if ( !is_null( $show ) ) {
$show = array_flip( $show );
if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
|| ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
|| ( isset( $show['top'] ) && isset( $show['!top'] ) )
|| ( isset( $show['new'] ) && isset( $show['!new'] ) )
) {
$this->dieUsageMsg( 'show' );
}
$this->addWhereIf( 'rev_minor_edit = 0', isset( $show['!minor'] ) );
$this->addWhereIf( 'rev_minor_edit != 0', isset( $show['minor'] ) );
$this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
$this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
$this->addWhereIf( 'rev_id != page_latest', isset( $show['!top'] ) );
$this->addWhereIf( 'rev_id = page_latest', isset( $show['top'] ) );
$this->addWhereIf( 'rev_parent_id != 0', isset( $show['!new'] ) );
$this->addWhereIf( 'rev_parent_id = 0', isset( $show['new'] ) );
}
$this->addOption( 'LIMIT', $this->params['limit'] + 1 );
$index = array( 'revision' => 'usertext_timestamp' );
// Mandatory fields: timestamp allows request continuation
// ns+title checks if the user has access rights for this page
// user_text is necessary if multiple users were specified
$this->addFields( array(
'rev_id',
'rev_timestamp',
'page_namespace',
'page_title',
'rev_user',
'rev_user_text',
'rev_deleted'
) );
if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ||
$this->fld_patrolled
) {
if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
$this->dieUsage(
'You need the patrol right to request the patrolled flag',
'permissiondenied'
);
}
// Use a redundant join condition on both
// timestamp and ID so we can use the timestamp
// index
$index['recentchanges'] = 'rc_user_text';
if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) {
// Put the tables in the right order for
// STRAIGHT_JOIN
$tables = array( 'revision', 'recentchanges', 'page' );
$this->addOption( 'STRAIGHT_JOIN' );
$this->addWhere( 'rc_user_text=rev_user_text' );
$this->addWhere( 'rc_timestamp=rev_timestamp' );
$this->addWhere( 'rc_this_oldid=rev_id' );
} else {
$tables[] = 'recentchanges';
$this->addJoinConds( array( 'recentchanges' => array(
'LEFT JOIN', array(
'rc_user_text=rev_user_text',
'rc_timestamp=rev_timestamp',
'rc_this_oldid=rev_id' ) ) ) );
}
}
$this->addTables( $tables );
$this->addFieldsIf( 'rev_page', $this->fld_ids );
$this->addFieldsIf( 'page_latest', $this->fld_flags );
// $this->addFieldsIf( 'rev_text_id', $this->fld_ids ); // Should this field be exposed?
$this->addFieldsIf( 'rev_comment', $this->fld_comment || $this->fld_parsedcomment );
$this->addFieldsIf( 'rev_len', $this->fld_size || $this->fld_sizediff );
$this->addFieldsIf( 'rev_minor_edit', $this->fld_flags );
$this->addFieldsIf( 'rev_parent_id', $this->fld_flags || $this->fld_sizediff || $this->fld_ids );
$this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled );
if ( $this->fld_tags ) {
$this->addTables( 'tag_summary' );
$this->addJoinConds(
array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) )
);
$this->addFields( 'ts_tags' );
}
if ( isset( $this->params['tag'] ) ) {
$this->addTables( 'change_tag' );
$this->addJoinConds(
array( 'change_tag' => array( 'INNER JOIN', array( 'rev_id=ct_rev_id' ) ) )
);
$this->addWhereFld( 'ct_tag', $this->params['tag'] );
}
$this->addOption( 'USE INDEX', $index );
}
/**
* Extract fields from the database row and append them to a result array
*
* @param stdClass $row
* @return array
*/
private function extractRowInfo( $row ) {
$vals = array();
$anyHidden = false;
if ( $row->rev_deleted & Revision::DELETED_TEXT ) {
$vals['texthidden'] = '';
$anyHidden = true;
}
// Any rows where we can't view the user were filtered out in the query.
$vals['userid'] = $row->rev_user;
$vals['user'] = $row->rev_user_text;
if ( $row->rev_deleted & Revision::DELETED_USER ) {
$vals['userhidden'] = '';
$anyHidden = true;
}
if ( $this->fld_ids ) {
$vals['pageid'] = intval( $row->rev_page );
$vals['revid'] = intval( $row->rev_id );
// $vals['textid'] = intval( $row->rev_text_id ); // todo: Should this field be exposed?
if ( !is_null( $row->rev_parent_id ) ) {
$vals['parentid'] = intval( $row->rev_parent_id );
}
}
$title = Title::makeTitle( $row->page_namespace, $row->page_title );
if ( $this->fld_title ) {
ApiQueryBase::addTitleInfo( $vals, $title );
}
if ( $this->fld_timestamp ) {
$vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
}
if ( $this->fld_flags ) {
if ( $row->rev_parent_id == 0 && !is_null( $row->rev_parent_id ) ) {
$vals['new'] = '';
}
if ( $row->rev_minor_edit ) {
$vals['minor'] = '';
}
if ( $row->page_latest == $row->rev_id ) {
$vals['top'] = '';
}
}
if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->rev_comment ) ) {
if ( $row->rev_deleted & Revision::DELETED_COMMENT ) {
$vals['commenthidden'] = '';
$anyHidden = true;
}
$userCanView = Revision::userCanBitfield(
$row->rev_deleted,
Revision::DELETED_COMMENT, $this->getUser()
);
if ( $userCanView ) {
if ( $this->fld_comment ) {
$vals['comment'] = $row->rev_comment;
}
if ( $this->fld_parsedcomment ) {
$vals['parsedcomment'] = Linker::formatComment( $row->rev_comment, $title );
}
}
}
if ( $this->fld_patrolled && $row->rc_patrolled ) {
$vals['patrolled'] = '';
}
if ( $this->fld_size && !is_null( $row->rev_len ) ) {
$vals['size'] = intval( $row->rev_len );
}
if ( $this->fld_sizediff
&& !is_null( $row->rev_len )
&& !is_null( $row->rev_parent_id )
) {
$parentLen = isset( $this->parentLens[$row->rev_parent_id] )
? $this->parentLens[$row->rev_parent_id]
: 0;
$vals['sizediff'] = intval( $row->rev_len - $parentLen );
}
if ( $this->fld_tags ) {
if ( $row->ts_tags ) {
$tags = explode( ',', $row->ts_tags );
$this->getResult()->setIndexedTagName( $tags, 'tag' );
$vals['tags'] = $tags;
} else {
$vals['tags'] = array();
}
}
if ( $anyHidden && $row->rev_deleted & Revision::DELETED_RESTRICTED ) {
$vals['suppressed'] = '';
}
return $vals;
}
private function continueStr( $row ) {
if ( $this->multiUserMode ) {
return "$row->rev_user_text|$row->rev_timestamp|$row->rev_id";
} else {
return "$row->rev_timestamp|$row->rev_id";
}
}
public function getCacheMode( $params ) {
// This module provides access to deleted revisions and patrol flags if
// the requester is logged in
return 'anon-public-user-private';
}
public function getAllowedParams() {
return array(
'limit' => array(
ApiBase::PARAM_DFLT => 10,
ApiBase::PARAM_TYPE => 'limit',
ApiBase::PARAM_MIN => 1,
ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
),
'start' => array(
ApiBase::PARAM_TYPE => 'timestamp'
),
'end' => array(
ApiBase::PARAM_TYPE => 'timestamp'
),
'continue' => null,
'user' => array(
ApiBase::PARAM_ISMULTI => true
),
'userprefix' => null,
'dir' => array(
ApiBase::PARAM_DFLT => 'older',
ApiBase::PARAM_TYPE => array(
'newer',
'older'
)
),
'namespace' => array(
ApiBase::PARAM_ISMULTI => true,
ApiBase::PARAM_TYPE => 'namespace'
),
'prop' => array(
ApiBase::PARAM_ISMULTI => true,
ApiBase::PARAM_DFLT => 'ids|title|timestamp|comment|size|flags',
ApiBase::PARAM_TYPE => array(
'ids',
'title',
'timestamp',
'comment',
'parsedcomment',
'size',
'sizediff',
'flags',
'patrolled',
'tags'
)
),
'show' => array(
ApiBase::PARAM_ISMULTI => true,
ApiBase::PARAM_TYPE => array(
'minor',
'!minor',
'patrolled',
'!patrolled',
'top',
'!top',
'new',
'!new',
)
),
'tag' => null,
'toponly' => array(
ApiBase::PARAM_DFLT => false,
ApiBase::PARAM_DEPRECATED => true,
),
);
}
public function getParamDescription() {
$p = $this->getModulePrefix();
$RCMaxAge = $this->getConfig()->get( 'RCMaxAge' );
return array(
'limit' => 'The maximum number of contributions to return',
'start' => 'The start timestamp to return from',
'end' => 'The end timestamp to return to',
'continue' => 'When more results are available, use this to continue',
'user' => 'The users to retrieve contributions for',
'userprefix' => array(
"Retrieve contributions for all users whose names begin with this value.",
"Overrides {$p}user",
),
'dir' => $this->getDirectionDescription( $p ),
'namespace' => 'Only list contributions in these namespaces',
'prop' => array(
'Include additional pieces of information',
' ids - Adds the page ID and revision ID',
' title - Adds the title and namespace ID of the page',
' timestamp - Adds the timestamp of the edit',
' comment - Adds the comment of the edit',
' parsedcomment - Adds the parsed comment of the edit',
' size - Adds the new size of the edit',
' sizediff - Adds the size delta of the edit against its parent',
' flags - Adds flags of the edit',
' patrolled - Tags patrolled edits',
' tags - Lists tags for the edit',
),
'show' => array(
"Show only items that meet thse criteria, e.g. non minor edits only: {$p}show=!minor",
"NOTE: If {$p}show=patrolled or {$p}show=!patrolled is set, revisions older than",
"\$wgRCMaxAge ($RCMaxAge) won't be shown",
),
'tag' => 'Only list revisions tagged with this tag',
'toponly' => 'Only list changes which are the latest revision',
);
}
public function getDescription() {
return 'Get all edits by a user.';
}
public function getExamples() {
return array(
'api.php?action=query&list=usercontribs&ucuser=YurikBot',
'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
);
}
public function getHelpUrls() {
return 'https://www.mediawiki.org/wiki/API:Usercontribs';
}
}
|