Correct the address of the FSF in some of the GPL headers
[lhc/web/wiklou.git] / includes / specials / SpecialDeletedContributions.php
1 <?php
2 /**
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 */
19
20 /**
21 * Implements Special:DeletedContributions to display archived revisions
22 * @ingroup SpecialPage
23 */
24
25 class DeletedContribsPager extends IndexPager {
26 public $mDefaultDirection = true;
27 var $messages, $target;
28 var $namespace = '', $mDb;
29
30 function __construct( $target, $namespace = false ) {
31 parent::__construct();
32 $msgs = array( 'deletionlog', 'undeleteviewlink', 'diff' );
33 foreach( $msgs as $msg ) {
34 $this->messages[$msg] = wfMsgExt( $msg, array( 'escapenoentities') );
35 }
36 $this->target = $target;
37 $this->namespace = $namespace;
38 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
39 }
40
41 function getDefaultQuery() {
42 $query = parent::getDefaultQuery();
43 $query['target'] = $this->target;
44 return $query;
45 }
46
47 function getQueryInfo() {
48 global $wgUser;
49 list( $index, $userCond ) = $this->getUserCond();
50 $conds = array_merge( $userCond, $this->getNamespaceCond() );
51 // Paranoia: avoid brute force searches (bug 17792)
52 if( !$wgUser->isAllowed( 'deletedhistory' ) ) {
53 $conds[] = $this->mDb->bitAnd('ar_deleted',Revision::DELETED_USER) . ' = 0';
54 } else if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
55 $conds[] = $this->mDb->bitAnd('ar_deleted',Revision::SUPPRESSED_USER) .
56 ' != ' . Revision::SUPPRESSED_USER;
57 }
58 return array(
59 'tables' => array( 'archive' ),
60 'fields' => array(
61 'ar_rev_id', 'ar_namespace', 'ar_title', 'ar_timestamp', 'ar_comment', 'ar_minor_edit',
62 'ar_user', 'ar_user_text', 'ar_deleted'
63 ),
64 'conds' => $conds,
65 'options' => array( 'USE INDEX' => $index )
66 );
67 }
68
69 function getUserCond() {
70 $condition = array();
71
72 $condition['ar_user_text'] = $this->target;
73 $index = 'usertext_timestamp';
74
75 return array( $index, $condition );
76 }
77
78 function getIndexField() {
79 return 'ar_timestamp';
80 }
81
82 function getStartBody() {
83 return "<ul>\n";
84 }
85
86 function getEndBody() {
87 return "</ul>\n";
88 }
89
90 function getNavigationBar() {
91 global $wgLang;
92
93 if ( isset( $this->mNavigationBar ) ) {
94 return $this->mNavigationBar;
95 }
96 $fmtLimit = $wgLang->formatNum( $this->mLimit );
97 $linkTexts = array(
98 'prev' => wfMsgExt( 'pager-newer-n', array( 'escape', 'parsemag' ), $fmtLimit ),
99 'next' => wfMsgExt( 'pager-older-n', array( 'escape', 'parsemag' ), $fmtLimit ),
100 'first' => wfMsgHtml( 'histlast' ),
101 'last' => wfMsgHtml( 'histfirst' )
102 );
103
104 $pagingLinks = $this->getPagingLinks( $linkTexts );
105 $limitLinks = $this->getLimitLinks();
106 $limits = $wgLang->pipeList( $limitLinks );
107
108 $this->mNavigationBar = "(" . $wgLang->pipeList( array( $pagingLinks['first'], $pagingLinks['last'] ) ) . ") " .
109 wfMsgExt( 'viewprevnext', array( 'parsemag', 'escape', 'replaceafter' ), $pagingLinks['prev'], $pagingLinks['next'], $limits );
110 return $this->mNavigationBar;
111 }
112
113 function getNamespaceCond() {
114 if ( $this->namespace !== '' ) {
115 return array( 'ar_namespace' => (int)$this->namespace );
116 } else {
117 return array();
118 }
119 }
120
121 /**
122 * Generates each row in the contributions list.
123 *
124 * Contributions which are marked "top" are currently on top of the history.
125 * For these contributions, a [rollback] link is shown for users with sysop
126 * privileges. The rollback link restores the most recent version that was not
127 * written by the target user.
128 *
129 * @todo This would probably look a lot nicer in a table.
130 */
131 function formatRow( $row ) {
132 global $wgUser, $wgLang;
133 wfProfileIn( __METHOD__ );
134
135 $sk = $this->getSkin();
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 = $sk->linkKnown(
153 $logs,
154 $this->messages['deletionlog'],
155 array(),
156 array(
157 'type' => 'delete',
158 'page' => $page->getPrefixedText()
159 )
160 );
161
162 $reviewlink = $sk->linkKnown(
163 SpecialPage::getTitleFor( 'Undelete', $page->getPrefixedDBkey() ),
164 $this->messages['undeleteviewlink']
165 );
166
167 if( $wgUser->isAllowed('deletedtext') ) {
168 $last = $sk->linkKnown(
169 $undelete,
170 $this->messages['diff'],
171 array(),
172 array(
173 'target' => $page->getPrefixedText(),
174 'timestamp' => $rev->getTimestamp(),
175 'diff' => 'prev'
176 )
177 );
178 } else {
179 $last = $this->messages['diff'];
180 }
181
182 $comment = $sk->revComment( $rev );
183 $date = htmlspecialchars( $wgLang->timeanddate( $rev->getTimestamp(), true ) );
184
185 if( !$wgUser->isAllowed('undelete') || !$rev->userCan(Revision::DELETED_TEXT) ) {
186 $link = $date; // unusable link
187 } else {
188 $link = $sk->linkKnown(
189 $undelete,
190 $date,
191 array(),
192 array(
193 'target' => $page->getPrefixedText(),
194 'timestamp' => $rev->getTimestamp()
195 )
196 );
197 }
198 // Style deleted items
199 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
200 $link = '<span class="history-deleted">' . $link . '</span>';
201 }
202
203 $pagelink = $sk->link( $page );
204
205 if( $rev->isMinor() ) {
206 $mflag = ChangesList::flag( 'minor' );
207 } else {
208 $mflag = '';
209 }
210
211 // Revision delete link
212 $canHide = $wgUser->isAllowed( 'deleterevision' );
213 if( $canHide || ($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
214 if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
215 $del = $this->mSkin->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
216 } else {
217 $query = array(
218 'type' => 'archive',
219 'target' => $page->getPrefixedDbkey(),
220 'ids' => $rev->getTimestamp() );
221 $del = $this->mSkin->revDeleteLink( $query,
222 $rev->isDeleted( Revision::DELETED_RESTRICTED ), $canHide ) . ' ';
223 }
224 } else {
225 $del = '';
226 }
227
228 $tools = Html::rawElement(
229 'span',
230 array( 'class' => 'mw-deletedcontribs-tools' ),
231 wfMsg( 'parentheses', $wgLang->pipeList( array( $last, $dellog, $reviewlink ) ) )
232 );
233
234 $ret = "{$del}{$link} {$tools} . . {$mflag} {$pagelink} {$comment}";
235
236 # Denote if username is redacted for this edit
237 if( $rev->isDeleted( Revision::DELETED_USER ) ) {
238 $ret .= " <strong>" . wfMsgHtml('rev-deleted-user-contribs') . "</strong>";
239 }
240
241 $ret = Html::rawElement( 'li', array(), $ret ) . "\n";
242
243 wfProfileOut( __METHOD__ );
244 return $ret;
245 }
246
247 /**
248 * Get the Database object in use
249 *
250 * @return Database
251 */
252 public function getDatabase() {
253 return $this->mDb;
254 }
255 }
256
257 class DeletedContributionsPage extends SpecialPage {
258 function __construct() {
259 parent::__construct( 'DeletedContributions', 'deletedhistory',
260 /*listed*/ true, /*function*/ false, /*file*/ false );
261 }
262
263 /**
264 * Special page "deleted user contributions".
265 * Shows a list of the deleted contributions of a user.
266 *
267 * @return none
268 * @param $par String: (optional) user name of the user for which to show the contributions
269 */
270 function execute( $par ) {
271 global $wgUser;
272 $this->setHeaders();
273
274 if ( !$this->userCanExecute( $wgUser ) ) {
275 $this->displayRestrictionError();
276 return;
277 }
278
279 global $wgOut, $wgLang, $wgRequest;
280
281 $wgOut->setPageTitle( wfMsgExt( 'deletedcontributions-title', array( 'parsemag' ) ) );
282
283 $options = array();
284
285 if ( isset( $par ) ) {
286 $target = $par;
287 } else {
288 $target = $wgRequest->getVal( 'target' );
289 }
290
291 if ( !strlen( $target ) ) {
292 $wgOut->addHTML( $this->getForm( '' ) );
293 return;
294 }
295
296 $options['limit'] = $wgRequest->getInt( 'limit', 50 );
297 $options['target'] = $target;
298
299 $nt = Title::makeTitleSafe( NS_USER, $target );
300 if ( !$nt ) {
301 $wgOut->addHTML( $this->getForm( '' ) );
302 return;
303 }
304 $id = User::idFromName( $nt->getText() );
305
306 $target = $nt->getText();
307 $wgOut->setSubtitle( $this->getSubTitle( $nt, $id ) );
308
309 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
310 $options['namespace'] = intval( $ns );
311 } else {
312 $options['namespace'] = '';
313 }
314
315 $wgOut->addHTML( $this->getForm( $options ) );
316
317 $pager = new DeletedContribsPager( $target, $options['namespace'] );
318 if ( !$pager->getNumRows() ) {
319 $wgOut->addWikiMsg( 'nocontribs' );
320 return;
321 }
322
323 # Show a message about slave lag, if applicable
324 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
325 $wgOut->showLagWarning( $lag );
326
327 $wgOut->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
340 $text = wfMsgNoTrans( $message, $target );
341 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
342 $wgOut->wrapWikiMsg( "<div class='mw-contributions-footer'>\n$1\n</div>", array( $message, $target ) );
343 }
344 }
345 }
346
347 /**
348 * Generates the subheading with links
349 * @param $nt Title object for the target
350 * @param $id Integer: User ID for the target
351 * @return String: appropriately-escaped HTML to be output literally
352 * @todo Fixme: almost the same as contributionsSub in SpecialContributions.php. Could be combined.
353 */
354 function getSubTitle( $nt, $id ) {
355 global $wgSysopUserBans, $wgLang, $wgUser, $wgOut;
356
357 $sk = $wgUser->getSkin();
358
359 if ( $id === null ) {
360 $user = htmlspecialchars( $nt->getText() );
361 } else {
362 $user = $sk->link( $nt, htmlspecialchars( $nt->getText() ) );
363 }
364 $userObj = User::newFromName( $nt->getText(), /* check for username validity not needed */ false );
365 $talk = $nt->getTalkPage();
366 if( $talk ) {
367 # Talk page link
368 $tools[] = $sk->link( $talk, wfMsgHtml( 'sp-contributions-talk' ) );
369 if( ( $id !== null && $wgSysopUserBans ) || ( $id === null && IP::isIPAddress( $nt->getText() ) ) ) {
370 if( $wgUser->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
371 if ( $userObj->isBlocked() ) {
372 $tools[] = $sk->linkKnown( # Change block link
373 SpecialPage::getTitleFor( 'Blockip', $nt->getDBkey() ),
374 wfMsgHtml( 'change-blocklink' )
375 );
376 $tools[] = $sk->linkKnown( # Unblock link
377 SpecialPage::getTitleFor( 'BlockList' ),
378 wfMsgHtml( 'unblocklink' ),
379 array(),
380 array(
381 'action' => 'unblock',
382 'ip' => $nt->getDBkey()
383 )
384 );
385 }
386 else { # User is not blocked
387 $tools[] = $sk->linkKnown( # Block link
388 SpecialPage::getTitleFor( 'Blockip', $nt->getDBkey() ),
389 wfMsgHtml( 'blocklink' )
390 );
391 }
392 }
393 # Block log link
394 $tools[] = $sk->linkKnown(
395 SpecialPage::getTitleFor( 'Log' ),
396 wfMsgHtml( 'sp-contributions-blocklog' ),
397 array(),
398 array(
399 'type' => 'block',
400 'page' => $nt->getPrefixedText()
401 )
402 );
403 }
404 # Other logs link
405 $tools[] = $sk->linkKnown(
406 SpecialPage::getTitleFor( 'Log' ),
407 wfMsgHtml( 'sp-contributions-logs' ),
408 array(),
409 array( 'user' => $nt->getText() )
410 );
411 # Link to contributions
412 $tools[] = $sk->linkKnown(
413 SpecialPage::getTitleFor( 'Contributions', $nt->getDBkey() ),
414 wfMsgHtml( 'sp-deletedcontributions-contribs' )
415 );
416
417 # Add a link to change user rights for privileged users
418 $userrightsPage = new UserrightsPage();
419 if( $id !== null && $userrightsPage->userCanChangeRights( User::newFromId( $id ) ) ) {
420 $tools[] = $sk->linkKnown(
421 SpecialPage::getTitleFor( 'Userrights', $nt->getDBkey() ),
422 wfMsgHtml( 'sp-contributions-userrights' )
423 );
424 }
425
426 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
427
428 $links = $wgLang->pipeList( $tools );
429
430 // Show a note if the user is blocked and display the last block log entry.
431 if ( $userObj->isBlocked() ) {
432 LogEventsList::showLogExtract(
433 $wgOut,
434 'block',
435 $nt->getPrefixedText(),
436 '',
437 array(
438 'lim' => 1,
439 'showIfEmpty' => false,
440 'msgKey' => array(
441 'sp-contributions-blocked-notice',
442 $nt->getText() # Support GENDER in 'sp-contributions-blocked-notice'
443 ),
444 'offset' => '' # don't use $wgRequest parameter offset
445 )
446 );
447 }
448 }
449
450 // Old message 'contribsub' had one parameter, but that doesn't work for
451 // languages that want to put the "for" bit right after $user but before
452 // $links. If 'contribsub' is around, use it for reverse compatibility,
453 // otherwise use 'contribsub2'.
454 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
455 return wfMsgHtml( 'contribsub2', $user, $links );
456 } else {
457 return wfMsgHtml( 'contribsub', "$user ($links)" );
458 }
459 }
460
461 /**
462 * Generates the namespace selector form with hidden attributes.
463 * @param $options Array: the options to be included.
464 */
465 function getForm( $options ) {
466 global $wgScript, $wgRequest;
467
468 $options['title'] = SpecialPage::getTitleFor( 'DeletedContributions' )->getPrefixedText();
469 if ( !isset( $options['target'] ) ) {
470 $options['target'] = '';
471 } else {
472 $options['target'] = str_replace( '_' , ' ' , $options['target'] );
473 }
474
475 if ( !isset( $options['namespace'] ) ) {
476 $options['namespace'] = '';
477 }
478
479 if ( !isset( $options['contribs'] ) ) {
480 $options['contribs'] = 'user';
481 }
482
483 if ( $options['contribs'] == 'newbie' ) {
484 $options['target'] = '';
485 }
486
487 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
488
489 foreach ( $options as $name => $value ) {
490 if ( in_array( $name, array( 'namespace', 'target', 'contribs' ) ) ) {
491 continue;
492 }
493 $f .= "\t" . Xml::hidden( $name, $value ) . "\n";
494 }
495
496 $f .= Xml::openElement( 'fieldset' ) .
497 Xml::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
498 Xml::tags( 'label', array( 'for' => 'target' ), wfMsgExt( 'sp-contributions-username', 'parseinline' ) ) . ' ' .
499 Html::input( 'target', $options['target'], 'text', array(
500 'size' => '20',
501 'required' => ''
502 ) + ( $options['target'] ? array() : array( 'autofocus' ) ) ) . ' '.
503 Xml::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
504 Xml::namespaceSelector( $options['namespace'], '' ) . ' ' .
505 Xml::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
506 Xml::closeElement( 'fieldset' ) .
507 Xml::closeElement( 'form' );
508 return $f;
509 }
510 }