MessageCache invalidation improvements
[mediawiki.git] / includes / actions / CreditsAction.php
blob1332ab489f92fb6b4c9f6455ed0eb202ee96f677
1 <?php
2 /**
3 * Formats credits for articles
5 * Copyright 2004, Evan Prodromou <evan@wikitravel.org>.
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.
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.
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
21 * @file
22 * @ingroup Actions
23 * @author <evan@wikitravel.org>
26 /**
27 * @ingroup Actions
29 class CreditsAction extends FormlessAction {
31 public function getName() {
32 return 'credits';
35 protected function getDescription() {
36 return $this->msg( 'creditspage' )->escaped();
39 /**
40 * This is largely cadged from PageHistory::history
42 * @return string HTML
44 public function onView() {
46 if ( $this->page->getID() == 0 ) {
47 $s = $this->msg( 'nocredits' )->parse();
48 } else {
49 $s = $this->getCredits( -1 );
52 return Html::rawElement( 'div', [ 'id' => 'mw-credits' ], $s );
55 /**
56 * Get a list of contributors
58 * @param int $cnt Maximum list of contributors to show
59 * @param bool $showIfMax Whether to contributors if there more than $cnt
60 * @return string Html
62 public function getCredits( $cnt, $showIfMax = true ) {
63 $s = '';
65 if ( $cnt != 0 ) {
66 $s = $this->getAuthor( $this->page );
67 if ( $cnt > 1 || $cnt < 0 ) {
68 $s .= ' ' . $this->getContributors( $cnt - 1, $showIfMax );
72 return $s;
75 /**
76 * Get the last author with the last modification time
77 * @param Page $page
78 * @return string HTML
80 protected function getAuthor( Page $page ) {
81 $user = User::newFromName( $page->getUserText(), false );
83 $timestamp = $page->getTimestamp();
84 if ( $timestamp ) {
85 $lang = $this->getLanguage();
86 $d = $lang->date( $page->getTimestamp(), true );
87 $t = $lang->time( $page->getTimestamp(), true );
88 } else {
89 $d = '';
90 $t = '';
93 return $this->msg( 'lastmodifiedatby', $d, $t )->rawParams(
94 $this->userLink( $user ) )->params( $user->getName() )->escaped();
97 /**
98 * Whether we can display the user's real name (not a hidden pref)
100 * @since 1.24
101 * @return bool
103 protected function canShowRealUserName() {
104 $hiddenPrefs = $this->context->getConfig()->get( 'HiddenPrefs' );
105 return !in_array( 'realname', $hiddenPrefs );
109 * Get a list of contributors of $article
110 * @param int $cnt Maximum list of contributors to show
111 * @param bool $showIfMax Whether to contributors if there more than $cnt
112 * @return string Html
114 protected function getContributors( $cnt, $showIfMax ) {
115 $contributors = $this->page->getContributors();
117 $others_link = false;
119 # Hmm... too many to fit!
120 if ( $cnt > 0 && $contributors->count() > $cnt ) {
121 $others_link = $this->othersLink();
122 if ( !$showIfMax ) {
123 return $this->msg( 'othercontribs' )->rawParams(
124 $others_link )->params( $contributors->count() )->escaped();
128 $real_names = [];
129 $user_names = [];
130 $anon_ips = [];
132 # Sift for real versus user names
133 /** @var $user User */
134 foreach ( $contributors as $user ) {
135 $cnt--;
136 if ( $user->isLoggedIn() ) {
137 $link = $this->link( $user );
138 if ( $this->canShowRealUserName() && $user->getRealName() ) {
139 $real_names[] = $link;
140 } else {
141 $user_names[] = $link;
143 } else {
144 $anon_ips[] = $this->link( $user );
147 if ( $cnt == 0 ) {
148 break;
152 $lang = $this->getLanguage();
154 if ( count( $real_names ) ) {
155 $real = $lang->listToText( $real_names );
156 } else {
157 $real = false;
160 # "ThisSite user(s) A, B and C"
161 if ( count( $user_names ) ) {
162 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
163 count( $user_names ) )->escaped();
164 } else {
165 $user = false;
168 if ( count( $anon_ips ) ) {
169 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
170 count( $anon_ips ) )->escaped();
171 } else {
172 $anon = false;
175 # This is the big list, all mooshed together. We sift for blank strings
176 $fulllist = [];
177 foreach ( [ $real, $user, $anon, $others_link ] as $s ) {
178 if ( $s !== false ) {
179 array_push( $fulllist, $s );
183 $count = count( $fulllist );
185 # "Based on work by ..."
186 return $count
187 ? $this->msg( 'othercontribs' )->rawParams(
188 $lang->listToText( $fulllist ) )->params( $count )->escaped()
189 : '';
193 * Get a link to $user's user page
194 * @param User $user
195 * @return string Html
197 protected function link( User $user ) {
198 if ( $this->canShowRealUserName() && !$user->isAnon() ) {
199 $real = $user->getRealName();
200 } else {
201 $real = false;
204 $page = $user->isAnon()
205 ? SpecialPage::getTitleFor( 'Contributions', $user->getName() )
206 : $user->getUserPage();
208 return Linker::link( $page, htmlspecialchars( $real ? $real : $user->getName() ) );
212 * Get a link to $user's user page
213 * @param User $user
214 * @return string Html
216 protected function userLink( User $user ) {
217 $link = $this->link( $user );
218 if ( $user->isAnon() ) {
219 return $this->msg( 'anonuser' )->rawParams( $link )->parse();
220 } else {
221 if ( $this->canShowRealUserName() && $user->getRealName() ) {
222 return $link;
223 } else {
224 return $this->msg( 'siteuser' )->rawParams( $link )->params( $user->getName() )->escaped();
230 * Get a link to action=credits of $article page
231 * @return string HTML link
233 protected function othersLink() {
234 return Linker::linkKnown(
235 $this->getTitle(),
236 $this->msg( 'others' )->escaped(),
238 [ 'action' => 'credits' ]