(bug 12205) Bidirectional names in action=credits are split and displayed incorrectly...
[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 = wfMsg( 'nocredits' );
50 } else {
51 $s = $this->getCredits( -1 );
52 }
53
54 wfProfileOut( __METHOD__ );
55
56 return $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 ( isset( $cnt ) && $cnt != 0 ) {
71 $s = self::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 static function getAuthor( Page $article ) {
87 global $wgLang;
88
89 $user = User::newFromId( $article->getUser() );
90
91 $timestamp = $article->getTimestamp();
92 if ( $timestamp ) {
93 $d = $wgLang->date( $article->getTimestamp(), true );
94 $t = $wgLang->time( $article->getTimestamp(), true );
95 } else {
96 $d = '';
97 $t = '';
98 }
99 return wfMessage( 'lastmodifiedatby', $d, $t )->rawParams( self::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 $wgLang, $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 wfMessage( 'othercontribs' )->rawParams( $others_link )->params( $contributors->count() )->escaped();
120 }
121
122 $real_names = array();
123 $user_names = array();
124 $anon_ips = array();
125
126 # Sift for real versus user names
127 foreach ( $contributors as $user ) {
128 $cnt--;
129 if ( $user->isLoggedIn() ) {
130 $link = self::link( $user );
131 if ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
132 $real_names[] = $link;
133 } else {
134 $user_names[] = $link;
135 }
136 } else {
137 $anon_ips[] = self::link( $user );
138 }
139
140 if ( $cnt == 0 ) {
141 break;
142 }
143 }
144
145 if ( count( $real_names ) ) {
146 $real = $wgLang->listToText( $real_names );
147 } else {
148 $real = false;
149 }
150
151 # "ThisSite user(s) A, B and C"
152 if ( count( $user_names ) ) {
153 $user = wfMessage( 'siteusers' )->rawParams( $wgLang->listToText( $user_names ) )->params(
154 count( $user_names ) )->escaped();
155 } else {
156 $user = false;
157 }
158
159 if ( count( $anon_ips ) ) {
160 $anon = wfMessage( 'anonusers' )->rawParams( $wgLang->listToText( $anon_ips ) )->params(
161 count( $anon_ips ) )->escaped();
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 !== false ) {
170 array_push( $fulllist, $s );
171 }
172 }
173
174 $count = count( $fulllist );
175 # "Based on work by ..."
176 return $count
177 ? wfMessage( 'othercontribs' )->rawParams(
178 $wgLang->listToText( $fulllist ) )->params( $count )->escaped()
179 : '';
180 }
181
182 /**
183 * Get a link to $user's user page
184 * @param $user User object
185 * @return String: html
186 */
187 protected static function link( User $user ) {
188 global $wgHiddenPrefs;
189 if ( !in_array( 'realname', $wgHiddenPrefs ) && !$user->isAnon() ) {
190 $real = $user->getRealName();
191 } else {
192 $real = false;
193 }
194
195 $page = $user->isAnon()
196 ? SpecialPage::getTitleFor( 'Contributions', $user->getName() )
197 : $user->getUserPage();
198
199 return Html::rawElement( 'span',
200 array( 'class' => 'mw-link-nowrap' ),
201 Linker::link( $page, htmlspecialchars( $real ? $real : $user->getName() ) )
202 );
203 }
204
205 /**
206 * Get a link to $user's user page
207 * @param $user User object
208 * @return String: html
209 */
210 protected static function userLink( User $user ) {
211 $link = self::link( $user );
212 if ( $user->isAnon() ) {
213 return wfMsgExt( 'anonuser', array( 'parseinline', 'replaceafter' ), $link );
214 } else {
215 global $wgHiddenPrefs;
216 if ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
217 return $link;
218 } else {
219 return wfMessage( '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::link(
231 $this->getTitle(),
232 wfMsgHtml( 'others' ),
233 array(),
234 array( 'action' => 'credits' ),
235 array( 'known' )
236 );
237 }
238 }