* Made IndexPager extend ContextSource
[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( $target, $namespace = false ) {
35 parent::__construct();
36 $msgs = array( 'deletionlog', 'undeleteviewlink', 'diff' );
37 foreach( $msgs as $msg ) {
38 $this->messages[$msg] = wfMsgExt( $msg, array( 'escapenoentities') );
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->getLang();
99 $fmtLimit = $lang->formatNum( $this->mLimit );
100 $linkTexts = array(
101 'prev' => wfMsgExt( 'pager-newer-n', array( 'escape', 'parsemag' ), $fmtLimit ),
102 'next' => wfMsgExt( 'pager-older-n', array( 'escape', 'parsemag' ), $fmtLimit ),
103 'first' => wfMsgHtml( 'histlast' ),
104 'last' => wfMsgHtml( 'histfirst' )
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 wfMsgExt( 'viewprevnext', array( 'parsemag', 'escape', 'replaceafter' ), $pagingLinks['prev'], $pagingLinks['next'], $limits );
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->getLang()->timeanddate( $rev->getTimestamp(), true ) );
186
187 if( !$user->isAllowed('undelete') || !$rev->userCan(Revision::DELETED_TEXT) ) {
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 wfMsg( 'parentheses', $this->getLang()->pipeList( array( $last, $dellog, $reviewlink ) ) )
221 );
222
223 $ret = "{$del}{$link} {$tools} . . {$mflag} {$pagelink} {$comment}";
224
225 # Denote if username is redacted for this edit
226 if( $rev->isDeleted( Revision::DELETED_USER ) ) {
227 $ret .= " <strong>" . wfMsgHtml('rev-deleted-user-contribs') . "</strong>";
228 }
229
230 $ret = Html::rawElement( 'li', array(), $ret ) . "\n";
231
232 wfProfileOut( __METHOD__ );
233 return $ret;
234 }
235
236 /**
237 * Get the Database object in use
238 *
239 * @return Database
240 */
241 public function getDatabase() {
242 return $this->mDb;
243 }
244 }
245
246 class DeletedContributionsPage extends SpecialPage {
247 function __construct() {
248 parent::__construct( 'DeletedContributions', 'deletedhistory',
249 /*listed*/ true, /*function*/ false, /*file*/ false );
250 }
251
252 /**
253 * Special page "deleted user contributions".
254 * Shows a list of the deleted contributions of a user.
255 *
256 * @return none
257 * @param $par String: (optional) user name of the user for which to show the contributions
258 */
259 function execute( $par ) {
260 $this->setHeaders();
261
262 $user = $this->getUser();
263
264 if ( !$this->userCanExecute( $user ) ) {
265 $this->displayRestrictionError();
266 return;
267 }
268
269 if( $user->isBlocked() ){
270 throw new UserBlockedError( $user->getBlock() );
271 }
272
273 $request = $this->getRequest();
274 $out = $this->getOutput();
275 $out->setPageTitle( wfMsgExt( 'deletedcontributions-title', array( 'parsemag' ) ) );
276
277 $options = array();
278
279 if ( $par !== null ) {
280 $target = $par;
281 } else {
282 $target = $request->getVal( 'target' );
283 }
284
285 if ( !strlen( $target ) ) {
286 $out->addHTML( $this->getForm( '' ) );
287 return;
288 }
289
290 $options['limit'] = $request->getInt( 'limit', 50 );
291 $options['target'] = $target;
292
293 $nt = Title::makeTitleSafe( NS_USER, $target );
294 if ( !$nt ) {
295 $out->addHTML( $this->getForm( '' ) );
296 return;
297 }
298 $id = User::idFromName( $nt->getText() );
299
300 $target = $nt->getText();
301 $out->setSubtitle( $this->getSubTitle( $nt, $id ) );
302
303 if ( ( $ns = $request->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
304 $options['namespace'] = intval( $ns );
305 } else {
306 $options['namespace'] = '';
307 }
308
309 $out->addHTML( $this->getForm( $options ) );
310
311 $pager = new DeletedContribsPager( $target, $options['namespace'] );
312 if ( !$pager->getNumRows() ) {
313 $out->addWikiMsg( 'nocontribs' );
314 return;
315 }
316
317 # Show a message about slave lag, if applicable
318 $lag = wfGetLB()->safeGetLag( $pager->getDatabase() );
319 if( $lag > 0 )
320 $out->showLagWarning( $lag );
321
322 $out->addHTML(
323 '<p>' . $pager->getNavigationBar() . '</p>' .
324 $pager->getBody() .
325 '<p>' . $pager->getNavigationBar() . '</p>' );
326
327 # If there were contributions, and it was a valid user or IP, show
328 # the appropriate "footer" message - WHOIS tools, etc.
329 if( $target != 'newbies' ) {
330 $message = IP::isIPAddress( $target )
331 ? 'sp-contributions-footer-anon'
332 : 'sp-contributions-footer';
333
334 if( !wfMessage( $message )->isDisabled() ) {
335 $out->wrapWikiMsg( "<div class='mw-contributions-footer'>\n$1\n</div>", array( $message, $target ) );
336 }
337 }
338 }
339
340 /**
341 * Generates the subheading with links
342 * @param $nt Title object for the target
343 * @param $id Integer: User ID for the target
344 * @return String: appropriately-escaped HTML to be output literally
345 * @todo FIXME: Almost the same as contributionsSub in SpecialContributions.php. Could be combined.
346 */
347 function getSubTitle( $nt, $id ) {
348 if ( $id === null ) {
349 $user = htmlspecialchars( $nt->getText() );
350 } else {
351 $user = Linker::link( $nt, htmlspecialchars( $nt->getText() ) );
352 }
353 $userObj = User::newFromName( $nt->getText(), /* check for username validity not needed */ false );
354 $talk = $nt->getTalkPage();
355 if( $talk ) {
356 # Talk page link
357 $tools[] = Linker::link( $talk, wfMsgHtml( 'sp-contributions-talk' ) );
358 if( ( $id !== null ) || ( $id === null && IP::isIPAddress( $nt->getText() ) ) ) {
359 if( $this->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
360 if ( $userObj->isBlocked() ) {
361 $tools[] = Linker::linkKnown( # Change block link
362 SpecialPage::getTitleFor( 'Block', $nt->getDBkey() ),
363 wfMsgHtml( 'change-blocklink' )
364 );
365 $tools[] = Linker::linkKnown( # Unblock link
366 SpecialPage::getTitleFor( 'BlockList' ),
367 wfMsgHtml( 'unblocklink' ),
368 array(),
369 array(
370 'action' => 'unblock',
371 'ip' => $nt->getDBkey()
372 )
373 );
374 }
375 else { # User is not blocked
376 $tools[] = Linker::linkKnown( # Block link
377 SpecialPage::getTitleFor( 'Block', $nt->getDBkey() ),
378 wfMsgHtml( 'blocklink' )
379 );
380 }
381 }
382 # Block log link
383 $tools[] = Linker::linkKnown(
384 SpecialPage::getTitleFor( 'Log' ),
385 wfMsgHtml( 'sp-contributions-blocklog' ),
386 array(),
387 array(
388 'type' => 'block',
389 'page' => $nt->getPrefixedText()
390 )
391 );
392 }
393 # Other logs link
394 $tools[] = Linker::linkKnown(
395 SpecialPage::getTitleFor( 'Log' ),
396 wfMsgHtml( 'sp-contributions-logs' ),
397 array(),
398 array( 'user' => $nt->getText() )
399 );
400 # Link to contributions
401 $tools[] = Linker::linkKnown(
402 SpecialPage::getTitleFor( 'Contributions', $nt->getDBkey() ),
403 wfMsgHtml( 'sp-deletedcontributions-contribs' )
404 );
405
406 # Add a link to change user rights for privileged users
407 $userrightsPage = new UserrightsPage();
408 if( $id !== null && $userrightsPage->userCanChangeRights( User::newFromId( $id ) ) ) {
409 $tools[] = Linker::linkKnown(
410 SpecialPage::getTitleFor( 'Userrights', $nt->getDBkey() ),
411 wfMsgHtml( 'sp-contributions-userrights' )
412 );
413 }
414
415 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
416
417 $links = $this->getLang()->pipeList( $tools );
418
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->getPrefixedText(),
426 '',
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'
433 ),
434 'offset' => '' # don't use $this->getRequest() parameter offset
435 )
436 );
437 }
438 }
439
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 if( wfEmptyMsg( 'contribsub' ) ) {
445 return wfMsgHtml( 'contribsub2', $user, $links );
446 } else {
447 return wfMsgHtml( 'contribsub', "$user ($links)" );
448 }
449 }
450
451 /**
452 * Generates the namespace selector form with hidden attributes.
453 * @param $options Array: the options to be included.
454 */
455 function getForm( $options ) {
456 global $wgScript;
457
458 $options['title'] = $this->getTitle()->getPrefixedText();
459 if ( !isset( $options['target'] ) ) {
460 $options['target'] = '';
461 } else {
462 $options['target'] = str_replace( '_' , ' ' , $options['target'] );
463 }
464
465 if ( !isset( $options['namespace'] ) ) {
466 $options['namespace'] = '';
467 }
468
469 if ( !isset( $options['contribs'] ) ) {
470 $options['contribs'] = 'user';
471 }
472
473 if ( $options['contribs'] == 'newbie' ) {
474 $options['target'] = '';
475 }
476
477 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
478
479 foreach ( $options as $name => $value ) {
480 if ( in_array( $name, array( 'namespace', 'target', 'contribs' ) ) ) {
481 continue;
482 }
483 $f .= "\t" . Html::hidden( $name, $value ) . "\n";
484 }
485
486 $f .= Xml::openElement( 'fieldset' ) .
487 Xml::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
488 Xml::tags( 'label', array( 'for' => 'target' ), wfMsgExt( 'sp-contributions-username', 'parseinline' ) ) . ' ' .
489 Html::input( 'target', $options['target'], 'text', array(
490 'size' => '20',
491 'required' => ''
492 ) + ( $options['target'] ? array() : array( 'autofocus' ) ) ) . ' '.
493 Xml::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
494 Xml::namespaceSelector( $options['namespace'], '' ) . ' ' .
495 Xml::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
496 Xml::closeElement( 'fieldset' ) .
497 Xml::closeElement( 'form' );
498 return $f;
499 }
500 }