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