MediaWiki master
CreditsAction.php
Go to the documentation of this file.
1<?php
13namespace MediaWiki\Actions;
14
24
29
30 public function __construct(
31 Article $article,
32 IContextSource $context,
33 private readonly LinkRenderer $linkRenderer,
34 private readonly UserFactory $userFactory,
35 ) {
36 parent::__construct( $article, $context );
37 }
38
40 public function getName() {
41 return 'credits';
42 }
43
45 protected function getDescription() {
46 return $this->msg( 'creditspage' )->escaped();
47 }
48
54 public function onView() {
55 $this->getOutput()->addModuleStyles( [
56 'mediawiki.action.styles',
57 ] );
58
59 if ( $this->getWikiPage()->getId() === 0 ) {
60 $s = $this->msg( 'nocredits' )->parse();
61 } else {
62 $s = $this->getCredits( -1 );
63 }
64
65 return Html::rawElement( 'div', [ 'id' => 'mw-credits' ], $s );
66 }
67
75 public function getCredits( $cnt, $showIfMax = true ) {
76 $s = '';
77
78 if ( $cnt !== 0 ) {
79 $s = $this->getAuthor();
80 if ( $cnt > 1 || $cnt < 0 ) {
81 $s .= ' ' . $this->getContributors( $cnt - 1, $showIfMax );
82 }
83 }
84
85 return $s;
86 }
87
93 private function getAuthor() {
94 $page = $this->getWikiPage();
95 $user = $this->userFactory->newFromName( $page->getUserText(), UserRigorOptions::RIGOR_NONE );
96
97 $timestamp = $page->getTimestamp();
98 if ( $timestamp ) {
99 $lang = $this->getLanguage();
100 $d = $lang->date( $page->getTimestamp(), true );
101 $t = $lang->time( $page->getTimestamp(), true );
102 } else {
103 $d = '';
104 $t = '';
105 }
106
107 return $this->msg( 'lastmodifiedatby', $d, $t )->rawParams(
108 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable RIGOR_NONE never returns null
109 $this->userLink( $user ) )->params( $user->getName() )->escaped();
110 }
111
118 protected function canShowRealUserName() {
119 $hiddenPrefs = $this->context->getConfig()->get( MainConfigNames::HiddenPrefs );
120 return !in_array( 'realname', $hiddenPrefs );
121 }
122
129 protected function getContributors( $cnt, $showIfMax ) {
130 $contributors = $this->getWikiPage()->getContributors();
131
132 $others_link = false;
133
134 # Hmm... too many to fit!
135 if ( $cnt > 0 && $contributors->count() > $cnt ) {
136 $others_link = $this->othersLink();
137 if ( !$showIfMax ) {
138 return $this->msg( 'othercontribs' )->rawParams(
139 $others_link )->params( $contributors->count() )->escaped();
140 }
141 }
142
143 $real_names = [];
144 $user_names = [];
145 $anon_ips = [];
146
147 # Sift for real versus user names
149 foreach ( $contributors as $user ) {
150 $cnt--;
151 if ( $user->isRegistered() ) {
152 $link = $this->link( $user );
153 if ( $this->canShowRealUserName() && $user->getRealName() ) {
154 $real_names[] = $link;
155 } else {
156 $user_names[] = $link;
157 }
158 } else {
159 $anon_ips[] = $this->link( $user );
160 }
161
162 if ( $cnt === 0 ) {
163 break;
164 }
165 }
166
167 $lang = $this->getLanguage();
168
169 if ( count( $real_names ) ) {
170 $real = $lang->listToText( $real_names );
171 } else {
172 $real = false;
173 }
174
175 # "ThisSite user(s) A, B and C"
176 if ( count( $user_names ) ) {
177 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
178 count( $user_names ) )->escaped();
179 } else {
180 $user = false;
181 }
182
183 if ( count( $anon_ips ) ) {
184 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
185 count( $anon_ips ) )->escaped();
186 } else {
187 $anon = false;
188 }
189
190 # This is the big list, all mooshed together. We sift for blank strings
191 $fullList = [];
192 foreach ( [ $real, $user, $anon, $others_link ] as $s ) {
193 if ( $s !== false ) {
194 $fullList[] = $s;
195 }
196 }
197
198 $count = count( $fullList );
199
200 # "Based on work by ..."
201 return $count
202 ? $this->msg( 'othercontribs' )->rawParams(
203 $lang->listToText( $fullList ) )->params( $count )->escaped()
204 : '';
205 }
206
212 protected function link( User $user ) {
213 if ( $this->canShowRealUserName() && !$user->isAnon() ) {
214 $real = $user->getRealName();
215 if ( $real === '' ) {
216 $real = $user->getName();
217 }
218 } else {
219 $real = $user->getName();
220 }
221
222 return Linker::userLink( $user->getId(), $user->getName(), $real );
223 }
224
230 protected function userLink( User $user ) {
231 $link = $this->link( $user );
232 if ( $user->isAnon() ) {
233 return $this->msg( 'anonuser' )->rawParams( $link )->parse();
234 } elseif ( $this->canShowRealUserName() && $user->getRealName() ) {
235 return $link;
236 } else {
237 return $this->msg( 'siteuser' )->rawParams( $link )->params( $user->getName() )->escaped();
238 }
239 }
240
245 protected function othersLink() {
246 return $this->linkRenderer->makeKnownLink(
247 $this->getTitle(),
248 $this->msg( 'others' )->text(),
249 [],
250 [ 'action' => 'credits' ]
251 );
252 }
253}
254
256class_alias( CreditsAction::class, 'CreditsAction' );
getWikiPage()
Get a WikiPage object.
Definition Action.php:171
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition Action.php:205
getTitle()
Shortcut to get the Title object from the page.
Definition Action.php:191
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition Action.php:161
getOutput()
Get the OutputPage being used for this instance.
Definition Action.php:122
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)
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
__construct(Article $article, IContextSource $context, private readonly LinkRenderer $linkRenderer, private readonly UserFactory $userFactory,)
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:44
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:66
Create User objects.
User class for the MediaWiki software.
Definition User.php:130
getId( $wikiId=self::LOCAL)
Get the user's ID.
Definition User.php:1492
isAnon()
Get whether the user is anonymous.
Definition User.php:2099
getRealName()
Get the user's real name.
Definition User.php:1958
getName()
Get the user name, or the IP of an anonymous user.
Definition User.php:1525
Interface for objects which can provide a MediaWiki context on request.
Shared interface for rigor levels when dealing with User methods.