MediaWiki master
CreditsAction.php
Go to the documentation of this file.
1<?php
34
39
40 private LinkRenderer $linkRenderer;
41 private UserFactory $userFactory;
42
49 public function __construct(
50 Article $article,
51 IContextSource $context,
52 LinkRenderer $linkRenderer,
53 UserFactory $userFactory
54 ) {
55 parent::__construct( $article, $context );
56 $this->linkRenderer = $linkRenderer;
57 $this->userFactory = $userFactory;
58 }
59
60 public function getName() {
61 return 'credits';
62 }
63
64 protected function getDescription() {
65 return $this->msg( 'creditspage' )->escaped();
66 }
67
73 public function onView() {
74 $this->getOutput()->addModuleStyles( [
75 'mediawiki.action.styles',
76 ] );
77
78 if ( $this->getWikiPage()->getId() === 0 ) {
79 $s = $this->msg( 'nocredits' )->parse();
80 } else {
81 $s = $this->getCredits( -1 );
82 }
83
84 return Html::rawElement( 'div', [ 'id' => 'mw-credits' ], $s );
85 }
86
94 public function getCredits( $cnt, $showIfMax = true ) {
95 $s = '';
96
97 if ( $cnt !== 0 ) {
98 $s = $this->getAuthor();
99 if ( $cnt > 1 || $cnt < 0 ) {
100 $s .= ' ' . $this->getContributors( $cnt - 1, $showIfMax );
101 }
102 }
103
104 return $s;
105 }
106
112 private function getAuthor() {
113 $page = $this->getWikiPage();
114 $user = $this->userFactory->newFromName( $page->getUserText(), UserRigorOptions::RIGOR_NONE );
115
116 $timestamp = $page->getTimestamp();
117 if ( $timestamp ) {
118 $lang = $this->getLanguage();
119 $d = $lang->date( $page->getTimestamp(), true );
120 $t = $lang->time( $page->getTimestamp(), true );
121 } else {
122 $d = '';
123 $t = '';
124 }
125
126 return $this->msg( 'lastmodifiedatby', $d, $t )->rawParams(
127 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable RIGOR_NONE never returns null
128 $this->userLink( $user ) )->params( $user->getName() )->escaped();
129 }
130
137 protected function canShowRealUserName() {
138 $hiddenPrefs = $this->context->getConfig()->get( MainConfigNames::HiddenPrefs );
139 return !in_array( 'realname', $hiddenPrefs );
140 }
141
148 protected function getContributors( $cnt, $showIfMax ) {
149 $contributors = $this->getWikiPage()->getContributors();
150
151 $others_link = false;
152
153 # Hmm... too many to fit!
154 if ( $cnt > 0 && $contributors->count() > $cnt ) {
155 $others_link = $this->othersLink();
156 if ( !$showIfMax ) {
157 return $this->msg( 'othercontribs' )->rawParams(
158 $others_link )->params( $contributors->count() )->escaped();
159 }
160 }
161
162 $real_names = [];
163 $user_names = [];
164 $anon_ips = [];
165
166 # Sift for real versus user names
168 foreach ( $contributors as $user ) {
169 $cnt--;
170 if ( $user->isRegistered() ) {
171 $link = $this->link( $user );
172 if ( $this->canShowRealUserName() && $user->getRealName() ) {
173 $real_names[] = $link;
174 } else {
175 $user_names[] = $link;
176 }
177 } else {
178 $anon_ips[] = $this->link( $user );
179 }
180
181 if ( $cnt === 0 ) {
182 break;
183 }
184 }
185
186 $lang = $this->getLanguage();
187
188 if ( count( $real_names ) ) {
189 $real = $lang->listToText( $real_names );
190 } else {
191 $real = false;
192 }
193
194 # "ThisSite user(s) A, B and C"
195 if ( count( $user_names ) ) {
196 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
197 count( $user_names ) )->escaped();
198 } else {
199 $user = false;
200 }
201
202 if ( count( $anon_ips ) ) {
203 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
204 count( $anon_ips ) )->escaped();
205 } else {
206 $anon = false;
207 }
208
209 # This is the big list, all mooshed together. We sift for blank strings
210 $fullList = [];
211 foreach ( [ $real, $user, $anon, $others_link ] as $s ) {
212 if ( $s !== false ) {
213 $fullList[] = $s;
214 }
215 }
216
217 $count = count( $fullList );
218
219 # "Based on work by ..."
220 return $count
221 ? $this->msg( 'othercontribs' )->rawParams(
222 $lang->listToText( $fullList ) )->params( $count )->escaped()
223 : '';
224 }
225
231 protected function link( User $user ) {
232 if ( $this->canShowRealUserName() && !$user->isAnon() ) {
233 $real = $user->getRealName();
234 if ( $real === '' ) {
235 $real = $user->getName();
236 }
237 } else {
238 $real = $user->getName();
239 }
240
241 return Linker::userLink( $user->getId(), $user->getName(), $real );
242 }
243
249 protected function userLink( User $user ) {
250 $link = $this->link( $user );
251 if ( $user->isAnon() ) {
252 return $this->msg( 'anonuser' )->rawParams( $link )->parse();
253 } elseif ( $this->canShowRealUserName() && $user->getRealName() ) {
254 return $link;
255 } else {
256 return $this->msg( 'siteuser' )->rawParams( $link )->params( $user->getName() )->escaped();
257 }
258 }
259
264 protected function othersLink() {
265 return $this->linkRenderer->makeKnownLink(
266 $this->getTitle(),
267 $this->msg( 'others' )->text(),
268 [],
269 [ 'action' => 'credits' ]
270 );
271 }
272}
getWikiPage()
Get a WikiPage object.
Definition Action.php:191
getOutput()
Get the OutputPage being used for this instance.
Definition Action.php:142
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition Action.php:224
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition Action.php:181
Legacy class representing an editable page and handling UI for some page actions.
Definition Article.php:70
getCredits( $cnt, $showIfMax=true)
Get a list of contributors.
getName()
Return the name of the action this object responds to.
getContributors( $cnt, $showIfMax)
Get a list of contributors of $article.
canShowRealUserName()
Whether we can display the user's real name (not a hidden pref)
othersLink()
Get a link to action=credits of $article page.
getDescription()
Returns the description that goes below the <h1> element.
link(User $user)
Get a link to $user's user page.
__construct(Article $article, IContextSource $context, LinkRenderer $linkRenderer, UserFactory $userFactory)
onView()
This is largely cadged from PageHistory::history.
userLink(User $user)
Get a link to $user's user page.
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:56
Class that generates HTML for internal links.
Some internal bits split of from Skin.php.
Definition Linker.php:63
A class containing constants representing the names of configuration variables.
Creates User objects.
internal since 1.36
Definition User.php:93
getId( $wikiId=self::LOCAL)
Get the user's ID.
Definition User.php:1535
isAnon()
Get whether the user is anonymous.
Definition User.php:2124
getRealName()
Get the user's real name.
Definition User.php:1983
getName()
Get the user name, or the IP of an anonymous user.
Definition User.php:1568
Interface for objects which can provide a MediaWiki context on request.
Shared interface for rigor levels when dealing with User methods.
$contributors