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