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