Show change block / unblock link on Special:Contributions if user is blocked
[lhc/web/wiklou.git] / includes / specials / SpecialContributions.php
1 <?php
2 /**
3 * Special:Contributions, show user contributions in a paged list
4 * @file
5 * @ingroup SpecialPage
6 */
7
8 class SpecialContributions extends SpecialPage {
9
10 public function __construct() {
11 parent::__construct( 'Contributions' );
12 }
13
14 public function execute( $par ) {
15 global $wgUser, $wgOut, $wgLang, $wgRequest;
16
17 $this->setHeaders();
18 $this->outputHeader();
19
20 $this->opts = array();
21
22 if( $par == 'newbies' ) {
23 $target = 'newbies';
24 $this->opts['contribs'] = 'newbie';
25 } elseif( isset( $par ) ) {
26 $target = $par;
27 } else {
28 $target = $wgRequest->getVal( 'target' );
29 }
30
31 // check for radiobox
32 if( $wgRequest->getVal( 'contribs' ) == 'newbie' ) {
33 $target = 'newbies';
34 $this->opts['contribs'] = 'newbie';
35 }
36
37 if( !strlen( $target ) ) {
38 $wgOut->addHTML( $this->getForm() );
39 return;
40 }
41
42 $this->opts['limit'] = $wgRequest->getInt( 'limit', $wgUser->getOption('rclimit') );
43 $this->opts['target'] = $target;
44
45 $nt = Title::makeTitleSafe( NS_USER, $target );
46 if( !$nt ) {
47 $wgOut->addHTML( $this->getForm() );
48 return;
49 }
50 $id = User::idFromName( $nt->getText() );
51
52 if( $target != 'newbies' ) {
53 $target = $nt->getText();
54 $wgOut->setSubtitle( $this->contributionsSub( $nt, $id ) );
55 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsgExt( 'contributions-title', array( 'parsemag' ),$target ) ) );
56 } else {
57 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
58 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'sp-contributions-newbies-title' ) ) );
59 }
60
61 if( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
62 $this->opts['namespace'] = intval( $ns );
63 } else {
64 $this->opts['namespace'] = '';
65 }
66
67 $this->opts['tagfilter'] = (string) $wgRequest->getVal( 'tagfilter' );
68
69 // Allows reverts to have the bot flag in recent changes. It is just here to
70 // be passed in the form at the top of the page
71 if( $wgUser->isAllowed( 'markbotedits' ) && $wgRequest->getBool( 'bot' ) ) {
72 $this->opts['bot'] = '1';
73 }
74
75 $skip = $wgRequest->getText( 'offset' ) || $wgRequest->getText( 'dir' ) == 'prev';
76 # Offset overrides year/month selection
77 if( $skip ) {
78 $this->opts['year'] = '';
79 $this->opts['month'] = '';
80 } else {
81 $this->opts['year'] = $wgRequest->getIntOrNull( 'year' );
82 $this->opts['month'] = $wgRequest->getIntOrNull( 'month' );
83 }
84
85 // Add RSS/atom links
86 $this->setSyndicated();
87 $feedType = $wgRequest->getVal( 'feed' );
88 if( $feedType ) {
89 return $this->feed( $feedType );
90 }
91
92 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
93
94 $wgOut->addHTML( $this->getForm() );
95
96 $pager = new ContribsPager( $target, $this->opts['namespace'], $this->opts['year'], $this->opts['month'] );
97 if( !$pager->getNumRows() ) {
98 $wgOut->addWikiMsg( 'nocontribs', $target );
99 } else {
100 # Show a message about slave lag, if applicable
101 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
102 $wgOut->showLagWarning( $lag );
103
104 $wgOut->addHTML(
105 '<p>' . $pager->getNavigationBar() . '</p>' .
106 $pager->getBody() .
107 '<p>' . $pager->getNavigationBar() . '</p>'
108 );
109 }
110
111
112 # Show the appropriate "footer" message - WHOIS tools, etc.
113 if( $target != 'newbies' ) {
114 $message = 'sp-contributions-footer';
115 if ( IP::isIPAddress( $target ) ) {
116 $message = 'sp-contributions-footer-anon';
117 } else {
118 $user = User::newFromName( $target );
119 if ( !$user || $user->isAnon() ) {
120 // No message for non-existing users
121 return;
122 }
123 }
124
125 $text = wfMsgNoTrans( $message, $target );
126 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
127 $wgOut->wrapWikiMsg(
128 "<div class='mw-contributions-footer'>\n$1\n</div>", $message );
129 }
130 }
131 }
132
133 protected function setSyndicated() {
134 global $wgOut;
135 $queryParams = array(
136 'namespace' => $this->opts['namespace'],
137 'target' => $this->opts['target']
138 );
139 $wgOut->setSyndicated( true );
140 $wgOut->setFeedAppendQuery( wfArrayToCGI( $queryParams ) );
141 }
142
143 /**
144 * Generates the subheading with links
145 * @param Title $nt Title object for the target
146 * @param integer $id User ID for the target
147 * @return String: appropriately-escaped HTML to be output literally
148 */
149 protected function contributionsSub( $nt, $id ) {
150 global $wgSysopUserBans, $wgLang, $wgUser;
151
152 $sk = $wgUser->getSkin();
153
154 if( 0 == $id ) {
155 $user = htmlspecialchars( $nt->getText() );
156 } else {
157 $user = $sk->link( $nt, htmlspecialchars( $nt->getText() ) );
158 }
159 $talk = $nt->getTalkPage();
160 if( $talk ) {
161 # Talk page link
162 $tools[] = $sk->link( $talk, wfMsgHtml( 'sp-contributions-talk' ) );
163 if( ( $id != 0 && $wgSysopUserBans ) || ( $id == 0 && IP::isIPAddress( $nt->getText() ) ) ) {
164 if( $wgUser->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
165 if ( $wgUser->isBlocked() ) {
166 $tools[] = $sk->linkKnown( # Change block link
167 SpecialPage::getTitleFor( 'Blockip', $nt->getDBkey() ),
168 wfMsgHtml( 'change-blocklink' )
169 );
170 $tools[] = $sk->linkKnown( # Unblock link
171 SpecialPage::getTitleFor( 'BlockList' ),
172 wfMsgHtml( 'unblocklink' ),
173 array(),
174 array(
175 'action' => 'unblock',
176 'ip' => $nt->getDBkey()
177 ),
178 array( 'known' )
179 );
180 }
181 else { # User is not blocked
182 $tools[] = $sk->linkKnown( # Block link
183 SpecialPage::getTitleFor( 'Blockip', $nt->getDBkey() ),
184 wfMsgHtml( 'blocklink' )
185 );
186 }
187 }
188 # Block log link
189 $tools[] = $sk->linkKnown(
190 SpecialPage::getTitleFor( 'Log' ),
191 wfMsgHtml( 'sp-contributions-blocklog' ),
192 array(),
193 array(
194 'type' => 'block',
195 'page' => $nt->getPrefixedText()
196 )
197 );
198 }
199 # Other logs link
200 $tools[] = $sk->linkKnown(
201 SpecialPage::getTitleFor( 'Log' ),
202 wfMsgHtml( 'sp-contributions-logs' ),
203 array(),
204 array( 'user' => $nt->getText() )
205 );
206
207 # Add link to deleted user contributions for priviledged users
208 if( $wgUser->isAllowed( 'deletedhistory' ) ) {
209 $tools[] = $sk->linkKnown(
210 SpecialPage::getTitleFor( 'DeletedContributions', $nt->getDBkey() ),
211 wfMsgHtml( 'sp-contributions-deleted' )
212 );
213 }
214
215 # Add a link to change user rights for privileged users
216 $userrightsPage = new UserrightsPage();
217 if( 0 !== $id && $userrightsPage->userCanChangeRights( User::newFromId( $id ) ) ) {
218 $tools[] = $sk->linkKnown(
219 SpecialPage::getTitleFor( 'Userrights', $nt->getDBkey() ),
220 wfMsgHtml( 'sp-contributions-userrights' )
221 );
222 }
223
224 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
225
226 $links = $wgLang->pipeList( $tools );
227 }
228
229 // Old message 'contribsub' had one parameter, but that doesn't work for
230 // languages that want to put the "for" bit right after $user but before
231 // $links. If 'contribsub' is around, use it for reverse compatibility,
232 // otherwise use 'contribsub2'.
233 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
234 return wfMsgHtml( 'contribsub2', $user, $links );
235 } else {
236 return wfMsgHtml( 'contribsub', "$user ($links)" );
237 }
238 }
239
240 /**
241 * Generates the namespace selector form with hidden attributes.
242 * @param $this->opts Array: the options to be included.
243 */
244 protected function getForm() {
245 global $wgScript;
246
247 $this->opts['title'] = $this->getTitle()->getPrefixedText();
248 if( !isset( $this->opts['target'] ) ) {
249 $this->opts['target'] = '';
250 } else {
251 $this->opts['target'] = str_replace( '_' , ' ' , $this->opts['target'] );
252 }
253
254 if( !isset( $this->opts['namespace'] ) ) {
255 $this->opts['namespace'] = '';
256 }
257
258 if( !isset( $this->opts['contribs'] ) ) {
259 $this->opts['contribs'] = 'user';
260 }
261
262 if( !isset( $this->opts['year'] ) ) {
263 $this->opts['year'] = '';
264 }
265
266 if( !isset( $this->opts['month'] ) ) {
267 $this->opts['month'] = '';
268 }
269
270 if( $this->opts['contribs'] == 'newbie' ) {
271 $this->opts['target'] = '';
272 }
273
274 if( !isset( $this->opts['tagfilter'] ) ) {
275 $this->opts['tagfilter'] = '';
276 }
277
278 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
279 # Add hidden params for tracking
280 foreach ( $this->opts as $name => $value ) {
281 if( in_array( $name, array( 'namespace', 'target', 'contribs', 'year', 'month' ) ) ) {
282 continue;
283 }
284 $f .= "\t" . Xml::hidden( $name, $value ) . "\n";
285 }
286
287 $tagFilter = ChangeTags::buildTagFilterSelector( $this->opts['tagfilter'] );
288
289 $f .= '<fieldset>' .
290 Xml::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
291 Xml::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parsemag' ) ),
292 'contribs', 'newbie' , 'newbie', $this->opts['contribs'] == 'newbie' ? true : false ) . '<br />' .
293 Xml::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parsemag' ) ),
294 'contribs' , 'user', 'user', $this->opts['contribs'] == 'user' ? true : false ) . ' ' .
295 Html::input( 'target', $this->opts['target'], 'text', array(
296 'size' => '20',
297 'required' => ''
298 ) + ( $this->opts['target'] ? array() : array( 'autofocus' ) ) ) . ' '.
299 '<span style="white-space: nowrap">' .
300 Xml::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
301 Xml::namespaceSelector( $this->opts['namespace'], '' ) .
302 '</span>' .
303 ( $tagFilter ? Xml::tags( 'p', null, implode( '&nbsp;', $tagFilter ) ) : '' ) .
304 Xml::openElement( 'p' ) .
305 '<span style="white-space: nowrap">' .
306 Xml::dateMenu( $this->opts['year'], $this->opts['month'] ) .
307 '</span>' . ' ' .
308 Xml::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
309 Xml::closeElement( 'p' );
310
311 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
312 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
313 $f .= "<p>{$explain}</p>";
314
315 $f .= '</fieldset>' .
316 Xml::closeElement( 'form' );
317 return $f;
318 }
319
320 /**
321 * Output a subscription feed listing recent edits to this page.
322 * @param string $type
323 */
324 protected function feed( $type ) {
325 global $wgRequest, $wgFeed, $wgFeedClasses, $wgFeedLimit;
326
327 if( !$wgFeed ) {
328 global $wgOut;
329 $wgOut->addWikiMsg( 'feed-unavailable' );
330 return;
331 }
332
333 if( !isset( $wgFeedClasses[$type] ) ) {
334 global $wgOut;
335 $wgOut->addWikiMsg( 'feed-invalid' );
336 return;
337 }
338
339 $feed = new $wgFeedClasses[$type](
340 $this->feedTitle(),
341 wfMsgExt( 'tagline', 'parsemag' ),
342 $this->getTitle()->getFullUrl() . "/" . urlencode($this->opts['target'])
343 );
344
345 // Already valid title
346 $nt = Title::makeTitleSafe( NS_USER, $this->opts['target'] );
347 $target = $this->opts['target'] == 'newbies' ? 'newbies' : $nt->getText();
348
349 $pager = new ContribsPager( $target, $this->opts['namespace'],
350 $this->opts['year'], $this->opts['month'], $this->opts['tagfilter'] );
351
352 $pager->mLimit = min( $this->opts['limit'], $wgFeedLimit );
353
354 $feed->outHeader();
355 if( $pager->getNumRows() > 0 ) {
356 while( $row = $pager->mResult->fetchObject() ) {
357 $feed->outItem( $this->feedItem( $row ) );
358 }
359 }
360 $feed->outFooter();
361 }
362
363 protected function feedTitle() {
364 global $wgContLanguageCode, $wgSitename;
365 $page = SpecialPage::getPage( 'Contributions' );
366 $desc = $page->getDescription();
367 return "$wgSitename - $desc [$wgContLanguageCode]";
368 }
369
370 protected function feedItem( $row ) {
371 $title = Title::MakeTitle( intval( $row->page_namespace ), $row->page_title );
372 if( $title ) {
373 $date = $row->rev_timestamp;
374 $comments = $title->getTalkPage()->getFullURL();
375 $revision = Revision::newFromTitle( $title, $row->rev_id );
376
377 return new FeedItem(
378 $title->getPrefixedText(),
379 $this->feedItemDesc( $revision ),
380 $title->getFullURL(),
381 $date,
382 $this->feedItemAuthor( $revision ),
383 $comments
384 );
385 } else {
386 return NULL;
387 }
388 }
389
390 protected function feedItemAuthor( $revision ) {
391 return $revision->getUserText();
392 }
393
394 protected function feedItemDesc( $revision ) {
395 if( $revision ) {
396 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
397 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
398 "</p>\n<hr />\n<div>" .
399 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
400 }
401 return '';
402 }
403 }
404
405 /**
406 * Pager for Special:Contributions
407 * @ingroup SpecialPage Pager
408 */
409 class ContribsPager extends ReverseChronologicalPager {
410 public $mDefaultDirection = true;
411 var $messages, $target;
412 var $namespace = '', $mDb;
413
414 function __construct( $target, $namespace = false, $year = false, $month = false, $tagFilter = false ) {
415 parent::__construct();
416 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist' ) as $msg ) {
417 $this->messages[$msg] = wfMsgExt( $msg, array( 'escape') );
418 }
419 $this->target = $target;
420 $this->namespace = $namespace;
421 $this->tagFilter = $tagFilter;
422
423 $this->getDateCond( $year, $month );
424
425 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
426 }
427
428 function getDefaultQuery() {
429 $query = parent::getDefaultQuery();
430 $query['target'] = $this->target;
431 return $query;
432 }
433
434 function getQueryInfo() {
435 global $wgUser;
436 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
437
438 $conds = array_merge( $userCond, $this->getNamespaceCond() );
439 // Paranoia: avoid brute force searches (bug 17342)
440 if( !$wgUser->isAllowed( 'deleterevision' ) ) {
441 $conds[] = $this->mDb->bitAnd('rev_deleted',Revision::DELETED_USER) . ' = 0';
442 } else if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
443 $conds[] = $this->mDb->bitAnd('rev_deleted',Revision::SUPPRESSED_USER) .
444 ' != ' . Revision::SUPPRESSED_USER;
445 }
446 $join_cond['page'] = array( 'INNER JOIN', 'page_id=rev_page' );
447
448 $queryInfo = array(
449 'tables' => $tables,
450 'fields' => array(
451 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'page_is_redirect',
452 'page_len','rev_id', 'rev_page', 'rev_text_id', 'rev_timestamp', 'rev_comment',
453 'rev_minor_edit', 'rev_user', 'rev_user_text', 'rev_parent_id', 'rev_deleted'
454 ),
455 'conds' => $conds,
456 'options' => array( 'USE INDEX' => array('revision' => $index) ),
457 'join_conds' => $join_cond
458 );
459
460 ChangeTags::modifyDisplayQuery( $queryInfo['tables'],
461 $queryInfo['fields'],
462 $queryInfo['conds'],
463 $queryInfo['join_conds'],
464 $queryInfo['options'],
465 $this->tagFilter );
466
467 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
468 return $queryInfo;
469 }
470
471 function getUserCond() {
472 $condition = array();
473 $join_conds = array();
474 if( $this->target == 'newbies' ) {
475 $tables = array( 'user_groups', 'page', 'revision' );
476 $max = $this->mDb->selectField( 'user', 'max(user_id)', false, __METHOD__ );
477 $condition[] = 'rev_user >' . (int)($max - $max / 100);
478 $condition[] = 'ug_group IS NULL';
479 $index = 'user_timestamp';
480 # FIXME: other groups may have 'bot' rights
481 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
482 } else {
483 $tables = array( 'page', 'revision' );
484 $condition['rev_user_text'] = $this->target;
485 $index = 'usertext_timestamp';
486 }
487 return array( $tables, $index, $condition, $join_conds );
488 }
489
490 function getNamespaceCond() {
491 if( $this->namespace !== '' ) {
492 return array( 'page_namespace' => (int)$this->namespace );
493 } else {
494 return array();
495 }
496 }
497
498 function getIndexField() {
499 return 'rev_timestamp';
500 }
501
502 function getStartBody() {
503 return "<ul>\n";
504 }
505
506 function getEndBody() {
507 return "</ul>\n";
508 }
509
510 /**
511 * Generates each row in the contributions list.
512 *
513 * Contributions which are marked "top" are currently on top of the history.
514 * For these contributions, a [rollback] link is shown for users with roll-
515 * back privileges. The rollback link restores the most recent version that
516 * was not written by the target user.
517 *
518 * @todo This would probably look a lot nicer in a table.
519 */
520 function formatRow( $row ) {
521 global $wgUser, $wgLang, $wgContLang;
522 wfProfileIn( __METHOD__ );
523
524 $sk = $this->getSkin();
525 $rev = new Revision( $row );
526 $classes = array();
527
528 $page = Title::newFromRow( $row );
529 $page->resetArticleId( $row->rev_page ); // use process cache
530 $link = $sk->link(
531 $page,
532 htmlspecialchars( $page->getPrefixedText() ),
533 array(),
534 $page->isRedirect() ? array( 'redirect' => 'no' ) : array()
535 );
536 # Mark current revisions
537 $difftext = $topmarktext = '';
538 if( $row->rev_id == $row->page_latest ) {
539 $topmarktext .= '<span class="mw-uctop">' . $this->messages['uctop'] . '</span>';
540 if( !$row->page_is_new ) {
541 $difftext .= '(' . $sk->linkKnown(
542 $page,
543 $this->messages['diff'],
544 array(),
545 array( 'diff' => 0 )
546 ) . ')';
547 # Add rollback link
548 if( $page->quickUserCan( 'rollback') && $page->quickUserCan( 'edit' ) ) {
549 $topmarktext .= ' '.$sk->generateRollback( $rev );
550 }
551 } else {
552 $difftext .= $this->messages['newarticle'];
553 }
554 }
555 # Is there a visible previous revision?
556 if( !$rev->isDeleted(Revision::DELETED_TEXT) ) {
557 $difftext = '(' . $sk->linkKnown(
558 $page,
559 $this->messages['diff'],
560 array(),
561 array(
562 'diff' => 'prev',
563 'oldid' => $row->rev_id
564 )
565 ) . ')';
566 } else {
567 $difftext = '(' . $this->messages['diff'] . ')';
568 }
569 $histlink = '('.$sk->linkKnown(
570 $page,
571 $this->messages['hist'],
572 array(),
573 array( 'action' => 'history' )
574 ) . ')';
575
576 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
577 $date = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->rev_timestamp ), true );
578 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
579 $d = '<span class="history-deleted">' . $date . '</span>';
580 } else {
581 $d = $sk->linkKnown(
582 $page,
583 htmlspecialchars($date),
584 array(),
585 array( 'oldid' => intval( $row->rev_id ) )
586 );
587 }
588
589 if( $this->target == 'newbies' ) {
590 $userlink = ' . . ' . $sk->userLink( $row->rev_user, $row->rev_user_text );
591 $userlink .= ' (' . $sk->userTalkLink( $row->rev_user, $row->rev_user_text ) . ') ';
592 } else {
593 $userlink = '';
594 }
595
596 if( $rev->getParentId() === 0 ) {
597 $nflag = ChangesList::flag( 'newpage' );
598 } else {
599 $nflag = '';
600 }
601
602 if( $rev->isMinor() ) {
603 $mflag = ChangesList::flag( 'minor' );
604 } else {
605 $mflag = '';
606 }
607
608 if( $wgUser->isAllowed( 'deleterevision' ) ) {
609 // If revision was hidden from sysops
610 if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
611 $del = Xml::tags( 'span', array( 'class'=>'mw-revdelundel-link' ),
612 '(' . $this->message['rev-delundel'] . ')' ) . ' ';
613 // Otherwise, show the link...
614 } else {
615 $query = array(
616 'type' => 'revision',
617 'target' => $page->getPrefixedDbkey(),
618 'ids' => $rev->getId() );
619 $del = $this->mSkin->revDeleteLink( $query,
620 $rev->isDeleted( Revision::DELETED_RESTRICTED ) ) . ' ';
621 }
622 } else {
623 $del = '';
624 }
625
626 $ret = "{$del}{$d} {$histlink} {$difftext} {$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
627
628 # Tags, if any.
629 list($tagSummary, $newClasses) = ChangeTags::formatSummaryRow( $row->ts_tags, 'contributions' );
630 $classes = array_merge( $classes, $newClasses );
631 $ret .= " $tagSummary";
632
633 // Let extensions add data
634 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
635
636 $classes = implode( ' ', $classes );
637 $ret = "<li class=\"$classes\">$ret</li>\n";
638 wfProfileOut( __METHOD__ );
639 return $ret;
640 }
641
642 /**
643 * Get the Database object in use
644 *
645 * @return Database
646 */
647 public function getDatabase() {
648 return $this->mDb;
649 }
650
651 }