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