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