-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpecialGlobalContributions.php
545 lines (468 loc) · 16.6 KB
/
SpecialGlobalContributions.php
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
<?php
/**
* Implements Special:GlobalContributions
*
* 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
* @ingroup SpecialPage
*/
/**
* Special:GlobalContributions, show user contributions in a paged list
*
* @ingroup SpecialPage
*/
class SpecialGlobalContributions extends SpecialContributions {
public function __construct() {
SpecialPage::__construct( 'GlobalContributions' );
}
public function execute( $par ) {
$this->setHeaders();
$this->outputHeader();
$out = $this->getOutput();
$out->addModuleStyles( 'mediawiki.special' );
$this->opts = array();
$request = $this->getRequest();
if ( $par !== null ) {
$target = $par;
} else {
$target = $request->getVal( 'target' );
}
// check for radiobox
if ( $request->getVal( 'contribs' ) == 'newbie' ) {
$target = 'newbies';
$this->opts['contribs'] = 'newbie';
} elseif ( $par === 'newbies' ) { // b/c for WMF
$target = 'newbies';
$this->opts['contribs'] = 'newbie';
} else {
$this->opts['contribs'] = 'user';
}
$this->opts['deletedOnly'] = $request->getBool( 'deletedOnly' );
if ( !strlen( $target ) ) {
$out->addHTML( $this->getForm() );
return;
}
$user = $this->getUser();
$this->opts['limit'] = $request->getInt( 'limit', $user->getOption( 'rclimit' ) );
$this->opts['target'] = $target;
$this->opts['topOnly'] = $request->getBool( 'topOnly' );
$nt = Title::makeTitleSafe( NS_USER, $target );
if ( !$nt ) {
$out->addHTML( $this->getForm() );
return;
}
$userObj = User::newFromName( $nt->getText(), false );
if ( !$userObj ) {
$out->addHTML( $this->getForm() );
return;
}
$id = $userObj->getID();
if ( $this->opts['contribs'] != 'newbie' ) {
$target = $nt->getText();
$out->addSubtitle( $this->contributionsSub( $userObj ) );
$out->setHTMLTitle( $this->msg( 'pagetitle', $this->msg( 'contributions-title', $target )->plain() ) );
$this->getSkin()->setRelevantUser( $userObj );
} else {
$out->addSubtitle( $this->msg( 'sp-contributions-newbies-sub' ) );
$out->setHTMLTitle( $this->msg( 'pagetitle', $this->msg( 'sp-contributions-newbies-title' )->plain() ) );
}
if ( ( $ns = $request->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
$this->opts['namespace'] = intval( $ns );
} else {
$this->opts['namespace'] = '';
}
$this->opts['associated'] = $request->getBool( 'associated' );
$this->opts['nsInvert'] = (bool) $request->getVal( 'nsInvert' );
$this->opts['tagfilter'] = (string) $request->getVal( 'tagfilter' );
// Allows reverts to have the bot flag in recent changes. It is just here to
// be passed in the form at the top of the page
if ( $user->isAllowed( 'markbotedits' ) && $request->getBool( 'bot' ) ) {
$this->opts['bot'] = '1';
}
$skip = $request->getText( 'offset' ) || $request->getText( 'dir' ) == 'prev';
# Offset overrides year/month selection
if ( $skip ) {
$this->opts['year'] = '';
$this->opts['month'] = '';
} else {
$this->opts['year'] = $request->getIntOrNull( 'year' );
$this->opts['month'] = $request->getIntOrNull( 'month' );
}
$feedType = $request->getVal( 'feed' );
if ( $feedType ) {
// Maintain some level of backwards compatability
// If people request feeds using the old parameters, redirect to API
$apiParams = array(
'action' => 'feedcontributions',
'feedformat' => $feedType,
'user' => $target,
);
if ( $this->opts['topOnly'] ) {
$apiParams['toponly'] = true;
}
if ( $this->opts['deletedOnly'] ) {
$apiParams['deletedonly'] = true;
}
if ( $this->opts['tagfilter'] !== '' ) {
$apiParams['tagfilter'] = $this->opts['tagfilter'];
}
if ( $this->opts['namespace'] !== '' ) {
$apiParams['namespace'] = $this->opts['namespace'];
}
if ( $this->opts['year'] !== null ) {
$apiParams['year'] = $this->opts['year'];
}
if ( $this->opts['month'] !== null ) {
$apiParams['month'] = $this->opts['month'];
}
$url = wfScript( 'api' ) . '?' . wfArrayToCGI( $apiParams );
$out->redirect( $url, '301' );
return;
}
// Add RSS/atom links
$this->addFeedLinks( array( 'action' => 'feedcontributions', 'user' => $target ) );
if ( Hooks::run( 'SpecialContributionsBeforeMainOutput', array( $id ) ) ) {
$out->addHTML( $this->getForm() );
$pager = new GlobalContribsPager( $this->getContext(), array(
'target' => $target,
'contribs' => $this->opts['contribs'],
'namespace' => $this->opts['namespace'],
'year' => $this->opts['year'],
'month' => $this->opts['month'],
'deletedOnly' => $this->opts['deletedOnly'],
'topOnly' => $this->opts['topOnly'],
'nsInvert' => $this->opts['nsInvert'],
'associated' => $this->opts['associated'],
) );
if ( !$pager->getNumRows() ) {
$out->addWikiMsg( 'nocontribs', $target );
} else {
# Show a message about slave lag, if applicable
$lag = wfGetLB()->safeGetLag( $pager->getDatabase() );
if ( $lag > 0 ) {
$out->showLagWarning( $lag );
}
$out->addHTML(
'<p>' . $pager->getNavigationBar() . '</p>' .
$pager->getBody() .
'<p>' . $pager->getNavigationBar() . '</p>'
);
}
$out->preventClickjacking( $pager->getPreventClickjacking() );
# Show the appropriate "footer" message - WHOIS tools, etc.
if ( $this->opts['contribs'] == 'newbie' ) {
$message = 'sp-contributions-footer-newbies';
} elseif ( IP::isIPAddress( $target ) ) {
$message = 'sp-contributions-footer-anon';
} elseif ( $userObj->isAnon() ) {
// No message for non-existing users
$message = '';
} else {
$message = 'sp-contributions-footer';
}
if ( $message ) {
if ( !$this->msg( $message, $target )->isDisabled() ) {
$out->wrapWikiMsg(
"<div class='mw-contributions-footer'>\n$1\n</div>",
array( $message, $target )
);
}
}
}
}
}
/**
* Pager for Special:GlobalContributions
* @ingroup SpecialPage Pager
*/
class GlobalContribsPager extends ContribsPager {
/**
* This method basically executes the exact same code as the parent class, though with
* a hook added, to allow extentions to add additional queries.
*
* @param $offset String: index offset, inclusive
* @param $limit Integer: exact query limit
* @param $descending Boolean: query direction, false for ascending, true for descending
* @return ResultWrapper
*/
function reallyDoQuery( $offset, $limit, $descending ) {
list( $tables, $fields, $conds, $fname, $options, $join_conds ) = $this->buildQueryInfo( $offset, $limit, $descending );
$pager = $this;
/*
* This hook will allow extensions to add in additional queries, so they can get their data
* in My Contributions as well. Extensions should append their results to the $data array.
*
* Extension queries have to implement the navbar requirement as well. They should
* - have a column aliased as $pager->getIndexField()
* - have LIMIT set
* - have a WHERE-clause that compares the $pager->getIndexField()-equivalent column to the offset
* - have the ORDER BY specified based upon the details provided by the navbar
*
* See includes/Pager.php buildQueryInfo() method on how to build LIMIT, WHERE & ORDER BY
*
* &$data: an array of results of all contribs queries
* $pager: the ContribsPager object hooked into
* $offset: see phpdoc above
* $limit: see phpdoc above
* $descending: see phpdoc above
*/
$data = array();
global $wgConf;
foreach ( $wgConf->wikis as $wiki ) {
$dbr = wfGetDB( DB_SLAVE, array(), $wiki );
$thisData = $dbr->select( $tables, $fields, $conds, $fname, $options, $join_conds );
$newData = array();
foreach ( $thisData as $i => $row ) {
//$dat[$i] -> wiki = $wiki;
$row->wiki = $wiki;
$newData[] = $row;
//$newData[$i] -> wiki = $wiki;
}
$data[] = $newData;
}
//$data = array( GlobalDB::selectAll( $tables, $fields, $conds, $fname, $options, $join_conds ), );
Hooks::run( 'GlobalContribsPager::reallyDoQuery', array( &$data, $pager, $offset, $limit, $descending ) );
$result = array();
// loop all results and collect them in an array
foreach ( $data as $j => $query ) {
foreach ( $query as $i => $row ) {
// use index column as key, allowing us to easily sort in PHP
$result[$row->{$this->getIndexField()} . "-$i"] = $row;
}
}
// sort results
if ( $descending ) {
ksort( $result );
} else {
krsort( $result );
}
// enforce limit
$result = array_slice( $result, 0, $limit );
// get rid of array keys
$result = array_values( $result );
return new FakeResultWrapper( $result );
}
function getUserCond() {
$condition = array();
$join_conds = array();
$tables = array( 'revision', 'page' );
$uid = User::idFromName( $this->target );
if ( $uid ) {
$condition['rev_user'] = $uid;
$index = 'user_timestamp';
} else {
$condition['rev_user_text'] = $this->target;
$index = 'usertext_timestamp';
}
if ( $this->deletedOnly ) {
$condition[] = "rev_deleted != '0'";
}
if ( $this->topOnly ) {
$condition[] = 'rev_id = page_latest';
}
return array( $tables, $index, $condition, $join_conds );
}
function getQueryInfo() {
list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
$user = $this->getUser();
$conds = array_merge( $userCond, $this->getNamespaceCond() );
// Paranoia: avoid brute force searches (bug 17342)
if ( !$user->isAllowed( 'deletedhistory' ) ) {
$conds[] = $this->mDb->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0';
} elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
$conds[] = $this->mDb->bitAnd( 'rev_deleted', Revision::SUPPRESSED_USER ) .
' != ' . Revision::SUPPRESSED_USER;
}
# Don't include orphaned revisions
$join_cond['page'] = Revision::pageJoinCond();
# Get the current user name for accounts
$join_cond['user'] = Revision::userJoinCond();
$options = array();
if ( $index ) {
$options['USE INDEX'] = array( 'revision' => $index );
}
$queryInfo = array(
'tables' => $tables,
'fields' => array_merge(
Revision::selectFields(),
array( 'page_namespace', 'page_title', 'page_is_new',
'page_latest', 'page_is_redirect', 'page_len' )
),
'conds' => $conds,
'options' => $options,
'join_conds' => $join_cond
);
ChangeTags::modifyDisplayQuery(
$queryInfo['tables'],
$queryInfo['fields'],
$queryInfo['conds'],
$queryInfo['join_conds'],
$queryInfo['options'],
$this->tagFilter
);
Hooks::run( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
return $queryInfo;
}
function doBatchLookups() {
global $wgConf;
# Do a link batch query
$this->mResult->seek( 0 );
$revIds = array();
$batch = new LinkBatch();
# Give some pointers to make (last) links
foreach ( $this->mResult as $row ) {
if ( isset( $row->rev_parent_id ) && $row->rev_parent_id ) {
$revIds[] = $row->rev_parent_id;
}
if ( isset( $row->rev_id ) ) {
$batch->add( $row->page_namespace, $row->page_title );
}
}
foreach ( $wgConf->wikis as $wiki ) {
$this->mParentLensArr[$wiki] = Revision::getParentLengths( wfGetDB( DB_SLAVE, array(), $wiki ), $revIds );
}
$batch->execute();
$this->mResult->seek( 0 );
}
function formatRow( $row ) {
global $wgConf;
$ret = '';
$classes = array();
/*
* There may be more than just revision rows. To make sure that we'll only be processing
* revisions here, let's _try_ to build a revision out of our row (without displaying
* notices though) and then trying to grab data from the built object. If we succeed,
* we're definitely dealing with revision data and we may proceed, if not, we'll leave it
* to extensions to subscribe to the hook to parse the row.
*/
wfSuppressWarnings();
$rev = new Revision( $row );
$validRevision = $rev->getParentId() !== null;
wfRestoreWarnings();
if ( $validRevision ) {
$classes = array();
$page = Title::newFromRow( $row );
$url = $wgConf->get( 'wgServer', $row->wiki ) . str_replace( '$1', $page->getFullText(), $wgConf->get( 'wgArticlePath', $row->wiki ) );
$link = Html::element(
'a',
array(
'href' => $url,
'class' => 'mw-contributions-title',
),
htmlspecialchars( $page->getPrefixedText() )
);
# Mark current revisions
$topmarktext = '';
$user = $this->getUser();
if ( $row->rev_id == $row->page_latest ) {
$topmarktext .= '<span class="mw-uctop">' . $this->messages['uctop'] . '</span>';
# Add rollback link
if (
!$row->page_is_new &&
$page->quickUserCan( 'rollback', $user ) &&
$page->quickUserCan( 'edit', $user )
)
{
$this->preventClickjacking();
$topmarktext .= ' ' . Linker::generateRollback( $rev, $this->getContext() );
}
}
# Is there a visible previous revision?
if ( $rev->userCan( Revision::DELETED_TEXT, $user ) && $rev->getParentId() !== 0 ) {
$difftext = Html::element(
'a',
array(
'href' => $url . '?diff=prev&oldid=' . $row->rev_id,
),
$this->messages['diff']
);
} else {
$difftext = $this->messages['diff'];
}
$histlink = Html::element(
'a',
array(
'href' => $url . '?action=history',
),
$this->messages['hist']
);
if ( $row->rev_parent_id === null ) {
// For some reason rev_parent_id isn't populated for this row.
// Its rumoured this is true on wikipedia for some revisions (bug 34922).
// Next best thing is to have the total number of bytes.
$chardiff = ' <span class="mw-changeslist-separator">. .</span> ' . Linker::formatRevisionSize( $row->rev_len ) . ' <span class="mw-changeslist-separator">. .</span> ';
} else {
$parentLen = isset( $this->mParentLensArr[$row->wiki][$row->rev_parent_id] ) ? $this->mParentLensArr[$row->wiki][$row->rev_parent_id] : 0;
$chardiff = ' <span class="mw-changeslist-separator">. .</span> ' . ChangesList::showCharacterDifference(
$parentLen, $row->rev_len, $this->getContext() ) . ' <span class="mw-changeslist-separator">. .</span> ';
}
$lang = $this->getLanguage();
$comment = $lang->getDirMark() . Linker::revComment( $rev, false, true );
$date = $lang->userTimeAndDate( $row->rev_timestamp, $user );
if ( $rev->userCan( Revision::DELETED_TEXT, $user ) ) {
$d = Html::element(
'a',
array(
'href' => $url . '?oldid=' . intval( $row->rev_id ),
'class' => 'mw-changeslist-date',
),
htmlspecialchars( $date )
);
} else {
$d = htmlspecialchars( $date );
}
if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
$d = '<span class="history-deleted">' . $d . '</span>';
}
# Show user names for /newbies as there may be different users.
# Note that we already excluded rows with hidden user names.
if ( $this->contribs == 'newbie' ) {
$userlink = ' . . ' . Linker::userLink( $rev->getUser(), $rev->getUserText() );
$userlink .= ' ' . $this->msg( 'parentheses' )->rawParams(
Linker::userTalkLink( $rev->getUser(), $rev->getUserText() ) )->escaped() . ' ';
} else {
$userlink = '';
}
if ( $rev->getParentId() === 0 ) {
$nflag = ChangesList::flag( 'newpage' );
} else {
$nflag = '';
}
if ( $rev->isMinor() ) {
$mflag = ChangesList::flag( 'minor' );
} else {
$mflag = '';
}
$diffHistLinks = $this->msg( 'parentheses' )->rawParams( $difftext . $this->messages['pipe-separator'] . $histlink )->escaped();
$ret = "{$d} {$diffHistLinks}{$chardiff}{$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
# Denote if username is redacted for this edit
if ( $rev->isDeleted( Revision::DELETED_USER ) ) {
$ret .= ' <strong>' . $this->msg( 'rev-deleted-user-contribs' )->escaped() . '</strong>';
}
# Tags, if any.
//list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow( $row->ts_tags, 'contributions' );
//$classes = array_merge( $classes, $newClasses );
//$ret .= " $tagSummary";
}
// Let extensions add data
Hooks::run( 'ContributionsLineEnding', array( $this, &$ret, $row, &$classes ) );
$wiki = "<span class='gc-wiki'>{$row->wiki} - </span>";
$classes = implode( ' ', $classes );
$ret = "<li class=\"$classes\">$wiki$ret</li>\n";
return $ret;
}
}