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
23 * @author <evan@wikitravel.org>
26 use MediaWiki\MediaWikiServices
;
31 class CreditsAction
extends FormlessAction
{
33 public function getName() {
37 protected function getDescription() {
38 return $this->msg( 'creditspage' )->escaped();
42 * This is largely cadged from PageHistory::history
46 public function onView() {
48 if ( $this->page
->getID() == 0 ) {
49 $s = $this->msg( 'nocredits' )->parse();
51 $s = $this->getCredits( -1 );
54 return Html
::rawElement( 'div', [ 'id' => 'mw-credits' ], $s );
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
64 public function getCredits( $cnt, $showIfMax = true ) {
68 $s = $this->getAuthor( $this->page
);
69 if ( $cnt > 1 ||
$cnt < 0 ) {
70 $s .= ' ' . $this->getContributors( $cnt - 1, $showIfMax );
78 * Get the last author with the last modification time
82 protected function getAuthor( Page
$page ) {
83 $user = User
::newFromName( $page->getUserText(), false );
85 $timestamp = $page->getTimestamp();
87 $lang = $this->getLanguage();
88 $d = $lang->date( $page->getTimestamp(), true );
89 $t = $lang->time( $page->getTimestamp(), true );
95 return $this->msg( 'lastmodifiedatby', $d, $t )->rawParams(
96 $this->userLink( $user ) )->params( $user->getName() )->escaped();
100 * Whether we can display the user's real name (not a hidden pref)
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();
125 return $this->msg( 'othercontribs' )->rawParams(
126 $others_link )->params( $contributors->count() )->escaped();
134 # Sift for real versus user names
135 /** @var $user User */
136 foreach ( $contributors as $user ) {
138 if ( $user->isLoggedIn() ) {
139 $link = $this->link( $user );
140 if ( $this->canShowRealUserName() && $user->getRealName() ) {
141 $real_names[] = $link;
143 $user_names[] = $link;
146 $anon_ips[] = $this->link( $user );
154 $lang = $this->getLanguage();
156 if ( count( $real_names ) ) {
157 $real = $lang->listToText( $real_names );
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();
170 if ( count( $anon_ips ) ) {
171 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
172 count( $anon_ips ) )->escaped();
177 # This is the big list, all mooshed together. We sift for blank strings
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 ..."
189 ?
$this->msg( 'othercontribs' )->rawParams(
190 $lang->listToText( $fulllist ) )->params( $count )->escaped()
195 * Get a link to $user's user page
197 * @return string Html
199 protected function link( User
$user ) {
200 if ( $this->canShowRealUserName() && !$user->isAnon() ) {
201 $real = $user->getRealName();
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
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();
224 if ( $this->canShowRealUserName() && $user->getRealName() ) {
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(
239 $this->msg( 'others' )->text(),
241 [ 'action' => 'credits' ]