3 * Implements Special:DeletedContributions
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 * Implements Special:DeletedContributions to display archived revisions
26 * @ingroup SpecialPage
28 class DeletedContribsPager
extends IndexPager
{
29 public $mDefaultDirection = IndexPager
::DIR_DESCENDING
;
32 public $namespace = '';
36 * @var string Navigation bar with paging links.
38 protected $mNavigationBar;
40 function __construct( IContextSource
$context, $target, $namespace = false ) {
41 parent
::__construct( $context );
42 $msgs = array( 'deletionlog', 'undeleteviewlink', 'diff' );
43 foreach ( $msgs as $msg ) {
44 $this->messages
[$msg] = $this->msg( $msg )->escaped();
46 $this->target
= $target;
47 $this->namespace = $namespace;
48 $this->mDb
= wfGetDB( DB_SLAVE
, 'contributions' );
51 function getDefaultQuery() {
52 $query = parent
::getDefaultQuery();
53 $query['target'] = $this->target
;
58 function getQueryInfo() {
59 list( $index, $userCond ) = $this->getUserCond();
60 $conds = array_merge( $userCond, $this->getNamespaceCond() );
61 $user = $this->getUser();
62 // Paranoia: avoid brute force searches (bug 17792)
63 if ( !$user->isAllowed( 'deletedhistory' ) ) {
64 $conds[] = $this->mDb
->bitAnd( 'ar_deleted', Revision
::DELETED_USER
) . ' = 0';
65 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
66 $conds[] = $this->mDb
->bitAnd( 'ar_deleted', Revision
::SUPPRESSED_USER
) .
67 ' != ' . Revision
::SUPPRESSED_USER
;
71 'tables' => array( 'archive' ),
73 'ar_rev_id', 'ar_namespace', 'ar_title', 'ar_timestamp', 'ar_comment',
74 'ar_minor_edit', 'ar_user', 'ar_user_text', 'ar_deleted'
77 'options' => array( 'USE INDEX' => $index )
82 * This method basically executes the exact same code as the parent class, though with
83 * a hook added, to allow extensions to add additional queries.
85 * @param string $offset Index offset, inclusive
86 * @param int $limit Exact query limit
87 * @param bool $descending Query direction, false for ascending, true for descending
88 * @return ResultWrapper
90 function reallyDoQuery( $offset, $limit, $descending ) {
93 $data = array( parent
::reallyDoQuery( $offset, $limit, $descending ) );
95 // This hook will allow extensions to add in additional queries, nearly
96 // identical to ContribsPager::reallyDoQuery.
98 'DeletedContribsPager::reallyDoQuery',
99 array( &$data, $pager, $offset, $limit, $descending )
104 // loop all results and collect them in an array
105 foreach ( $data as $query ) {
106 foreach ( $query as $i => $row ) {
107 // use index column as key, allowing us to easily sort in PHP
108 $result[$row->{$this->getIndexField()} . "-$i"] = $row;
120 $result = array_slice( $result, 0, $limit );
122 // get rid of array keys
123 $result = array_values( $result );
125 return new FakeResultWrapper( $result );
128 function getUserCond() {
129 $condition = array();
131 $condition['ar_user_text'] = $this->target
;
132 $index = 'usertext_timestamp';
134 return array( $index, $condition );
137 function getIndexField() {
138 return 'ar_timestamp';
141 function getStartBody() {
145 function getEndBody() {
149 function getNavigationBar() {
150 if ( isset( $this->mNavigationBar
) ) {
151 return $this->mNavigationBar
;
155 'prev' => $this->msg( 'pager-newer-n' )->numParams( $this->mLimit
)->escaped(),
156 'next' => $this->msg( 'pager-older-n' )->numParams( $this->mLimit
)->escaped(),
157 'first' => $this->msg( 'histlast' )->escaped(),
158 'last' => $this->msg( 'histfirst' )->escaped()
161 $pagingLinks = $this->getPagingLinks( $linkTexts );
162 $limitLinks = $this->getLimitLinks();
163 $lang = $this->getLanguage();
164 $limits = $lang->pipeList( $limitLinks );
166 $firstLast = $lang->pipeList( array( $pagingLinks['first'], $pagingLinks['last'] ) );
167 $firstLast = $this->msg( 'parentheses' )->rawParams( $firstLast )->escaped();
168 $prevNext = $this->msg( 'viewprevnext' )
170 $pagingLinks['prev'],
171 $pagingLinks['next'],
174 $separator = $this->msg( 'word-separator' )->escaped();
175 $this->mNavigationBar
= $firstLast . $separator . $prevNext;
177 return $this->mNavigationBar
;
180 function getNamespaceCond() {
181 if ( $this->namespace !== '' ) {
182 return array( 'ar_namespace' => (int)$this->namespace );
189 * Generates each row in the contributions list.
191 * @todo This would probably look a lot nicer in a table.
192 * @param stdClass $row
195 function formatRow( $row ) {
200 * There may be more than just revision rows. To make sure that we'll only be processing
201 * revisions here, let's _try_ to build a revision out of our row (without displaying
202 * notices though) and then trying to grab data from the built object. If we succeed,
203 * we're definitely dealing with revision data and we may proceed, if not, we'll leave it
204 * to extensions to subscribe to the hook to parse the row.
206 wfSuppressWarnings();
208 $rev = Revision
::newFromArchiveRow( $row );
209 $validRevision = (bool)$rev->getId();
210 } catch ( Exception
$e ) {
211 $validRevision = false;
215 if ( $validRevision ) {
216 $ret = $this->formatRevisionRow( $row );
219 // Let extensions add data
220 Hooks
::run( 'DeletedContributionsLineEnding', array( $this, &$ret, $row, &$classes ) );
222 if ( $classes === array() && $ret === '' ) {
223 wfDebug( "Dropping Special:DeletedContribution row that could not be formatted\n" );
224 $ret = "<!-- Could not format Special:DeletedContribution row. -->\n";
226 $ret = Html
::rawElement( 'li', array( 'class' => $classes ), $ret ) . "\n";
233 * Generates each row in the contributions list for archive entries.
235 * Contributions which are marked "top" are currently on top of the history.
236 * For these contributions, a [rollback] link is shown for users with sysop
237 * privileges. The rollback link restores the most recent version that was not
238 * written by the target user.
240 * @todo This would probably look a lot nicer in a table.
241 * @param stdClass $row
244 function formatRevisionRow( $row ) {
245 $page = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
247 $rev = new Revision( array(
249 'id' => $row->ar_rev_id
,
250 'comment' => $row->ar_comment
,
251 'user' => $row->ar_user
,
252 'user_text' => $row->ar_user_text
,
253 'timestamp' => $row->ar_timestamp
,
254 'minor_edit' => $row->ar_minor_edit
,
255 'deleted' => $row->ar_deleted
,
258 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
260 $logs = SpecialPage
::getTitleFor( 'Log' );
261 $dellog = Linker
::linkKnown(
263 $this->messages
['deletionlog'],
267 'page' => $page->getPrefixedText()
271 $reviewlink = Linker
::linkKnown(
272 SpecialPage
::getTitleFor( 'Undelete', $page->getPrefixedDBkey() ),
273 $this->messages
['undeleteviewlink']
276 $user = $this->getUser();
278 if ( $user->isAllowed( 'deletedtext' ) ) {
279 $last = Linker
::linkKnown(
281 $this->messages
['diff'],
284 'target' => $page->getPrefixedText(),
285 'timestamp' => $rev->getTimestamp(),
290 $last = $this->messages
['diff'];
293 $comment = Linker
::revComment( $rev );
294 $date = $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $user );
295 $date = htmlspecialchars( $date );
297 if ( !$user->isAllowed( 'undelete' ) ||
!$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
298 $link = $date; // unusable link
300 $link = Linker
::linkKnown(
303 array( 'class' => 'mw-changeslist-date' ),
305 'target' => $page->getPrefixedText(),
306 'timestamp' => $rev->getTimestamp()
310 // Style deleted items
311 if ( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
312 $link = '<span class="history-deleted">' . $link . '</span>';
315 $pagelink = Linker
::link(
318 array( 'class' => 'mw-changeslist-title' )
321 if ( $rev->isMinor() ) {
322 $mflag = ChangesList
::flag( 'minor' );
327 // Revision delete link
328 $del = Linker
::getRevDeleteLink( $user, $rev, $page );
333 $tools = Html
::rawElement(
335 array( 'class' => 'mw-deletedcontribs-tools' ),
336 $this->msg( 'parentheses' )->rawParams( $this->getLanguage()->pipeList(
337 array( $last, $dellog, $reviewlink ) ) )->escaped()
340 $separator = '<span class="mw-changeslist-separator">. .</span>';
341 $ret = "{$del}{$link} {$tools} {$separator} {$mflag} {$pagelink} {$comment}";
343 # Denote if username is redacted for this edit
344 if ( $rev->isDeleted( Revision
::DELETED_USER
) ) {
345 $ret .= " <strong>" . $this->msg( 'rev-deleted-user-contribs' )->escaped() . "</strong>";
352 * Get the Database object in use
356 public function getDatabase() {
361 class DeletedContributionsPage
extends SpecialPage
{
362 function __construct() {
363 parent
::__construct( 'DeletedContributions', 'deletedhistory',
364 /*listed*/true, /*function*/false, /*file*/false );
368 * Special page "deleted user contributions".
369 * Shows a list of the deleted contributions of a user.
371 * @param string $par (optional) user name of the user for which to show the contributions
373 function execute( $par ) {
375 $this->outputHeader();
377 $user = $this->getUser();
379 if ( !$this->userCanExecute( $user ) ) {
380 $this->displayRestrictionError();
385 $request = $this->getRequest();
386 $out = $this->getOutput();
387 $out->setPageTitle( $this->msg( 'deletedcontributions-title' ) );
391 if ( $par !== null ) {
394 $target = $request->getVal( 'target' );
397 if ( !strlen( $target ) ) {
398 $out->addHTML( $this->getForm( '' ) );
403 $options['limit'] = $request->getInt( 'limit',
404 $this->getConfig()->get( 'QueryPageDefaultLimit' ) );
405 $options['target'] = $target;
407 $userObj = User
::newFromName( $target, false );
409 $out->addHTML( $this->getForm( '' ) );
413 $this->getSkin()->setRelevantUser( $userObj );
415 $target = $userObj->getName();
416 $out->addSubtitle( $this->getSubTitle( $userObj ) );
418 if ( ( $ns = $request->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
419 $options['namespace'] = intval( $ns );
421 $options['namespace'] = '';
424 $out->addHTML( $this->getForm( $options ) );
426 $pager = new DeletedContribsPager( $this->getContext(), $target, $options['namespace'] );
427 if ( !$pager->getNumRows() ) {
428 $out->addWikiMsg( 'nocontribs' );
433 # Show a message about slave lag, if applicable
434 $lag = wfGetLB()->safeGetLag( $pager->getDatabase() );
436 $out->showLagWarning( $lag );
440 '<p>' . $pager->getNavigationBar() . '</p>' .
442 '<p>' . $pager->getNavigationBar() . '</p>' );
444 # If there were contributions, and it was a valid user or IP, show
445 # the appropriate "footer" message - WHOIS tools, etc.
446 if ( $target != 'newbies' ) {
447 $message = IP
::isIPAddress( $target ) ?
448 'sp-contributions-footer-anon' :
449 'sp-contributions-footer';
451 if ( !$this->msg( $message )->isDisabled() ) {
453 "<div class='mw-contributions-footer'>\n$1\n</div>",
454 array( $message, $target )
461 * Generates the subheading with links
462 * @param User $userObj User object for the target
463 * @return string Appropriately-escaped HTML to be output literally
464 * @todo FIXME: Almost the same as contributionsSub in SpecialContributions.php. Could be combined.
466 function getSubTitle( $userObj ) {
467 if ( $userObj->isAnon() ) {
468 $user = htmlspecialchars( $userObj->getName() );
470 $user = Linker
::link( $userObj->getUserPage(), htmlspecialchars( $userObj->getName() ) );
473 $nt = $userObj->getUserPage();
474 $id = $userObj->getID();
475 $talk = $nt->getTalkPage();
478 $tools[] = Linker
::link( $talk, $this->msg( 'sp-contributions-talk' )->escaped() );
479 if ( ( $id !== null ) ||
( $id === null && IP
::isIPAddress( $nt->getText() ) ) ) {
480 # Block / Change block / Unblock links
481 if ( $this->getUser()->isAllowed( 'block' ) ) {
482 if ( $userObj->isBlocked() ) {
483 $tools[] = Linker
::linkKnown( # Change block link
484 SpecialPage
::getTitleFor( 'Block', $nt->getDBkey() ),
485 $this->msg( 'change-blocklink' )->escaped()
487 $tools[] = Linker
::linkKnown( # Unblock link
488 SpecialPage
::getTitleFor( 'BlockList' ),
489 $this->msg( 'unblocklink' )->escaped(),
492 'action' => 'unblock',
493 'ip' => $nt->getDBkey()
497 # User is not blocked
498 $tools[] = Linker
::linkKnown( # Block link
499 SpecialPage
::getTitleFor( 'Block', $nt->getDBkey() ),
500 $this->msg( 'blocklink' )->escaped()
505 $tools[] = Linker
::linkKnown(
506 SpecialPage
::getTitleFor( 'Log' ),
507 $this->msg( 'sp-contributions-blocklog' )->escaped(),
511 'page' => $nt->getPrefixedText()
514 # Suppression log link (bug 59120)
515 if ( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
516 $tools[] = Linker
::linkKnown(
517 SpecialPage
::getTitleFor( 'Log', 'suppress' ),
518 $this->msg( 'sp-contributions-suppresslog' )->escaped(),
520 array( 'offender' => $userObj->getName() )
526 $tools[] = Linker
::linkKnown(
527 SpecialPage
::getTitleFor( 'Listfiles', $userObj->getName() ),
528 $this->msg( 'sp-contributions-uploads' )->escaped()
532 $tools[] = Linker
::linkKnown(
533 SpecialPage
::getTitleFor( 'Log' ),
534 $this->msg( 'sp-contributions-logs' )->escaped(),
536 array( 'user' => $nt->getText() )
538 # Link to contributions
539 $tools[] = Linker
::linkKnown(
540 SpecialPage
::getTitleFor( 'Contributions', $nt->getDBkey() ),
541 $this->msg( 'sp-deletedcontributions-contribs' )->escaped()
544 # Add a link to change user rights for privileged users
545 $userrightsPage = new UserrightsPage();
546 $userrightsPage->setContext( $this->getContext() );
547 if ( $userrightsPage->userCanChangeRights( $userObj ) ) {
548 $tools[] = Linker
::linkKnown(
549 SpecialPage
::getTitleFor( 'Userrights', $nt->getDBkey() ),
550 $this->msg( 'sp-contributions-userrights' )->escaped()
554 Hooks
::run( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
556 $links = $this->getLanguage()->pipeList( $tools );
558 // Show a note if the user is blocked and display the last block log entry.
559 $block = Block
::newFromTarget( $userObj, $userObj );
560 if ( !is_null( $block ) && $block->getType() != Block
::TYPE_AUTO
) {
561 if ( $block->getType() == Block
::TYPE_RANGE
) {
562 $nt = MWNamespace
::getCanonicalName( NS_USER
) . ':' . $block->getTarget();
565 // LogEventsList::showLogExtract() wants the first parameter by ref
566 $out = $this->getOutput();
567 LogEventsList
::showLogExtract(
574 'showIfEmpty' => false,
576 'sp-contributions-blocked-notice',
577 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
579 'offset' => '' # don't use $this->getRequest() parameter offset
585 return $this->msg( 'contribsub2' )->rawParams( $user, $links )->params( $userObj->getName() );
589 * Generates the namespace selector form with hidden attributes.
590 * @param array $options The options to be included.
593 function getForm( $options ) {
594 $options['title'] = $this->getPageTitle()->getPrefixedText();
595 if ( !isset( $options['target'] ) ) {
596 $options['target'] = '';
598 $options['target'] = str_replace( '_', ' ', $options['target'] );
601 if ( !isset( $options['namespace'] ) ) {
602 $options['namespace'] = '';
605 if ( !isset( $options['contribs'] ) ) {
606 $options['contribs'] = 'user';
609 if ( $options['contribs'] == 'newbie' ) {
610 $options['target'] = '';
613 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => wfScript() ) );
615 foreach ( $options as $name => $value ) {
616 if ( in_array( $name, array( 'namespace', 'target', 'contribs' ) ) ) {
619 $f .= "\t" . Html
::hidden( $name, $value ) . "\n";
622 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
624 $f .= Xml
::openElement( 'fieldset' );
625 $f .= Xml
::element( 'legend', array(), $this->msg( 'sp-contributions-search' )->text() );
628 array( 'for' => 'target' ),
629 $this->msg( 'sp-contributions-username' )->parse()
639 'mw-autocomplete-user', // used by mediawiki.userSuggest
641 ) +
( $options['target'] ?
array() : array( 'autofocus' ) )
643 $f .= Html
::namespaceSelector(
645 'selected' => $options['namespace'],
647 'label' => $this->msg( 'namespace' )->text()
650 'name' => 'namespace',
652 'class' => 'namespaceselector',
655 $f .= Xml
::submitButton( $this->msg( 'sp-contributions-submit' )->text() );
656 $f .= Xml
::closeElement( 'fieldset' );
657 $f .= Xml
::closeElement( 'form' );
662 protected function getGroupName() {