Rewrite Special:Export to subclass SpecialPage.
[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( ( $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() );
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 if( !isset( $this->opts['tagfilter'] ) ) {
242 $this->opts['tagfilter'] = '';
243 }
244
245 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
246
247 foreach ( $this->opts as $name => $value ) {
248 if( in_array( $name, array( 'namespace', 'target', 'contribs', 'year', 'month' ) ) ) {
249 continue;
250 }
251 $f .= "\t" . Xml::hidden( $name, $value ) . "\n";
252 }
253
254 $tagFilter = ChangeTags::buildTagFilterSelector( $this->opts['tagfilter'] );
255
256 $f .= '<fieldset>' .
257 Xml::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
258 Xml::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ),
259 'contribs', 'newbie' , 'newbie', $this->opts['contribs'] == 'newbie' ? true : false ) . '<br />' .
260 Xml::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parseinline' ) ),
261 'contribs' , 'user', 'user', $this->opts['contribs'] == 'user' ? true : false ) . ' ' .
262 Xml::input( 'target', 20, $this->opts['target']) . ' '.
263 '<span style="white-space: nowrap">' .
264 Xml::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
265 Xml::namespaceSelector( $this->opts['namespace'], '' ) .
266 '</span>' .
267 ( $tagFilter ? Xml::tags( 'p', null, implode( '&nbsp;', $tagFilter ) ) : '' ) .
268 Xml::openElement( 'p' ) .
269 '<span style="white-space: nowrap">' .
270 Xml::label( wfMsg( 'year' ), 'year' ) . ' '.
271 Xml::input( 'year', 4, $this->opts['year'], array('id' => 'year', 'maxlength' => 4) ) .
272 '</span>' .
273 ' '.
274 '<span style="white-space: nowrap">' .
275 Xml::label( wfMsg( 'month' ), 'month' ) . ' '.
276 Xml::monthSelector( $this->opts['month'], -1 ) . ' '.
277 '</span>' .
278 Xml::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
279 Xml::closeElement( 'p' );
280
281 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
282 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
283 $f .= "<p>{$explain}</p>";
284
285 $f .= '</fieldset>' .
286 Xml::closeElement( 'form' );
287 return $f;
288 }
289
290 /**
291 * Output a subscription feed listing recent edits to this page.
292 * @param string $type
293 */
294 protected function feed( $type ) {
295 global $wgRequest, $wgFeed, $wgFeedClasses, $wgFeedLimit;
296
297 if( !$wgFeed ) {
298 global $wgOut;
299 $wgOut->addWikiMsg( 'feed-unavailable' );
300 return;
301 }
302
303 if( !isset( $wgFeedClasses[$type] ) ) {
304 global $wgOut;
305 $wgOut->addWikiMsg( 'feed-invalid' );
306 return;
307 }
308
309 $feed = new $wgFeedClasses[$type](
310 $this->feedTitle(),
311 wfMsgExt( 'tagline', 'parsemag' ),
312 $this->getTitle()->getFullUrl() );
313
314 // Already valid title
315 $nt = Title::makeTitleSafe( NS_USER, $this->opts['target'] );
316 $target = $this->opts['target'] == 'newbies' ? 'newbies' : $nt->getText();
317
318 $pager = new ContribsPager( $target, $this->opts['namespace'],
319 $this->opts['year'], $this->opts['month'], $this->opts['tagfilter'] );
320
321 $pager->mLimit = min( $this->opts['limit'], $wgFeedLimit );
322
323 $feed->outHeader();
324 if( $pager->getNumRows() > 0 ) {
325 while( $row = $pager->mResult->fetchObject() ) {
326 $feed->outItem( $this->feedItem( $row ) );
327 }
328 }
329 $feed->outFooter();
330 }
331
332 protected function feedTitle() {
333 global $wgContLanguageCode, $wgSitename;
334 $page = SpecialPage::getPage( 'Contributions' );
335 $desc = $page->getDescription();
336 return "$wgSitename - $desc [$wgContLanguageCode]";
337 }
338
339 protected function feedItem( $row ) {
340 $title = Title::MakeTitle( intval( $row->page_namespace ), $row->page_title );
341 if( $title ) {
342 $date = $row->rev_timestamp;
343 $comments = $title->getTalkPage()->getFullURL();
344 $revision = Revision::newFromTitle( $title, $row->rev_id );
345
346 return new FeedItem(
347 $title->getPrefixedText(),
348 $this->feedItemDesc( $revision ),
349 $title->getFullURL(),
350 $date,
351 $this->feedItemAuthor( $revision ),
352 $comments
353 );
354 } else {
355 return NULL;
356 }
357 }
358
359 protected function feedItemAuthor( $revision ) {
360 return $revision->getUserText();
361 }
362
363 protected function feedItemDesc( $revision ) {
364 if( $revision ) {
365 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
366 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
367 "</p>\n<hr />\n<div>" .
368 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
369 }
370 return '';
371 }
372 }
373
374 /**
375 * Pager for Special:Contributions
376 * @ingroup SpecialPage Pager
377 */
378 class ContribsPager extends ReverseChronologicalPager {
379 public $mDefaultDirection = true;
380 var $messages, $target;
381 var $namespace = '', $mDb;
382
383 function __construct( $target, $namespace = false, $year = false, $month = false, $tagFilter = false ) {
384 parent::__construct();
385 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist newpageletter minoreditletter' ) as $msg ) {
386 $this->messages[$msg] = wfMsgExt( $msg, array( 'escape') );
387 }
388 $this->target = $target;
389 $this->namespace = $namespace;
390 $this->tagFilter = $tagFilter;
391
392 $this->getDateCond( $year, $month );
393
394 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
395 }
396
397 function getDefaultQuery() {
398 $query = parent::getDefaultQuery();
399 $query['target'] = $this->target;
400 return $query;
401 }
402
403 function getQueryInfo() {
404 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
405
406 $conds = array_merge( $userCond, $this->getNamespaceCond() );
407 $join_cond['page'] = array( 'INNER JOIN', 'page_id=rev_page' );
408
409 $queryInfo = array(
410 'tables' => $tables,
411 'fields' => array(
412 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'page_is_redirect',
413 'page_len','rev_id', 'rev_page', 'rev_text_id', 'rev_timestamp', 'rev_comment',
414 'rev_minor_edit', 'rev_user', 'rev_user_text', 'rev_parent_id', 'rev_deleted'
415 ),
416 'conds' => $conds,
417 'options' => array( 'USE INDEX' => array('revision' => $index) ),
418 'join_conds' => $join_cond
419 );
420
421 ChangeTags::modifyDisplayQuery( $queryInfo['tables'], $queryInfo['fields'], $queryInfo['conds'], $queryInfo['join_conds'], $this->tagFilter );
422
423 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
424 return $queryInfo;
425 }
426
427 function getUserCond() {
428 $condition = array();
429 $join_conds = array();
430 if( $this->target == 'newbies' ) {
431 $tables = array( 'user_groups', 'page', 'revision' );
432 $max = $this->mDb->selectField( 'user', 'max(user_id)', false, __METHOD__ );
433 $condition[] = 'rev_user >' . (int)($max - $max / 100);
434 $condition[] = 'ug_group IS NULL';
435 $index = 'user_timestamp';
436 # FIXME: other groups may have 'bot' rights
437 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
438 } else {
439 $tables = array( 'page', 'revision' );
440 $condition['rev_user_text'] = $this->target;
441 $index = 'usertext_timestamp';
442 }
443 return array( $tables, $index, $condition, $join_conds );
444 }
445
446 function getNamespaceCond() {
447 if( $this->namespace !== '' ) {
448 return array( 'page_namespace' => (int)$this->namespace );
449 } else {
450 return array();
451 }
452 }
453
454 function getIndexField() {
455 return 'rev_timestamp';
456 }
457
458 function getStartBody() {
459 return "<ul>\n";
460 }
461
462 function getEndBody() {
463 return "</ul>\n";
464 }
465
466 /**
467 * Generates each row in the contributions list.
468 *
469 * Contributions which are marked "top" are currently on top of the history.
470 * For these contributions, a [rollback] link is shown for users with roll-
471 * back privileges. The rollback link restores the most recent version that
472 * was not written by the target user.
473 *
474 * @todo This would probably look a lot nicer in a table.
475 */
476 function formatRow( $row ) {
477 global $wgLang, $wgUser, $wgContLang;
478 wfProfileIn( __METHOD__ );
479
480 $sk = $this->getSkin();
481 $rev = new Revision( $row );
482 $classes = array();
483
484 $page = Title::newFromRow( $row );
485 $page->resetArticleId( $row->rev_page ); // use process cache
486 $link = $sk->makeLinkObj( $page, $page->getPrefixedText(), $page->isRedirect() ? 'redirect=no' : '' );
487 # Mark current revisions
488 $difftext = $topmarktext = '';
489 if( $row->rev_id == $row->page_latest ) {
490 $topmarktext .= '<strong>' . $this->messages['uctop'] . '</strong>';
491 if( !$row->page_is_new ) {
492 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $this->messages['diff'], 'diff=0' ) . ')';
493 # Add rollback link
494 if( $page->quickUserCan( 'rollback') && $page->quickUserCan( 'edit' ) ) {
495 $topmarktext .= ' '.$sk->generateRollback( $rev );
496 }
497 } else {
498 $difftext .= $this->messages['newarticle'];
499 }
500 }
501 # Is there a visible previous revision?
502 if( $rev->userCan(Revision::DELETED_TEXT) ) {
503 $difftext = '(' . $sk->makeKnownLinkObj( $page, $this->messages['diff'],
504 'diff=prev&oldid='.$row->rev_id ) . ')';
505 } else {
506 $difftext = '(' . $this->messages['diff'] . ')';
507 }
508 $histlink = '('.$sk->makeKnownLinkObj( $page, $this->messages['hist'], 'action=history' ) . ')';
509
510 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
511 $date = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->rev_timestamp ), true );
512 $d = $sk->makeKnownLinkObj( $page, $date, 'oldid='.intval($row->rev_id) );
513
514 if( $this->target == 'newbies' ) {
515 $userlink = ' . . ' . $sk->userLink( $row->rev_user, $row->rev_user_text );
516 $userlink .= ' (' . $sk->userTalkLink( $row->rev_user, $row->rev_user_text ) . ') ';
517 } else {
518 $userlink = '';
519 }
520
521 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
522 $d = '<span class="history-deleted">' . $d . '</span>';
523 }
524
525 if( $rev->getParentId() === 0 ) {
526 $nflag = '<span class="newpage">' . $this->messages['newpageletter'] . '</span>';
527 } else {
528 $nflag = '';
529 }
530
531 if( $rev->isMinor() ) {
532 $mflag = '<span class="minor">' . $this->messages['minoreditletter'] . '</span> ';
533 } else {
534 $mflag = '';
535 }
536
537 $ret = "{$d} {$histlink} {$difftext} {$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
538 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
539 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
540 }
541
542 # Tags, if any.
543 list($tagSummary, $newClasses) = ChangeTags::formatSummaryRow( $row->ts_tags, 'contributions' );
544 $classes = array_merge( $classes, $newClasses );
545 $ret .= " $tagSummary";
546
547 // Let extensions add data
548 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
549
550 $classes = implode( ' ', $classes );
551 $ret = "<li class=\"$classes\">$ret</li>\n";
552 wfProfileOut( __METHOD__ );
553 return $ret;
554 }
555
556 /**
557 * Get the Database object in use
558 *
559 * @return Database
560 */
561 public function getDatabase() {
562 return $this->mDb;
563 }
564
565 }