SpecialLinkSearch: clean up munged query variable handling
[mediawiki.git] / includes / specials / SpecialDeletedContributions.php
blob680aa35933fa8e41b05d4dc218c03878b560630e
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 function getUserCond() {
82 $condition = array();
84 $condition['ar_user_text'] = $this->target;
85 $index = 'usertext_timestamp';
87 return array( $index, $condition );
90 function getIndexField() {
91 return 'ar_timestamp';
94 function getStartBody() {
95 return "<ul>\n";
98 function getEndBody() {
99 return "</ul>\n";
102 function getNavigationBar() {
103 if ( isset( $this->mNavigationBar ) ) {
104 return $this->mNavigationBar;
107 $linkTexts = array(
108 'prev' => $this->msg( 'pager-newer-n' )->numParams( $this->mLimit )->escaped(),
109 'next' => $this->msg( 'pager-older-n' )->numParams( $this->mLimit )->escaped(),
110 'first' => $this->msg( 'histlast' )->escaped(),
111 'last' => $this->msg( 'histfirst' )->escaped()
114 $pagingLinks = $this->getPagingLinks( $linkTexts );
115 $limitLinks = $this->getLimitLinks();
116 $lang = $this->getLanguage();
117 $limits = $lang->pipeList( $limitLinks );
119 $firstLast = $lang->pipeList( array( $pagingLinks['first'], $pagingLinks['last'] ) );
120 $firstLast = $this->msg( 'parentheses' )->rawParams( $firstLast )->escaped();
121 $prevNext = $this->msg( 'viewprevnext' )
122 ->rawParams(
123 $pagingLinks['prev'],
124 $pagingLinks['next'],
125 $limits
126 )->escaped();
127 $separator = $this->msg( 'word-separator' )->escaped();
128 $this->mNavigationBar = $firstLast . $separator . $prevNext;
130 return $this->mNavigationBar;
133 function getNamespaceCond() {
134 if ( $this->namespace !== '' ) {
135 return array( 'ar_namespace' => (int)$this->namespace );
136 } else {
137 return array();
142 * Generates each row in the contributions list.
144 * Contributions which are marked "top" are currently on top of the history.
145 * For these contributions, a [rollback] link is shown for users with sysop
146 * privileges. The rollback link restores the most recent version that was not
147 * written by the target user.
149 * @todo This would probably look a lot nicer in a table.
150 * @param stdClass $row
151 * @return string
153 function formatRow( $row ) {
155 $page = Title::makeTitle( $row->ar_namespace, $row->ar_title );
157 $rev = new Revision( array(
158 'title' => $page,
159 'id' => $row->ar_rev_id,
160 'comment' => $row->ar_comment,
161 'user' => $row->ar_user,
162 'user_text' => $row->ar_user_text,
163 'timestamp' => $row->ar_timestamp,
164 'minor_edit' => $row->ar_minor_edit,
165 'deleted' => $row->ar_deleted,
166 ) );
168 $undelete = SpecialPage::getTitleFor( 'Undelete' );
170 $logs = SpecialPage::getTitleFor( 'Log' );
171 $dellog = Linker::linkKnown(
172 $logs,
173 $this->messages['deletionlog'],
174 array(),
175 array(
176 'type' => 'delete',
177 'page' => $page->getPrefixedText()
181 $reviewlink = Linker::linkKnown(
182 SpecialPage::getTitleFor( 'Undelete', $page->getPrefixedDBkey() ),
183 $this->messages['undeleteviewlink']
186 $user = $this->getUser();
188 if ( $user->isAllowed( 'deletedtext' ) ) {
189 $last = Linker::linkKnown(
190 $undelete,
191 $this->messages['diff'],
192 array(),
193 array(
194 'target' => $page->getPrefixedText(),
195 'timestamp' => $rev->getTimestamp(),
196 'diff' => 'prev'
199 } else {
200 $last = $this->messages['diff'];
203 $comment = Linker::revComment( $rev );
204 $date = $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $user );
205 $date = htmlspecialchars( $date );
207 if ( !$user->isAllowed( 'undelete' ) || !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
208 $link = $date; // unusable link
209 } else {
210 $link = Linker::linkKnown(
211 $undelete,
212 $date,
213 array( 'class' => 'mw-changeslist-date' ),
214 array(
215 'target' => $page->getPrefixedText(),
216 'timestamp' => $rev->getTimestamp()
220 // Style deleted items
221 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
222 $link = '<span class="history-deleted">' . $link . '</span>';
225 $pagelink = Linker::link(
226 $page,
227 null,
228 array( 'class' => 'mw-changeslist-title' )
231 if ( $rev->isMinor() ) {
232 $mflag = ChangesList::flag( 'minor' );
233 } else {
234 $mflag = '';
237 // Revision delete link
238 $del = Linker::getRevDeleteLink( $user, $rev, $page );
239 if ( $del ) {
240 $del .= ' ';
243 $tools = Html::rawElement(
244 'span',
245 array( 'class' => 'mw-deletedcontribs-tools' ),
246 $this->msg( 'parentheses' )->rawParams( $this->getLanguage()->pipeList(
247 array( $last, $dellog, $reviewlink ) ) )->escaped()
250 $separator = '<span class="mw-changeslist-separator">. .</span>';
251 $ret = "{$del}{$link} {$tools} {$separator} {$mflag} {$pagelink} {$comment}";
253 # Denote if username is redacted for this edit
254 if ( $rev->isDeleted( Revision::DELETED_USER ) ) {
255 $ret .= " <strong>" . $this->msg( 'rev-deleted-user-contribs' )->escaped() . "</strong>";
258 $ret = Html::rawElement( 'li', array(), $ret ) . "\n";
260 return $ret;
264 * Get the Database object in use
266 * @return IDatabase
268 public function getDatabase() {
269 return $this->mDb;
273 class DeletedContributionsPage extends SpecialPage {
274 function __construct() {
275 parent::__construct( 'DeletedContributions', 'deletedhistory',
276 /*listed*/true, /*function*/false, /*file*/false );
280 * Special page "deleted user contributions".
281 * Shows a list of the deleted contributions of a user.
283 * @param string $par (optional) user name of the user for which to show the contributions
285 function execute( $par ) {
286 $this->setHeaders();
287 $this->outputHeader();
289 $user = $this->getUser();
291 if ( !$this->userCanExecute( $user ) ) {
292 $this->displayRestrictionError();
294 return;
297 $request = $this->getRequest();
298 $out = $this->getOutput();
299 $out->setPageTitle( $this->msg( 'deletedcontributions-title' ) );
301 $options = array();
303 if ( $par !== null ) {
304 $target = $par;
305 } else {
306 $target = $request->getVal( 'target' );
309 if ( !strlen( $target ) ) {
310 $out->addHTML( $this->getForm( '' ) );
312 return;
315 $options['limit'] = $request->getInt( 'limit',
316 $this->getConfig()->get( 'QueryPageDefaultLimit' ) );
317 $options['target'] = $target;
319 $userObj = User::newFromName( $target, false );
320 if ( !$userObj ) {
321 $out->addHTML( $this->getForm( '' ) );
323 return;
325 $this->getSkin()->setRelevantUser( $userObj );
327 $target = $userObj->getName();
328 $out->addSubtitle( $this->getSubTitle( $userObj ) );
330 if ( ( $ns = $request->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
331 $options['namespace'] = intval( $ns );
332 } else {
333 $options['namespace'] = '';
336 $out->addHTML( $this->getForm( $options ) );
338 $pager = new DeletedContribsPager( $this->getContext(), $target, $options['namespace'] );
339 if ( !$pager->getNumRows() ) {
340 $out->addWikiMsg( 'nocontribs' );
342 return;
345 # Show a message about slave lag, if applicable
346 $lag = wfGetLB()->safeGetLag( $pager->getDatabase() );
347 if ( $lag > 0 ) {
348 $out->showLagWarning( $lag );
351 $out->addHTML(
352 '<p>' . $pager->getNavigationBar() . '</p>' .
353 $pager->getBody() .
354 '<p>' . $pager->getNavigationBar() . '</p>' );
356 # If there were contributions, and it was a valid user or IP, show
357 # the appropriate "footer" message - WHOIS tools, etc.
358 if ( $target != 'newbies' ) {
359 $message = IP::isIPAddress( $target ) ?
360 'sp-contributions-footer-anon' :
361 'sp-contributions-footer';
363 if ( !$this->msg( $message )->isDisabled() ) {
364 $out->wrapWikiMsg(
365 "<div class='mw-contributions-footer'>\n$1\n</div>",
366 array( $message, $target )
373 * Generates the subheading with links
374 * @param User $userObj User object for the target
375 * @return string Appropriately-escaped HTML to be output literally
376 * @todo FIXME: Almost the same as contributionsSub in SpecialContributions.php. Could be combined.
378 function getSubTitle( $userObj ) {
379 if ( $userObj->isAnon() ) {
380 $user = htmlspecialchars( $userObj->getName() );
381 } else {
382 $user = Linker::link( $userObj->getUserPage(), htmlspecialchars( $userObj->getName() ) );
384 $links = '';
385 $nt = $userObj->getUserPage();
386 $id = $userObj->getID();
387 $talk = $nt->getTalkPage();
388 if ( $talk ) {
389 # Talk page link
390 $tools[] = Linker::link( $talk, $this->msg( 'sp-contributions-talk' )->escaped() );
391 if ( ( $id !== null ) || ( $id === null && IP::isIPAddress( $nt->getText() ) ) ) {
392 # Block / Change block / Unblock links
393 if ( $this->getUser()->isAllowed( 'block' ) ) {
394 if ( $userObj->isBlocked() ) {
395 $tools[] = Linker::linkKnown( # Change block link
396 SpecialPage::getTitleFor( 'Block', $nt->getDBkey() ),
397 $this->msg( 'change-blocklink' )->escaped()
399 $tools[] = Linker::linkKnown( # Unblock link
400 SpecialPage::getTitleFor( 'BlockList' ),
401 $this->msg( 'unblocklink' )->escaped(),
402 array(),
403 array(
404 'action' => 'unblock',
405 'ip' => $nt->getDBkey()
408 } else {
409 # User is not blocked
410 $tools[] = Linker::linkKnown( # Block link
411 SpecialPage::getTitleFor( 'Block', $nt->getDBkey() ),
412 $this->msg( 'blocklink' )->escaped()
416 # Block log link
417 $tools[] = Linker::linkKnown(
418 SpecialPage::getTitleFor( 'Log' ),
419 $this->msg( 'sp-contributions-blocklog' )->escaped(),
420 array(),
421 array(
422 'type' => 'block',
423 'page' => $nt->getPrefixedText()
426 # Suppression log link (bug 59120)
427 if ( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
428 $tools[] = Linker::linkKnown(
429 SpecialPage::getTitleFor( 'Log', 'suppress' ),
430 $this->msg( 'sp-contributions-suppresslog' )->escaped(),
431 array(),
432 array( 'offender' => $userObj->getName() )
437 # Uploads
438 $tools[] = Linker::linkKnown(
439 SpecialPage::getTitleFor( 'Listfiles', $userObj->getName() ),
440 $this->msg( 'sp-contributions-uploads' )->escaped()
443 # Other logs link
444 $tools[] = Linker::linkKnown(
445 SpecialPage::getTitleFor( 'Log' ),
446 $this->msg( 'sp-contributions-logs' )->escaped(),
447 array(),
448 array( 'user' => $nt->getText() )
450 # Link to contributions
451 $tools[] = Linker::linkKnown(
452 SpecialPage::getTitleFor( 'Contributions', $nt->getDBkey() ),
453 $this->msg( 'sp-deletedcontributions-contribs' )->escaped()
456 # Add a link to change user rights for privileged users
457 $userrightsPage = new UserrightsPage();
458 $userrightsPage->setContext( $this->getContext() );
459 if ( $userrightsPage->userCanChangeRights( $userObj ) ) {
460 $tools[] = Linker::linkKnown(
461 SpecialPage::getTitleFor( 'Userrights', $nt->getDBkey() ),
462 $this->msg( 'sp-contributions-userrights' )->escaped()
466 Hooks::run( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
468 $links = $this->getLanguage()->pipeList( $tools );
470 // Show a note if the user is blocked and display the last block log entry.
471 $block = Block::newFromTarget( $userObj, $userObj );
472 if ( !is_null( $block ) && $block->getType() != Block::TYPE_AUTO ) {
473 if ( $block->getType() == Block::TYPE_RANGE ) {
474 $nt = MWNamespace::getCanonicalName( NS_USER ) . ':' . $block->getTarget();
477 // LogEventsList::showLogExtract() wants the first parameter by ref
478 $out = $this->getOutput();
479 LogEventsList::showLogExtract(
480 $out,
481 'block',
482 $nt,
484 array(
485 'lim' => 1,
486 'showIfEmpty' => false,
487 'msgKey' => array(
488 'sp-contributions-blocked-notice',
489 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
491 'offset' => '' # don't use $this->getRequest() parameter offset
497 return $this->msg( 'contribsub2' )->rawParams( $user, $links )->params( $userObj->getName() );
501 * Generates the namespace selector form with hidden attributes.
502 * @param array $options The options to be included.
503 * @return string
505 function getForm( $options ) {
506 $options['title'] = $this->getPageTitle()->getPrefixedText();
507 if ( !isset( $options['target'] ) ) {
508 $options['target'] = '';
509 } else {
510 $options['target'] = str_replace( '_', ' ', $options['target'] );
513 if ( !isset( $options['namespace'] ) ) {
514 $options['namespace'] = '';
517 if ( !isset( $options['contribs'] ) ) {
518 $options['contribs'] = 'user';
521 if ( $options['contribs'] == 'newbie' ) {
522 $options['target'] = '';
525 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => wfScript() ) );
527 foreach ( $options as $name => $value ) {
528 if ( in_array( $name, array( 'namespace', 'target', 'contribs' ) ) ) {
529 continue;
531 $f .= "\t" . Html::hidden( $name, $value ) . "\n";
534 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
536 $f .= Xml::openElement( 'fieldset' );
537 $f .= Xml::element( 'legend', array(), $this->msg( 'sp-contributions-search' )->text() );
538 $f .= Xml::tags(
539 'label',
540 array( 'for' => 'target' ),
541 $this->msg( 'sp-contributions-username' )->parse()
542 ) . ' ';
543 $f .= Html::input(
544 'target',
545 $options['target'],
546 'text',
547 array(
548 'size' => '20',
549 'required' => '',
550 'class' => array(
551 'mw-autocomplete-user', // used by mediawiki.userSuggest
553 ) + ( $options['target'] ? array() : array( 'autofocus' ) )
554 ) . ' ';
555 $f .= Html::namespaceSelector(
556 array(
557 'selected' => $options['namespace'],
558 'all' => '',
559 'label' => $this->msg( 'namespace' )->text()
561 array(
562 'name' => 'namespace',
563 'id' => 'namespace',
564 'class' => 'namespaceselector',
566 ) . ' ';
567 $f .= Xml::submitButton( $this->msg( 'sp-contributions-submit' )->text() );
568 $f .= Xml::closeElement( 'fieldset' );
569 $f .= Xml::closeElement( 'form' );
571 return $f;
574 protected function getGroupName() {
575 return 'users';