MediaWiki  REL1_24
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 
00078         return $s;
00079     }
00080 
00086     protected function getAuthor( Page $page ) {
00087         $user = User::newFromName( $page->getUserText(), false );
00088 
00089         $timestamp = $page->getTimestamp();
00090         if ( $timestamp ) {
00091             $lang = $this->getLanguage();
00092             $d = $lang->date( $page->getTimestamp(), true );
00093             $t = $lang->time( $page->getTimestamp(), true );
00094         } else {
00095             $d = '';
00096             $t = '';
00097         }
00098 
00099         return $this->msg( 'lastmodifiedatby', $d, $t )->rawParams(
00100             $this->userLink( $user ) )->params( $user->getName() )->escaped();
00101     }
00102 
00109     protected function canShowRealUserName() {
00110         $hiddenPrefs = $this->context->getConfig()->get( 'HiddenPrefs' );
00111         return !in_array( 'realname', $hiddenPrefs );
00112     }
00113 
00120     protected function getContributors( $cnt, $showIfMax ) {
00121         $contributors = $this->page->getContributors();
00122 
00123         $others_link = false;
00124 
00125         # Hmm... too many to fit!
00126         if ( $cnt > 0 && $contributors->count() > $cnt ) {
00127             $others_link = $this->othersLink();
00128             if ( !$showIfMax ) {
00129                 return $this->msg( 'othercontribs' )->rawParams(
00130                     $others_link )->params( $contributors->count() )->escaped();
00131             }
00132         }
00133 
00134         $real_names = array();
00135         $user_names = array();
00136         $anon_ips = array();
00137 
00138         # Sift for real versus user names
00139 
00140         foreach ( $contributors as $user ) {
00141             $cnt--;
00142             if ( $user->isLoggedIn() ) {
00143                 $link = $this->link( $user );
00144                 if ( $this->canShowRealUserName() && $user->getRealName() ) {
00145                     $real_names[] = $link;
00146                 } else {
00147                     $user_names[] = $link;
00148                 }
00149             } else {
00150                 $anon_ips[] = $this->link( $user );
00151             }
00152 
00153             if ( $cnt == 0 ) {
00154                 break;
00155             }
00156         }
00157 
00158         $lang = $this->getLanguage();
00159 
00160         if ( count( $real_names ) ) {
00161             $real = $lang->listToText( $real_names );
00162         } else {
00163             $real = false;
00164         }
00165 
00166         # "ThisSite user(s) A, B and C"
00167         if ( count( $user_names ) ) {
00168             $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
00169                 count( $user_names ) )->escaped();
00170         } else {
00171             $user = false;
00172         }
00173 
00174         if ( count( $anon_ips ) ) {
00175             $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
00176                 count( $anon_ips ) )->escaped();
00177         } else {
00178             $anon = false;
00179         }
00180 
00181         # This is the big list, all mooshed together. We sift for blank strings
00182         $fulllist = array();
00183         foreach ( array( $real, $user, $anon, $others_link ) as $s ) {
00184             if ( $s !== false ) {
00185                 array_push( $fulllist, $s );
00186             }
00187         }
00188 
00189         $count = count( $fulllist );
00190 
00191         # "Based on work by ..."
00192         return $count
00193             ? $this->msg( 'othercontribs' )->rawParams(
00194                 $lang->listToText( $fulllist ) )->params( $count )->escaped()
00195             : '';
00196     }
00197 
00203     protected function link( User $user ) {
00204         if ( $this->canShowRealUserName() && !$user->isAnon() ) {
00205             $real = $user->getRealName();
00206         } else {
00207             $real = false;
00208         }
00209 
00210         $page = $user->isAnon()
00211             ? SpecialPage::getTitleFor( 'Contributions', $user->getName() )
00212             : $user->getUserPage();
00213 
00214         return Linker::link( $page, htmlspecialchars( $real ? $real : $user->getName() ) );
00215     }
00216 
00222     protected function userLink( User $user ) {
00223         $link = $this->link( $user );
00224         if ( $user->isAnon() ) {
00225             return $this->msg( 'anonuser' )->rawParams( $link )->parse();
00226         } else {
00227             if ( $this->canShowRealUserName() && $user->getRealName() ) {
00228                 return $link;
00229             } else {
00230                 return $this->msg( 'siteuser' )->rawParams( $link )->params( $user->getName() )->escaped();
00231             }
00232         }
00233     }
00234 
00239     protected function othersLink() {
00240         return Linker::linkKnown(
00241             $this->getTitle(),
00242             $this->msg( 'others' )->escaped(),
00243             array(),
00244             array( 'action' => 'credits' )
00245         );
00246     }
00247 }