Revert previous commit, breaks cache.
[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 wfMsg( 'tagline' ),
303 $this->getTitle()->getFullUrl() );
304
305 $pager = new ContribsPager( $this->opts['target'], $this->opts['namespace'],
306 $this->opts['year'], $this->opts['month'] );
307
308 $pager->mLimit = min( $this->opts['limit'], $wgFeedLimit );
309
310 $feed->outHeader();
311 if( $pager->getNumRows() > 0 ) {
312 while( $row = $pager->mResult->fetchObject() ) {
313 $feed->outItem( $this->feedItem( $row ) );
314 }
315 }
316 $feed->outFooter();
317 }
318
319 protected function feedTitle() {
320 global $wgContLanguageCode, $wgSitename;
321 $page = SpecialPage::getPage( 'Contributions' );
322 $desc = $page->getDescription();
323 return "$wgSitename - $desc [$wgContLanguageCode]";
324 }
325
326 protected function feedItem( $row ) {
327 $title = Title::MakeTitle( intval( $row->page_namespace ), $row->page_title );
328 if( $title ) {
329 $date = $row->rev_timestamp;
330 $comments = $title->getTalkPage()->getFullURL();
331 $revision = Revision::newFromTitle( $title, $row->rev_id );
332
333 return new FeedItem(
334 $title->getPrefixedText(),
335 $this->feedItemDesc( $revision ),
336 $title->getFullURL(),
337 $date,
338 $this->feedItemAuthor( $revision ),
339 $comments
340 );
341 } else {
342 return NULL;
343 }
344 }
345
346 /**
347 * Quickie hack... strip out wikilinks to more legible form from the comment.
348 */
349 protected function stripComment( $text ) {
350 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
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() ) . ': ' .
360 htmlspecialchars( $this->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 ) {
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
385 $this->getDateCond( $year, $month );
386
387 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
388 }
389
390 function getDefaultQuery() {
391 $query = parent::getDefaultQuery();
392 $query['target'] = $this->target;
393 return $query;
394 }
395
396 function getQueryInfo() {
397 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
398 $conds = array_merge( array('page_id=rev_page'), $userCond, $this->getNamespaceCond() );
399 $queryInfo = array(
400 'tables' => $tables,
401 'fields' => array(
402 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'rev_id', 'rev_page',
403 'rev_text_id', 'rev_timestamp', 'rev_comment', 'rev_minor_edit', 'rev_user',
404 'rev_user_text', 'rev_parent_id', 'rev_deleted'
405 ),
406 'conds' => $conds,
407 'options' => array( 'USE INDEX' => array('revision' => $index) ),
408 'join_conds' => $join_cond
409 );
410 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
411 return $queryInfo;
412 }
413
414 function getUserCond() {
415 $condition = array();
416 $join_conds = array();
417 if( $this->target == 'newbies' ) {
418 $tables = array( 'user_groups', 'page', 'revision' );
419 $max = $this->mDb->selectField( 'user', 'max(user_id)', false, __METHOD__ );
420 $condition[] = 'rev_user >' . (int)($max - $max / 100);
421 $condition[] = 'ug_group IS NULL';
422 $index = 'user_timestamp';
423 # FIXME: other groups may have 'bot' rights
424 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
425 } else {
426 $tables = array( 'page', 'revision' );
427 $condition['rev_user_text'] = $this->target;
428 $index = 'usertext_timestamp';
429 }
430 return array( $tables, $index, $condition, $join_conds );
431 }
432
433 function getNamespaceCond() {
434 if( $this->namespace !== '' ) {
435 return array( 'page_namespace' => (int)$this->namespace );
436 } else {
437 return array();
438 }
439 }
440
441 function getIndexField() {
442 return 'rev_timestamp';
443 }
444
445 function getStartBody() {
446 return "<ul>\n";
447 }
448
449 function getEndBody() {
450 return "</ul>\n";
451 }
452
453 /**
454 * Generates each row in the contributions list.
455 *
456 * Contributions which are marked "top" are currently on top of the history.
457 * For these contributions, a [rollback] link is shown for users with roll-
458 * back privileges. The rollback link restores the most recent version that
459 * was not written by the target user.
460 *
461 * @todo This would probably look a lot nicer in a table.
462 */
463 function formatRow( $row ) {
464 wfProfileIn( __METHOD__ );
465
466 global $wgLang, $wgUser, $wgContLang;
467
468 $sk = $this->getSkin();
469 $rev = new Revision( $row );
470
471 $page = Title::makeTitle( $row->page_namespace, $row->page_title );
472 $link = $sk->makeKnownLinkObj( $page );
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 } else {
479 $difftext .= $this->messages['newarticle'];
480 }
481
482 if( !$page->getUserPermissionsErrors( 'rollback', $wgUser )
483 && !$page->getUserPermissionsErrors( 'edit', $wgUser ) ) {
484 $topmarktext .= ' '.$sk->generateRollback( $rev );
485 }
486
487 }
488 # Is there a visible previous revision?
489 if( $rev->userCan(Revision::DELETED_TEXT) ) {
490 $difftext = '(' . $sk->makeKnownLinkObj( $page, $this->messages['diff'], 'diff=prev&oldid='.$row->rev_id ) . ')';
491 } else {
492 $difftext = '(' . $this->messages['diff'] . ')';
493 }
494 $histlink='('.$sk->makeKnownLinkObj( $page, $this->messages['hist'], 'action=history' ) . ')';
495
496 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
497 $d = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->rev_timestamp ), true );
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( $row->rev_minor_edit ) {
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 // Let extensions add data
527 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
528
529 $ret = "<li>$ret</li>\n";
530 wfProfileOut( __METHOD__ );
531 return $ret;
532 }
533
534 /**
535 * Get the Database object in use
536 *
537 * @return Database
538 */
539 public function getDatabase() {
540 return $this->mDb;
541 }
542
543 }