MediaWiki  master
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  $this->getOutput()->addModuleStyles( [
48  'mediawiki.action.styles',
49  ] );
50 
51  if ( $this->getWikiPage()->getId() == 0 ) {
52  $s = $this->msg( 'nocredits' )->parse();
53  } else {
54  $s = $this->getCredits( -1 );
55  }
56 
57  return Html::rawElement( 'div', [ 'id' => 'mw-credits' ], $s );
58  }
59 
67  public function getCredits( $cnt, $showIfMax = true ) {
68  $s = '';
69 
70  if ( $cnt != 0 ) {
71  $s = $this->getAuthor();
72  if ( $cnt > 1 || $cnt < 0 ) {
73  $s .= ' ' . $this->getContributors( $cnt - 1, $showIfMax );
74  }
75  }
76 
77  return $s;
78  }
79 
85  private function getAuthor() {
86  $page = $this->getWikiPage();
87  $user = User::newFromName( $page->getUserText(), false );
88 
89  $timestamp = $page->getTimestamp();
90  if ( $timestamp ) {
91  $lang = $this->getLanguage();
92  $d = $lang->date( $page->getTimestamp(), true );
93  $t = $lang->time( $page->getTimestamp(), true );
94  } else {
95  $d = '';
96  $t = '';
97  }
98 
99  return $this->msg( 'lastmodifiedatby', $d, $t )->rawParams(
100  $this->userLink( $user ) )->params( $user->getName() )->escaped();
101  }
102 
109  protected function canShowRealUserName() {
110  $hiddenPrefs = $this->context->getConfig()->get( 'HiddenPrefs' );
111  return !in_array( 'realname', $hiddenPrefs );
112  }
113 
120  protected function getContributors( $cnt, $showIfMax ) {
121  $contributors = $this->getWikiPage()->getContributors();
122 
123  $others_link = false;
124 
125  # Hmm... too many to fit!
126  if ( $cnt > 0 && $contributors->count() > $cnt ) {
127  $others_link = $this->othersLink();
128  if ( !$showIfMax ) {
129  return $this->msg( 'othercontribs' )->rawParams(
130  $others_link )->params( $contributors->count() )->escaped();
131  }
132  }
133 
134  $real_names = [];
135  $user_names = [];
136  $anon_ips = [];
137 
138  # Sift for real versus user names
139 
140  foreach ( $contributors as $user ) {
141  $cnt--;
142  if ( $user->isRegistered() ) {
143  $link = $this->link( $user );
144  if ( $this->canShowRealUserName() && $user->getRealName() ) {
145  $real_names[] = $link;
146  } else {
147  $user_names[] = $link;
148  }
149  } else {
150  $anon_ips[] = $this->link( $user );
151  }
152 
153  if ( $cnt == 0 ) {
154  break;
155  }
156  }
157 
158  $lang = $this->getLanguage();
159 
160  if ( count( $real_names ) ) {
161  $real = $lang->listToText( $real_names );
162  } else {
163  $real = false;
164  }
165 
166  # "ThisSite user(s) A, B and C"
167  if ( count( $user_names ) ) {
168  $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
169  count( $user_names ) )->escaped();
170  } else {
171  $user = false;
172  }
173 
174  if ( count( $anon_ips ) ) {
175  $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
176  count( $anon_ips ) )->escaped();
177  } else {
178  $anon = false;
179  }
180 
181  # This is the big list, all mooshed together. We sift for blank strings
182  $fulllist = [];
183  foreach ( [ $real, $user, $anon, $others_link ] as $s ) {
184  if ( $s !== false ) {
185  array_push( $fulllist, $s );
186  }
187  }
188 
189  $count = count( $fulllist );
190 
191  # "Based on work by ..."
192  return $count
193  ? $this->msg( 'othercontribs' )->rawParams(
194  $lang->listToText( $fulllist ) )->params( $count )->escaped()
195  : '';
196  }
197 
203  protected function link( User $user ) {
204  if ( $this->canShowRealUserName() && !$user->isAnon() ) {
205  $real = $user->getRealName();
206  if ( $real === '' ) {
207  $real = $user->getName();
208  }
209  } else {
210  $real = $user->getName();
211  }
212 
213  return Linker::userLink( $user->getId(), $user->getName(), $real );
214  }
215 
221  protected function userLink( User $user ) {
222  $link = $this->link( $user );
223  if ( $user->isAnon() ) {
224  return $this->msg( 'anonuser' )->rawParams( $link )->parse();
225  } elseif ( $this->canShowRealUserName() && $user->getRealName() ) {
226  return $link;
227  } else {
228  return $this->msg( 'siteuser' )->rawParams( $link )->params( $user->getName() )->escaped();
229  }
230  }
231 
236  protected function othersLink() {
237  return MediaWikiServices::getInstance()->getLinkRenderer()->makeKnownLink(
238  $this->getTitle(),
239  $this->msg( 'others' )->text(),
240  [],
241  [ 'action' => 'credits' ]
242  );
243  }
244 }
CreditsAction\canShowRealUserName
canShowRealUserName()
Whether we can display the user's real name (not a hidden pref)
Definition: CreditsAction.php:109
User\isAnon
isAnon()
Get whether the user is anonymous.
Definition: User.php:2943
Linker\userLink
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition: Linker.php:900
FormlessAction
An action which just does something, without showing a form first.
Definition: FormlessAction.php:30
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:180
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
WikiPage\getUserText
getUserText( $audience=RevisionRecord::FOR_PUBLIC, Authority $performer=null)
Definition: WikiPage.php:967
CreditsAction\getName
getName()
Return the name of the action this object responds to.
Definition: CreditsAction.php:33
User\newFromName
static newFromName( $name, $validate='valid')
Definition: User.php:592
CreditsAction\othersLink
othersLink()
Get a link to action=credits of $article page.
Definition: CreditsAction.php:236
$contributors
$contributors
Definition: updateCredits.php:38
User\getId
getId( $wikiId=self::LOCAL)
Get the user's ID.
Definition: User.php:2049
CreditsAction\getCredits
getCredits( $cnt, $showIfMax=true)
Get a list of contributors.
Definition: CreditsAction.php:67
Action\getWikiPage
getWikiPage()
Get a WikiPage object.
Definition: Action.php:267
CreditsAction\link
link(User $user)
Get a link to $user's user page.
Definition: CreditsAction.php:203
$s
foreach( $mmfl['setupFiles'] as $fileName) if( $queue) if(empty( $mmfl['quiet'])) $s
Definition: mergeMessageFileList.php:206
CreditsAction\getDescription
getDescription()
Returns the description that goes below the <h1> tag.
Definition: CreditsAction.php:37
User\getRealName
getRealName()
Get the user's real name.
Definition: User.php:2493
CreditsAction\getAuthor
getAuthor()
Get the last author with the last modification time.
Definition: CreditsAction.php:85
Action\getTitle
getTitle()
Shortcut to get the Title object from the page.
Definition: Action.php:288
CreditsAction\onView
onView()
This is largely cadged from PageHistory::history.
Definition: CreditsAction.php:46
Action\$page
WikiPage Article ImagePage CategoryPage Page $page
Page on which we're performing the action.
Definition: Action.php:53
Action\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: Action.php:300
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:212
Action\getLanguage
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition: Action.php:257
Action\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: Action.php:228
CreditsAction\getContributors
getContributors( $cnt, $showIfMax)
Get a list of contributors of $article.
Definition: CreditsAction.php:120
CreditsAction
Definition: CreditsAction.php:31
$t
$t
Definition: testCompression.php:74
WikiPage\getTimestamp
getTimestamp()
Definition: WikiPage.php:899
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:67
User\getName
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2082
CreditsAction\userLink
userLink(User $user)
Get a link to $user's user page.
Definition: CreditsAction.php:221