MediaWiki  master
CreditsAction.php
Go to the documentation of this file.
1 <?php
28 
33 
35  private $linkRenderer;
36 
38  private $userFactory;
39 
46  public function __construct(
47  Page $page,
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
163 
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 }
CreditsAction\$userFactory
UserFactory $userFactory
Definition: CreditsAction.php:38
CreditsAction\canShowRealUserName
canShowRealUserName()
Whether we can display the user's real name (not a hidden pref)
Definition: CreditsAction.php:133
Page
Interface for type hinting (accepts WikiPage, Article, ImagePage, CategoryPage)
Definition: Page.php:29
User\isAnon
isAnon()
Get whether the user is anonymous.
Definition: User.php:2989
Linker\userLink
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition: Linker.php:1064
FormlessAction
An action which just does something, without showing a form first.
Definition: FormlessAction.php:30
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
MediaWiki\Linker\LinkRenderer
Class that generates HTML links for pages.
Definition: LinkRenderer.php:43
WikiPage\getUserText
getUserText( $audience=RevisionRecord::FOR_PUBLIC, Authority $performer=null)
Definition: WikiPage.php:916
CreditsAction\getName
getName()
Return the name of the action this object responds to.
Definition: CreditsAction.php:57
CreditsAction\othersLink
othersLink()
Get a link to action=credits of $article page.
Definition: CreditsAction.php:260
$contributors
$contributors
Definition: updateCredits.php:38
CreditsAction\$linkRenderer
LinkRenderer $linkRenderer
Definition: CreditsAction.php:35
User\getId
getId( $wikiId=self::LOCAL)
Get the user's ID.
Definition: User.php:2082
CreditsAction\getCredits
getCredits( $cnt, $showIfMax=true)
Get a list of contributors.
Definition: CreditsAction.php:91
Action\getWikiPage
getWikiPage()
Get a WikiPage object.
Definition: Action.php:195
CreditsAction\link
link(User $user)
Get a link to $user's user page.
Definition: CreditsAction.php:227
Action\$context
IContextSource $context
IContextSource if specified; otherwise we'll use the Context from the Page.
Definition: Action.php:66
$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:61
User\getRealName
getRealName()
Get the user's real name.
Definition: User.php:2526
CreditsAction\getAuthor
getAuthor()
Get the last author with the last modification time.
Definition: CreditsAction.php:109
Action\getTitle
getTitle()
Shortcut to get the Title object from the page.
Definition: Action.php:216
CreditsAction\onView
onView()
This is largely cadged from PageHistory::history.
Definition: CreditsAction.php:70
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:58
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:228
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:210
Action\getLanguage
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition: Action.php:185
Action\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: Action.php:156
CreditsAction\getContributors
getContributors( $cnt, $showIfMax)
Get a list of contributors of $article.
Definition: CreditsAction.php:144
CreditsAction
Definition: CreditsAction.php:32
$t
$t
Definition: testCompression.php:74
WikiPage\getTimestamp
getTimestamp()
Definition: WikiPage.php:848
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:68
User\getName
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2115
CreditsAction\__construct
__construct(Page $page, IContextSource $context, LinkRenderer $linkRenderer, UserFactory $userFactory)
Definition: CreditsAction.php:46
MediaWiki\User\UserFactory
Creates User objects.
Definition: UserFactory.php:41
CreditsAction\userLink
userLink(User $user)
Get a link to $user's user page.
Definition: CreditsAction.php:245