MediaWiki 1.44.0
CreditsAction.php
Go to the documentation of this file.
1<?php
26namespace MediaWiki\Actions;
27
37
42
43 private LinkRenderer $linkRenderer;
44 private UserFactory $userFactory;
45
52 public function __construct(
53 Article $article,
55 LinkRenderer $linkRenderer,
56 UserFactory $userFactory
57 ) {
58 parent::__construct( $article, $context );
59 $this->linkRenderer = $linkRenderer;
60 $this->userFactory = $userFactory;
61 }
62
63 public function getName() {
64 return 'credits';
65 }
66
67 protected function getDescription() {
68 return $this->msg( 'creditspage' )->escaped();
69 }
70
76 public function onView() {
77 $this->getOutput()->addModuleStyles( [
78 'mediawiki.action.styles',
79 ] );
80
81 if ( $this->getWikiPage()->getId() === 0 ) {
82 $s = $this->msg( 'nocredits' )->parse();
83 } else {
84 $s = $this->getCredits( -1 );
85 }
86
87 return Html::rawElement( 'div', [ 'id' => 'mw-credits' ], $s );
88 }
89
97 public function getCredits( $cnt, $showIfMax = true ) {
98 $s = '';
99
100 if ( $cnt !== 0 ) {
101 $s = $this->getAuthor();
102 if ( $cnt > 1 || $cnt < 0 ) {
103 $s .= ' ' . $this->getContributors( $cnt - 1, $showIfMax );
104 }
105 }
106
107 return $s;
108 }
109
115 private function getAuthor() {
116 $page = $this->getWikiPage();
117 $user = $this->userFactory->newFromName( $page->getUserText(), UserRigorOptions::RIGOR_NONE );
118
119 $timestamp = $page->getTimestamp();
120 if ( $timestamp ) {
121 $lang = $this->getLanguage();
122 $d = $lang->date( $page->getTimestamp(), true );
123 $t = $lang->time( $page->getTimestamp(), true );
124 } else {
125 $d = '';
126 $t = '';
127 }
128
129 return $this->msg( 'lastmodifiedatby', $d, $t )->rawParams(
130 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable RIGOR_NONE never returns null
131 $this->userLink( $user ) )->params( $user->getName() )->escaped();
132 }
133
140 protected function canShowRealUserName() {
141 $hiddenPrefs = $this->context->getConfig()->get( MainConfigNames::HiddenPrefs );
142 return !in_array( 'realname', $hiddenPrefs );
143 }
144
151 protected function getContributors( $cnt, $showIfMax ) {
152 $contributors = $this->getWikiPage()->getContributors();
153
154 $others_link = false;
155
156 # Hmm... too many to fit!
157 if ( $cnt > 0 && $contributors->count() > $cnt ) {
158 $others_link = $this->othersLink();
159 if ( !$showIfMax ) {
160 return $this->msg( 'othercontribs' )->rawParams(
161 $others_link )->params( $contributors->count() )->escaped();
162 }
163 }
164
165 $real_names = [];
166 $user_names = [];
167 $anon_ips = [];
168
169 # Sift for real versus user names
171 foreach ( $contributors as $user ) {
172 $cnt--;
173 if ( $user->isRegistered() ) {
174 $link = $this->link( $user );
175 if ( $this->canShowRealUserName() && $user->getRealName() ) {
176 $real_names[] = $link;
177 } else {
178 $user_names[] = $link;
179 }
180 } else {
181 $anon_ips[] = $this->link( $user );
182 }
183
184 if ( $cnt === 0 ) {
185 break;
186 }
187 }
188
189 $lang = $this->getLanguage();
190
191 if ( count( $real_names ) ) {
192 $real = $lang->listToText( $real_names );
193 } else {
194 $real = false;
195 }
196
197 # "ThisSite user(s) A, B and C"
198 if ( count( $user_names ) ) {
199 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
200 count( $user_names ) )->escaped();
201 } else {
202 $user = false;
203 }
204
205 if ( count( $anon_ips ) ) {
206 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
207 count( $anon_ips ) )->escaped();
208 } else {
209 $anon = false;
210 }
211
212 # This is the big list, all mooshed together. We sift for blank strings
213 $fullList = [];
214 foreach ( [ $real, $user, $anon, $others_link ] as $s ) {
215 if ( $s !== false ) {
216 $fullList[] = $s;
217 }
218 }
219
220 $count = count( $fullList );
221
222 # "Based on work by ..."
223 return $count
224 ? $this->msg( 'othercontribs' )->rawParams(
225 $lang->listToText( $fullList ) )->params( $count )->escaped()
226 : '';
227 }
228
234 protected function link( User $user ) {
235 if ( $this->canShowRealUserName() && !$user->isAnon() ) {
236 $real = $user->getRealName();
237 if ( $real === '' ) {
238 $real = $user->getName();
239 }
240 } else {
241 $real = $user->getName();
242 }
243
244 return Linker::userLink( $user->getId(), $user->getName(), $real );
245 }
246
252 protected function userLink( User $user ) {
253 $link = $this->link( $user );
254 if ( $user->isAnon() ) {
255 return $this->msg( 'anonuser' )->rawParams( $link )->parse();
256 } elseif ( $this->canShowRealUserName() && $user->getRealName() ) {
257 return $link;
258 } else {
259 return $this->msg( 'siteuser' )->rawParams( $link )->params( $user->getName() )->escaped();
260 }
261 }
262
267 protected function othersLink() {
268 return $this->linkRenderer->makeKnownLink(
269 $this->getTitle(),
270 $this->msg( 'others' )->text(),
271 [],
272 [ 'action' => 'credits' ]
273 );
274 }
275}
276
278class_alias( CreditsAction::class, 'CreditsAction' );
getWikiPage()
Get a WikiPage object.
Definition Action.php:205
IContextSource null $context
IContextSource if specified; otherwise we'll use the Context from the Page.
Definition Action.php:79
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition Action.php:240
getTitle()
Shortcut to get the Title object from the page.
Definition Action.php:226
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition Action.php:195
getOutput()
Get the OutputPage being used for this instance.
Definition Action.php:156
link(User $user)
Get a link to $user's user page.
onView()
This is largely cadged from PageHistory::history.
othersLink()
Get a link to action=credits of $article page.
getContributors( $cnt, $showIfMax)
Get a list of contributors of $article.
canShowRealUserName()
Whether we can display the user's real name (not a hidden pref)
__construct(Article $article, IContextSource $context, LinkRenderer $linkRenderer, UserFactory $userFactory)
getDescription()
Returns the description that goes below the <h1> element.
userLink(User $user)
Get a link to $user's user page.
getCredits( $cnt, $showIfMax=true)
Get a list of contributors.
getName()
Return the name of the action this object responds to.
An action which just does something, without showing a form first.
This class is a collection of static functions that serve two purposes:
Definition Html.php:57
Class that generates HTML for internal links.
Some internal bits split of from Skin.php.
Definition Linker.php:61
A class containing constants representing the names of configuration variables.
const HiddenPrefs
Name constant for the HiddenPrefs setting, for use with Config::get()
Legacy class representing an editable page and handling UI for some page actions.
Definition Article.php:76
Create User objects.
User class for the MediaWiki software.
Definition User.php:121
getId( $wikiId=self::LOCAL)
Get the user's ID.
Definition User.php:1579
isAnon()
Get whether the user is anonymous.
Definition User.php:2168
getRealName()
Get the user's real name.
Definition User.php:2027
getName()
Get the user name, or the IP of an anonymous user.
Definition User.php:1612
Interface for objects which can provide a MediaWiki context on request.
Shared interface for rigor levels when dealing with User methods.
$contributors