MediaWiki  REL1_23
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 getContributors( $cnt, $showIfMax ) {
00110         global $wgHiddenPrefs;
00111 
00112         $contributors = $this->page->getContributors();
00113 
00114         $others_link = false;
00115 
00116         # Hmm... too many to fit!
00117         if ( $cnt > 0 && $contributors->count() > $cnt ) {
00118             $others_link = $this->othersLink();
00119             if ( !$showIfMax ) {
00120                 return $this->msg( 'othercontribs' )->rawParams(
00121                     $others_link )->params( $contributors->count() )->escaped();
00122             }
00123         }
00124 
00125         $real_names = array();
00126         $user_names = array();
00127         $anon_ips = array();
00128 
00129         # Sift for real versus user names
00130 
00131         foreach ( $contributors as $user ) {
00132             $cnt--;
00133             if ( $user->isLoggedIn() ) {
00134                 $link = $this->link( $user );
00135                 if ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
00136                     $real_names[] = $link;
00137                 } else {
00138                     $user_names[] = $link;
00139                 }
00140             } else {
00141                 $anon_ips[] = $this->link( $user );
00142             }
00143 
00144             if ( $cnt == 0 ) {
00145                 break;
00146             }
00147         }
00148 
00149         $lang = $this->getLanguage();
00150 
00151         if ( count( $real_names ) ) {
00152             $real = $lang->listToText( $real_names );
00153         } else {
00154             $real = false;
00155         }
00156 
00157         # "ThisSite user(s) A, B and C"
00158         if ( count( $user_names ) ) {
00159             $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
00160                 count( $user_names ) )->escaped();
00161         } else {
00162             $user = false;
00163         }
00164 
00165         if ( count( $anon_ips ) ) {
00166             $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
00167                 count( $anon_ips ) )->escaped();
00168         } else {
00169             $anon = false;
00170         }
00171 
00172         # This is the big list, all mooshed together. We sift for blank strings
00173         $fulllist = array();
00174         foreach ( array( $real, $user, $anon, $others_link ) as $s ) {
00175             if ( $s !== false ) {
00176                 array_push( $fulllist, $s );
00177             }
00178         }
00179 
00180         $count = count( $fulllist );
00181 
00182         # "Based on work by ..."
00183         return $count
00184             ? $this->msg( 'othercontribs' )->rawParams(
00185                 $lang->listToText( $fulllist ) )->params( $count )->escaped()
00186             : '';
00187     }
00188 
00194     protected function link( User $user ) {
00195         global $wgHiddenPrefs;
00196         if ( !in_array( 'realname', $wgHiddenPrefs ) && !$user->isAnon() ) {
00197             $real = $user->getRealName();
00198         } else {
00199             $real = false;
00200         }
00201 
00202         $page = $user->isAnon()
00203             ? SpecialPage::getTitleFor( 'Contributions', $user->getName() )
00204             : $user->getUserPage();
00205 
00206         return Linker::link( $page, htmlspecialchars( $real ? $real : $user->getName() ) );
00207     }
00208 
00214     protected function userLink( User $user ) {
00215         $link = $this->link( $user );
00216         if ( $user->isAnon() ) {
00217             return $this->msg( 'anonuser' )->rawParams( $link )->parse();
00218         } else {
00219             global $wgHiddenPrefs;
00220             if ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
00221                 return $link;
00222             } else {
00223                 return $this->msg( 'siteuser' )->rawParams( $link )->params( $user->getName() )->escaped();
00224             }
00225         }
00226     }
00227 
00232     protected function othersLink() {
00233         return Linker::linkKnown(
00234             $this->getTitle(),
00235             $this->msg( 'others' )->escaped(),
00236             array(),
00237             array( 'action' => 'credits' )
00238         );
00239     }
00240 }