MediaWiki  master
CreditsAction.php
Go to the documentation of this file.
1 <?php
30 
35 
37  private $linkRenderer;
38 
40  private $userFactory;
41 
48  public function __construct(
49  Page $page,
53  ) {
54  parent::__construct( $page, $context );
55  $this->linkRenderer = $linkRenderer;
56  $this->userFactory = $userFactory;
57  }
58 
59  public function getName() {
60  return 'credits';
61  }
62 
63  protected function getDescription() {
64  return $this->msg( 'creditspage' )->escaped();
65  }
66 
72  public function onView() {
73  $this->getOutput()->addModuleStyles( [
74  'mediawiki.action.styles',
75  ] );
76 
77  if ( $this->getWikiPage()->getId() === 0 ) {
78  $s = $this->msg( 'nocredits' )->parse();
79  } else {
80  $s = $this->getCredits( -1 );
81  }
82 
83  return Html::rawElement( 'div', [ 'id' => 'mw-credits' ], $s );
84  }
85 
93  public function getCredits( $cnt, $showIfMax = true ) {
94  $s = '';
95 
96  if ( $cnt !== 0 ) {
97  $s = $this->getAuthor();
98  if ( $cnt > 1 || $cnt < 0 ) {
99  $s .= ' ' . $this->getContributors( $cnt - 1, $showIfMax );
100  }
101  }
102 
103  return $s;
104  }
105 
111  private function getAuthor() {
112  $page = $this->getWikiPage();
113  $user = $this->userFactory->newFromName( $page->getUserText(), UserRigorOptions::RIGOR_NONE );
114 
115  $timestamp = $page->getTimestamp();
116  if ( $timestamp ) {
117  $lang = $this->getLanguage();
118  $d = $lang->date( $page->getTimestamp(), true );
119  $t = $lang->time( $page->getTimestamp(), true );
120  } else {
121  $d = '';
122  $t = '';
123  }
124 
125  return $this->msg( 'lastmodifiedatby', $d, $t )->rawParams(
126  // @phan-suppress-next-line PhanTypeMismatchArgumentNullable RIGOR_NONE never returns null
127  $this->userLink( $user ) )->params( $user->getName() )->escaped();
128  }
129 
136  protected function canShowRealUserName() {
137  $hiddenPrefs = $this->context->getConfig()->get( MainConfigNames::HiddenPrefs );
138  return !in_array( 'realname', $hiddenPrefs );
139  }
140 
147  protected function getContributors( $cnt, $showIfMax ) {
148  $contributors = $this->getWikiPage()->getContributors();
149 
150  $others_link = false;
151 
152  # Hmm... too many to fit!
153  if ( $cnt > 0 && $contributors->count() > $cnt ) {
154  $others_link = $this->othersLink();
155  if ( !$showIfMax ) {
156  return $this->msg( 'othercontribs' )->rawParams(
157  $others_link )->params( $contributors->count() )->escaped();
158  }
159  }
160 
161  $real_names = [];
162  $user_names = [];
163  $anon_ips = [];
164 
165  # Sift for real versus user names
167  foreach ( $contributors as $user ) {
168  $cnt--;
169  if ( $user->isRegistered() ) {
170  $link = $this->link( $user );
171  if ( $this->canShowRealUserName() && $user->getRealName() ) {
172  $real_names[] = $link;
173  } else {
174  $user_names[] = $link;
175  }
176  } else {
177  $anon_ips[] = $this->link( $user );
178  }
179 
180  if ( $cnt === 0 ) {
181  break;
182  }
183  }
184 
185  $lang = $this->getLanguage();
186 
187  if ( count( $real_names ) ) {
188  $real = $lang->listToText( $real_names );
189  } else {
190  $real = false;
191  }
192 
193  # "ThisSite user(s) A, B and C"
194  if ( count( $user_names ) ) {
195  $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
196  count( $user_names ) )->escaped();
197  } else {
198  $user = false;
199  }
200 
201  if ( count( $anon_ips ) ) {
202  $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
203  count( $anon_ips ) )->escaped();
204  } else {
205  $anon = false;
206  }
207 
208  # This is the big list, all mooshed together. We sift for blank strings
209  $fullList = [];
210  foreach ( [ $real, $user, $anon, $others_link ] as $s ) {
211  if ( $s !== false ) {
212  $fullList[] = $s;
213  }
214  }
215 
216  $count = count( $fullList );
217 
218  # "Based on work by ..."
219  return $count
220  ? $this->msg( 'othercontribs' )->rawParams(
221  $lang->listToText( $fullList ) )->params( $count )->escaped()
222  : '';
223  }
224 
230  protected function link( User $user ) {
231  if ( $this->canShowRealUserName() && !$user->isAnon() ) {
232  $real = $user->getRealName();
233  if ( $real === '' ) {
234  $real = $user->getName();
235  }
236  } else {
237  $real = $user->getName();
238  }
239 
240  return Linker::userLink( $user->getId(), $user->getName(), $real );
241  }
242 
248  protected function userLink( User $user ) {
249  $link = $this->link( $user );
250  if ( $user->isAnon() ) {
251  return $this->msg( 'anonuser' )->rawParams( $link )->parse();
252  } elseif ( $this->canShowRealUserName() && $user->getRealName() ) {
253  return $link;
254  } else {
255  return $this->msg( 'siteuser' )->rawParams( $link )->params( $user->getName() )->escaped();
256  }
257  }
258 
263  protected function othersLink() {
264  return $this->linkRenderer->makeKnownLink(
265  $this->getTitle(),
266  $this->msg( 'others' )->text(),
267  [],
268  [ 'action' => 'credits' ]
269  );
270  }
271 }
getWikiPage()
Get a WikiPage object.
Definition: Action.php:198
IContextSource null $context
IContextSource if specified; otherwise we'll use the Context from the Page.
Definition: Action.php:66
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:219
getOutput()
Get the OutputPage being used for this instance.
Definition: Action.php:159
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: Action.php:231
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition: Action.php:188
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> element.
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 rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:214
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition: Linker.php:1091
Class that generates HTML anchor link elements for pages.
A class containing constants representing the names of configuration variables.
Creates User objects.
Definition: UserFactory.php:38
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:68
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:1922
getRealName()
Get the user's real name.
Definition: User.php:2337
getId( $wikiId=self::LOCAL)
Get the user's ID.
Definition: User.php:1889
isAnon()
Get whether the user is anonymous.
Definition: User.php:2555
getTimestamp()
Definition: WikiPage.php:840
getUserText( $audience=RevisionRecord::FOR_PUBLIC, Authority $performer=null)
Definition: WikiPage.php:908
Interface for objects which can provide a MediaWiki context on request.
Shared interface for rigor levels when dealing with User methods.
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