(bug 35565) Special:Log/patrol doesn't indicate whether patrolling was automatic
[mediawiki.git] / includes / specials / SpecialDeletedContributions.php
blob64455d4c1423d400ea781958cf26cc4308d26b96
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 = true;
30 var $messages, $target;
31 var $namespace = '', $mDb;
33 function __construct( IContextSource $context, $target, $namespace = false ) {
34 parent::__construct( $context );
35 $msgs = array( 'deletionlog', 'undeleteviewlink', 'diff' );
36 foreach( $msgs as $msg ) {
37 $this->messages[$msg] = $this->msg( $msg )->escaped();
39 $this->target = $target;
40 $this->namespace = $namespace;
41 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
44 function getDefaultQuery() {
45 $query = parent::getDefaultQuery();
46 $query['target'] = $this->target;
47 return $query;
50 function getQueryInfo() {
51 list( $index, $userCond ) = $this->getUserCond();
52 $conds = array_merge( $userCond, $this->getNamespaceCond() );
53 $user = $this->getUser();
54 // Paranoia: avoid brute force searches (bug 17792)
55 if( !$user->isAllowed( 'deletedhistory' ) ) {
56 $conds[] = $this->mDb->bitAnd( 'ar_deleted', Revision::DELETED_USER ) . ' = 0';
57 } elseif( !$user->isAllowed( 'suppressrevision' ) ) {
58 $conds[] = $this->mDb->bitAnd( 'ar_deleted', Revision::SUPPRESSED_USER ) .
59 ' != ' . Revision::SUPPRESSED_USER;
61 return array(
62 'tables' => array( 'archive' ),
63 'fields' => array(
64 'ar_rev_id', 'ar_namespace', 'ar_title', 'ar_timestamp', 'ar_comment', 'ar_minor_edit',
65 'ar_user', 'ar_user_text', 'ar_deleted'
67 'conds' => $conds,
68 'options' => array( 'USE INDEX' => $index )
72 function getUserCond() {
73 $condition = array();
75 $condition['ar_user_text'] = $this->target;
76 $index = 'usertext_timestamp';
78 return array( $index, $condition );
81 function getIndexField() {
82 return 'ar_timestamp';
85 function getStartBody() {
86 return "<ul>\n";
89 function getEndBody() {
90 return "</ul>\n";
93 function getNavigationBar() {
94 if ( isset( $this->mNavigationBar ) ) {
95 return $this->mNavigationBar;
97 $lang = $this->getLanguage();
98 $fmtLimit = $lang->formatNum( $this->mLimit );
99 $linkTexts = array(
100 'prev' => $this->msg( 'pager-newer-n', $fmtLimit )->escaped(),
101 'next' => $this->msg( 'pager-older-n', $fmtLimit )->escaped(),
102 'first' => $this->msg( 'histlast' )->escaped(),
103 'last' => $this->msg( 'histfirst' )->escaped()
106 $pagingLinks = $this->getPagingLinks( $linkTexts );
107 $limitLinks = $this->getLimitLinks();
108 $limits = $lang->pipeList( $limitLinks );
110 $this->mNavigationBar = "(" . $lang->pipeList( array( $pagingLinks['first'], $pagingLinks['last'] ) ) . ") " .
111 $this->msg( 'viewprevnext' )->rawParams( $pagingLinks['prev'], $pagingLinks['next'], $limits )->escaped();
112 return $this->mNavigationBar;
115 function getNamespaceCond() {
116 if ( $this->namespace !== '' ) {
117 return array( 'ar_namespace' => (int)$this->namespace );
118 } else {
119 return array();
124 * Generates each row in the contributions list.
126 * Contributions which are marked "top" are currently on top of the history.
127 * For these contributions, a [rollback] link is shown for users with sysop
128 * privileges. The rollback link restores the most recent version that was not
129 * written by the target user.
131 * @todo This would probably look a lot nicer in a table.
132 * @param $row
133 * @return string
135 function formatRow( $row ) {
136 wfProfileIn( __METHOD__ );
138 $rev = new Revision( array(
139 'id' => $row->ar_rev_id,
140 'comment' => $row->ar_comment,
141 'user' => $row->ar_user,
142 'user_text' => $row->ar_user_text,
143 'timestamp' => $row->ar_timestamp,
144 'minor_edit' => $row->ar_minor_edit,
145 'deleted' => $row->ar_deleted,
146 ) );
148 $page = Title::makeTitle( $row->ar_namespace, $row->ar_title );
150 $undelete = SpecialPage::getTitleFor( 'Undelete' );
152 $logs = SpecialPage::getTitleFor( 'Log' );
153 $dellog = Linker::linkKnown(
154 $logs,
155 $this->messages['deletionlog'],
156 array(),
157 array(
158 'type' => 'delete',
159 'page' => $page->getPrefixedText()
163 $reviewlink = Linker::linkKnown(
164 SpecialPage::getTitleFor( 'Undelete', $page->getPrefixedDBkey() ),
165 $this->messages['undeleteviewlink']
168 $user = $this->getUser();
170 if( $user->isAllowed('deletedtext') ) {
171 $last = Linker::linkKnown(
172 $undelete,
173 $this->messages['diff'],
174 array(),
175 array(
176 'target' => $page->getPrefixedText(),
177 'timestamp' => $rev->getTimestamp(),
178 'diff' => 'prev'
181 } else {
182 $last = $this->messages['diff'];
185 $comment = Linker::revComment( $rev );
186 $date = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $user ) );
188 if( !$user->isAllowed( 'undelete' ) || !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
189 $link = $date; // unusable link
190 } else {
191 $link = Linker::linkKnown(
192 $undelete,
193 $date,
194 array(),
195 array(
196 'target' => $page->getPrefixedText(),
197 'timestamp' => $rev->getTimestamp()
201 // Style deleted items
202 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
203 $link = '<span class="history-deleted">' . $link . '</span>';
206 $pagelink = Linker::link( $page );
208 if( $rev->isMinor() ) {
209 $mflag = ChangesList::flag( 'minor' );
210 } else {
211 $mflag = '';
214 // Revision delete link
215 $del = Linker::getRevDeleteLink( $user, $rev, $page );
216 if ( $del ) $del .= ' ';
218 $tools = Html::rawElement(
219 'span',
220 array( 'class' => 'mw-deletedcontribs-tools' ),
221 $this->msg( 'parentheses' )->rawParams( $this->getLanguage()->pipeList(
222 array( $last, $dellog, $reviewlink ) ) )->escaped()
225 $ret = "{$del}{$link} {$tools} . . {$mflag} {$pagelink} {$comment}";
227 # Denote if username is redacted for this edit
228 if( $rev->isDeleted( Revision::DELETED_USER ) ) {
229 $ret .= " <strong>" . $this->msg( 'rev-deleted-user-contribs' )->escaped() . "</strong>";
232 $ret = Html::rawElement( 'li', array(), $ret ) . "\n";
234 wfProfileOut( __METHOD__ );
235 return $ret;
239 * Get the Database object in use
241 * @return DatabaseBase
243 public function getDatabase() {
244 return $this->mDb;
248 class DeletedContributionsPage extends SpecialPage {
249 function __construct() {
250 parent::__construct( 'DeletedContributions', 'deletedhistory',
251 /*listed*/ true, /*function*/ false, /*file*/ false );
255 * Special page "deleted user contributions".
256 * Shows a list of the deleted contributions of a user.
258 * @param $par String: (optional) user name of the user for which to show the contributions
260 function execute( $par ) {
261 global $wgQueryPageDefaultLimit;
262 $this->setHeaders();
263 $this->outputHeader();
265 $user = $this->getUser();
267 if ( !$this->userCanExecute( $user ) ) {
268 $this->displayRestrictionError();
269 return;
272 $request = $this->getRequest();
273 $out = $this->getOutput();
274 $out->setPageTitle( $this->msg( 'deletedcontributions-title' ) );
276 $options = array();
278 if ( $par !== null ) {
279 $target = $par;
280 } else {
281 $target = $request->getVal( 'target' );
284 if ( !strlen( $target ) ) {
285 $out->addHTML( $this->getForm( '' ) );
286 return;
289 $options['limit'] = $request->getInt( 'limit', $wgQueryPageDefaultLimit );
290 $options['target'] = $target;
292 $userObj = User::newFromName( $target, false );
293 if ( !$userObj ) {
294 $out->addHTML( $this->getForm( '' ) );
295 return;
298 $target = $userObj->getName();
299 $out->addSubtitle( $this->getSubTitle( $userObj ) );
301 if ( ( $ns = $request->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
302 $options['namespace'] = intval( $ns );
303 } else {
304 $options['namespace'] = '';
307 $out->addHTML( $this->getForm( $options ) );
309 $pager = new DeletedContribsPager( $this->getContext(), $target, $options['namespace'] );
310 if ( !$pager->getNumRows() ) {
311 $out->addWikiMsg( 'nocontribs' );
312 return;
315 # Show a message about slave lag, if applicable
316 $lag = wfGetLB()->safeGetLag( $pager->getDatabase() );
317 if( $lag > 0 )
318 $out->showLagWarning( $lag );
320 $out->addHTML(
321 '<p>' . $pager->getNavigationBar() . '</p>' .
322 $pager->getBody() .
323 '<p>' . $pager->getNavigationBar() . '</p>' );
325 # If there were contributions, and it was a valid user or IP, show
326 # the appropriate "footer" message - WHOIS tools, etc.
327 if( $target != 'newbies' ) {
328 $message = IP::isIPAddress( $target )
329 ? 'sp-contributions-footer-anon'
330 : 'sp-contributions-footer';
332 if( !$this->msg( $message )->isDisabled() ) {
333 $out->wrapWikiMsg( "<div class='mw-contributions-footer'>\n$1\n</div>", array( $message, $target ) );
339 * Generates the subheading with links
340 * @param $userObj User object for the target
341 * @return String: appropriately-escaped HTML to be output literally
342 * @todo FIXME: Almost the same as contributionsSub in SpecialContributions.php. Could be combined.
344 function getSubTitle( $userObj ) {
345 if ( $userObj->isAnon() ) {
346 $user = htmlspecialchars( $userObj->getName() );
347 } else {
348 $user = Linker::link( $userObj->getUserPage(), htmlspecialchars( $userObj->getName() ) );
350 $links = '';
351 $nt = $userObj->getUserPage();
352 $id = $userObj->getID();
353 $talk = $nt->getTalkPage();
354 if( $talk ) {
355 # Talk page link
356 $tools[] = Linker::link( $talk, $this->msg( 'sp-contributions-talk' )->escaped() );
357 if( ( $id !== null ) || ( $id === null && IP::isIPAddress( $nt->getText() ) ) ) {
358 if( $this->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
359 if ( $userObj->isBlocked() ) {
360 $tools[] = Linker::linkKnown( # Change block link
361 SpecialPage::getTitleFor( 'Block', $nt->getDBkey() ),
362 $this->msg( 'change-blocklink' )->escaped()
364 $tools[] = Linker::linkKnown( # Unblock link
365 SpecialPage::getTitleFor( 'BlockList' ),
366 $this->msg( 'unblocklink' )->escaped(),
367 array(),
368 array(
369 'action' => 'unblock',
370 'ip' => $nt->getDBkey()
374 else { # User is not blocked
375 $tools[] = Linker::linkKnown( # Block link
376 SpecialPage::getTitleFor( 'Block', $nt->getDBkey() ),
377 $this->msg( 'blocklink' )->escaped()
381 # Block log link
382 $tools[] = Linker::linkKnown(
383 SpecialPage::getTitleFor( 'Log' ),
384 $this->msg( 'sp-contributions-blocklog' )->escaped(),
385 array(),
386 array(
387 'type' => 'block',
388 'page' => $nt->getPrefixedText()
392 # Other logs link
393 $tools[] = Linker::linkKnown(
394 SpecialPage::getTitleFor( 'Log' ),
395 $this->msg( 'sp-contributions-logs' )->escaped(),
396 array(),
397 array( 'user' => $nt->getText() )
399 # Link to contributions
400 $tools[] = Linker::linkKnown(
401 SpecialPage::getTitleFor( 'Contributions', $nt->getDBkey() ),
402 $this->msg( 'sp-deletedcontributions-contribs' )->escaped()
405 # Add a link to change user rights for privileged users
406 $userrightsPage = new UserrightsPage();
407 $userrightsPage->setContext( $this->getContext() );
408 if( $id !== null && $userrightsPage->userCanChangeRights( User::newFromId( $id ) ) ) {
409 $tools[] = Linker::linkKnown(
410 SpecialPage::getTitleFor( 'Userrights', $nt->getDBkey() ),
411 $this->msg( 'sp-contributions-userrights' )->escaped()
415 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
417 $links = $this->getLanguage()->pipeList( $tools );
419 // Show a note if the user is blocked and display the last block log entry.
420 if ( $userObj->isBlocked() ) {
421 $out = $this->getOutput(); // LogEventsList::showLogExtract() wants the first parameter by ref
422 LogEventsList::showLogExtract(
423 $out,
424 'block',
425 $nt,
427 array(
428 'lim' => 1,
429 'showIfEmpty' => false,
430 'msgKey' => array(
431 'sp-contributions-blocked-notice',
432 $nt->getText() # Support GENDER in 'sp-contributions-blocked-notice'
434 'offset' => '' # don't use $this->getRequest() parameter offset
440 // Old message 'contribsub' had one parameter, but that doesn't work for
441 // languages that want to put the "for" bit right after $user but before
442 // $links. If 'contribsub' is around, use it for reverse compatibility,
443 // otherwise use 'contribsub2'.
444 $oldMsg = $this->msg( 'contribsub' );
445 if ( $oldMsg->exists() ) {
446 return $oldMsg->rawParams( "$user ($links)" );
447 } else {
448 return $this->msg( 'contribsub2' )->rawParams( $user, $links );
453 * Generates the namespace selector form with hidden attributes.
454 * @param $options Array: the options to be included.
455 * @return string
457 function getForm( $options ) {
458 global $wgScript;
460 $options['title'] = $this->getTitle()->getPrefixedText();
461 if ( !isset( $options['target'] ) ) {
462 $options['target'] = '';
463 } else {
464 $options['target'] = str_replace( '_' , ' ' , $options['target'] );
467 if ( !isset( $options['namespace'] ) ) {
468 $options['namespace'] = '';
471 if ( !isset( $options['contribs'] ) ) {
472 $options['contribs'] = 'user';
475 if ( $options['contribs'] == 'newbie' ) {
476 $options['target'] = '';
479 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
481 foreach ( $options as $name => $value ) {
482 if ( in_array( $name, array( 'namespace', 'target', 'contribs' ) ) ) {
483 continue;
485 $f .= "\t" . Html::hidden( $name, $value ) . "\n";
488 $f .= Xml::openElement( 'fieldset' ) .
489 Xml::element( 'legend', array(), $this->msg( 'sp-contributions-search' )->text() ) .
490 Xml::tags( 'label', array( 'for' => 'target' ), $this->msg( 'sp-contributions-username' )->parse() ) . ' ' .
491 Html::input( 'target', $options['target'], 'text', array(
492 'size' => '20',
493 'required' => ''
494 ) + ( $options['target'] ? array() : array( 'autofocus' ) ) ) . ' '.
495 Html::namespaceSelector(
496 array(
497 'selected' => $options['namespace'],
498 'all' => '',
499 'label' => $this->msg( 'namespace' )->text()
500 ), array(
501 'name' => 'namespace',
502 'id' => 'namespace',
503 'class' => 'namespaceselector',
505 ) . ' ' .
506 Xml::submitButton( $this->msg( 'sp-contributions-submit' )->text() ) .
507 Xml::closeElement( 'fieldset' ) .
508 Xml::closeElement( 'form' );
509 return $f;