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