MediaWiki  REL1_22
CreditsAction.php
Go to the documentation of this file.
00001 <?php
00029 class CreditsAction extends FormlessAction {
00030 
00031     public function getName() {
00032         return 'credits';
00033     }
00034 
00035     protected function getDescription() {
00036         return $this->msg( 'creditspage' )->escaped();
00037     }
00038 
00044     public function onView() {
00045         wfProfileIn( __METHOD__ );
00046 
00047         if ( $this->page->getID() == 0 ) {
00048             $s = $this->msg( 'nocredits' )->parse();
00049         } else {
00050             $s = $this->getCredits( -1 );
00051         }
00052 
00053         wfProfileOut( __METHOD__ );
00054 
00055         return Html::rawElement( 'div', array( 'id' => 'mw-credits' ), $s );
00056     }
00057 
00065     public function getCredits( $cnt, $showIfMax = true ) {
00066         wfProfileIn( __METHOD__ );
00067         $s = '';
00068 
00069         if ( $cnt != 0 ) {
00070             $s = $this->getAuthor( $this->page );
00071             if ( $cnt > 1 || $cnt < 0 ) {
00072                 $s .= ' ' . $this->getContributors( $cnt - 1, $showIfMax );
00073             }
00074         }
00075 
00076         wfProfileOut( __METHOD__ );
00077         return $s;
00078     }
00079 
00085     protected function getAuthor( Page $page ) {
00086         $user = User::newFromName( $page->getUserText(), false );
00087 
00088         $timestamp = $page->getTimestamp();
00089         if ( $timestamp ) {
00090             $lang = $this->getLanguage();
00091             $d = $lang->date( $page->getTimestamp(), true );
00092             $t = $lang->time( $page->getTimestamp(), true );
00093         } else {
00094             $d = '';
00095             $t = '';
00096         }
00097         return $this->msg( 'lastmodifiedatby', $d, $t )->rawParams(
00098             $this->userLink( $user ) )->params( $user->getName() )->escaped();
00099     }
00100 
00107     protected function getContributors( $cnt, $showIfMax ) {
00108         global $wgHiddenPrefs;
00109 
00110         $contributors = $this->page->getContributors();
00111 
00112         $others_link = false;
00113 
00114         # Hmm... too many to fit!
00115         if ( $cnt > 0 && $contributors->count() > $cnt ) {
00116             $others_link = $this->othersLink();
00117             if ( !$showIfMax ) {
00118                 return $this->msg( 'othercontribs' )->rawParams(
00119                     $others_link )->params( $contributors->count() )->escaped();
00120             }
00121         }
00122 
00123         $real_names = array();
00124         $user_names = array();
00125         $anon_ips = array();
00126 
00127         # Sift for real versus user names
00128         foreach ( $contributors as $user ) {
00129             $cnt--;
00130             if ( $user->isLoggedIn() ) {
00131                 $link = $this->link( $user );
00132                 if ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
00133                     $real_names[] = $link;
00134                 } else {
00135                     $user_names[] = $link;
00136                 }
00137             } else {
00138                 $anon_ips[] = $this->link( $user );
00139             }
00140 
00141             if ( $cnt == 0 ) {
00142                 break;
00143             }
00144         }
00145 
00146         $lang = $this->getLanguage();
00147 
00148         if ( count( $real_names ) ) {
00149             $real = $lang->listToText( $real_names );
00150         } else {
00151             $real = false;
00152         }
00153 
00154         # "ThisSite user(s) A, B and C"
00155         if ( count( $user_names ) ) {
00156             $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
00157                 count( $user_names ) )->escaped();
00158         } else {
00159             $user = false;
00160         }
00161 
00162         if ( count( $anon_ips ) ) {
00163             $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
00164                 count( $anon_ips ) )->escaped();
00165         } else {
00166             $anon = false;
00167         }
00168 
00169         # This is the big list, all mooshed together. We sift for blank strings
00170         $fulllist = array();
00171         foreach ( array( $real, $user, $anon, $others_link ) as $s ) {
00172             if ( $s !== false ) {
00173                 array_push( $fulllist, $s );
00174             }
00175         }
00176 
00177         $count = count( $fulllist );
00178         # "Based on work by ..."
00179         return $count
00180             ? $this->msg( 'othercontribs' )->rawParams(
00181                 $lang->listToText( $fulllist ) )->params( $count )->escaped()
00182             : '';
00183     }
00184 
00190     protected function link( User $user ) {
00191         global $wgHiddenPrefs;
00192         if ( !in_array( 'realname', $wgHiddenPrefs ) && !$user->isAnon() ) {
00193             $real = $user->getRealName();
00194         } else {
00195             $real = false;
00196         }
00197 
00198         $page = $user->isAnon()
00199             ? SpecialPage::getTitleFor( 'Contributions', $user->getName() )
00200             : $user->getUserPage();
00201 
00202         return Linker::link( $page, htmlspecialchars( $real ? $real : $user->getName() ) );
00203     }
00204 
00210     protected function userLink( User $user ) {
00211         $link = $this->link( $user );
00212         if ( $user->isAnon() ) {
00213             return $this->msg( 'anonuser' )->rawParams( $link )->parse();
00214         } else {
00215             global $wgHiddenPrefs;
00216             if ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
00217                 return $link;
00218             } else {
00219                 return $this->msg( 'siteuser' )->rawParams( $link )->params( $user->getName() )->escaped();
00220             }
00221         }
00222     }
00223 
00228     protected function othersLink() {
00229         return Linker::linkKnown(
00230             $this->getTitle(),
00231             $this->msg( 'others' )->escaped(),
00232             array(),
00233             array( 'action' => 'credits' )
00234         );
00235     }
00236 }