MediaWiki REL1_35
CreditsAction.php
Go to the documentation of this file.
1<?php
27
32
33 public function getName() {
34 return 'credits';
35 }
36
37 protected function getDescription() {
38 return $this->msg( 'creditspage' )->escaped();
39 }
40
46 public function onView() {
47 if ( $this->getWikiPage()->getId() == 0 ) {
48 $s = $this->msg( 'nocredits' )->parse();
49 } else {
50 $s = $this->getCredits( -1 );
51 }
52
53 return Html::rawElement( 'div', [ 'id' => 'mw-credits' ], $s );
54 }
55
63 public function getCredits( $cnt, $showIfMax = true ) {
64 $s = '';
65
66 if ( $cnt != 0 ) {
67 $s = $this->getAuthor();
68 if ( $cnt > 1 || $cnt < 0 ) {
69 $s .= ' ' . $this->getContributors( $cnt - 1, $showIfMax );
70 }
71 }
72
73 return $s;
74 }
75
81 private function getAuthor() {
82 $page = $this->getWikiPage();
83 $user = User::newFromName( $page->getUserText(), false );
84
85 $timestamp = $page->getTimestamp();
86 if ( $timestamp ) {
87 $lang = $this->getLanguage();
88 $d = $lang->date( $page->getTimestamp(), true );
89 $t = $lang->time( $page->getTimestamp(), true );
90 } else {
91 $d = '';
92 $t = '';
93 }
94
95 return $this->msg( 'lastmodifiedatby', $d, $t )->rawParams(
96 $this->userLink( $user ) )->params( $user->getName() )->escaped();
97 }
98
105 protected function canShowRealUserName() {
106 $hiddenPrefs = $this->context->getConfig()->get( 'HiddenPrefs' );
107 return !in_array( 'realname', $hiddenPrefs );
108 }
109
116 protected function getContributors( $cnt, $showIfMax ) {
117 $contributors = $this->getWikiPage()->getContributors();
118
119 $others_link = false;
120
121 # Hmm... too many to fit!
122 if ( $cnt > 0 && $contributors->count() > $cnt ) {
123 $others_link = $this->othersLink();
124 if ( !$showIfMax ) {
125 return $this->msg( 'othercontribs' )->rawParams(
126 $others_link )->params( $contributors->count() )->escaped();
127 }
128 }
129
130 $real_names = [];
131 $user_names = [];
132 $anon_ips = [];
133
134 # Sift for real versus user names
136 foreach ( $contributors as $user ) {
137 $cnt--;
138 if ( $user->isLoggedIn() ) {
139 $link = $this->link( $user );
140 if ( $this->canShowRealUserName() && $user->getRealName() ) {
141 $real_names[] = $link;
142 } else {
143 $user_names[] = $link;
144 }
145 } else {
146 $anon_ips[] = $this->link( $user );
147 }
148
149 if ( $cnt == 0 ) {
150 break;
151 }
152 }
153
154 $lang = $this->getLanguage();
155
156 if ( count( $real_names ) ) {
157 $real = $lang->listToText( $real_names );
158 } else {
159 $real = false;
160 }
161
162 # "ThisSite user(s) A, B and C"
163 if ( count( $user_names ) ) {
164 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
165 count( $user_names ) )->escaped();
166 } else {
167 $user = false;
168 }
169
170 if ( count( $anon_ips ) ) {
171 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
172 count( $anon_ips ) )->escaped();
173 } else {
174 $anon = false;
175 }
176
177 # This is the big list, all mooshed together. We sift for blank strings
178 $fulllist = [];
179 foreach ( [ $real, $user, $anon, $others_link ] as $s ) {
180 if ( $s !== false ) {
181 array_push( $fulllist, $s );
182 }
183 }
184
185 $count = count( $fulllist );
186
187 # "Based on work by ..."
188 return $count
189 ? $this->msg( 'othercontribs' )->rawParams(
190 $lang->listToText( $fulllist ) )->params( $count )->escaped()
191 : '';
192 }
193
199 protected function link( User $user ) {
200 if ( $this->canShowRealUserName() && !$user->isAnon() ) {
201 $real = $user->getRealName();
202 if ( $real === '' ) {
203 $real = $user->getName();
204 }
205 } else {
206 $real = $user->getName();
207 }
208
209 $page = $user->isAnon()
210 ? SpecialPage::getTitleFor( 'Contributions', $user->getName() )
211 : $user->getUserPage();
212
213 return MediaWikiServices::getInstance()
214 ->getLinkRenderer()->makeLink( $page, $real );
215 }
216
222 protected function userLink( User $user ) {
223 $link = $this->link( $user );
224 if ( $user->isAnon() ) {
225 return $this->msg( 'anonuser' )->rawParams( $link )->parse();
226 } elseif ( $this->canShowRealUserName() && $user->getRealName() ) {
227 return $link;
228 } else {
229 return $this->msg( 'siteuser' )->rawParams( $link )->params( $user->getName() )->escaped();
230 }
231 }
232
237 protected function othersLink() {
238 return MediaWikiServices::getInstance()->getLinkRenderer()->makeKnownLink(
239 $this->getTitle(),
240 $this->msg( 'others' )->text(),
241 [],
242 [ 'action' => 'credits' ]
243 );
244 }
245}
getWikiPage()
Get a WikiPage object.
Definition Action.php:278
WikiPage Article ImagePage CategoryPage Page $page
Page on which we're performing the action.
Definition Action.php:53
getTitle()
Shortcut to get the Title object from the page.
Definition Action.php:299
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition Action.php:311
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition Action.php:268
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)
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.
userLink(User $user)
Get a link to $user's user page.
An action which just does something, without showing a form first.
MediaWikiServices is the service locator for the application scope of MediaWiki.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:60
getName()
Get the user name, or the IP of an anonymous user.
Definition User.php:2150
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:541
getRealName()
Get the user's real name.
Definition User.php:2636
getUserPage()
Get this user's personal page title.
Definition User.php:3802
isAnon()
Get whether the user is anonymous.
Definition User.php:3087
getTimestamp()
Definition WikiPage.php:813
getUserText( $audience=RevisionRecord::FOR_PUBLIC, User $user=null)
Definition WikiPage.php:894
if(!isset( $args[0])) $lang
$contributors