Merge "Allow to send the memory usage with UDP profiler."
[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 * Whether we can display the user's real name (not a hidden pref)
105 *
106 * @since 1.24
107 * @return bool
108 */
109 protected function canShowRealUserName() {
110 $hiddenPrefs = $this->context->getConfig()->get( 'HiddenPrefs' );
111 return !in_array( 'realname', $hiddenPrefs );
112 }
113
114 /**
115 * Get a list of contributors of $article
116 * @param int $cnt Maximum list of contributors to show
117 * @param bool $showIfMax Whether to contributors if there more than $cnt
118 * @return string Html
119 */
120 protected function getContributors( $cnt, $showIfMax ) {
121 $contributors = $this->page->getContributors();
122
123 $others_link = false;
124
125 # Hmm... too many to fit!
126 if ( $cnt > 0 && $contributors->count() > $cnt ) {
127 $others_link = $this->othersLink();
128 if ( !$showIfMax ) {
129 return $this->msg( 'othercontribs' )->rawParams(
130 $others_link )->params( $contributors->count() )->escaped();
131 }
132 }
133
134 $real_names = array();
135 $user_names = array();
136 $anon_ips = array();
137
138 # Sift for real versus user names
139 /** @var $user User */
140 foreach ( $contributors as $user ) {
141 $cnt--;
142 if ( $user->isLoggedIn() ) {
143 $link = $this->link( $user );
144 if ( $this->canShowRealUserName() && $user->getRealName() ) {
145 $real_names[] = $link;
146 } else {
147 $user_names[] = $link;
148 }
149 } else {
150 $anon_ips[] = $this->link( $user );
151 }
152
153 if ( $cnt == 0 ) {
154 break;
155 }
156 }
157
158 $lang = $this->getLanguage();
159
160 if ( count( $real_names ) ) {
161 $real = $lang->listToText( $real_names );
162 } else {
163 $real = false;
164 }
165
166 # "ThisSite user(s) A, B and C"
167 if ( count( $user_names ) ) {
168 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
169 count( $user_names ) )->escaped();
170 } else {
171 $user = false;
172 }
173
174 if ( count( $anon_ips ) ) {
175 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
176 count( $anon_ips ) )->escaped();
177 } else {
178 $anon = false;
179 }
180
181 # This is the big list, all mooshed together. We sift for blank strings
182 $fulllist = array();
183 foreach ( array( $real, $user, $anon, $others_link ) as $s ) {
184 if ( $s !== false ) {
185 array_push( $fulllist, $s );
186 }
187 }
188
189 $count = count( $fulllist );
190
191 # "Based on work by ..."
192 return $count
193 ? $this->msg( 'othercontribs' )->rawParams(
194 $lang->listToText( $fulllist ) )->params( $count )->escaped()
195 : '';
196 }
197
198 /**
199 * Get a link to $user's user page
200 * @param User $user
201 * @return string Html
202 */
203 protected function link( User $user ) {
204 if ( $this->canShowRealUserName() && !$user->isAnon() ) {
205 $real = $user->getRealName();
206 } else {
207 $real = false;
208 }
209
210 $page = $user->isAnon()
211 ? SpecialPage::getTitleFor( 'Contributions', $user->getName() )
212 : $user->getUserPage();
213
214 return Linker::link( $page, htmlspecialchars( $real ? $real : $user->getName() ) );
215 }
216
217 /**
218 * Get a link to $user's user page
219 * @param User $user
220 * @return string Html
221 */
222 protected function userLink( User $user ) {
223 $link = $this->link( $user );
224 if ( $user->isAnon() ) {
225 return $this->msg( 'anonuser' )->rawParams( $link )->parse();
226 } else {
227 if ( $this->canShowRealUserName() && $user->getRealName() ) {
228 return $link;
229 } else {
230 return $this->msg( 'siteuser' )->rawParams( $link )->params( $user->getName() )->escaped();
231 }
232 }
233 }
234
235 /**
236 * Get a link to action=credits of $article page
237 * @return string HTML link
238 */
239 protected function othersLink() {
240 return Linker::linkKnown(
241 $this->getTitle(),
242 $this->msg( 'others' )->escaped(),
243 array(),
244 array( 'action' => 'credits' )
245 );
246 }
247 }