Merge "Changed LoginForm::addNewaccountInternal() to return Status."
[lhc/web/wiklou.git] / includes / specials / SpecialDeletedContributions.php
1 <?php
2 /**
3 * Implements Special:DeletedContributions
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Implements Special:DeletedContributions to display archived revisions
26 * @ingroup SpecialPage
27 */
28 class DeletedContribsPager extends IndexPager {
29 public $mDefaultDirection = true;
30 var $messages, $target;
31 var $namespace = '', $mDb;
32
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();
38 }
39 $this->target = $target;
40 $this->namespace = $namespace;
41 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
42 }
43
44 function getDefaultQuery() {
45 $query = parent::getDefaultQuery();
46 $query['target'] = $this->target;
47 return $query;
48 }
49
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;
60 }
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'
66 ),
67 'conds' => $conds,
68 'options' => array( 'USE INDEX' => $index )
69 );
70 }
71
72 function getUserCond() {
73 $condition = array();
74
75 $condition['ar_user_text'] = $this->target;
76 $index = 'usertext_timestamp';
77
78 return array( $index, $condition );
79 }
80
81 function getIndexField() {
82 return 'ar_timestamp';
83 }
84
85 function getStartBody() {
86 return "<ul>\n";
87 }
88
89 function getEndBody() {
90 return "</ul>\n";
91 }
92
93 function getNavigationBar() {
94 if ( isset( $this->mNavigationBar ) ) {
95 return $this->mNavigationBar;
96 }
97
98 $linkTexts = array(
99 'prev' => $this->msg( 'pager-newer-n' )->numParams( $this->mLimit )->escaped(),
100 'next' => $this->msg( 'pager-older-n' )->numParams( $this->mLimit )->escaped(),
101 'first' => $this->msg( 'histlast' )->escaped(),
102 'last' => $this->msg( 'histfirst' )->escaped()
103 );
104
105 $pagingLinks = $this->getPagingLinks( $linkTexts );
106 $limitLinks = $this->getLimitLinks();
107 $lang = $this->getLanguage();
108 $limits = $lang->pipeList( $limitLinks );
109
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;
113 }
114
115 function getNamespaceCond() {
116 if ( $this->namespace !== '' ) {
117 return array( 'ar_namespace' => (int)$this->namespace );
118 } else {
119 return array();
120 }
121 }
122
123 /**
124 * Generates each row in the contributions list.
125 *
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.
130 *
131 * @todo This would probably look a lot nicer in a table.
132 * @param $row
133 * @return string
134 */
135 function formatRow( $row ) {
136 wfProfileIn( __METHOD__ );
137
138 $page = Title::makeTitle( $row->ar_namespace, $row->ar_title );
139
140 $rev = new Revision( array(
141 'title' => $page,
142 'id' => $row->ar_rev_id,
143 'comment' => $row->ar_comment,
144 'user' => $row->ar_user,
145 'user_text' => $row->ar_user_text,
146 'timestamp' => $row->ar_timestamp,
147 'minor_edit' => $row->ar_minor_edit,
148 'deleted' => $row->ar_deleted,
149 ) );
150
151 $undelete = SpecialPage::getTitleFor( 'Undelete' );
152
153 $logs = SpecialPage::getTitleFor( 'Log' );
154 $dellog = Linker::linkKnown(
155 $logs,
156 $this->messages['deletionlog'],
157 array(),
158 array(
159 'type' => 'delete',
160 'page' => $page->getPrefixedText()
161 )
162 );
163
164 $reviewlink = Linker::linkKnown(
165 SpecialPage::getTitleFor( 'Undelete', $page->getPrefixedDBkey() ),
166 $this->messages['undeleteviewlink']
167 );
168
169 $user = $this->getUser();
170
171 if( $user->isAllowed('deletedtext') ) {
172 $last = Linker::linkKnown(
173 $undelete,
174 $this->messages['diff'],
175 array(),
176 array(
177 'target' => $page->getPrefixedText(),
178 'timestamp' => $rev->getTimestamp(),
179 'diff' => 'prev'
180 )
181 );
182 } else {
183 $last = $this->messages['diff'];
184 }
185
186 $comment = Linker::revComment( $rev );
187 $date = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $user ) );
188
189 if( !$user->isAllowed( 'undelete' ) || !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
190 $link = $date; // unusable link
191 } else {
192 $link = Linker::linkKnown(
193 $undelete,
194 $date,
195 array( 'class' => 'mw-changeslist-date' ),
196 array(
197 'target' => $page->getPrefixedText(),
198 'timestamp' => $rev->getTimestamp()
199 )
200 );
201 }
202 // Style deleted items
203 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
204 $link = '<span class="history-deleted">' . $link . '</span>';
205 }
206
207 $pagelink = Linker::link(
208 $page,
209 null,
210 array( 'class' => 'mw-changeslist-title' )
211 );
212
213 if( $rev->isMinor() ) {
214 $mflag = ChangesList::flag( 'minor' );
215 } else {
216 $mflag = '';
217 }
218
219 // Revision delete link
220 $del = Linker::getRevDeleteLink( $user, $rev, $page );
221 if ( $del ) $del .= ' ';
222
223 $tools = Html::rawElement(
224 'span',
225 array( 'class' => 'mw-deletedcontribs-tools' ),
226 $this->msg( 'parentheses' )->rawParams( $this->getLanguage()->pipeList(
227 array( $last, $dellog, $reviewlink ) ) )->escaped()
228 );
229
230 $separator = '<span class="mw-changeslist-separator">. .</span>';
231 $ret = "{$del}{$link} {$tools} {$separator} {$mflag} {$pagelink} {$comment}";
232
233 # Denote if username is redacted for this edit
234 if( $rev->isDeleted( Revision::DELETED_USER ) ) {
235 $ret .= " <strong>" . $this->msg( 'rev-deleted-user-contribs' )->escaped() . "</strong>";
236 }
237
238 $ret = Html::rawElement( 'li', array(), $ret ) . "\n";
239
240 wfProfileOut( __METHOD__ );
241 return $ret;
242 }
243
244 /**
245 * Get the Database object in use
246 *
247 * @return DatabaseBase
248 */
249 public function getDatabase() {
250 return $this->mDb;
251 }
252 }
253
254 class DeletedContributionsPage extends SpecialPage {
255 function __construct() {
256 parent::__construct( 'DeletedContributions', 'deletedhistory',
257 /*listed*/ true, /*function*/ false, /*file*/ false );
258 }
259
260 /**
261 * Special page "deleted user contributions".
262 * Shows a list of the deleted contributions of a user.
263 *
264 * @param $par String: (optional) user name of the user for which to show the contributions
265 */
266 function execute( $par ) {
267 global $wgQueryPageDefaultLimit;
268 $this->setHeaders();
269 $this->outputHeader();
270
271 $user = $this->getUser();
272
273 if ( !$this->userCanExecute( $user ) ) {
274 $this->displayRestrictionError();
275 return;
276 }
277
278 $request = $this->getRequest();
279 $out = $this->getOutput();
280 $out->setPageTitle( $this->msg( 'deletedcontributions-title' ) );
281
282 $options = array();
283
284 if ( $par !== null ) {
285 $target = $par;
286 } else {
287 $target = $request->getVal( 'target' );
288 }
289
290 if ( !strlen( $target ) ) {
291 $out->addHTML( $this->getForm( '' ) );
292 return;
293 }
294
295 $options['limit'] = $request->getInt( 'limit', $wgQueryPageDefaultLimit );
296 $options['target'] = $target;
297
298 $userObj = User::newFromName( $target, false );
299 if ( !$userObj ) {
300 $out->addHTML( $this->getForm( '' ) );
301 return;
302 }
303 $this->getSkin()->setRelevantUser( $userObj );
304
305 $target = $userObj->getName();
306 $out->addSubtitle( $this->getSubTitle( $userObj ) );
307
308 if ( ( $ns = $request->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
309 $options['namespace'] = intval( $ns );
310 } else {
311 $options['namespace'] = '';
312 }
313
314 $out->addHTML( $this->getForm( $options ) );
315
316 $pager = new DeletedContribsPager( $this->getContext(), $target, $options['namespace'] );
317 if ( !$pager->getNumRows() ) {
318 $out->addWikiMsg( 'nocontribs' );
319 return;
320 }
321
322 # Show a message about slave lag, if applicable
323 $lag = wfGetLB()->safeGetLag( $pager->getDatabase() );
324 if( $lag > 0 )
325 $out->showLagWarning( $lag );
326
327 $out->addHTML(
328 '<p>' . $pager->getNavigationBar() . '</p>' .
329 $pager->getBody() .
330 '<p>' . $pager->getNavigationBar() . '</p>' );
331
332 # If there were contributions, and it was a valid user or IP, show
333 # the appropriate "footer" message - WHOIS tools, etc.
334 if( $target != 'newbies' ) {
335 $message = IP::isIPAddress( $target )
336 ? 'sp-contributions-footer-anon'
337 : 'sp-contributions-footer';
338
339 if( !$this->msg( $message )->isDisabled() ) {
340 $out->wrapWikiMsg( "<div class='mw-contributions-footer'>\n$1\n</div>", array( $message, $target ) );
341 }
342 }
343 }
344
345 /**
346 * Generates the subheading with links
347 * @param $userObj User object for the target
348 * @return String: appropriately-escaped HTML to be output literally
349 * @todo FIXME: Almost the same as contributionsSub in SpecialContributions.php. Could be combined.
350 */
351 function getSubTitle( $userObj ) {
352 if ( $userObj->isAnon() ) {
353 $user = htmlspecialchars( $userObj->getName() );
354 } else {
355 $user = Linker::link( $userObj->getUserPage(), htmlspecialchars( $userObj->getName() ) );
356 }
357 $links = '';
358 $nt = $userObj->getUserPage();
359 $id = $userObj->getID();
360 $talk = $nt->getTalkPage();
361 if( $talk ) {
362 # Talk page link
363 $tools[] = Linker::link( $talk, $this->msg( 'sp-contributions-talk' )->escaped() );
364 if( ( $id !== null ) || ( $id === null && IP::isIPAddress( $nt->getText() ) ) ) {
365 if( $this->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
366 if ( $userObj->isBlocked() ) {
367 $tools[] = Linker::linkKnown( # Change block link
368 SpecialPage::getTitleFor( 'Block', $nt->getDBkey() ),
369 $this->msg( 'change-blocklink' )->escaped()
370 );
371 $tools[] = Linker::linkKnown( # Unblock link
372 SpecialPage::getTitleFor( 'BlockList' ),
373 $this->msg( 'unblocklink' )->escaped(),
374 array(),
375 array(
376 'action' => 'unblock',
377 'ip' => $nt->getDBkey()
378 )
379 );
380 }
381 else { # User is not blocked
382 $tools[] = Linker::linkKnown( # Block link
383 SpecialPage::getTitleFor( 'Block', $nt->getDBkey() ),
384 $this->msg( 'blocklink' )->escaped()
385 );
386 }
387 }
388 # Block log link
389 $tools[] = Linker::linkKnown(
390 SpecialPage::getTitleFor( 'Log' ),
391 $this->msg( 'sp-contributions-blocklog' )->escaped(),
392 array(),
393 array(
394 'type' => 'block',
395 'page' => $nt->getPrefixedText()
396 )
397 );
398 }
399
400 # Uploads
401 $tools[] = Linker::linkKnown(
402 SpecialPage::getTitleFor( 'Listfiles', $userObj->getName() ),
403 $this->msg( 'sp-contributions-uploads' )->escaped()
404 );
405
406 # Other logs link
407 $tools[] = Linker::linkKnown(
408 SpecialPage::getTitleFor( 'Log' ),
409 $this->msg( 'sp-contributions-logs' )->escaped(),
410 array(),
411 array( 'user' => $nt->getText() )
412 );
413 # Link to contributions
414 $tools[] = Linker::linkKnown(
415 SpecialPage::getTitleFor( 'Contributions', $nt->getDBkey() ),
416 $this->msg( 'sp-deletedcontributions-contribs' )->escaped()
417 );
418
419 # Add a link to change user rights for privileged users
420 $userrightsPage = new UserrightsPage();
421 $userrightsPage->setContext( $this->getContext() );
422 if( $userrightsPage->userCanChangeRights( $userObj ) ) {
423 $tools[] = Linker::linkKnown(
424 SpecialPage::getTitleFor( 'Userrights', $nt->getDBkey() ),
425 $this->msg( 'sp-contributions-userrights' )->escaped()
426 );
427 }
428
429 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
430
431 $links = $this->getLanguage()->pipeList( $tools );
432
433 // Show a note if the user is blocked and display the last block log entry.
434 if ( $userObj->isBlocked() ) {
435 $out = $this->getOutput(); // LogEventsList::showLogExtract() wants the first parameter by ref
436 LogEventsList::showLogExtract(
437 $out,
438 'block',
439 $nt,
440 '',
441 array(
442 'lim' => 1,
443 'showIfEmpty' => false,
444 'msgKey' => array(
445 'sp-contributions-blocked-notice',
446 $nt->getText() # Support GENDER in 'sp-contributions-blocked-notice'
447 ),
448 'offset' => '' # don't use $this->getRequest() parameter offset
449 )
450 );
451 }
452 }
453
454 // Old message 'contribsub' had one parameter, but that doesn't work for
455 // languages that want to put the "for" bit right after $user but before
456 // $links. If 'contribsub' is around, use it for reverse compatibility,
457 // otherwise use 'contribsub2'.
458 $oldMsg = $this->msg( 'contribsub' );
459 if ( $oldMsg->exists() ) {
460 return $oldMsg->rawParams( "$user ($links)" );
461 } else {
462 return $this->msg( 'contribsub2' )->rawParams( $user, $links );
463 }
464 }
465
466 /**
467 * Generates the namespace selector form with hidden attributes.
468 * @param $options Array: the options to be included.
469 * @return string
470 */
471 function getForm( $options ) {
472 global $wgScript;
473
474 $options['title'] = $this->getTitle()->getPrefixedText();
475 if ( !isset( $options['target'] ) ) {
476 $options['target'] = '';
477 } else {
478 $options['target'] = str_replace( '_' , ' ' , $options['target'] );
479 }
480
481 if ( !isset( $options['namespace'] ) ) {
482 $options['namespace'] = '';
483 }
484
485 if ( !isset( $options['contribs'] ) ) {
486 $options['contribs'] = 'user';
487 }
488
489 if ( $options['contribs'] == 'newbie' ) {
490 $options['target'] = '';
491 }
492
493 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
494
495 foreach ( $options as $name => $value ) {
496 if ( in_array( $name, array( 'namespace', 'target', 'contribs' ) ) ) {
497 continue;
498 }
499 $f .= "\t" . Html::hidden( $name, $value ) . "\n";
500 }
501
502 $f .= Xml::openElement( 'fieldset' ) .
503 Xml::element( 'legend', array(), $this->msg( 'sp-contributions-search' )->text() ) .
504 Xml::tags( 'label', array( 'for' => 'target' ), $this->msg( 'sp-contributions-username' )->parse() ) . ' ' .
505 Html::input( 'target', $options['target'], 'text', array(
506 'size' => '20',
507 'required' => ''
508 ) + ( $options['target'] ? array() : array( 'autofocus' ) ) ) . ' '.
509 Html::namespaceSelector(
510 array(
511 'selected' => $options['namespace'],
512 'all' => '',
513 'label' => $this->msg( 'namespace' )->text()
514 ), array(
515 'name' => 'namespace',
516 'id' => 'namespace',
517 'class' => 'namespaceselector',
518 )
519 ) . ' ' .
520 Xml::submitButton( $this->msg( 'sp-contributions-submit' )->text() ) .
521 Xml::closeElement( 'fieldset' ) .
522 Xml::closeElement( 'form' );
523 return $f;
524 }
525 }