MediaWiki REL1_37
CreditsAction.php
Go to the documentation of this file.
1<?php
28
33
36
38 private $userFactory;
39
46 public function __construct(
47 Page $page,
48 IContextSource $context,
49 LinkRenderer $linkRenderer,
50 UserFactory $userFactory
51 ) {
52 parent::__construct( $page, $context );
53 $this->linkRenderer = $linkRenderer;
54 $this->userFactory = $userFactory;
55 }
56
57 public function getName() {
58 return 'credits';
59 }
60
61 protected function getDescription() {
62 return $this->msg( 'creditspage' )->escaped();
63 }
64
70 public function onView() {
71 $this->getOutput()->addModuleStyles( [
72 'mediawiki.action.styles',
73 ] );
74
75 if ( $this->getWikiPage()->getId() == 0 ) {
76 $s = $this->msg( 'nocredits' )->parse();
77 } else {
78 $s = $this->getCredits( -1 );
79 }
80
81 return Html::rawElement( 'div', [ 'id' => 'mw-credits' ], $s );
82 }
83
91 public function getCredits( $cnt, $showIfMax = true ) {
92 $s = '';
93
94 if ( $cnt != 0 ) {
95 $s = $this->getAuthor();
96 if ( $cnt > 1 || $cnt < 0 ) {
97 $s .= ' ' . $this->getContributors( $cnt - 1, $showIfMax );
98 }
99 }
100
101 return $s;
102 }
103
109 private function getAuthor() {
110 $page = $this->getWikiPage();
111 $user = $this->userFactory->newFromName( $page->getUserText(), UserFactory::RIGOR_NONE );
112
113 $timestamp = $page->getTimestamp();
114 if ( $timestamp ) {
115 $lang = $this->getLanguage();
116 $d = $lang->date( $page->getTimestamp(), true );
117 $t = $lang->time( $page->getTimestamp(), true );
118 } else {
119 $d = '';
120 $t = '';
121 }
122
123 return $this->msg( 'lastmodifiedatby', $d, $t )->rawParams(
124 $this->userLink( $user ) )->params( $user->getName() )->escaped();
125 }
126
133 protected function canShowRealUserName() {
134 $hiddenPrefs = $this->context->getConfig()->get( 'HiddenPrefs' );
135 return !in_array( 'realname', $hiddenPrefs );
136 }
137
144 protected function getContributors( $cnt, $showIfMax ) {
145 $contributors = $this->getWikiPage()->getContributors();
146
147 $others_link = false;
148
149 # Hmm... too many to fit!
150 if ( $cnt > 0 && $contributors->count() > $cnt ) {
151 $others_link = $this->othersLink();
152 if ( !$showIfMax ) {
153 return $this->msg( 'othercontribs' )->rawParams(
154 $others_link )->params( $contributors->count() )->escaped();
155 }
156 }
157
158 $real_names = [];
159 $user_names = [];
160 $anon_ips = [];
161
162 # Sift for real versus user names
164 foreach ( $contributors as $user ) {
165 $cnt--;
166 if ( $user->isRegistered() ) {
167 $link = $this->link( $user );
168 if ( $this->canShowRealUserName() && $user->getRealName() ) {
169 $real_names[] = $link;
170 } else {
171 $user_names[] = $link;
172 }
173 } else {
174 $anon_ips[] = $this->link( $user );
175 }
176
177 if ( $cnt == 0 ) {
178 break;
179 }
180 }
181
182 $lang = $this->getLanguage();
183
184 if ( count( $real_names ) ) {
185 $real = $lang->listToText( $real_names );
186 } else {
187 $real = false;
188 }
189
190 # "ThisSite user(s) A, B and C"
191 if ( count( $user_names ) ) {
192 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
193 count( $user_names ) )->escaped();
194 } else {
195 $user = false;
196 }
197
198 if ( count( $anon_ips ) ) {
199 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
200 count( $anon_ips ) )->escaped();
201 } else {
202 $anon = false;
203 }
204
205 # This is the big list, all mooshed together. We sift for blank strings
206 $fulllist = [];
207 foreach ( [ $real, $user, $anon, $others_link ] as $s ) {
208 if ( $s !== false ) {
209 array_push( $fulllist, $s );
210 }
211 }
212
213 $count = count( $fulllist );
214
215 # "Based on work by ..."
216 return $count
217 ? $this->msg( 'othercontribs' )->rawParams(
218 $lang->listToText( $fulllist ) )->params( $count )->escaped()
219 : '';
220 }
221
227 protected function link( User $user ) {
228 if ( $this->canShowRealUserName() && !$user->isAnon() ) {
229 $real = $user->getRealName();
230 if ( $real === '' ) {
231 $real = $user->getName();
232 }
233 } else {
234 $real = $user->getName();
235 }
236
237 return Linker::userLink( $user->getId(), $user->getName(), $real );
238 }
239
245 protected function userLink( User $user ) {
246 $link = $this->link( $user );
247 if ( $user->isAnon() ) {
248 return $this->msg( 'anonuser' )->rawParams( $link )->parse();
249 } elseif ( $this->canShowRealUserName() && $user->getRealName() ) {
250 return $link;
251 } else {
252 return $this->msg( 'siteuser' )->rawParams( $link )->params( $user->getName() )->escaped();
253 }
254 }
255
260 protected function othersLink() {
261 return $this->linkRenderer->makeKnownLink(
262 $this->getTitle(),
263 $this->msg( 'others' )->text(),
264 [],
265 [ 'action' => 'credits' ]
266 );
267 }
268}
getWikiPage()
Get a WikiPage object.
Definition Action.php:195
WikiPage Article ImagePage CategoryPage Page $page
Page on which we're performing the action.
Definition Action.php:53
getOutput()
Get the OutputPage being used for this instance.
Definition Action.php:156
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition Action.php:228
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition Action.php:185
UserFactory $userFactory
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.
__construct(Page $page, IContextSource $context, LinkRenderer $linkRenderer, UserFactory $userFactory)
canShowRealUserName()
Whether we can display the user's real name (not a hidden pref)
getAuthor()
Get the last author with the last modification time.
othersLink()
Get a link to action=credits of $article page.
getDescription()
Returns the description that goes below the <h1> tag.
link(User $user)
Get a link to $user's user page.
onView()
This is largely cadged from PageHistory::history.
LinkRenderer $linkRenderer
userLink(User $user)
Get a link to $user's user page.
An action which just does something, without showing a form first.
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition Linker.php:1064
Class that generates HTML links for pages.
Creates User objects.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:69
getName()
Get the user name, or the IP of an anonymous user.
Definition User.php:2116
getRealName()
Get the user's real name.
Definition User.php:2527
getId( $wikiId=self::LOCAL)
Get the user's ID.
Definition User.php:2083
isAnon()
Get whether the user is anonymous.
Definition User.php:2986
getTimestamp()
Definition WikiPage.php:848
getUserText( $audience=RevisionRecord::FOR_PUBLIC, Authority $performer=null)
Definition WikiPage.php:916
Interface for objects which can provide a MediaWiki context on request.
Interface for type hinting (accepts WikiPage, Article, ImagePage, CategoryPage)
Definition Page.php:29
foreach( $mmfl['setupFiles'] as $fileName) if($queue) if(empty( $mmfl['quiet'])) $s
if(!isset( $args[0])) $lang
$contributors