stylize.php, remove trailing whitespace, break a long line
[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 } else {
127 $anon_ips[] = self::link( $user );
128 }
129 if ( $cnt == 0 ) break;
130 }
131
132 if ( count( $real_names ) ) {
133 $real = $wgLang->listToText( $real_names );
134 } else {
135 $real = false;
136 }
137
138 # "ThisSite user(s) A, B and C"
139 if ( count( $user_names ) ) {
140 $user = wfMsgExt( 'siteusers', array( 'parsemag' ),
141 $wgLang->listToText( $user_names ), count( $user_names ) );
142 } else {
143 $user = false;
144 }
145
146 if ( count( $anon_ips ) ) {
147 $anon = wfMsgExt( 'anonusers', array( 'parsemag' ),
148 $wgLang->listToText( $anon_ips ), count( $anon_ips ) );
149 } else {
150 $anon = false;
151 }
152
153 # This is the big list, all mooshed together. We sift for blank strings
154 $fulllist = array();
155 foreach ( array( $real, $user, $anon, $others_link ) as $s ) {
156 if ( $s ) {
157 array_push( $fulllist, $s );
158 }
159 }
160
161 # Make the list into text...
162 $creds = $wgLang->listToText( $fulllist );
163
164 # "Based on work by ..."
165 return strlen( $creds )
166 ? wfMsgExt( 'othercontribs', 'parsemag', $creds, count( $fulllist ) )
167 : '';
168 }
169
170 /**
171 * Get a link to $user's user page
172 * @param $user User object
173 * @return String: html
174 */
175 protected static function link( User $user ) {
176 global $wgUser, $wgHiddenPrefs;
177 if ( !in_array( 'realname', $wgHiddenPrefs ) && !$user->isAnon() )
178 $real = $user->getRealName();
179 else
180 $real = false;
181
182 $skin = $wgUser->getSkin();
183 $page = $user->isAnon() ?
184 SpecialPage::getTitleFor( 'Contributions', $user->getName() ) :
185 $user->getUserPage();
186
187 return $skin->link( $page, htmlspecialchars( $real ? $real : $user->getName() ) );
188 }
189
190 /**
191 * Get a link to $user's user page
192 * @param $user User object
193 * @return String: html
194 */
195 protected static function userLink( User $user ) {
196 $link = self::link( $user );
197 if ( $user->isAnon() ) {
198 return wfMsgExt( 'anonuser', array( 'parseinline', 'replaceafter' ), $link );
199 } else {
200 global $wgHiddenPrefs;
201 if ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() )
202 return $link;
203 else
204 return wfMsgExt( 'siteuser', array( 'parseinline', 'replaceafter' ), $link );
205 }
206 }
207
208 /**
209 * Get a link to action=credits of $article page
210 * @param $article Article object
211 * @return String: html
212 */
213 protected static function othersLink( Article $article ) {
214 global $wgUser;
215 $skin = $wgUser->getSkin();
216 return $skin->link(
217 $article->getTitle(),
218 wfMsgHtml( 'others' ),
219 array(),
220 array( 'action' => 'credits' ),
221 array( 'known' )
222 );
223 }
224 }