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