]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/specials/SpecialContributions.php
MediaWiki 1.15.4-scripts
[autoinstallsdev/mediawiki.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' ), wfMsg( 'sp-contributions-logs' ), 
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                         # Add a link to change user rights for privileged users
177                         $userrightsPage = new UserrightsPage();
178                         if( 0 !== $id && $userrightsPage->userCanChangeRights( User::newFromId( $id ) ) ) {
179                                 $tools[] = $sk->makeKnownLinkObj(
180                                         SpecialPage::getTitleFor( 'Userrights', $nt->getDBkey() ),
181                                         wfMsgHtml( 'userrights' )
182                                 );
183                         }
184
185                         wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
186         
187                         $links = $wgLang->pipeList( $tools );
188                 }
189         
190                 // Old message 'contribsub' had one parameter, but that doesn't work for
191                 // languages that want to put the "for" bit right after $user but before
192                 // $links.  If 'contribsub' is around, use it for reverse compatibility,
193                 // otherwise use 'contribsub2'.
194                 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
195                         return wfMsgHtml( 'contribsub2', $user, $links );
196                 } else {
197                         return wfMsgHtml( 'contribsub', "$user ($links)" );
198                 }
199         }
200
201         /**
202          * Generates the namespace selector form with hidden attributes.
203          * @param $this->opts Array: the options to be included.
204          */
205         protected function getForm() {
206                 global $wgScript, $wgTitle;
207         
208                 $this->opts['title'] = $wgTitle->getPrefixedText();
209                 if( !isset( $this->opts['target'] ) ) {
210                         $this->opts['target'] = '';
211                 } else {
212                         $this->opts['target'] = str_replace( '_' , ' ' , $this->opts['target'] );
213                 }
214         
215                 if( !isset( $this->opts['namespace'] ) ) {
216                         $this->opts['namespace'] = '';
217                 }
218         
219                 if( !isset( $this->opts['contribs'] ) ) {
220                         $this->opts['contribs'] = 'user';
221                 }
222         
223                 if( !isset( $this->opts['year'] ) ) {
224                         $this->opts['year'] = '';
225                 }
226         
227                 if( !isset( $this->opts['month'] ) ) {
228                         $this->opts['month'] = '';
229                 }
230         
231                 if( $this->opts['contribs'] == 'newbie' ) {
232                         $this->opts['target'] = '';
233                 }
234
235                 if( !isset( $this->opts['tagfilter'] ) ) {
236                         $this->opts['tagfilter'] = '';
237                 }
238         
239                 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
240                 # Add hidden params for tracking
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                 $tagFilter = ChangeTags::buildTagFilterSelector( $this->opts['tagfilter'] );
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                         ( $tagFilter ? Xml::tags( 'p', null, implode( '&nbsp;', $tagFilter ) ) : '' ) .
262                         Xml::openElement( 'p' ) .
263                         '<span style="white-space: nowrap">' .
264                         Xml::dateMenu( $this->opts['year'], $this->opts['month'] ) .
265                         '</span>' . ' ' .
266                         Xml::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
267                         Xml::closeElement( 'p' );
268         
269                 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
270                 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
271                         $f .= "<p>{$explain}</p>";
272         
273                 $f .= '</fieldset>' .
274                         Xml::closeElement( 'form' );
275                 return $f;
276         }
277         
278         /**
279          * Output a subscription feed listing recent edits to this page.
280          * @param string $type
281          */
282         protected function feed( $type ) {
283                 global $wgRequest, $wgFeed, $wgFeedClasses, $wgFeedLimit;
284
285                 if( !$wgFeed ) {
286                         global $wgOut;
287                         $wgOut->addWikiMsg( 'feed-unavailable' );
288                         return;
289                 }
290
291                 if( !isset( $wgFeedClasses[$type] ) ) {
292                         global $wgOut;
293                         $wgOut->addWikiMsg( 'feed-invalid' );
294                         return;
295                 }
296
297                 $feed = new $wgFeedClasses[$type](
298                         $this->feedTitle(),
299                         wfMsgExt( 'tagline', 'parsemag' ),
300                         $this->getTitle()->getFullUrl() . "/" . urlencode($this->opts['target'])
301                 );
302                         
303                 // Already valid title
304                 $nt = Title::makeTitleSafe( NS_USER, $this->opts['target'] );
305                 $target = $this->opts['target'] == 'newbies' ? 'newbies' : $nt->getText();
306                         
307                 $pager = new ContribsPager( $target, $this->opts['namespace'], 
308                         $this->opts['year'], $this->opts['month'], $this->opts['tagfilter'] );
309
310                 $pager->mLimit = min( $this->opts['limit'], $wgFeedLimit );
311
312                 $feed->outHeader();
313                 if( $pager->getNumRows() > 0 ) {
314                         while( $row = $pager->mResult->fetchObject() ) {
315                                 $feed->outItem( $this->feedItem( $row ) );
316                         }
317                 }
318                 $feed->outFooter();
319         }
320
321         protected function feedTitle() {
322                 global $wgContLanguageCode, $wgSitename;
323                 $page = SpecialPage::getPage( 'Contributions' );
324                 $desc = $page->getDescription();
325                 return "$wgSitename - $desc [$wgContLanguageCode]";
326         }
327
328         protected function feedItem( $row ) {
329                 $title = Title::MakeTitle( intval( $row->page_namespace ), $row->page_title );
330                 if( $title ) {
331                         $date = $row->rev_timestamp;
332                         $comments = $title->getTalkPage()->getFullURL();
333                         $revision = Revision::newFromTitle( $title, $row->rev_id );
334
335                         return new FeedItem(
336                                 $title->getPrefixedText(),
337                                 $this->feedItemDesc( $revision ),
338                                 $title->getFullURL(),
339                                 $date,
340                                 $this->feedItemAuthor( $revision ),
341                                 $comments
342                         );
343                 } else {
344                         return NULL;
345                 }
346         }
347
348         protected function feedItemAuthor( $revision ) {
349                 return $revision->getUserText();
350         }
351
352         protected function feedItemDesc( $revision ) {
353                 if( $revision ) {
354                         return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
355                                 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) . 
356                                 "</p>\n<hr />\n<div>" .
357                                 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
358                 }
359                 return '';
360         }
361 }
362
363 /**
364  * Pager for Special:Contributions
365  * @ingroup SpecialPage Pager
366  */
367 class ContribsPager extends ReverseChronologicalPager {
368         public $mDefaultDirection = true;
369         var $messages, $target;
370         var $namespace = '', $mDb;
371
372         function __construct( $target, $namespace = false, $year = false, $month = false, $tagFilter = false ) {
373                 parent::__construct();
374                 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist newpageletter minoreditletter' ) as $msg ) {
375                         $this->messages[$msg] = wfMsgExt( $msg, array( 'escape') );
376                 }
377                 $this->target = $target;
378                 $this->namespace = $namespace;
379                 $this->tagFilter = $tagFilter;
380
381                 $this->getDateCond( $year, $month );
382
383                 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
384         }
385
386         function getDefaultQuery() {
387                 $query = parent::getDefaultQuery();
388                 $query['target'] = $this->target;
389                 return $query;
390         }
391
392         function getQueryInfo() {
393                 global $wgUser;
394                 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
395                 
396                 $conds = array_merge( $userCond, $this->getNamespaceCond() );
397                 // Paranoia: avoid brute force searches (bug 17342)
398                 if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
399                         $conds[] = 'rev_deleted & ' . Revision::DELETED_USER . ' = 0';
400                 }
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'],
416                                                                                 $queryInfo['fields'],
417                                                                                 $queryInfo['conds'],
418                                                                                 $queryInfo['join_conds'],
419                                                                                 $queryInfo['options'],
420                                                                                 $this->tagFilter );
421                 
422                 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
423                 return $queryInfo;
424         }
425
426         function getUserCond() {
427                 $condition = array();
428                 $join_conds = array();
429                 if( $this->target == 'newbies' ) {
430                         $tables = array( 'user_groups', 'page', 'revision' );
431                         $max = $this->mDb->selectField( 'user', 'max(user_id)', false, __METHOD__ );
432                         $condition[] = 'rev_user >' . (int)($max - $max / 100);
433                         $condition[] = 'ug_group IS NULL';
434                         $index = 'user_timestamp';
435                         # FIXME: other groups may have 'bot' rights
436                         $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
437                 } else {
438                         $tables = array( 'page', 'revision' );
439                         $condition['rev_user_text'] = $this->target;
440                         $index = 'usertext_timestamp';
441                 }
442                 return array( $tables, $index, $condition, $join_conds );
443         }
444
445         function getNamespaceCond() {
446                 if( $this->namespace !== '' ) {
447                         return array( 'page_namespace' => (int)$this->namespace );
448                 } else {
449                         return array();
450                 }
451         }
452
453         function getIndexField() {
454                 return 'rev_timestamp';
455         }
456
457         function getStartBody() {
458                 return "<ul>\n";
459         }
460
461         function getEndBody() {
462                 return "</ul>\n";
463         }
464
465         /**
466          * Generates each row in the contributions list.
467          *
468          * Contributions which are marked "top" are currently on top of the history.
469          * For these contributions, a [rollback] link is shown for users with roll-
470          * back privileges. The rollback link restores the most recent version that
471          * was not written by the target user.
472          *
473          * @todo This would probably look a lot nicer in a table.
474          */
475         function formatRow( $row ) {
476                 global $wgLang, $wgUser, $wgContLang;
477                 wfProfileIn( __METHOD__ );
478
479                 $sk = $this->getSkin();
480                 $rev = new Revision( $row );
481                 $classes = array();
482
483                 $page = Title::newFromRow( $row );
484                 $page->resetArticleId( $row->rev_page ); // use process cache
485                 $link = $sk->makeLinkObj( $page, $page->getPrefixedText(), $page->isRedirect() ? 'redirect=no' : '' );
486                 # Mark current revisions
487                 $difftext = $topmarktext = '';
488                 if( $row->rev_id == $row->page_latest ) {
489                         $topmarktext .= '<strong>' . $this->messages['uctop'] . '</strong>';
490                         if( !$row->page_is_new ) {
491                                 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $this->messages['diff'], 'diff=0' ) . ')';
492                                 # Add rollback link
493                                 if( $page->quickUserCan( 'rollback') && $page->quickUserCan( 'edit' ) ) {
494                                         $topmarktext .= ' '.$sk->generateRollback( $rev );
495                                 }
496                         } else {
497                                 $difftext .= $this->messages['newarticle'];
498                         }
499                 }
500                 # Is there a visible previous revision?
501                 if( $rev->userCan(Revision::DELETED_TEXT) ) {
502                         $difftext = '(' . $sk->makeKnownLinkObj( $page, $this->messages['diff'],
503                                 'diff=prev&oldid='.$row->rev_id ) . ')';
504                 } else {
505                         $difftext = '(' . $this->messages['diff'] . ')';
506                 }
507                 $histlink = '('.$sk->makeKnownLinkObj( $page, $this->messages['hist'], 'action=history' ) . ')';
508
509                 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
510                 $date = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->rev_timestamp ), true );
511                 $d = $sk->makeKnownLinkObj( $page, $date, 'oldid='.intval($row->rev_id) );
512
513                 if( $this->target == 'newbies' ) {
514                         $userlink = ' . . ' . $sk->userLink( $row->rev_user, $row->rev_user_text );
515                         $userlink .= ' (' . $sk->userTalkLink( $row->rev_user, $row->rev_user_text ) . ') ';
516                 } else {
517                         $userlink = '';
518                 }
519
520                 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
521                         $d = '<span class="history-deleted">' . $d . '</span>';
522                 }
523
524                 if( $rev->getParentId() === 0 ) {
525                         $nflag = '<span class="newpage">' . $this->messages['newpageletter'] . '</span>';
526                 } else {
527                         $nflag = '';
528                 }
529
530                 if( $rev->isMinor() ) {
531                         $mflag = '<span class="minor">' . $this->messages['minoreditletter'] . '</span> ';
532                 } else {
533                         $mflag = '';
534                 }
535
536                 $ret = "{$d} {$histlink} {$difftext} {$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
537                 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
538                         $ret .= ' ' . wfMsgHtml( 'deletedrev' );
539                 }
540
541                 # Tags, if any.
542                 list($tagSummary, $newClasses) = ChangeTags::formatSummaryRow( $row->ts_tags, 'contributions' );
543                 $classes = array_merge( $classes, $newClasses );
544                 $ret .= " $tagSummary";
545
546                 // Let extensions add data
547                 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
548
549                 $classes = implode( ' ', $classes );
550                 $ret = "<li class=\"$classes\">$ret</li>\n";
551                 wfProfileOut( __METHOD__ );
552                 return $ret;
553         }
554
555         /**
556          * Get the Database object in use
557          *
558          * @return Database
559          */
560         public function getDatabase() {
561                 return $this->mDb;
562         }
563
564 }