MediaWiki master
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
64 public function getName() {
65 return 'credits';
66 }
67
69 protected function getDescription() {
70 return $this->msg( 'creditspage' )->escaped();
71 }
72
78 public function onView() {
79 $this->getOutput()->addModuleStyles( [
80 'mediawiki.action.styles',
81 ] );
82
83 if ( $this->getWikiPage()->getId() === 0 ) {
84 $s = $this->msg( 'nocredits' )->parse();
85 } else {
86 $s = $this->getCredits( -1 );
87 }
88
89 return Html::rawElement( 'div', [ 'id' => 'mw-credits' ], $s );
90 }
91
99 public function getCredits( $cnt, $showIfMax = true ) {
100 $s = '';
101
102 if ( $cnt !== 0 ) {
103 $s = $this->getAuthor();
104 if ( $cnt > 1 || $cnt < 0 ) {
105 $s .= ' ' . $this->getContributors( $cnt - 1, $showIfMax );
106 }
107 }
108
109 return $s;
110 }
111
117 private function getAuthor() {
118 $page = $this->getWikiPage();
119 $user = $this->userFactory->newFromName( $page->getUserText(), UserRigorOptions::RIGOR_NONE );
120
121 $timestamp = $page->getTimestamp();
122 if ( $timestamp ) {
123 $lang = $this->getLanguage();
124 $d = $lang->date( $page->getTimestamp(), true );
125 $t = $lang->time( $page->getTimestamp(), true );
126 } else {
127 $d = '';
128 $t = '';
129 }
130
131 return $this->msg( 'lastmodifiedatby', $d, $t )->rawParams(
132 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable RIGOR_NONE never returns null
133 $this->userLink( $user ) )->params( $user->getName() )->escaped();
134 }
135
142 protected function canShowRealUserName() {
143 $hiddenPrefs = $this->context->getConfig()->get( MainConfigNames::HiddenPrefs );
144 return !in_array( 'realname', $hiddenPrefs );
145 }
146
153 protected function getContributors( $cnt, $showIfMax ) {
154 $contributors = $this->getWikiPage()->getContributors();
155
156 $others_link = false;
157
158 # Hmm... too many to fit!
159 if ( $cnt > 0 && $contributors->count() > $cnt ) {
160 $others_link = $this->othersLink();
161 if ( !$showIfMax ) {
162 return $this->msg( 'othercontribs' )->rawParams(
163 $others_link )->params( $contributors->count() )->escaped();
164 }
165 }
166
167 $real_names = [];
168 $user_names = [];
169 $anon_ips = [];
170
171 # Sift for real versus user names
173 foreach ( $contributors as $user ) {
174 $cnt--;
175 if ( $user->isRegistered() ) {
176 $link = $this->link( $user );
177 if ( $this->canShowRealUserName() && $user->getRealName() ) {
178 $real_names[] = $link;
179 } else {
180 $user_names[] = $link;
181 }
182 } else {
183 $anon_ips[] = $this->link( $user );
184 }
185
186 if ( $cnt === 0 ) {
187 break;
188 }
189 }
190
191 $lang = $this->getLanguage();
192
193 if ( count( $real_names ) ) {
194 $real = $lang->listToText( $real_names );
195 } else {
196 $real = false;
197 }
198
199 # "ThisSite user(s) A, B and C"
200 if ( count( $user_names ) ) {
201 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
202 count( $user_names ) )->escaped();
203 } else {
204 $user = false;
205 }
206
207 if ( count( $anon_ips ) ) {
208 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
209 count( $anon_ips ) )->escaped();
210 } else {
211 $anon = false;
212 }
213
214 # This is the big list, all mooshed together. We sift for blank strings
215 $fullList = [];
216 foreach ( [ $real, $user, $anon, $others_link ] as $s ) {
217 if ( $s !== false ) {
218 $fullList[] = $s;
219 }
220 }
221
222 $count = count( $fullList );
223
224 # "Based on work by ..."
225 return $count
226 ? $this->msg( 'othercontribs' )->rawParams(
227 $lang->listToText( $fullList ) )->params( $count )->escaped()
228 : '';
229 }
230
236 protected function link( User $user ) {
237 if ( $this->canShowRealUserName() && !$user->isAnon() ) {
238 $real = $user->getRealName();
239 if ( $real === '' ) {
240 $real = $user->getName();
241 }
242 } else {
243 $real = $user->getName();
244 }
245
246 return Linker::userLink( $user->getId(), $user->getName(), $real );
247 }
248
254 protected function userLink( User $user ) {
255 $link = $this->link( $user );
256 if ( $user->isAnon() ) {
257 return $this->msg( 'anonuser' )->rawParams( $link )->parse();
258 } elseif ( $this->canShowRealUserName() && $user->getRealName() ) {
259 return $link;
260 } else {
261 return $this->msg( 'siteuser' )->rawParams( $link )->params( $user->getName() )->escaped();
262 }
263 }
264
269 protected function othersLink() {
270 return $this->linkRenderer->makeKnownLink(
271 $this->getTitle(),
272 $this->msg( 'others' )->text(),
273 [],
274 [ 'action' => 'credits' ]
275 );
276 }
277}
278
280class_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.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: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:123
getId( $wikiId=self::LOCAL)
Get the user's ID.
Definition User.php:1552
isAnon()
Get whether the user is anonymous.
Definition User.php:2141
getRealName()
Get the user's real name.
Definition User.php:2000
getName()
Get the user name, or the IP of an anonymous user.
Definition User.php:1585
Interface for objects which can provide a MediaWiki context on request.
Shared interface for rigor levels when dealing with User methods.