Merge "Fix positioning of jQuery.tipsy tooltip arrows"
[mediawiki.git] / includes / specials / SpecialDeletedContributions.php
blobf6d560f4e0f002a1d3d252b04b7a088301fa31bb
1 <?php
2 /**
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Implements Special:DeletedContributions to display archived revisions
26 * @ingroup SpecialPage
28 class DeletedContribsPager extends IndexPager {
29 public $mDefaultDirection = IndexPager::DIR_DESCENDING;
30 public $messages;
31 public $target;
32 public $namespace = '';
33 public $mDb;
35 /**
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;
55 return $query;
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;
70 return array(
71 'tables' => array( 'archive' ),
72 'fields' => array(
73 'ar_rev_id', 'ar_namespace', 'ar_title', 'ar_timestamp', 'ar_comment',
74 'ar_minor_edit', 'ar_user', 'ar_user_text', 'ar_deleted'
76 'conds' => $conds,
77 'options' => array( 'USE INDEX' => $index )
81 /**
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 ) {
91 $data = array( parent::reallyDoQuery( $offset, $limit, $descending ) );
93 // This hook will allow extensions to add in additional queries, nearly
94 // identical to ContribsPager::reallyDoQuery.
95 Hooks::run(
96 'DeletedContribsPager::reallyDoQuery',
97 array( &$data, $this, $offset, $limit, $descending )
100 $result = array();
102 // loop all results and collect them in an array
103 foreach ( $data as $query ) {
104 foreach ( $query as $i => $row ) {
105 // use index column as key, allowing us to easily sort in PHP
106 $result[$row->{$this->getIndexField()} . "-$i"] = $row;
110 // sort results
111 if ( $descending ) {
112 ksort( $result );
113 } else {
114 krsort( $result );
117 // enforce limit
118 $result = array_slice( $result, 0, $limit );
120 // get rid of array keys
121 $result = array_values( $result );
123 return new FakeResultWrapper( $result );
126 function getUserCond() {
127 $condition = array();
129 $condition['ar_user_text'] = $this->target;
130 $index = 'usertext_timestamp';
132 return array( $index, $condition );
135 function getIndexField() {
136 return 'ar_timestamp';
139 function getStartBody() {
140 return "<ul>\n";
143 function getEndBody() {
144 return "</ul>\n";
147 function getNavigationBar() {
148 if ( isset( $this->mNavigationBar ) ) {
149 return $this->mNavigationBar;
152 $linkTexts = array(
153 'prev' => $this->msg( 'pager-newer-n' )->numParams( $this->mLimit )->escaped(),
154 'next' => $this->msg( 'pager-older-n' )->numParams( $this->mLimit )->escaped(),
155 'first' => $this->msg( 'histlast' )->escaped(),
156 'last' => $this->msg( 'histfirst' )->escaped()
159 $pagingLinks = $this->getPagingLinks( $linkTexts );
160 $limitLinks = $this->getLimitLinks();
161 $lang = $this->getLanguage();
162 $limits = $lang->pipeList( $limitLinks );
164 $firstLast = $lang->pipeList( array( $pagingLinks['first'], $pagingLinks['last'] ) );
165 $firstLast = $this->msg( 'parentheses' )->rawParams( $firstLast )->escaped();
166 $prevNext = $this->msg( 'viewprevnext' )
167 ->rawParams(
168 $pagingLinks['prev'],
169 $pagingLinks['next'],
170 $limits
171 )->escaped();
172 $separator = $this->msg( 'word-separator' )->escaped();
173 $this->mNavigationBar = $firstLast . $separator . $prevNext;
175 return $this->mNavigationBar;
178 function getNamespaceCond() {
179 if ( $this->namespace !== '' ) {
180 return array( 'ar_namespace' => (int)$this->namespace );
181 } else {
182 return array();
187 * Generates each row in the contributions list.
189 * @todo This would probably look a lot nicer in a table.
190 * @param stdClass $row
191 * @return string
193 function formatRow( $row ) {
194 $ret = '';
195 $classes = array();
198 * There may be more than just revision rows. To make sure that we'll only be processing
199 * revisions here, let's _try_ to build a revision out of our row (without displaying
200 * notices though) and then trying to grab data from the built object. If we succeed,
201 * we're definitely dealing with revision data and we may proceed, if not, we'll leave it
202 * to extensions to subscribe to the hook to parse the row.
204 MediaWiki\suppressWarnings();
205 try {
206 $rev = Revision::newFromArchiveRow( $row );
207 $validRevision = (bool)$rev->getId();
208 } catch ( Exception $e ) {
209 $validRevision = false;
211 MediaWiki\restoreWarnings();
213 if ( $validRevision ) {
214 $ret = $this->formatRevisionRow( $row );
217 // Let extensions add data
218 Hooks::run( 'DeletedContributionsLineEnding', array( $this, &$ret, $row, &$classes ) );
220 if ( $classes === array() && $ret === '' ) {
221 wfDebug( "Dropping Special:DeletedContribution row that could not be formatted\n" );
222 $ret = "<!-- Could not format Special:DeletedContribution row. -->\n";
223 } else {
224 $ret = Html::rawElement( 'li', array( 'class' => $classes ), $ret ) . "\n";
227 return $ret;
231 * Generates each row in the contributions list for archive entries.
233 * Contributions which are marked "top" are currently on top of the history.
234 * For these contributions, a [rollback] link is shown for users with sysop
235 * privileges. The rollback link restores the most recent version that was not
236 * written by the target user.
238 * @todo This would probably look a lot nicer in a table.
239 * @param stdClass $row
240 * @return string
242 function formatRevisionRow( $row ) {
243 $page = Title::makeTitle( $row->ar_namespace, $row->ar_title );
245 $rev = new Revision( array(
246 'title' => $page,
247 'id' => $row->ar_rev_id,
248 'comment' => $row->ar_comment,
249 'user' => $row->ar_user,
250 'user_text' => $row->ar_user_text,
251 'timestamp' => $row->ar_timestamp,
252 'minor_edit' => $row->ar_minor_edit,
253 'deleted' => $row->ar_deleted,
254 ) );
256 $undelete = SpecialPage::getTitleFor( 'Undelete' );
258 $logs = SpecialPage::getTitleFor( 'Log' );
259 $dellog = Linker::linkKnown(
260 $logs,
261 $this->messages['deletionlog'],
262 array(),
263 array(
264 'type' => 'delete',
265 'page' => $page->getPrefixedText()
269 $reviewlink = Linker::linkKnown(
270 SpecialPage::getTitleFor( 'Undelete', $page->getPrefixedDBkey() ),
271 $this->messages['undeleteviewlink']
274 $user = $this->getUser();
276 if ( $user->isAllowed( 'deletedtext' ) ) {
277 $last = Linker::linkKnown(
278 $undelete,
279 $this->messages['diff'],
280 array(),
281 array(
282 'target' => $page->getPrefixedText(),
283 'timestamp' => $rev->getTimestamp(),
284 'diff' => 'prev'
287 } else {
288 $last = $this->messages['diff'];
291 $comment = Linker::revComment( $rev );
292 $date = $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $user );
293 $date = htmlspecialchars( $date );
295 if ( !$user->isAllowed( 'undelete' ) || !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
296 $link = $date; // unusable link
297 } else {
298 $link = Linker::linkKnown(
299 $undelete,
300 $date,
301 array( 'class' => 'mw-changeslist-date' ),
302 array(
303 'target' => $page->getPrefixedText(),
304 'timestamp' => $rev->getTimestamp()
308 // Style deleted items
309 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
310 $link = '<span class="history-deleted">' . $link . '</span>';
313 $pagelink = Linker::link(
314 $page,
315 null,
316 array( 'class' => 'mw-changeslist-title' )
319 if ( $rev->isMinor() ) {
320 $mflag = ChangesList::flag( 'minor' );
321 } else {
322 $mflag = '';
325 // Revision delete link
326 $del = Linker::getRevDeleteLink( $user, $rev, $page );
327 if ( $del ) {
328 $del .= ' ';
331 $tools = Html::rawElement(
332 'span',
333 array( 'class' => 'mw-deletedcontribs-tools' ),
334 $this->msg( 'parentheses' )->rawParams( $this->getLanguage()->pipeList(
335 array( $last, $dellog, $reviewlink ) ) )->escaped()
338 $separator = '<span class="mw-changeslist-separator">. .</span>';
339 $ret = "{$del}{$link} {$tools} {$separator} {$mflag} {$pagelink} {$comment}";
341 # Denote if username is redacted for this edit
342 if ( $rev->isDeleted( Revision::DELETED_USER ) ) {
343 $ret .= " <strong>" . $this->msg( 'rev-deleted-user-contribs' )->escaped() . "</strong>";
346 return $ret;
350 * Get the Database object in use
352 * @return IDatabase
354 public function getDatabase() {
355 return $this->mDb;
359 class DeletedContributionsPage extends SpecialPage {
360 function __construct() {
361 parent::__construct( 'DeletedContributions', 'deletedhistory',
362 /*listed*/true, /*function*/false, /*file*/false );
366 * Special page "deleted user contributions".
367 * Shows a list of the deleted contributions of a user.
369 * @param string $par (optional) user name of the user for which to show the contributions
371 function execute( $par ) {
372 $this->setHeaders();
373 $this->outputHeader();
375 $user = $this->getUser();
377 if ( !$this->userCanExecute( $user ) ) {
378 $this->displayRestrictionError();
380 return;
383 $request = $this->getRequest();
384 $out = $this->getOutput();
385 $out->setPageTitle( $this->msg( 'deletedcontributions-title' ) );
387 $options = array();
389 if ( $par !== null ) {
390 $target = $par;
391 } else {
392 $target = $request->getVal( 'target' );
395 if ( !strlen( $target ) ) {
396 $out->addHTML( $this->getForm( '' ) );
398 return;
401 $options['limit'] = $request->getInt( 'limit',
402 $this->getConfig()->get( 'QueryPageDefaultLimit' ) );
403 $options['target'] = $target;
405 $userObj = User::newFromName( $target, false );
406 if ( !$userObj ) {
407 $out->addHTML( $this->getForm( '' ) );
409 return;
411 $this->getSkin()->setRelevantUser( $userObj );
413 $target = $userObj->getName();
414 $out->addSubtitle( $this->getSubTitle( $userObj ) );
416 $ns = $request->getVal( 'namespace', null );
417 if ( $ns !== null && $ns !== '' ) {
418 $options['namespace'] = intval( $ns );
419 } else {
420 $options['namespace'] = '';
423 $out->addHTML( $this->getForm( $options ) );
425 $pager = new DeletedContribsPager( $this->getContext(), $target, $options['namespace'] );
426 if ( !$pager->getNumRows() ) {
427 $out->addWikiMsg( 'nocontribs' );
429 return;
432 # Show a message about slave lag, if applicable
433 $lag = wfGetLB()->safeGetLag( $pager->getDatabase() );
434 if ( $lag > 0 ) {
435 $out->showLagWarning( $lag );
438 $out->addHTML(
439 '<p>' . $pager->getNavigationBar() . '</p>' .
440 $pager->getBody() .
441 '<p>' . $pager->getNavigationBar() . '</p>' );
443 # If there were contributions, and it was a valid user or IP, show
444 # the appropriate "footer" message - WHOIS tools, etc.
445 if ( $target != 'newbies' ) {
446 $message = IP::isIPAddress( $target ) ?
447 'sp-contributions-footer-anon' :
448 'sp-contributions-footer';
450 if ( !$this->msg( $message )->isDisabled() ) {
451 $out->wrapWikiMsg(
452 "<div class='mw-contributions-footer'>\n$1\n</div>",
453 array( $message, $target )
460 * Generates the subheading with links
461 * @param User $userObj User object for the target
462 * @return string Appropriately-escaped HTML to be output literally
463 * @todo FIXME: Almost the same as contributionsSub in SpecialContributions.php. Could be combined.
465 function getSubTitle( $userObj ) {
466 if ( $userObj->isAnon() ) {
467 $user = htmlspecialchars( $userObj->getName() );
468 } else {
469 $user = Linker::link( $userObj->getUserPage(), htmlspecialchars( $userObj->getName() ) );
471 $links = '';
472 $nt = $userObj->getUserPage();
473 $id = $userObj->getID();
474 $talk = $nt->getTalkPage();
475 if ( $talk ) {
476 # Talk page link
477 $tools[] = Linker::link( $talk, $this->msg( 'sp-contributions-talk' )->escaped() );
478 if ( ( $id !== null ) || ( $id === null && IP::isIPAddress( $nt->getText() ) ) ) {
479 # Block / Change block / Unblock links
480 if ( $this->getUser()->isAllowed( 'block' ) ) {
481 if ( $userObj->isBlocked() && $userObj->getBlock()->getType() !== Block::TYPE_AUTO ) {
482 $tools[] = Linker::linkKnown( # Change block link
483 SpecialPage::getTitleFor( 'Block', $nt->getDBkey() ),
484 $this->msg( 'change-blocklink' )->escaped()
486 $tools[] = Linker::linkKnown( # Unblock link
487 SpecialPage::getTitleFor( 'BlockList' ),
488 $this->msg( 'unblocklink' )->escaped(),
489 array(),
490 array(
491 'action' => 'unblock',
492 'ip' => $nt->getDBkey()
495 } else {
496 # User is not blocked
497 $tools[] = Linker::linkKnown( # Block link
498 SpecialPage::getTitleFor( 'Block', $nt->getDBkey() ),
499 $this->msg( 'blocklink' )->escaped()
503 # Block log link
504 $tools[] = Linker::linkKnown(
505 SpecialPage::getTitleFor( 'Log' ),
506 $this->msg( 'sp-contributions-blocklog' )->escaped(),
507 array(),
508 array(
509 'type' => 'block',
510 'page' => $nt->getPrefixedText()
513 # Suppression log link (bug 59120)
514 if ( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
515 $tools[] = Linker::linkKnown(
516 SpecialPage::getTitleFor( 'Log', 'suppress' ),
517 $this->msg( 'sp-contributions-suppresslog' )->escaped(),
518 array(),
519 array( 'offender' => $userObj->getName() )
524 # Uploads
525 $tools[] = Linker::linkKnown(
526 SpecialPage::getTitleFor( 'Listfiles', $userObj->getName() ),
527 $this->msg( 'sp-contributions-uploads' )->escaped()
530 # Other logs link
531 $tools[] = Linker::linkKnown(
532 SpecialPage::getTitleFor( 'Log' ),
533 $this->msg( 'sp-contributions-logs' )->escaped(),
534 array(),
535 array( 'user' => $nt->getText() )
537 # Link to contributions
538 $tools[] = Linker::linkKnown(
539 SpecialPage::getTitleFor( 'Contributions', $nt->getDBkey() ),
540 $this->msg( 'sp-deletedcontributions-contribs' )->escaped()
543 # Add a link to change user rights for privileged users
544 $userrightsPage = new UserrightsPage();
545 $userrightsPage->setContext( $this->getContext() );
546 if ( $userrightsPage->userCanChangeRights( $userObj ) ) {
547 $tools[] = Linker::linkKnown(
548 SpecialPage::getTitleFor( 'Userrights', $nt->getDBkey() ),
549 $this->msg( 'sp-contributions-userrights' )->escaped()
553 Hooks::run( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
555 $links = $this->getLanguage()->pipeList( $tools );
557 // Show a note if the user is blocked and display the last block log entry.
558 $block = Block::newFromTarget( $userObj, $userObj );
559 if ( !is_null( $block ) && $block->getType() != Block::TYPE_AUTO ) {
560 if ( $block->getType() == Block::TYPE_RANGE ) {
561 $nt = MWNamespace::getCanonicalName( NS_USER ) . ':' . $block->getTarget();
564 // LogEventsList::showLogExtract() wants the first parameter by ref
565 $out = $this->getOutput();
566 LogEventsList::showLogExtract(
567 $out,
568 'block',
569 $nt,
571 array(
572 'lim' => 1,
573 'showIfEmpty' => false,
574 'msgKey' => array(
575 'sp-contributions-blocked-notice',
576 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
578 'offset' => '' # don't use $this->getRequest() parameter offset
584 return $this->msg( 'contribsub2' )->rawParams( $user, $links )->params( $userObj->getName() );
588 * Generates the namespace selector form with hidden attributes.
589 * @param array $options The options to be included.
590 * @return string
592 function getForm( $options ) {
593 $options['title'] = $this->getPageTitle()->getPrefixedText();
594 if ( !isset( $options['target'] ) ) {
595 $options['target'] = '';
596 } else {
597 $options['target'] = str_replace( '_', ' ', $options['target'] );
600 if ( !isset( $options['namespace'] ) ) {
601 $options['namespace'] = '';
604 if ( !isset( $options['contribs'] ) ) {
605 $options['contribs'] = 'user';
608 if ( $options['contribs'] == 'newbie' ) {
609 $options['target'] = '';
612 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => wfScript() ) );
614 foreach ( $options as $name => $value ) {
615 if ( in_array( $name, array( 'namespace', 'target', 'contribs' ) ) ) {
616 continue;
618 $f .= "\t" . Html::hidden( $name, $value ) . "\n";
621 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
623 $f .= Xml::openElement( 'fieldset' );
624 $f .= Xml::element( 'legend', array(), $this->msg( 'sp-contributions-search' )->text() );
625 $f .= Xml::tags(
626 'label',
627 array( 'for' => 'target' ),
628 $this->msg( 'sp-contributions-username' )->parse()
629 ) . ' ';
630 $f .= Html::input(
631 'target',
632 $options['target'],
633 'text',
634 array(
635 'size' => '20',
636 'required' => '',
637 'class' => array(
638 'mw-autocomplete-user', // used by mediawiki.userSuggest
640 ) + ( $options['target'] ? array() : array( 'autofocus' ) )
641 ) . ' ';
642 $f .= Html::namespaceSelector(
643 array(
644 'selected' => $options['namespace'],
645 'all' => '',
646 'label' => $this->msg( 'namespace' )->text()
648 array(
649 'name' => 'namespace',
650 'id' => 'namespace',
651 'class' => 'namespaceselector',
653 ) . ' ';
654 $f .= Xml::submitButton( $this->msg( 'sp-contributions-submit' )->text() );
655 $f .= Xml::closeElement( 'fieldset' );
656 $f .= Xml::closeElement( 'form' );
658 return $f;
662 * Return an array of subpages beginning with $search that this special page will accept.
664 * @param string $search Prefix to search for
665 * @param int $limit Maximum number of results to return (usually 10)
666 * @param int $offset Number of results to skip (usually 0)
667 * @return string[] Matching subpages
669 public function prefixSearchSubpages( $search, $limit, $offset ) {
670 $user = User::newFromName( $search );
671 if ( !$user ) {
672 // No prefix suggestion for invalid user
673 return array();
675 // Autocomplete subpage as user list - public to allow caching
676 return UserNamePrefixSearch::search( 'public', $search, $limit, $offset );
679 protected function getGroupName() {
680 return 'users';