Consistent casing for addHTML()
[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 // 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 /**
351 * Quickie hack... strip out wikilinks to more legible form from the comment.
352 */
353 protected function stripComment( $text ) {
354 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
355 }
356
357 protected function feedItemAuthor( $revision ) {
358 return $revision->getUserText();
359 }
360
361 protected function feedItemDesc( $revision ) {
362 if( $revision ) {
363 return '<p>' . htmlspecialchars( $revision->getUserText() ) . ': ' .
364 htmlspecialchars( $this->stripComment( $revision->getComment() ) ) .
365 "</p>\n<hr />\n<div>" .
366 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
367 }
368 return '';
369 }
370 }
371
372 /**
373 * Pager for Special:Contributions
374 * @ingroup SpecialPage Pager
375 */
376 class ContribsPager extends ReverseChronologicalPager {
377 public $mDefaultDirection = true;
378 var $messages, $target;
379 var $namespace = '', $mDb;
380
381 function __construct( $target, $namespace = false, $year = false, $month = false ) {
382 parent::__construct();
383 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist newpageletter minoreditletter' ) as $msg ) {
384 $this->messages[$msg] = wfMsgExt( $msg, array( 'escape') );
385 }
386 $this->target = $target;
387 $this->namespace = $namespace;
388
389 $this->getDateCond( $year, $month );
390
391 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
392 }
393
394 function getDefaultQuery() {
395 $query = parent::getDefaultQuery();
396 $query['target'] = $this->target;
397 return $query;
398 }
399
400 function getQueryInfo() {
401 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
402 $conds = array_merge( array('page_id=rev_page'), $userCond, $this->getNamespaceCond() );
403 $queryInfo = array(
404 'tables' => $tables,
405 'fields' => array(
406 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'rev_id', 'rev_page',
407 'rev_text_id', 'rev_timestamp', 'rev_comment', 'rev_minor_edit', 'rev_user',
408 '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 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
415 return $queryInfo;
416 }
417
418 function getUserCond() {
419 $condition = array();
420 $join_conds = array();
421 if( $this->target == 'newbies' ) {
422 $tables = array( 'user_groups', 'page', 'revision' );
423 $max = $this->mDb->selectField( 'user', 'max(user_id)', false, __METHOD__ );
424 $condition[] = 'rev_user >' . (int)($max - $max / 100);
425 $condition[] = 'ug_group IS NULL';
426 $index = 'user_timestamp';
427 # FIXME: other groups may have 'bot' rights
428 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
429 } else {
430 $tables = array( 'page', 'revision' );
431 $condition['rev_user_text'] = $this->target;
432 $index = 'usertext_timestamp';
433 }
434 return array( $tables, $index, $condition, $join_conds );
435 }
436
437 function getNamespaceCond() {
438 if( $this->namespace !== '' ) {
439 return array( 'page_namespace' => (int)$this->namespace );
440 } else {
441 return array();
442 }
443 }
444
445 function getIndexField() {
446 return 'rev_timestamp';
447 }
448
449 function getStartBody() {
450 return "<ul>\n";
451 }
452
453 function getEndBody() {
454 return "</ul>\n";
455 }
456
457 /**
458 * Generates each row in the contributions list.
459 *
460 * Contributions which are marked "top" are currently on top of the history.
461 * For these contributions, a [rollback] link is shown for users with roll-
462 * back privileges. The rollback link restores the most recent version that
463 * was not written by the target user.
464 *
465 * @todo This would probably look a lot nicer in a table.
466 */
467 function formatRow( $row ) {
468 wfProfileIn( __METHOD__ );
469
470 global $wgLang, $wgUser, $wgContLang;
471
472 $sk = $this->getSkin();
473 $rev = new Revision( $row );
474
475 $page = Title::makeTitle( $row->page_namespace, $row->page_title );
476 $link = $sk->makeKnownLinkObj( $page );
477 $difftext = $topmarktext = '';
478 if( $row->rev_id == $row->page_latest ) {
479 $topmarktext .= '<strong>' . $this->messages['uctop'] . '</strong>';
480 if( !$row->page_is_new ) {
481 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $this->messages['diff'], 'diff=0' ) . ')';
482 } else {
483 $difftext .= $this->messages['newarticle'];
484 }
485
486 if( !$page->getUserPermissionsErrors( 'rollback', $wgUser )
487 && !$page->getUserPermissionsErrors( 'edit', $wgUser ) ) {
488 $topmarktext .= ' '.$sk->generateRollback( $rev );
489 }
490
491 }
492 # Is there a visible previous revision?
493 if( $rev->userCan(Revision::DELETED_TEXT) ) {
494 $difftext = '(' . $sk->makeKnownLinkObj( $page, $this->messages['diff'], 'diff=prev&oldid='.$row->rev_id ) . ')';
495 } else {
496 $difftext = '(' . $this->messages['diff'] . ')';
497 }
498 $histlink='('.$sk->makeKnownLinkObj( $page, $this->messages['hist'], 'action=history' ) . ')';
499
500 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
501 $d = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->rev_timestamp ), true );
502
503 if( $this->target == 'newbies' ) {
504 $userlink = ' . . ' . $sk->userLink( $row->rev_user, $row->rev_user_text );
505 $userlink .= ' (' . $sk->userTalkLink( $row->rev_user, $row->rev_user_text ) . ') ';
506 } else {
507 $userlink = '';
508 }
509
510 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
511 $d = '<span class="history-deleted">' . $d . '</span>';
512 }
513
514 if( $rev->getParentId() === 0 ) {
515 $nflag = '<span class="newpage">' . $this->messages['newpageletter'] . '</span>';
516 } else {
517 $nflag = '';
518 }
519
520 if( $row->rev_minor_edit ) {
521 $mflag = '<span class="minor">' . $this->messages['minoreditletter'] . '</span> ';
522 } else {
523 $mflag = '';
524 }
525
526 $ret = "{$d} {$histlink} {$difftext} {$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
527 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
528 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
529 }
530 // Let extensions add data
531 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
532
533 $ret = "<li>$ret</li>\n";
534 wfProfileOut( __METHOD__ );
535 return $ret;
536 }
537
538 /**
539 * Get the Database object in use
540 *
541 * @return Database
542 */
543 public function getDatabase() {
544 return $this->mDb;
545 }
546
547 }