Merge "Set initial language and variant user preferences to user's preferred variant...
[lhc/web/wiklou.git] / includes / actions / CreditsAction.php
1 <?php
2 /**
3 * Formats credits for articles
4 *
5 * Copyright 2004, Evan Prodromou <evan@wikitravel.org>.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 *
21 * @file
22 * @ingroup Actions
23 * @author <evan@wikitravel.org>
24 */
25
26 /**
27 * @ingroup Actions
28 */
29 class CreditsAction extends FormlessAction {
30
31 public function getName() {
32 return 'credits';
33 }
34
35 protected function getDescription() {
36 return $this->msg( 'creditspage' )->escaped();
37 }
38
39 /**
40 * This is largely cadged from PageHistory::history
41 *
42 * @return String HTML
43 */
44 public function onView() {
45 wfProfileIn( __METHOD__ );
46
47 if ( $this->page->getID() == 0 ) {
48 $s = $this->msg( 'nocredits' )->parse();
49 } else {
50 $s = $this->getCredits( -1 );
51 }
52
53 wfProfileOut( __METHOD__ );
54
55 return Html::rawElement( 'div', array( 'id' => 'mw-credits' ), $s );
56 }
57
58 /**
59 * Get a list of contributors
60 *
61 * @param int $cnt maximum list of contributors to show
62 * @param bool $showIfMax whether to contributors if there more than $cnt
63 * @return String: html
64 */
65 public function getCredits( $cnt, $showIfMax = true ) {
66 wfProfileIn( __METHOD__ );
67 $s = '';
68
69 if ( $cnt != 0 ) {
70 $s = $this->getAuthor( $this->page );
71 if ( $cnt > 1 || $cnt < 0 ) {
72 $s .= ' ' . $this->getContributors( $cnt - 1, $showIfMax );
73 }
74 }
75
76 wfProfileOut( __METHOD__ );
77 return $s;
78 }
79
80 /**
81 * Get the last author with the last modification time
82 * @param $article Article object
83 * @return String HTML
84 */
85 protected function getAuthor( Page $article ) {
86 $user = User::newFromName( $article->getUserText(), false );
87
88 $timestamp = $article->getTimestamp();
89 if ( $timestamp ) {
90 $lang = $this->getLanguage();
91 $d = $lang->date( $article->getTimestamp(), true );
92 $t = $lang->time( $article->getTimestamp(), true );
93 } else {
94 $d = '';
95 $t = '';
96 }
97 return $this->msg( 'lastmodifiedatby', $d, $t )->rawParams(
98 $this->userLink( $user ) )->params( $user->getName() )->escaped();
99 }
100
101 /**
102 * Get a list of contributors of $article
103 * @param int $cnt maximum list of contributors to show
104 * @param bool $showIfMax whether to contributors if there more than $cnt
105 * @return String: html
106 */
107 protected function getContributors( $cnt, $showIfMax ) {
108 global $wgHiddenPrefs;
109
110 $contributors = $this->page->getContributors();
111
112 $others_link = false;
113
114 # Hmm... too many to fit!
115 if ( $cnt > 0 && $contributors->count() > $cnt ) {
116 $others_link = $this->othersLink();
117 if ( !$showIfMax )
118 return $this->msg( 'othercontribs' )->rawParams(
119 $others_link )->params( $contributors->count() )->escaped();
120 }
121
122 $real_names = array();
123 $user_names = array();
124 $anon_ips = array();
125
126 # Sift for real versus user names
127 foreach ( $contributors as $user ) {
128 $cnt--;
129 if ( $user->isLoggedIn() ) {
130 $link = $this->link( $user );
131 if ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
132 $real_names[] = $link;
133 } else {
134 $user_names[] = $link;
135 }
136 } else {
137 $anon_ips[] = $this->link( $user );
138 }
139
140 if ( $cnt == 0 ) {
141 break;
142 }
143 }
144
145 $lang = $this->getLanguage();
146
147 if ( count( $real_names ) ) {
148 $real = $lang->listToText( $real_names );
149 } else {
150 $real = false;
151 }
152
153 # "ThisSite user(s) A, B and C"
154 if ( count( $user_names ) ) {
155 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
156 count( $user_names ) )->escaped();
157 } else {
158 $user = false;
159 }
160
161 if ( count( $anon_ips ) ) {
162 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
163 count( $anon_ips ) )->escaped();
164 } else {
165 $anon = false;
166 }
167
168 # This is the big list, all mooshed together. We sift for blank strings
169 $fulllist = array();
170 foreach ( array( $real, $user, $anon, $others_link ) as $s ) {
171 if ( $s !== false ) {
172 array_push( $fulllist, $s );
173 }
174 }
175
176 $count = count( $fulllist );
177 # "Based on work by ..."
178 return $count
179 ? $this->msg( 'othercontribs' )->rawParams(
180 $lang->listToText( $fulllist ) )->params( $count )->escaped()
181 : '';
182 }
183
184 /**
185 * Get a link to $user's user page
186 * @param $user User object
187 * @return String: html
188 */
189 protected function link( User $user ) {
190 global $wgHiddenPrefs;
191 if ( !in_array( 'realname', $wgHiddenPrefs ) && !$user->isAnon() ) {
192 $real = $user->getRealName();
193 } else {
194 $real = false;
195 }
196
197 $page = $user->isAnon()
198 ? SpecialPage::getTitleFor( 'Contributions', $user->getName() )
199 : $user->getUserPage();
200
201 return Linker::link( $page, htmlspecialchars( $real ? $real : $user->getName() ) );
202 }
203
204 /**
205 * Get a link to $user's user page
206 * @param $user User object
207 * @return String: html
208 */
209 protected function userLink( User $user ) {
210 $link = $this->link( $user );
211 if ( $user->isAnon() ) {
212 return $this->msg( 'anonuser' )->rawParams( $link )->parse();
213 } else {
214 global $wgHiddenPrefs;
215 if ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
216 return $link;
217 } else {
218 return $this->msg( 'siteuser' )->rawParams( $link )->params( $user->getName() )->escaped();
219 }
220 }
221 }
222
223 /**
224 * Get a link to action=credits of $article page
225 * @return String: HTML link
226 */
227 protected function othersLink() {
228 return Linker::linkKnown(
229 $this->getTitle(),
230 $this->msg( 'others' )->escaped(),
231 array(),
232 array( 'action' => 'credits' )
233 );
234 }
235 }