Merge "Change loading order of Chinese conversion tables"
[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
46 if ( $this->page->getID() == 0 ) {
47 $s = $this->msg( 'nocredits' )->parse();
48 } else {
49 $s = $this->getCredits( -1 );
50 }
51
52
53 return Html::rawElement( 'div', array( 'id' => 'mw-credits' ), $s );
54 }
55
56 /**
57 * Get a list of contributors
58 *
59 * @param int $cnt Maximum list of contributors to show
60 * @param bool $showIfMax Whether to contributors if there more than $cnt
61 * @return string Html
62 */
63 public function getCredits( $cnt, $showIfMax = true ) {
64 $s = '';
65
66 if ( $cnt != 0 ) {
67 $s = $this->getAuthor( $this->page );
68 if ( $cnt > 1 || $cnt < 0 ) {
69 $s .= ' ' . $this->getContributors( $cnt - 1, $showIfMax );
70 }
71 }
72
73
74 return $s;
75 }
76
77 /**
78 * Get the last author with the last modification time
79 * @param Page $page
80 * @return string HTML
81 */
82 protected function getAuthor( Page $page ) {
83 $user = User::newFromName( $page->getUserText(), false );
84
85 $timestamp = $page->getTimestamp();
86 if ( $timestamp ) {
87 $lang = $this->getLanguage();
88 $d = $lang->date( $page->getTimestamp(), true );
89 $t = $lang->time( $page->getTimestamp(), true );
90 } else {
91 $d = '';
92 $t = '';
93 }
94
95 return $this->msg( 'lastmodifiedatby', $d, $t )->rawParams(
96 $this->userLink( $user ) )->params( $user->getName() )->escaped();
97 }
98
99 /**
100 * Whether we can display the user's real name (not a hidden pref)
101 *
102 * @since 1.24
103 * @return bool
104 */
105 protected function canShowRealUserName() {
106 $hiddenPrefs = $this->context->getConfig()->get( 'HiddenPrefs' );
107 return !in_array( 'realname', $hiddenPrefs );
108 }
109
110 /**
111 * Get a list of contributors of $article
112 * @param int $cnt Maximum list of contributors to show
113 * @param bool $showIfMax Whether to contributors if there more than $cnt
114 * @return string Html
115 */
116 protected function getContributors( $cnt, $showIfMax ) {
117 $contributors = $this->page->getContributors();
118
119 $others_link = false;
120
121 # Hmm... too many to fit!
122 if ( $cnt > 0 && $contributors->count() > $cnt ) {
123 $others_link = $this->othersLink();
124 if ( !$showIfMax ) {
125 return $this->msg( 'othercontribs' )->rawParams(
126 $others_link )->params( $contributors->count() )->escaped();
127 }
128 }
129
130 $real_names = array();
131 $user_names = array();
132 $anon_ips = array();
133
134 # Sift for real versus user names
135 /** @var $user User */
136 foreach ( $contributors as $user ) {
137 $cnt--;
138 if ( $user->isLoggedIn() ) {
139 $link = $this->link( $user );
140 if ( $this->canShowRealUserName() && $user->getRealName() ) {
141 $real_names[] = $link;
142 } else {
143 $user_names[] = $link;
144 }
145 } else {
146 $anon_ips[] = $this->link( $user );
147 }
148
149 if ( $cnt == 0 ) {
150 break;
151 }
152 }
153
154 $lang = $this->getLanguage();
155
156 if ( count( $real_names ) ) {
157 $real = $lang->listToText( $real_names );
158 } else {
159 $real = false;
160 }
161
162 # "ThisSite user(s) A, B and C"
163 if ( count( $user_names ) ) {
164 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
165 count( $user_names ) )->escaped();
166 } else {
167 $user = false;
168 }
169
170 if ( count( $anon_ips ) ) {
171 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
172 count( $anon_ips ) )->escaped();
173 } else {
174 $anon = false;
175 }
176
177 # This is the big list, all mooshed together. We sift for blank strings
178 $fulllist = array();
179 foreach ( array( $real, $user, $anon, $others_link ) as $s ) {
180 if ( $s !== false ) {
181 array_push( $fulllist, $s );
182 }
183 }
184
185 $count = count( $fulllist );
186
187 # "Based on work by ..."
188 return $count
189 ? $this->msg( 'othercontribs' )->rawParams(
190 $lang->listToText( $fulllist ) )->params( $count )->escaped()
191 : '';
192 }
193
194 /**
195 * Get a link to $user's user page
196 * @param User $user
197 * @return string Html
198 */
199 protected function link( User $user ) {
200 if ( $this->canShowRealUserName() && !$user->isAnon() ) {
201 $real = $user->getRealName();
202 } else {
203 $real = false;
204 }
205
206 $page = $user->isAnon()
207 ? SpecialPage::getTitleFor( 'Contributions', $user->getName() )
208 : $user->getUserPage();
209
210 return Linker::link( $page, htmlspecialchars( $real ? $real : $user->getName() ) );
211 }
212
213 /**
214 * Get a link to $user's user page
215 * @param User $user
216 * @return string Html
217 */
218 protected function userLink( User $user ) {
219 $link = $this->link( $user );
220 if ( $user->isAnon() ) {
221 return $this->msg( 'anonuser' )->rawParams( $link )->parse();
222 } else {
223 if ( $this->canShowRealUserName() && $user->getRealName() ) {
224 return $link;
225 } else {
226 return $this->msg( 'siteuser' )->rawParams( $link )->params( $user->getName() )->escaped();
227 }
228 }
229 }
230
231 /**
232 * Get a link to action=credits of $article page
233 * @return string HTML link
234 */
235 protected function othersLink() {
236 return Linker::linkKnown(
237 $this->getTitle(),
238 $this->msg( 'others' )->escaped(),
239 array(),
240 array( 'action' => 'credits' )
241 );
242 }
243 }