3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
23 * Pager for Special:Contributions
26 class ContribsPager
extends ReverseChronologicalPager
{
28 public $mDefaultDirection = IndexPager
::DIR_DESCENDING
;
31 public $namespace = '';
33 public $preventClickjacking = false;
41 protected $mParentLens;
43 function __construct( IContextSource
$context, array $options ) {
44 parent
::__construct( $context );
53 foreach ( $msgs as $msg ) {
54 $this->messages
[$msg] = $this->msg( $msg )->escaped();
57 $this->target
= isset( $options['target'] ) ?
$options['target'] : '';
58 $this->contribs
= isset( $options['contribs'] ) ?
$options['contribs'] : 'users';
59 $this->namespace = isset( $options['namespace'] ) ?
$options['namespace'] : '';
60 $this->tagFilter
= isset( $options['tagfilter'] ) ?
$options['tagfilter'] : false;
61 $this->nsInvert
= isset( $options['nsInvert'] ) ?
$options['nsInvert'] : false;
62 $this->associated
= isset( $options['associated'] ) ?
$options['associated'] : false;
64 $this->deletedOnly
= !empty( $options['deletedOnly'] );
65 $this->topOnly
= !empty( $options['topOnly'] );
66 $this->newOnly
= !empty( $options['newOnly'] );
68 $year = isset( $options['year'] ) ?
$options['year'] : false;
69 $month = isset( $options['month'] ) ?
$options['month'] : false;
70 $this->getDateCond( $year, $month );
72 // Most of this code will use the 'contributions' group DB, which can map to slaves
73 // with extra user based indexes or partioning by user. The additional metadata
74 // queries should use a regular slave since the lookup pattern is not all by user.
75 $this->mDbSecondary
= wfGetDB( DB_SLAVE
); // any random slave
76 $this->mDb
= wfGetDB( DB_SLAVE
, 'contributions' );
79 function getDefaultQuery() {
80 $query = parent
::getDefaultQuery();
81 $query['target'] = $this->target
;
87 * This method basically executes the exact same code as the parent class, though with
88 * a hook added, to allow extensions to add additional queries.
90 * @param string $offset Index offset, inclusive
91 * @param int $limit Exact query limit
92 * @param bool $descending Query direction, false for ascending, true for descending
93 * @return ResultWrapper
95 function reallyDoQuery( $offset, $limit, $descending ) {
96 list( $tables, $fields, $conds, $fname, $options, $join_conds ) = $this->buildQueryInfo(
103 * This hook will allow extensions to add in additional queries, so they can get their data
104 * in My Contributions as well. Extensions should append their results to the $data array.
106 * Extension queries have to implement the navbar requirement as well. They should
107 * - have a column aliased as $pager->getIndexField()
109 * - have a WHERE-clause that compares the $pager->getIndexField()-equivalent column to the offset
110 * - have the ORDER BY specified based upon the details provided by the navbar
112 * See includes/Pager.php buildQueryInfo() method on how to build LIMIT, WHERE & ORDER BY
114 * &$data: an array of results of all contribs queries
115 * $pager: the ContribsPager object hooked into
116 * $offset: see phpdoc above
117 * $limit: see phpdoc above
118 * $descending: see phpdoc above
120 $data = [ $this->mDb
->select(
121 $tables, $fields, $conds, $fname, $options, $join_conds
124 'ContribsPager::reallyDoQuery',
125 [ &$data, $this, $offset, $limit, $descending ]
130 // loop all results and collect them in an array
131 foreach ( $data as $query ) {
132 foreach ( $query as $i => $row ) {
133 // use index column as key, allowing us to easily sort in PHP
134 $result[$row->{$this->getIndexField()} . "-$i"] = $row;
146 $result = array_slice( $result, 0, $limit );
148 // get rid of array keys
149 $result = array_values( $result );
151 return new FakeResultWrapper( $result );
154 function getQueryInfo() {
155 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
157 $user = $this->getUser();
158 $conds = array_merge( $userCond, $this->getNamespaceCond() );
160 // Paranoia: avoid brute force searches (bug 17342)
161 if ( !$user->isAllowed( 'deletedhistory' ) ) {
162 $conds[] = $this->mDb
->bitAnd( 'rev_deleted', Revision
::DELETED_USER
) . ' = 0';
163 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
164 $conds[] = $this->mDb
->bitAnd( 'rev_deleted', Revision
::SUPPRESSED_USER
) .
165 ' != ' . Revision
::SUPPRESSED_USER
;
168 # Don't include orphaned revisions
169 $join_cond['page'] = Revision
::pageJoinCond();
170 # Get the current user name for accounts
171 $join_cond['user'] = Revision
::userJoinCond();
175 $options['USE INDEX'] = [ 'revision' => $index ];
180 'fields' => array_merge(
181 Revision
::selectFields(),
182 Revision
::selectUserFields(),
183 [ 'page_namespace', 'page_title', 'page_is_new',
184 'page_latest', 'page_is_redirect', 'page_len' ]
187 'options' => $options,
188 'join_conds' => $join_cond
191 ChangeTags
::modifyDisplayQuery(
192 $queryInfo['tables'],
193 $queryInfo['fields'],
195 $queryInfo['join_conds'],
196 $queryInfo['options'],
200 Hooks
::run( 'ContribsPager::getQueryInfo', [ &$this, &$queryInfo ] );
205 function getUserCond() {
208 $tables = [ 'revision', 'page', 'user' ];
210 if ( $this->contribs
== 'newbie' ) {
211 $max = $this->mDb
->selectField( 'user', 'max(user_id)', false, __METHOD__
);
212 $condition[] = 'rev_user >' . (int)( $max - $max / 100 );
213 # ignore local groups with the bot right
214 # @todo FIXME: Global groups may have 'bot' rights
215 $groupsWithBotPermission = User
::getGroupsWithPermission( 'bot' );
216 if ( count( $groupsWithBotPermission ) ) {
217 $tables[] = 'user_groups';
218 $condition[] = 'ug_group IS NULL';
219 $join_conds['user_groups'] = [
221 'ug_user = rev_user',
222 'ug_group' => $groupsWithBotPermission
227 $uid = User
::idFromName( $this->target
);
229 $condition['rev_user'] = $uid;
230 $index = 'user_timestamp';
232 $condition['rev_user_text'] = $this->target
;
233 $index = 'usertext_timestamp';
237 if ( $this->deletedOnly
) {
238 $condition[] = 'rev_deleted != 0';
241 if ( $this->topOnly
) {
242 $condition[] = 'rev_id = page_latest';
245 if ( $this->newOnly
) {
246 $condition[] = 'rev_parent_id = 0';
249 return [ $tables, $index, $condition, $join_conds ];
252 function getNamespaceCond() {
253 if ( $this->namespace !== '' ) {
254 $selectedNS = $this->mDb
->addQuotes( $this->namespace );
255 $eq_op = $this->nsInvert ?
'!=' : '=';
256 $bool_op = $this->nsInvert ?
'AND' : 'OR';
258 if ( !$this->associated
) {
259 return [ "page_namespace $eq_op $selectedNS" ];
262 $associatedNS = $this->mDb
->addQuotes(
263 MWNamespace
::getAssociated( $this->namespace )
267 "page_namespace $eq_op $selectedNS " .
269 " page_namespace $eq_op $associatedNS"
276 function getIndexField() {
277 return 'rev_timestamp';
280 function doBatchLookups() {
281 # Do a link batch query
282 $this->mResult
->seek( 0 );
284 $this->mParentLens
= [];
285 $batch = new LinkBatch();
286 # Give some pointers to make (last) links
287 foreach ( $this->mResult
as $row ) {
288 if ( isset( $row->rev_parent_id
) && $row->rev_parent_id
) {
289 $parentRevIds[] = $row->rev_parent_id
;
291 if ( isset( $row->rev_id
) ) {
292 $this->mParentLens
[$row->rev_id
] = $row->rev_len
;
293 if ( $this->contribs
=== 'newbie' ) { // multiple users
294 $batch->add( NS_USER
, $row->user_name
);
295 $batch->add( NS_USER_TALK
, $row->user_name
);
297 $batch->add( $row->page_namespace
, $row->page_title
);
300 # Fetch rev_len for revisions not already scanned above
301 $this->mParentLens +
= Revision
::getParentLengths(
303 array_diff( $parentRevIds, array_keys( $this->mParentLens
) )
306 $this->mResult
->seek( 0 );
312 function getStartBody() {
313 return "<ul class=\"mw-contributions-list\">\n";
319 function getEndBody() {
324 * Generates each row in the contributions list.
326 * Contributions which are marked "top" are currently on top of the history.
327 * For these contributions, a [rollback] link is shown for users with roll-
328 * back privileges. The rollback link restores the most recent version that
329 * was not written by the target user.
331 * @todo This would probably look a lot nicer in a table.
335 function formatRow( $row ) {
341 * There may be more than just revision rows. To make sure that we'll only be processing
342 * revisions here, let's _try_ to build a revision out of our row (without displaying
343 * notices though) and then trying to grab data from the built object. If we succeed,
344 * we're definitely dealing with revision data and we may proceed, if not, we'll leave it
345 * to extensions to subscribe to the hook to parse the row.
347 MediaWiki\
suppressWarnings();
349 $rev = new Revision( $row );
350 $validRevision = (bool)$rev->getId();
351 } catch ( Exception
$e ) {
352 $validRevision = false;
354 MediaWiki\restoreWarnings
();
356 if ( $validRevision ) {
359 $page = Title
::newFromRow( $row );
360 $link = Linker
::link(
362 htmlspecialchars( $page->getPrefixedText() ),
363 [ 'class' => 'mw-contributions-title' ],
364 $page->isRedirect() ?
[ 'redirect' => 'no' ] : []
366 # Mark current revisions
368 $user = $this->getUser();
369 if ( $row->rev_id
== $row->page_latest
) {
370 $topmarktext .= '<span class="mw-uctop">' . $this->messages
['uctop'] . '</span>';
372 if ( !$row->page_is_new
&& $page->quickUserCan( 'rollback', $user )
373 && $page->quickUserCan( 'edit', $user )
375 $this->preventClickjacking();
376 $topmarktext .= ' ' . Linker
::generateRollback( $rev, $this->getContext() );
379 # Is there a visible previous revision?
380 if ( $rev->userCan( Revision
::DELETED_TEXT
, $user ) && $rev->getParentId() !== 0 ) {
381 $difftext = Linker
::linkKnown(
383 $this->messages
['diff'],
387 'oldid' => $row->rev_id
391 $difftext = $this->messages
['diff'];
393 $histlink = Linker
::linkKnown(
395 $this->messages
['hist'],
397 [ 'action' => 'history' ]
400 if ( $row->rev_parent_id
=== null ) {
401 // For some reason rev_parent_id isn't populated for this row.
402 // Its rumoured this is true on wikipedia for some revisions (bug 34922).
403 // Next best thing is to have the total number of bytes.
404 $chardiff = ' <span class="mw-changeslist-separator">. .</span> ';
405 $chardiff .= Linker
::formatRevisionSize( $row->rev_len
);
406 $chardiff .= ' <span class="mw-changeslist-separator">. .</span> ';
409 if ( isset( $this->mParentLens
[$row->rev_parent_id
] ) ) {
410 $parentLen = $this->mParentLens
[$row->rev_parent_id
];
413 $chardiff = ' <span class="mw-changeslist-separator">. .</span> ';
414 $chardiff .= ChangesList
::showCharacterDifference(
419 $chardiff .= ' <span class="mw-changeslist-separator">. .</span> ';
422 $lang = $this->getLanguage();
423 $comment = $lang->getDirMark() . Linker
::revComment( $rev, false, true );
424 $date = $lang->userTimeAndDate( $row->rev_timestamp
, $user );
425 if ( $rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
426 $d = Linker
::linkKnown(
428 htmlspecialchars( $date ),
429 [ 'class' => 'mw-changeslist-date' ],
430 [ 'oldid' => intval( $row->rev_id
) ]
433 $d = htmlspecialchars( $date );
435 if ( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
436 $d = '<span class="history-deleted">' . $d . '</span>';
439 # Show user names for /newbies as there may be different users.
440 # Note that we already excluded rows with hidden user names.
441 if ( $this->contribs
== 'newbie' ) {
442 $userlink = ' . . ' . $lang->getDirMark()
443 . Linker
::userLink( $rev->getUser(), $rev->getUserText() );
444 $userlink .= ' ' . $this->msg( 'parentheses' )->rawParams(
445 Linker
::userTalkLink( $rev->getUser(), $rev->getUserText() ) )->escaped() . ' ';
451 if ( $rev->getParentId() === 0 ) {
452 $flags[] = ChangesList
::flag( 'newpage' );
455 if ( $rev->isMinor() ) {
456 $flags[] = ChangesList
::flag( 'minor' );
459 $del = Linker
::getRevDeleteLink( $user, $rev, $page );
464 $diffHistLinks = $this->msg( 'parentheses' )
465 ->rawParams( $difftext . $this->messages
['pipe-separator'] . $histlink )
469 list( $tagSummary, $newClasses ) = ChangeTags
::formatSummaryRow(
474 $classes = array_merge( $classes, $newClasses );
479 'diffHistLinks' => $diffHistLinks,
480 'charDifference' => $chardiff,
482 'articleLink' => $link,
483 'userlink' => $userlink,
484 'logText' => $comment,
485 'topmarktext' => $topmarktext,
486 'tagSummary' => $tagSummary,
489 # Denote if username is redacted for this edit
490 if ( $rev->isDeleted( Revision
::DELETED_USER
) ) {
491 $templateParams['rev-deleted-user-contribs'] =
492 $this->msg( 'rev-deleted-user-contribs' )->escaped();
495 $templateParser = new TemplateParser();
496 $ret = $templateParser->processTemplate(
497 'SpecialContributionsLine',
502 // Let extensions add data
503 Hooks
::run( 'ContributionsLineEnding', [ $this, &$ret, $row, &$classes ] );
505 // TODO: Handle exceptions in the catch block above. Do any extensions rely on
506 // receiving empty rows?
508 if ( $classes === [] && $ret === '' ) {
509 wfDebug( "Dropping Special:Contribution row that could not be formatted\n" );
510 return "<!-- Could not format Special:Contribution row. -->\n";
513 // FIXME: The signature of the ContributionsLineEnding hook makes it
514 // very awkward to move this LI wrapper into the template.
515 return Html
::rawElement( 'li', [ 'class' => $classes ], $ret ) . "\n";
519 * Overwrite Pager function and return a helpful comment
522 function getSqlComment() {
523 if ( $this->namespace ||
$this->deletedOnly
) {
524 // potentially slow, see CR r58153
525 return 'contributions page filtered for namespace or RevisionDeleted edits';
527 return 'contributions page unfiltered';
531 protected function preventClickjacking() {
532 $this->preventClickjacking
= true;
538 public function getPreventClickjacking() {
539 return $this->preventClickjacking
;