Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / includes / actions / CreditsAction.php
blob803695a77f36db22f8db09a76d09b28ce3fad9e4
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 use MediaWiki\MediaWikiServices;
28 /**
29 * @ingroup Actions
31 class CreditsAction extends FormlessAction {
33 public function getName() {
34 return 'credits';
37 protected function getDescription() {
38 return $this->msg( 'creditspage' )->escaped();
41 /**
42 * This is largely cadged from PageHistory::history
44 * @return string HTML
46 public function onView() {
48 if ( $this->page->getID() == 0 ) {
49 $s = $this->msg( 'nocredits' )->parse();
50 } else {
51 $s = $this->getCredits( -1 );
54 return Html::rawElement( 'div', [ 'id' => 'mw-credits' ], $s );
57 /**
58 * Get a list of contributors
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
64 public function getCredits( $cnt, $showIfMax = true ) {
65 $s = '';
67 if ( $cnt != 0 ) {
68 $s = $this->getAuthor( $this->page );
69 if ( $cnt > 1 || $cnt < 0 ) {
70 $s .= ' ' . $this->getContributors( $cnt - 1, $showIfMax );
74 return $s;
77 /**
78 * Get the last author with the last modification time
79 * @param Page $page
80 * @return string HTML
82 protected function getAuthor( Page $page ) {
83 $user = User::newFromName( $page->getUserText(), false );
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 = '';
95 return $this->msg( 'lastmodifiedatby', $d, $t )->rawParams(
96 $this->userLink( $user ) )->params( $user->getName() )->escaped();
99 /**
100 * Whether we can display the user's real name (not a hidden pref)
102 * @since 1.24
103 * @return bool
105 protected function canShowRealUserName() {
106 $hiddenPrefs = $this->context->getConfig()->get( 'HiddenPrefs' );
107 return !in_array( 'realname', $hiddenPrefs );
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
116 protected function getContributors( $cnt, $showIfMax ) {
117 $contributors = $this->page->getContributors();
119 $others_link = false;
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();
130 $real_names = [];
131 $user_names = [];
132 $anon_ips = [];
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;
145 } else {
146 $anon_ips[] = $this->link( $user );
149 if ( $cnt == 0 ) {
150 break;
154 $lang = $this->getLanguage();
156 if ( count( $real_names ) ) {
157 $real = $lang->listToText( $real_names );
158 } else {
159 $real = false;
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;
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;
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 );
185 $count = count( $fulllist );
187 # "Based on work by ..."
188 return $count
189 ? $this->msg( 'othercontribs' )->rawParams(
190 $lang->listToText( $fulllist ) )->params( $count )->escaped()
191 : '';
195 * Get a link to $user's user page
196 * @param User $user
197 * @return string Html
199 protected function link( User $user ) {
200 if ( $this->canShowRealUserName() && !$user->isAnon() ) {
201 $real = $user->getRealName();
202 } else {
203 $real = $user->getName();
206 $page = $user->isAnon()
207 ? SpecialPage::getTitleFor( 'Contributions', $user->getName() )
208 : $user->getUserPage();
210 return MediaWikiServices::getInstance()
211 ->getLinkRenderer()->makeLink( $page, $real );
215 * Get a link to $user's user page
216 * @param User $user
217 * @return string Html
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();
233 * Get a link to action=credits of $article page
234 * @return string HTML link
236 protected function othersLink() {
237 return MediaWikiServices::getInstance()->getLinkRenderer()->makeKnownLink(
238 $this->getTitle(),
239 $this->msg( 'others' )->text(),
241 [ 'action' => 'credits' ]