]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/Credits.php
MediaWiki 1.15.0
[autoinstalls/mediawiki.git] / includes / Credits.php
1 <?php
2 /**
3  * Credits.php -- formats credits for articles
4  * Copyright 2004, Evan Prodromou <evan@wikitravel.org>.
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * @author <evan@wikitravel.org>
21  */
22
23 class Credits {
24
25         /**
26          * This is largely cadged from PageHistory::history
27          * @param $article Article object
28          */
29         public static function showPage( Article $article ) {
30                 global $wgOut;
31         
32                 wfProfileIn( __METHOD__ );
33         
34                 $wgOut->setPageTitle( $article->mTitle->getPrefixedText() );
35                 $wgOut->setSubtitle( wfMsg( 'creditspage' ) );
36                 $wgOut->setArticleFlag( false );
37                 $wgOut->setArticleRelated( true );
38                 $wgOut->setRobotPolicy( 'noindex,nofollow' );
39         
40                 if( $article->mTitle->getArticleID() == 0 ) {
41                         $s = wfMsg( 'nocredits' );
42                 } else {
43                         $s = self::getCredits($article, -1 );
44                 }
45         
46                 $wgOut->addHTML( $s );
47         
48                 wfProfileOut( __METHOD__ );
49         }
50
51         /**
52          * Get a list of contributors of $article
53          * @param $article Article object
54          * @param $cnt Int: maximum list of contributors to show
55          * @param $showIfMax Bool: whether to contributors if there more than $cnt
56          * @return String: html
57          */
58         public static function getCredits($article, $cnt, $showIfMax=true) {
59                 wfProfileIn( __METHOD__ );
60                 $s = '';
61
62                 if( isset( $cnt ) && $cnt != 0 ){
63                         $s = self::getAuthor( $article );
64                         if ($cnt > 1 || $cnt < 0) {
65                                 $s .= ' ' . self::getContributors( $article, $cnt - 1, $showIfMax );
66                         }
67                 }
68
69                 wfProfileOut( __METHOD__ );
70                 return $s;
71         }
72
73         /**
74          * Get the last author with the last modification time
75          * @param $article Article object
76          */
77         protected static function getAuthor( Article $article ){
78                 global $wgLang, $wgAllowRealName;
79
80                 $user = User::newFromId( $article->getUser() );
81
82                 $timestamp = $article->getTimestamp();
83                 if( $timestamp ){
84                         $d = $wgLang->date( $article->getTimestamp(), true );
85                         $t = $wgLang->time( $article->getTimestamp(), true );
86                 } else {
87                         $d = '';
88                         $t = '';
89                 }
90                 return wfMsg( 'lastmodifiedatby', $d, $t, self::userLink( $user ) );
91         }
92
93         /**
94          * Get a list of contributors of $article
95          * @param $article Article object
96          * @param $cnt Int: maximum list of contributors to show
97          * @param $showIfMax Bool: whether to contributors if there more than $cnt
98          * @return String: html
99          */
100         protected static function getContributors( Article $article, $cnt, $showIfMax ) {
101                 global $wgLang, $wgAllowRealName;
102         
103                 $contributors = $article->getContributors();
104         
105                 $others_link = '';
106         
107                 # Hmm... too many to fit!
108                 if( $cnt > 0 && $contributors->count() > $cnt ){
109                         $others_link = self::othersLink( $article );
110                         if( !$showIfMax )
111                                 return wfMsg( 'othercontribs', $others_link );
112                 }
113         
114                 $real_names = array();
115                 $user_names = array();
116                 $anon = 0;
117         
118                 # Sift for real versus user names
119                 foreach( $contributors as $user ) {
120                         $cnt--;
121                         if( $user->isLoggedIn() ){
122                                 $link = self::link( $user );
123                                 if( $wgAllowRealName && $user->getRealName() )
124                                         $real_names[] = $link;
125                                 else
126                                         $user_names[] = $link;
127                         } else {
128                                 $anon++;
129                         }
130                         if( $cnt == 0 ) break;
131                 }
132         
133                 # Two strings: real names, and user names
134                 $real = $wgLang->listToText( $real_names );
135                 $user = $wgLang->listToText( $user_names );
136                 if( $anon )
137                         $anon = wfMsgExt( 'anonymous', array( 'parseinline' ), $anon );
138         
139                 # "ThisSite user(s) A, B and C"
140                 if( !empty( $user ) ){
141                         $user = wfMsgExt( 'siteusers', array( 'parsemag' ), $user, count( $user_names ) );
142                 }
143         
144                 # This is the big list, all mooshed together. We sift for blank strings
145                 $fulllist = array();
146                 foreach( array( $real, $user, $anon, $others_link ) as $s ){
147                         if( !empty( $s ) ){
148                                 array_push( $fulllist, $s );
149                         }
150                 }
151
152                 # Make the list into text...
153                 $creds = $wgLang->listToText( $fulllist );
154
155                 # "Based on work by ..."
156                 return empty( $creds ) ? '' : wfMsg( 'othercontribs', $creds );
157         }
158
159         /**
160          * Get a link to $user_name page
161          * @param $user User object
162          * @return String: html
163          */
164         protected static function link( User $user ) {
165                 global $wgUser, $wgAllowRealName;
166                 if( $wgAllowRealName )
167                         $real = $user->getRealName();
168                 else
169                         $real = false;
170
171                 $skin = $wgUser->getSkin();
172                 $page = $user->getUserPage();
173                         
174                 return $skin->link( $page, htmlspecialchars( $real ? $real : $user->getName() ) );
175         }
176
177         /**
178          * Get a link to $user_name page
179          * @param $user_name String: user name
180          * @param $linkText String: optional display
181          * @return String: html
182          */
183         protected static function userLink( User $user ) {
184                 global $wgUser, $wgAllowRealName;
185                 if( $user->isAnon() ){
186                         return wfMsgExt( 'anonymous', array( 'parseinline' ), 1 );
187                 } else {
188                         $link = self::link( $user );
189                         if( $wgAllowRealName && $user->getRealName() )
190                                 return $link;
191                         else 
192                                 return wfMsgExt( 'siteuser', array( 'parseinline', 'replaceafter' ), $link );
193                 }
194         }
195
196         /**
197          * Get a link to action=credits of $article page
198          * @param $article Article object
199          * @return String: html
200          */
201         protected static function othersLink( Article $article ) {
202                 global $wgUser;
203                 $skin = $wgUser->getSkin();
204                 return $skin->link( $article->getTitle(), wfMsgHtml( 'others' ), array(), array( 'action' => 'credits' ), array( 'known' ) );
205         }
206 }