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