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