Update the Chinese conversion tables.
[lhc/web/wiklou.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 * This is largely cadged from PageHistory::history
26 * @param $article Article object
27 */
28 public static function showPage( Article $article ) {
29 global $wgOut;
30
31 wfProfileIn( __METHOD__ );
32
33 $wgOut->setPageTitle( $article->mTitle->getPrefixedText() );
34 $wgOut->setSubtitle( wfMsg( 'creditspage' ) );
35 $wgOut->setArticleFlag( false );
36 $wgOut->setArticleRelated( true );
37 $wgOut->setRobotPolicy( 'noindex,nofollow' );
38
39 if ( $article->mTitle->getArticleID() == 0 ) {
40 $s = wfMsg( 'nocredits' );
41 } else {
42 $s = self::getCredits( $article, -1 );
43 }
44
45 $wgOut->addHTML( $s );
46
47 wfProfileOut( __METHOD__ );
48 }
49
50 /**
51 * Get a list of contributors of $article
52 * @param $article Article object
53 * @param $cnt Int: maximum list of contributors to show
54 * @param $showIfMax Bool: whether to contributors if there more than $cnt
55 * @return String: html
56 */
57 public static function getCredits( Article $article, $cnt, $showIfMax = true ) {
58 wfProfileIn( __METHOD__ );
59 $s = '';
60
61 if ( isset( $cnt ) && $cnt != 0 ) {
62 $s = self::getAuthor( $article );
63 if ( $cnt > 1 || $cnt < 0 ) {
64 $s .= ' ' . self::getContributors( $article, $cnt - 1, $showIfMax );
65 }
66 }
67
68 wfProfileOut( __METHOD__ );
69 return $s;
70 }
71
72 /**
73 * Get the last author with the last modification time
74 * @param $article Article object
75 */
76 protected static function getAuthor( Article $article ) {
77 global $wgLang;
78
79 $user = User::newFromId( $article->getUser() );
80
81 $timestamp = $article->getTimestamp();
82 if ( $timestamp ) {
83 $d = $wgLang->date( $article->getTimestamp(), true );
84 $t = $wgLang->time( $article->getTimestamp(), true );
85 } else {
86 $d = '';
87 $t = '';
88 }
89 return wfMsgExt( 'lastmodifiedatby', 'parsemag', $d, $t, self::userLink( $user ), $user->getName() );
90 }
91
92 /**
93 * Get a list of contributors of $article
94 * @param $article Article object
95 * @param $cnt Int: maximum list of contributors to show
96 * @param $showIfMax Bool: whether to contributors if there more than $cnt
97 * @return String: html
98 */
99 protected static function getContributors( Article $article, $cnt, $showIfMax ) {
100 global $wgLang, $wgHiddenPrefs;
101
102 $contributors = $article->getContributors();
103
104 $others_link = false;
105
106 # Hmm... too many to fit!
107 if ( $cnt > 0 && $contributors->count() > $cnt ) {
108 $others_link = self::othersLink( $article );
109 if ( !$showIfMax )
110 return wfMsgExt( 'othercontribs', 'parsemag', $others_link, $contributors->count() );
111 }
112
113 $real_names = array();
114 $user_names = array();
115 $anon_ips = array();
116
117 # Sift for real versus user names
118 foreach ( $contributors as $user ) {
119 $cnt--;
120 if ( $user->isLoggedIn() ) {
121 $link = self::link( $user );
122 if ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
123 $real_names[] = $link;
124 } else {
125 $user_names[] = $link;
126 }
127 } else {
128 $anon_ips[] = self::link( $user );
129 }
130
131 if ( $cnt == 0 ) {
132 break;
133 }
134 }
135
136 if ( count( $real_names ) ) {
137 $real = $wgLang->listToText( $real_names );
138 } else {
139 $real = false;
140 }
141
142 # "ThisSite user(s) A, B and C"
143 if ( count( $user_names ) ) {
144 $user = wfMsgExt(
145 'siteusers',
146 'parsemag',
147 $wgLang->listToText( $user_names ), count( $user_names )
148 );
149 } else {
150 $user = false;
151 }
152
153 if ( count( $anon_ips ) ) {
154 $anon = wfMsgExt(
155 'anonusers',
156 'parsemag',
157 $wgLang->listToText( $anon_ips ), count( $anon_ips )
158 );
159 } else {
160 $anon = false;
161 }
162
163 # This is the big list, all mooshed together. We sift for blank strings
164 $fulllist = array();
165 foreach ( array( $real, $user, $anon, $others_link ) as $s ) {
166 if ( $s ) {
167 array_push( $fulllist, $s );
168 }
169 }
170
171 # Make the list into text...
172 $creds = $wgLang->listToText( $fulllist );
173
174 # "Based on work by ..."
175 return strlen( $creds )
176 ? wfMsgExt( 'othercontribs', 'parsemag', $creds, count( $fulllist ) )
177 : '';
178 }
179
180 /**
181 * Get a link to $user's user page
182 * @param $user User object
183 * @return String: html
184 */
185 protected static function link( User $user ) {
186 global $wgUser, $wgHiddenPrefs;
187 if ( !in_array( 'realname', $wgHiddenPrefs ) && !$user->isAnon() ) {
188 $real = $user->getRealName();
189 } else {
190 $real = false;
191 }
192
193 $skin = $wgUser->getSkin();
194 $page = $user->isAnon() ?
195 SpecialPage::getTitleFor( 'Contributions', $user->getName() ) :
196 $user->getUserPage();
197
198 return $skin->link( $page, htmlspecialchars( $real ? $real : $user->getName() ) );
199 }
200
201 /**
202 * Get a link to $user's user page
203 * @param $user User object
204 * @return String: html
205 */
206 protected static function userLink( User $user ) {
207 $link = self::link( $user );
208 if ( $user->isAnon() ) {
209 return wfMsgExt( 'anonuser', array( 'parseinline', 'replaceafter' ), $link );
210 } else {
211 global $wgHiddenPrefs;
212 if ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
213 return $link;
214 } else {
215 return wfMsgExt( 'siteuser', 'parsemag', $link, $user->getName() );
216 }
217 }
218 }
219
220 /**
221 * Get a link to action=credits of $article page
222 * @param $article Article object
223 * @return String: html
224 */
225 protected static function othersLink( Article $article ) {
226 global $wgUser;
227 $skin = $wgUser->getSkin();
228 return $skin->link(
229 $article->getTitle(),
230 wfMsgHtml( 'others' ),
231 array(),
232 array( 'action' => 'credits' ),
233 array( 'known' )
234 );
235 }
236 }