MediaWiki master
CreditsAction.php
Go to the documentation of this file.
1<?php
13namespace MediaWiki\Actions;
14
24
29
30 private LinkRenderer $linkRenderer;
31 private UserFactory $userFactory;
32
39 public function __construct(
40 Article $article,
42 LinkRenderer $linkRenderer,
43 UserFactory $userFactory
44 ) {
45 parent::__construct( $article, $context );
46 $this->linkRenderer = $linkRenderer;
47 $this->userFactory = $userFactory;
48 }
49
51 public function getName() {
52 return 'credits';
53 }
54
56 protected function getDescription() {
57 return $this->msg( 'creditspage' )->escaped();
58 }
59
65 public function onView() {
66 $this->getOutput()->addModuleStyles( [
67 'mediawiki.action.styles',
68 ] );
69
70 if ( $this->getWikiPage()->getId() === 0 ) {
71 $s = $this->msg( 'nocredits' )->parse();
72 } else {
73 $s = $this->getCredits( -1 );
74 }
75
76 return Html::rawElement( 'div', [ 'id' => 'mw-credits' ], $s );
77 }
78
86 public function getCredits( $cnt, $showIfMax = true ) {
87 $s = '';
88
89 if ( $cnt !== 0 ) {
90 $s = $this->getAuthor();
91 if ( $cnt > 1 || $cnt < 0 ) {
92 $s .= ' ' . $this->getContributors( $cnt - 1, $showIfMax );
93 }
94 }
95
96 return $s;
97 }
98
104 private function getAuthor() {
105 $page = $this->getWikiPage();
106 $user = $this->userFactory->newFromName( $page->getUserText(), UserRigorOptions::RIGOR_NONE );
107
108 $timestamp = $page->getTimestamp();
109 if ( $timestamp ) {
110 $lang = $this->getLanguage();
111 $d = $lang->date( $page->getTimestamp(), true );
112 $t = $lang->time( $page->getTimestamp(), true );
113 } else {
114 $d = '';
115 $t = '';
116 }
117
118 return $this->msg( 'lastmodifiedatby', $d, $t )->rawParams(
119 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable RIGOR_NONE never returns null
120 $this->userLink( $user ) )->params( $user->getName() )->escaped();
121 }
122
129 protected function canShowRealUserName() {
130 $hiddenPrefs = $this->context->getConfig()->get( MainConfigNames::HiddenPrefs );
131 return !in_array( 'realname', $hiddenPrefs );
132 }
133
140 protected function getContributors( $cnt, $showIfMax ) {
141 $contributors = $this->getWikiPage()->getContributors();
142
143 $others_link = false;
144
145 # Hmm... too many to fit!
146 if ( $cnt > 0 && $contributors->count() > $cnt ) {
147 $others_link = $this->othersLink();
148 if ( !$showIfMax ) {
149 return $this->msg( 'othercontribs' )->rawParams(
150 $others_link )->params( $contributors->count() )->escaped();
151 }
152 }
153
154 $real_names = [];
155 $user_names = [];
156 $anon_ips = [];
157
158 # Sift for real versus user names
160 foreach ( $contributors as $user ) {
161 $cnt--;
162 if ( $user->isRegistered() ) {
163 $link = $this->link( $user );
164 if ( $this->canShowRealUserName() && $user->getRealName() ) {
165 $real_names[] = $link;
166 } else {
167 $user_names[] = $link;
168 }
169 } else {
170 $anon_ips[] = $this->link( $user );
171 }
172
173 if ( $cnt === 0 ) {
174 break;
175 }
176 }
177
178 $lang = $this->getLanguage();
179
180 if ( count( $real_names ) ) {
181 $real = $lang->listToText( $real_names );
182 } else {
183 $real = false;
184 }
185
186 # "ThisSite user(s) A, B and C"
187 if ( count( $user_names ) ) {
188 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
189 count( $user_names ) )->escaped();
190 } else {
191 $user = false;
192 }
193
194 if ( count( $anon_ips ) ) {
195 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
196 count( $anon_ips ) )->escaped();
197 } else {
198 $anon = false;
199 }
200
201 # This is the big list, all mooshed together. We sift for blank strings
202 $fullList = [];
203 foreach ( [ $real, $user, $anon, $others_link ] as $s ) {
204 if ( $s !== false ) {
205 $fullList[] = $s;
206 }
207 }
208
209 $count = count( $fullList );
210
211 # "Based on work by ..."
212 return $count
213 ? $this->msg( 'othercontribs' )->rawParams(
214 $lang->listToText( $fullList ) )->params( $count )->escaped()
215 : '';
216 }
217
223 protected function link( User $user ) {
224 if ( $this->canShowRealUserName() && !$user->isAnon() ) {
225 $real = $user->getRealName();
226 if ( $real === '' ) {
227 $real = $user->getName();
228 }
229 } else {
230 $real = $user->getName();
231 }
232
233 return Linker::userLink( $user->getId(), $user->getName(), $real );
234 }
235
241 protected function userLink( User $user ) {
242 $link = $this->link( $user );
243 if ( $user->isAnon() ) {
244 return $this->msg( 'anonuser' )->rawParams( $link )->parse();
245 } elseif ( $this->canShowRealUserName() && $user->getRealName() ) {
246 return $link;
247 } else {
248 return $this->msg( 'siteuser' )->rawParams( $link )->params( $user->getName() )->escaped();
249 }
250 }
251
256 protected function othersLink() {
257 return $this->linkRenderer->makeKnownLink(
258 $this->getTitle(),
259 $this->msg( 'others' )->text(),
260 [],
261 [ 'action' => 'credits' ]
262 );
263 }
264}
265
267class_alias( CreditsAction::class, 'CreditsAction' );
getWikiPage()
Get a WikiPage object.
Definition Action.php:192
IContextSource null $context
IContextSource if specified; otherwise we'll use the Context from the Page.
Definition Action.php:66
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition Action.php:227
getTitle()
Shortcut to get the Title object from the page.
Definition Action.php:213
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition Action.php:182
getOutput()
Get the OutputPage being used for this instance.
Definition Action.php:143
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.1.17 to override string HTML
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.1.17string Lowercase name
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:43
Class that generates HTML for internal links.
Some internal bits split of from Skin.php.
Definition Linker.php:47
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:64
Create User objects.
User class for the MediaWiki software.
Definition User.php:110
getId( $wikiId=self::LOCAL)
Get the user's ID.
Definition User.php:1471
isAnon()
Get whether the user is anonymous.
Definition User.php:2079
getRealName()
Get the user's real name.
Definition User.php:1938
getName()
Get the user name, or the IP of an anonymous user.
Definition User.php:1504
Interface for objects which can provide a MediaWiki context on request.
Shared interface for rigor levels when dealing with User methods.