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