(bug 15498) Provide links to revisions listed at the Special:Contributions page
[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', 50 );
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', wfMsg( 'contributions-title', $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 // Allows reverts to have the bot flag in recent changes. It is just here to
68 // be passed in the form at the top of the page
69 if( $wgUser->isAllowed( 'markbotedits' ) && $wgRequest->getBool( 'bot' ) ) {
70 $this->opts['bot'] = '1';
71 }
72
73 $skip = $wgRequest->getText( 'offset' ) || $wgRequest->getText( 'dir' ) == 'prev';
74 # Offset overrides year/month selection
75 if( ( $month = $wgRequest->getIntOrNull( 'month' ) ) !== null && $month !== -1 ) {
76 $this->opts['month'] = intval( $month );
77 } else {
78 $this->opts['month'] = '';
79 }
80 if( ( $year = $wgRequest->getIntOrNull( 'year' ) ) !== null ) {
81 $this->opts['year'] = intval( $year );
82 } else if( $this->opts['month'] ) {
83 $thisMonth = intval( gmdate( 'n' ) );
84 $thisYear = intval( gmdate( 'Y' ) );
85 if( intval( $this->opts['month'] ) > $thisMonth ) {
86 $thisYear--;
87 }
88 $this->opts['year'] = $thisYear;
89 } else {
90 $this->opts['year'] = '';
91 }
92
93 if( $skip ) {
94 $this->opts['year'] = '';
95 $this->opts['month'] = '';
96 }
97
98 // Add RSS/atom links
99 $this->setSyndicated();
100 $feedType = $wgRequest->getVal( 'feed' );
101 if( $feedType ) {
102 return $this->feed( $feedType );
103 }
104
105 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
106
107 $wgOut->addHTML( $this->getForm( $this->opts ) );
108
109 $pager = new ContribsPager( $target, $this->opts['namespace'], $this->opts['year'], $this->opts['month'] );
110 if( !$pager->getNumRows() ) {
111 $wgOut->addWikiMsg( 'nocontribs' );
112 return;
113 }
114
115 # Show a message about slave lag, if applicable
116 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
117 $wgOut->showLagWarning( $lag );
118
119 $wgOut->addHTML(
120 '<p>' . $pager->getNavigationBar() . '</p>' .
121 $pager->getBody() .
122 '<p>' . $pager->getNavigationBar() . '</p>'
123 );
124
125 # If there were contributions, and it was a valid user or IP, show
126 # the appropriate "footer" message - WHOIS tools, etc.
127 if( $target != 'newbies' ) {
128 $message = IP::isIPAddress( $target ) ?
129 'sp-contributions-footer-anon' : 'sp-contributions-footer';
130
131 $text = wfMsgNoTrans( $message, $target );
132 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
133 $wgOut->addHTML( '<div class="mw-contributions-footer">' );
134 $wgOut->addWikiText( $text );
135 $wgOut->addHTML( '</div>' );
136 }
137 }
138 }
139
140 protected function setSyndicated() {
141 global $wgOut;
142 $queryParams = array(
143 'namespace' => $this->opts['namespace'],
144 'target' => $this->opts['target']
145 );
146 $wgOut->setSyndicated( true );
147 $wgOut->setFeedAppendQuery( wfArrayToCGI( $queryParams ) );
148 }
149
150 /**
151 * Generates the subheading with links
152 * @param Title $nt Title object for the target
153 * @param integer $id User ID for the target
154 * @return String: appropriately-escaped HTML to be output literally
155 */
156 protected function contributionsSub( $nt, $id ) {
157 global $wgSysopUserBans, $wgLang, $wgUser;
158
159 $sk = $wgUser->getSkin();
160
161 if( 0 == $id ) {
162 $user = $nt->getText();
163 } else {
164 $user = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
165 }
166 $talk = $nt->getTalkPage();
167 if( $talk ) {
168 # Talk page link
169 $tools[] = $sk->makeLinkObj( $talk, wfMsgHtml( 'talkpagelinktext' ) );
170 if( ( $id != 0 && $wgSysopUserBans ) || ( $id == 0 && IP::isIPAddress( $nt->getText() ) ) ) {
171 # Block link
172 if( $wgUser->isAllowed( 'block' ) )
173 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Blockip',
174 $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
175 # Block log link
176 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ),
177 wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
178 }
179 # Other logs link
180 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ), wfMsgHtml( 'log' ),
181 'user=' . $nt->getPartialUrl() );
182
183 # Add link to deleted user contributions for priviledged users
184 if( $wgUser->isAllowed( 'deletedhistory' ) ) {
185 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'DeletedContributions',
186 $nt->getDBkey() ), wfMsgHtml( 'deletedcontributions' ) );
187 }
188
189 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
190
191 $links = implode( ' | ', $tools );
192 }
193
194 // Old message 'contribsub' had one parameter, but that doesn't work for
195 // languages that want to put the "for" bit right after $user but before
196 // $links. If 'contribsub' is around, use it for reverse compatibility,
197 // otherwise use 'contribsub2'.
198 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
199 return wfMsgHtml( 'contribsub2', $user, $links );
200 } else {
201 return wfMsgHtml( 'contribsub', "$user ($links)" );
202 }
203 }
204
205 /**
206 * Generates the namespace selector form with hidden attributes.
207 * @param $this->opts Array: the options to be included.
208 */
209 protected function getForm() {
210 global $wgScript, $wgTitle;
211
212 $this->opts['title'] = $wgTitle->getPrefixedText();
213 if( !isset( $this->opts['target'] ) ) {
214 $this->opts['target'] = '';
215 } else {
216 $this->opts['target'] = str_replace( '_' , ' ' , $this->opts['target'] );
217 }
218
219 if( !isset( $this->opts['namespace'] ) ) {
220 $this->opts['namespace'] = '';
221 }
222
223 if( !isset( $this->opts['contribs'] ) ) {
224 $this->opts['contribs'] = 'user';
225 }
226
227 if( !isset( $this->opts['year'] ) ) {
228 $this->opts['year'] = '';
229 }
230
231 if( !isset( $this->opts['month'] ) ) {
232 $this->opts['month'] = '';
233 }
234
235 if( $this->opts['contribs'] == 'newbie' ) {
236 $this->opts['target'] = '';
237 }
238
239 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
240
241 foreach ( $this->opts as $name => $value ) {
242 if( in_array( $name, array( 'namespace', 'target', 'contribs', 'year', 'month' ) ) ) {
243 continue;
244 }
245 $f .= "\t" . Xml::hidden( $name, $value ) . "\n";
246 }
247
248 $f .= '<fieldset>' .
249 Xml::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
250 Xml::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ),
251 'contribs', 'newbie' , 'newbie', $this->opts['contribs'] == 'newbie' ? true : false ) . '<br />' .
252 Xml::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parseinline' ) ),
253 'contribs' , 'user', 'user', $this->opts['contribs'] == 'user' ? true : false ) . ' ' .
254 Xml::input( 'target', 20, $this->opts['target']) . ' '.
255 '<span style="white-space: nowrap">' .
256 Xml::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
257 Xml::namespaceSelector( $this->opts['namespace'], '' ) .
258 '</span>' .
259 Xml::openElement( 'p' ) .
260 '<span style="white-space: nowrap">' .
261 Xml::label( wfMsg( 'year' ), 'year' ) . ' '.
262 Xml::input( 'year', 4, $this->opts['year'], array('id' => 'year', 'maxlength' => 4) ) .
263 '</span>' .
264 ' '.
265 '<span style="white-space: nowrap">' .
266 Xml::label( wfMsg( 'month' ), 'month' ) . ' '.
267 Xml::monthSelector( $this->opts['month'], -1 ) . ' '.
268 '</span>' .
269 Xml::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
270 Xml::closeElement( 'p' );
271
272 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
273 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
274 $f .= "<p>{$explain}</p>";
275
276 $f .= '</fieldset>' .
277 Xml::closeElement( 'form' );
278 return $f;
279 }
280
281 /**
282 * Output a subscription feed listing recent edits to this page.
283 * @param string $type
284 */
285 protected function feed( $type ) {
286 global $wgRequest, $wgFeed, $wgFeedClasses, $wgFeedLimit;
287
288 if( !$wgFeed ) {
289 global $wgOut;
290 $wgOut->addWikiMsg( 'feed-unavailable' );
291 return;
292 }
293
294 if( !isset( $wgFeedClasses[$type] ) ) {
295 global $wgOut;
296 $wgOut->addWikiMsg( 'feed-invalid' );
297 return;
298 }
299
300 $feed = new $wgFeedClasses[$type](
301 $this->feedTitle(),
302 wfMsgExt( 'tagline', 'parsemag' ),
303 $this->getTitle()->getFullUrl() );
304
305 // Already valid title
306 $nt = Title::makeTitleSafe( NS_USER, $this->opts['target'] );
307 $target = $this->opts['target'] == 'newbies' ? 'newbies' : $nt->getText();
308
309 $pager = new ContribsPager( $target, $this->opts['namespace'],
310 $this->opts['year'], $this->opts['month'] );
311
312 $pager->mLimit = min( $this->opts['limit'], $wgFeedLimit );
313
314 $feed->outHeader();
315 if( $pager->getNumRows() > 0 ) {
316 while( $row = $pager->mResult->fetchObject() ) {
317 $feed->outItem( $this->feedItem( $row ) );
318 }
319 }
320 $feed->outFooter();
321 }
322
323 protected function feedTitle() {
324 global $wgContLanguageCode, $wgSitename;
325 $page = SpecialPage::getPage( 'Contributions' );
326 $desc = $page->getDescription();
327 return "$wgSitename - $desc [$wgContLanguageCode]";
328 }
329
330 protected function feedItem( $row ) {
331 $title = Title::MakeTitle( intval( $row->page_namespace ), $row->page_title );
332 if( $title ) {
333 $date = $row->rev_timestamp;
334 $comments = $title->getTalkPage()->getFullURL();
335 $revision = Revision::newFromTitle( $title, $row->rev_id );
336
337 return new FeedItem(
338 $title->getPrefixedText(),
339 $this->feedItemDesc( $revision ),
340 $title->getFullURL(),
341 $date,
342 $this->feedItemAuthor( $revision ),
343 $comments
344 );
345 } else {
346 return NULL;
347 }
348 }
349
350 protected function feedItemAuthor( $revision ) {
351 return $revision->getUserText();
352 }
353
354 protected function feedItemDesc( $revision ) {
355 if( $revision ) {
356 return '<p>' . htmlspecialchars( $revision->getUserText() ) . ': ' .
357 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
358 "</p>\n<hr />\n<div>" .
359 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
360 }
361 return '';
362 }
363 }
364
365 /**
366 * Pager for Special:Contributions
367 * @ingroup SpecialPage Pager
368 */
369 class ContribsPager extends ReverseChronologicalPager {
370 public $mDefaultDirection = true;
371 var $messages, $target;
372 var $namespace = '', $mDb;
373
374 function __construct( $target, $namespace = false, $year = false, $month = false ) {
375 parent::__construct();
376 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist newpageletter minoreditletter' ) as $msg ) {
377 $this->messages[$msg] = wfMsgExt( $msg, array( 'escape') );
378 }
379 $this->target = $target;
380 $this->namespace = $namespace;
381
382 $this->getDateCond( $year, $month );
383
384 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
385 }
386
387 function getDefaultQuery() {
388 $query = parent::getDefaultQuery();
389 $query['target'] = $this->target;
390 return $query;
391 }
392
393 function getQueryInfo() {
394 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
395 $conds = array_merge( array('page_id=rev_page'), $userCond, $this->getNamespaceCond() );
396 $queryInfo = array(
397 'tables' => $tables,
398 'fields' => array(
399 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'rev_id', 'rev_page',
400 'rev_text_id', 'rev_timestamp', 'rev_comment', 'rev_minor_edit', 'rev_user',
401 'rev_user_text', 'rev_parent_id', 'rev_deleted'
402 ),
403 'conds' => $conds,
404 'options' => array( 'USE INDEX' => array('revision' => $index) ),
405 'join_conds' => $join_cond
406 );
407 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
408 return $queryInfo;
409 }
410
411 function getUserCond() {
412 $condition = array();
413 $join_conds = array();
414 if( $this->target == 'newbies' ) {
415 $tables = array( 'user_groups', 'page', 'revision' );
416 $max = $this->mDb->selectField( 'user', 'max(user_id)', false, __METHOD__ );
417 $condition[] = 'rev_user >' . (int)($max - $max / 100);
418 $condition[] = 'ug_group IS NULL';
419 $index = 'user_timestamp';
420 # FIXME: other groups may have 'bot' rights
421 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
422 } else {
423 $tables = array( 'page', 'revision' );
424 $condition['rev_user_text'] = $this->target;
425 $index = 'usertext_timestamp';
426 }
427 return array( $tables, $index, $condition, $join_conds );
428 }
429
430 function getNamespaceCond() {
431 if( $this->namespace !== '' ) {
432 return array( 'page_namespace' => (int)$this->namespace );
433 } else {
434 return array();
435 }
436 }
437
438 function getIndexField() {
439 return 'rev_timestamp';
440 }
441
442 function getStartBody() {
443 return "<ul>\n";
444 }
445
446 function getEndBody() {
447 return "</ul>\n";
448 }
449
450 /**
451 * Generates each row in the contributions list.
452 *
453 * Contributions which are marked "top" are currently on top of the history.
454 * For these contributions, a [rollback] link is shown for users with roll-
455 * back privileges. The rollback link restores the most recent version that
456 * was not written by the target user.
457 *
458 * @todo This would probably look a lot nicer in a table.
459 */
460 function formatRow( $row ) {
461 wfProfileIn( __METHOD__ );
462
463 global $wgLang, $wgUser, $wgContLang;
464
465 $sk = $this->getSkin();
466 $rev = new Revision( $row );
467
468 $page = Title::makeTitle( $row->page_namespace, $row->page_title );
469 $link = $sk->makeKnownLinkObj( $page );
470 $difftext = $topmarktext = '';
471 if( $row->rev_id == $row->page_latest ) {
472 $topmarktext .= '<strong>' . $this->messages['uctop'] . '</strong>';
473 if( !$row->page_is_new ) {
474 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $this->messages['diff'], 'diff=0' ) . ')';
475 } else {
476 $difftext .= $this->messages['newarticle'];
477 }
478
479 if( $page->userCan( 'rollback') && $page->userCan( 'edit' ) ) {
480 $topmarktext .= ' '.$sk->generateRollback( $rev );
481 }
482
483 }
484 # Is there a visible previous revision?
485 if( $rev->userCan(Revision::DELETED_TEXT) ) {
486 $difftext = '(' . $sk->makeKnownLinkObj( $page, $this->messages['diff'], 'diff=prev&oldid='.$row->rev_id ) . ')';
487 } else {
488 $difftext = '(' . $this->messages['diff'] . ')';
489 }
490 $histlink='('.$sk->makeKnownLinkObj( $page, $this->messages['hist'], 'action=history' ) . ')';
491
492 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
493 $date = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->rev_timestamp ), true );
494 $d = $sk->makeKnownLinkObj( $page, $date, 'oldid='.intval($row->rev_id) );
495
496 if( $this->target == 'newbies' ) {
497 $userlink = ' . . ' . $sk->userLink( $row->rev_user, $row->rev_user_text );
498 $userlink .= ' (' . $sk->userTalkLink( $row->rev_user, $row->rev_user_text ) . ') ';
499 } else {
500 $userlink = '';
501 }
502
503 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
504 $d = '<span class="history-deleted">' . $d . '</span>';
505 }
506
507 if( $rev->getParentId() === 0 ) {
508 $nflag = '<span class="newpage">' . $this->messages['newpageletter'] . '</span>';
509 } else {
510 $nflag = '';
511 }
512
513 if( $row->rev_minor_edit ) {
514 $mflag = '<span class="minor">' . $this->messages['minoreditletter'] . '</span> ';
515 } else {
516 $mflag = '';
517 }
518
519 $ret = "{$d} {$histlink} {$difftext} {$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
520 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
521 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
522 }
523 // Let extensions add data
524 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
525
526 $ret = "<li>$ret</li>\n";
527 wfProfileOut( __METHOD__ );
528 return $ret;
529 }
530
531 /**
532 * Get the Database object in use
533 *
534 * @return Database
535 */
536 public function getDatabase() {
537 return $this->mDb;
538 }
539
540 }