(bug 19864) Removed overzealous hiding of deleted (but not suppressed) rows from...
[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( 'deleterevision' ) ) {
412 $conds[] = $this->mDb->bitAnd('rev_deleted',Revision::DELETED_USER) . ' = 0';
413 } else if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
414 $conds[] = $this->mDb->bitAnd('rev_deleted',Revision::SUPPRESSED_USER) .
415 ' != ' . Revision::SUPPRESSED_USER;
416 }
417 $join_cond['page'] = array( 'INNER JOIN', 'page_id=rev_page' );
418
419 $queryInfo = array(
420 'tables' => $tables,
421 'fields' => array(
422 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'page_is_redirect',
423 'page_len','rev_id', 'rev_page', 'rev_text_id', 'rev_timestamp', 'rev_comment',
424 'rev_minor_edit', 'rev_user', 'rev_user_text', 'rev_parent_id', 'rev_deleted'
425 ),
426 'conds' => $conds,
427 'options' => array( 'USE INDEX' => array('revision' => $index) ),
428 'join_conds' => $join_cond
429 );
430
431 ChangeTags::modifyDisplayQuery( $queryInfo['tables'],
432 $queryInfo['fields'],
433 $queryInfo['conds'],
434 $queryInfo['join_conds'],
435 $queryInfo['options'],
436 $this->tagFilter );
437
438 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
439 return $queryInfo;
440 }
441
442 function getUserCond() {
443 $condition = array();
444 $join_conds = array();
445 if( $this->target == 'newbies' ) {
446 $tables = array( 'user_groups', 'page', 'revision' );
447 $max = $this->mDb->selectField( 'user', 'max(user_id)', false, __METHOD__ );
448 $condition[] = 'rev_user >' . (int)($max - $max / 100);
449 $condition[] = 'ug_group IS NULL';
450 $index = 'user_timestamp';
451 # FIXME: other groups may have 'bot' rights
452 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
453 } else {
454 $tables = array( 'page', 'revision' );
455 $condition['rev_user_text'] = $this->target;
456 $index = 'usertext_timestamp';
457 }
458 return array( $tables, $index, $condition, $join_conds );
459 }
460
461 function getNamespaceCond() {
462 if( $this->namespace !== '' ) {
463 return array( 'page_namespace' => (int)$this->namespace );
464 } else {
465 return array();
466 }
467 }
468
469 function getIndexField() {
470 return 'rev_timestamp';
471 }
472
473 function getStartBody() {
474 return "<ul>\n";
475 }
476
477 function getEndBody() {
478 return "</ul>\n";
479 }
480
481 /**
482 * Generates each row in the contributions list.
483 *
484 * Contributions which are marked "top" are currently on top of the history.
485 * For these contributions, a [rollback] link is shown for users with roll-
486 * back privileges. The rollback link restores the most recent version that
487 * was not written by the target user.
488 *
489 * @todo This would probably look a lot nicer in a table.
490 */
491 function formatRow( $row ) {
492 global $wgUser, $wgLang, $wgContLang;
493 wfProfileIn( __METHOD__ );
494
495 $sk = $this->getSkin();
496 $rev = new Revision( $row );
497 $classes = array();
498
499 $page = Title::newFromRow( $row );
500 $page->resetArticleId( $row->rev_page ); // use process cache
501 $link = $sk->link(
502 $page,
503 htmlspecialchars( $page->getPrefixedText() ),
504 array(),
505 $page->isRedirect() ? array( 'redirect' => 'no' ) : array()
506 );
507 # Mark current revisions
508 $difftext = $topmarktext = '';
509 if( $row->rev_id == $row->page_latest ) {
510 $topmarktext .= '<span class="mw-uctop">' . $this->messages['uctop'] . '</span>';
511 if( !$row->page_is_new ) {
512 $difftext .= '(' . $sk->linkKnown(
513 $page,
514 $this->messages['diff'],
515 array(),
516 array( 'diff' => 0 )
517 ) . ')';
518 # Add rollback link
519 if( $page->quickUserCan( 'rollback') && $page->quickUserCan( 'edit' ) ) {
520 $topmarktext .= ' '.$sk->generateRollback( $rev );
521 }
522 } else {
523 $difftext .= $this->messages['newarticle'];
524 }
525 }
526 # Is there a visible previous revision?
527 if( !$rev->isDeleted(Revision::DELETED_TEXT) ) {
528 $difftext = '(' . $sk->linkKnown(
529 $page,
530 $this->messages['diff'],
531 array(),
532 array(
533 'diff' => 'prev',
534 'oldid' => $row->rev_id
535 )
536 ) . ')';
537 } else {
538 $difftext = '(' . $this->messages['diff'] . ')';
539 }
540 $histlink = '('.$sk->linkKnown(
541 $page,
542 $this->messages['hist'],
543 array(),
544 array( 'action' => 'history' )
545 ) . ')';
546
547 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
548 $date = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->rev_timestamp ), true );
549 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
550 $d = '<span class="history-deleted">' . $date . '</span>';
551 } else {
552 $d = $sk->linkKnown(
553 $page,
554 htmlspecialchars($date),
555 array(),
556 array( 'oldid' => intval( $row->rev_id ) )
557 );
558 }
559
560 if( $this->target == 'newbies' ) {
561 $userlink = ' . . ' . $sk->userLink( $row->rev_user, $row->rev_user_text );
562 $userlink .= ' (' . $sk->userTalkLink( $row->rev_user, $row->rev_user_text ) . ') ';
563 } else {
564 $userlink = '';
565 }
566
567 if( $rev->getParentId() === 0 ) {
568 $nflag = '<abbr class="newpage">' . $this->messages['newpageletter'] . '</abbr>';
569 } else {
570 $nflag = '';
571 }
572
573 if( $rev->isMinor() ) {
574 $mflag = '<abbr class="minor">' . $this->messages['minoreditletter'] . '</abbr> ';
575 } else {
576 $mflag = '';
577 }
578
579 if( $wgUser->isAllowed( 'deleterevision' ) ) {
580 // If revision was hidden from sysops
581 if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
582 $del = Xml::tags( 'span', array( 'class'=>'mw-revdelundel-link' ),
583 '(' . $this->message['rev-delundel'] . ')' ) . ' ';
584 // Otherwise, show the link...
585 } else {
586 $query = array(
587 'type' => 'revision',
588 'target' => $page->getPrefixedDbkey(),
589 'ids' => $rev->getId() );
590 $del = $this->mSkin->revDeleteLink( $query,
591 $rev->isDeleted( Revision::DELETED_RESTRICTED ) ) . ' ';
592 }
593 } else {
594 $del = '';
595 }
596
597 $ret = "{$del}{$d} {$histlink} {$difftext} {$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
598
599 # Tags, if any.
600 list($tagSummary, $newClasses) = ChangeTags::formatSummaryRow( $row->ts_tags, 'contributions' );
601 $classes = array_merge( $classes, $newClasses );
602 $ret .= " $tagSummary";
603
604 // Let extensions add data
605 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
606
607 $classes = implode( ' ', $classes );
608 $ret = "<li class=\"$classes\">$ret</li>\n";
609 wfProfileOut( __METHOD__ );
610 return $ret;
611 }
612
613 /**
614 * Get the Database object in use
615 *
616 * @return Database
617 */
618 public function getDatabase() {
619 return $this->mDb;
620 }
621
622 }