Merge "Update formatting"
[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
78 return $s;
79 }
80
81 /**
82 * Get the last author with the last modification time
83 * @param Page $page
84 * @return String HTML
85 */
86 protected function getAuthor( Page $page ) {
87 $user = User::newFromName( $page->getUserText(), false );
88
89 $timestamp = $page->getTimestamp();
90 if ( $timestamp ) {
91 $lang = $this->getLanguage();
92 $d = $lang->date( $page->getTimestamp(), true );
93 $t = $lang->time( $page->getTimestamp(), true );
94 } else {
95 $d = '';
96 $t = '';
97 }
98
99 return $this->msg( 'lastmodifiedatby', $d, $t )->rawParams(
100 $this->userLink( $user ) )->params( $user->getName() )->escaped();
101 }
102
103 /**
104 * Get a list of contributors of $article
105 * @param int $cnt maximum list of contributors to show
106 * @param bool $showIfMax whether to contributors if there more than $cnt
107 * @return String: html
108 */
109 protected function getContributors( $cnt, $showIfMax ) {
110 global $wgHiddenPrefs;
111
112 $contributors = $this->page->getContributors();
113
114 $others_link = false;
115
116 # Hmm... too many to fit!
117 if ( $cnt > 0 && $contributors->count() > $cnt ) {
118 $others_link = $this->othersLink();
119 if ( !$showIfMax ) {
120 return $this->msg( 'othercontribs' )->rawParams(
121 $others_link )->params( $contributors->count() )->escaped();
122 }
123 }
124
125 $real_names = array();
126 $user_names = array();
127 $anon_ips = array();
128
129 # Sift for real versus user names
130 foreach ( $contributors as $user ) {
131 $cnt--;
132 if ( $user->isLoggedIn() ) {
133 $link = $this->link( $user );
134 if ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
135 $real_names[] = $link;
136 } else {
137 $user_names[] = $link;
138 }
139 } else {
140 $anon_ips[] = $this->link( $user );
141 }
142
143 if ( $cnt == 0 ) {
144 break;
145 }
146 }
147
148 $lang = $this->getLanguage();
149
150 if ( count( $real_names ) ) {
151 $real = $lang->listToText( $real_names );
152 } else {
153 $real = false;
154 }
155
156 # "ThisSite user(s) A, B and C"
157 if ( count( $user_names ) ) {
158 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
159 count( $user_names ) )->escaped();
160 } else {
161 $user = false;
162 }
163
164 if ( count( $anon_ips ) ) {
165 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
166 count( $anon_ips ) )->escaped();
167 } else {
168 $anon = false;
169 }
170
171 # This is the big list, all mooshed together. We sift for blank strings
172 $fulllist = array();
173 foreach ( array( $real, $user, $anon, $others_link ) as $s ) {
174 if ( $s !== false ) {
175 array_push( $fulllist, $s );
176 }
177 }
178
179 $count = count( $fulllist );
180
181 # "Based on work by ..."
182 return $count
183 ? $this->msg( 'othercontribs' )->rawParams(
184 $lang->listToText( $fulllist ) )->params( $count )->escaped()
185 : '';
186 }
187
188 /**
189 * Get a link to $user's user page
190 * @param $user User object
191 * @return String: html
192 */
193 protected function link( User $user ) {
194 global $wgHiddenPrefs;
195 if ( !in_array( 'realname', $wgHiddenPrefs ) && !$user->isAnon() ) {
196 $real = $user->getRealName();
197 } else {
198 $real = false;
199 }
200
201 $page = $user->isAnon()
202 ? SpecialPage::getTitleFor( 'Contributions', $user->getName() )
203 : $user->getUserPage();
204
205 return Linker::link( $page, htmlspecialchars( $real ? $real : $user->getName() ) );
206 }
207
208 /**
209 * Get a link to $user's user page
210 * @param $user User object
211 * @return String: html
212 */
213 protected function userLink( User $user ) {
214 $link = $this->link( $user );
215 if ( $user->isAnon() ) {
216 return $this->msg( 'anonuser' )->rawParams( $link )->parse();
217 } else {
218 global $wgHiddenPrefs;
219 if ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
220 return $link;
221 } else {
222 return $this->msg( 'siteuser' )->rawParams( $link )->params( $user->getName() )->escaped();
223 }
224 }
225 }
226
227 /**
228 * Get a link to action=credits of $article page
229 * @return String: HTML link
230 */
231 protected function othersLink() {
232 return Linker::linkKnown(
233 $this->getTitle(),
234 $this->msg( 'others' )->escaped(),
235 array(),
236 array( 'action' => 'credits' )
237 );
238 }
239 }