(bug 40820) Revert my changes to includes/actions/CreditsAction.php
[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 class CreditsAction extends FormlessAction {
27
28 public function getName() {
29 return 'credits';
30 }
31
32 protected function getDescription() {
33 return $this->msg( 'creditspage' )->escaped();
34 }
35
36 /**
37 * This is largely cadged from PageHistory::history
38 *
39 * @return String HTML
40 */
41 public function onView() {
42 wfProfileIn( __METHOD__ );
43
44 if ( $this->page->getID() == 0 ) {
45 $s = $this->msg( 'nocredits' )->parse();
46 } else {
47 $s = $this->getCredits( -1 );
48 }
49
50 wfProfileOut( __METHOD__ );
51
52 return Html::rawElement( 'div', array( 'id' => 'mw-credits' ), $s );
53 }
54
55 /**
56 * Get a list of contributors
57 *
58 * @param $cnt Int: maximum list of contributors to show
59 * @param $showIfMax Bool: whether to contributors if there more than $cnt
60 * @return String: html
61 */
62 public function getCredits( $cnt, $showIfMax = true ) {
63 wfProfileIn( __METHOD__ );
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 wfProfileOut( __METHOD__ );
74 return $s;
75 }
76
77 /**
78 * Get the last author with the last modification time
79 * @param $article Article object
80 * @return String HTML
81 */
82 protected function getAuthor( Page $article ) {
83 $user = User::newFromName( $article->getUserText(), false );
84
85 $timestamp = $article->getTimestamp();
86 if ( $timestamp ) {
87 $lang = $this->getLanguage();
88 $d = $lang->date( $article->getTimestamp(), true );
89 $t = $lang->time( $article->getTimestamp(), true );
90 } else {
91 $d = '';
92 $t = '';
93 }
94 return $this->msg( 'lastmodifiedatby', $d, $t )->rawParams(
95 $this->userLink( $user ) )->params( $user->getName() )->escaped();
96 }
97
98 /**
99 * Get a list of contributors of $article
100 * @param $cnt Int: maximum list of contributors to show
101 * @param $showIfMax Bool: whether to contributors if there more than $cnt
102 * @return String: html
103 */
104 protected function getContributors( $cnt, $showIfMax ) {
105 global $wgHiddenPrefs;
106
107 $contributors = $this->page->getContributors();
108
109 $others_link = false;
110
111 # Hmm... too many to fit!
112 if ( $cnt > 0 && $contributors->count() > $cnt ) {
113 $others_link = $this->othersLink();
114 if ( !$showIfMax )
115 return $this->msg( 'othercontribs' )->rawParams(
116 $others_link )->params( $contributors->count() )->escaped();
117 }
118
119 $real_names = array();
120 $user_names = array();
121 $anon_ips = array();
122
123 # Sift for real versus user names
124 foreach ( $contributors as $user ) {
125 $cnt--;
126 if ( $user->isLoggedIn() ) {
127 $link = $this->link( $user );
128 if ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
129 $real_names[] = $link;
130 } else {
131 $user_names[] = $link;
132 }
133 } else {
134 $anon_ips[] = $this->link( $user );
135 }
136
137 if ( $cnt == 0 ) {
138 break;
139 }
140 }
141
142 $lang = $this->getLanguage();
143
144 if ( count( $real_names ) ) {
145 $real = $lang->listToText( $real_names );
146 } else {
147 $real = false;
148 }
149
150 # "ThisSite user(s) A, B and C"
151 if ( count( $user_names ) ) {
152 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
153 count( $user_names ) )->escaped();
154 } else {
155 $user = false;
156 }
157
158 if ( count( $anon_ips ) ) {
159 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
160 count( $anon_ips ) )->escaped();
161 } else {
162 $anon = false;
163 }
164
165 # This is the big list, all mooshed together. We sift for blank strings
166 $fulllist = array();
167 foreach ( array( $real, $user, $anon, $others_link ) as $s ) {
168 if ( $s !== false ) {
169 array_push( $fulllist, $s );
170 }
171 }
172
173 $count = count( $fulllist );
174 # "Based on work by ..."
175 return $count
176 ? $this->msg( 'othercontribs' )->rawParams(
177 $lang->listToText( $fulllist ) )->params( $count )->escaped()
178 : '';
179 }
180
181 /**
182 * Get a link to $user's user page
183 * @param $user User object
184 * @return String: html
185 */
186 protected function link( User $user ) {
187 global $wgHiddenPrefs;
188 if ( !in_array( 'realname', $wgHiddenPrefs ) && !$user->isAnon() ) {
189 $real = $user->getRealName();
190 } else {
191 $real = false;
192 }
193
194 $page = $user->isAnon()
195 ? SpecialPage::getTitleFor( 'Contributions', $user->getName() )
196 : $user->getUserPage();
197
198 return Linker::link( $page, htmlspecialchars( $real ? $real : $user->getName() ) );
199 }
200
201 /**
202 * Get a link to $user's user page
203 * @param $user User object
204 * @return String: html
205 */
206 protected function userLink( User $user ) {
207 $link = $this->link( $user );
208 if ( $user->isAnon() ) {
209 return $this->msg( 'anonuser' )->rawParams( $link )->parse();
210 } else {
211 global $wgHiddenPrefs;
212 if ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
213 return $link;
214 } else {
215 return $this->msg( 'siteuser' )->rawParams( $link )->params( $user->getName() )->escaped();
216 }
217 }
218 }
219
220 /**
221 * Get a link to action=credits of $article page
222 * @return String: HTML link
223 */
224 protected function othersLink() {
225 return Linker::linkKnown(
226 $this->getTitle(),
227 $this->msg( 'others' )->escaped(),
228 array(),
229 array( 'action' => 'credits' )
230 );
231 }
232 }