MediaWiki  master
CreditsAction.php
Go to the documentation of this file.
1 <?php
32 
37 
39  private $linkRenderer;
40 
42  private $userFactory;
43 
50  public function __construct(
51  Article $article,
53  LinkRenderer $linkRenderer,
54  UserFactory $userFactory
55  ) {
56  parent::__construct( $article, $context );
57  $this->linkRenderer = $linkRenderer;
58  $this->userFactory = $userFactory;
59  }
60 
61  public function getName() {
62  return 'credits';
63  }
64 
65  protected function getDescription() {
66  return $this->msg( 'creditspage' )->escaped();
67  }
68 
74  public function onView() {
75  $this->getOutput()->addModuleStyles( [
76  'mediawiki.action.styles',
77  ] );
78 
79  if ( $this->getWikiPage()->getId() === 0 ) {
80  $s = $this->msg( 'nocredits' )->parse();
81  } else {
82  $s = $this->getCredits( -1 );
83  }
84 
85  return Html::rawElement( 'div', [ 'id' => 'mw-credits' ], $s );
86  }
87 
95  public function getCredits( $cnt, $showIfMax = true ) {
96  $s = '';
97 
98  if ( $cnt !== 0 ) {
99  $s = $this->getAuthor();
100  if ( $cnt > 1 || $cnt < 0 ) {
101  $s .= ' ' . $this->getContributors( $cnt - 1, $showIfMax );
102  }
103  }
104 
105  return $s;
106  }
107 
113  private function getAuthor() {
114  $page = $this->getWikiPage();
115  $user = $this->userFactory->newFromName( $page->getUserText(), UserRigorOptions::RIGOR_NONE );
116 
117  $timestamp = $page->getTimestamp();
118  if ( $timestamp ) {
119  $lang = $this->getLanguage();
120  $d = $lang->date( $page->getTimestamp(), true );
121  $t = $lang->time( $page->getTimestamp(), true );
122  } else {
123  $d = '';
124  $t = '';
125  }
126 
127  return $this->msg( 'lastmodifiedatby', $d, $t )->rawParams(
128  // @phan-suppress-next-line PhanTypeMismatchArgumentNullable RIGOR_NONE never returns null
129  $this->userLink( $user ) )->params( $user->getName() )->escaped();
130  }
131 
138  protected function canShowRealUserName() {
139  $hiddenPrefs = $this->context->getConfig()->get( MainConfigNames::HiddenPrefs );
140  return !in_array( 'realname', $hiddenPrefs );
141  }
142 
149  protected function getContributors( $cnt, $showIfMax ) {
150  $contributors = $this->getWikiPage()->getContributors();
151 
152  $others_link = false;
153 
154  # Hmm... too many to fit!
155  if ( $cnt > 0 && $contributors->count() > $cnt ) {
156  $others_link = $this->othersLink();
157  if ( !$showIfMax ) {
158  return $this->msg( 'othercontribs' )->rawParams(
159  $others_link )->params( $contributors->count() )->escaped();
160  }
161  }
162 
163  $real_names = [];
164  $user_names = [];
165  $anon_ips = [];
166 
167  # Sift for real versus user names
169  foreach ( $contributors as $user ) {
170  $cnt--;
171  if ( $user->isRegistered() ) {
172  $link = $this->link( $user );
173  if ( $this->canShowRealUserName() && $user->getRealName() ) {
174  $real_names[] = $link;
175  } else {
176  $user_names[] = $link;
177  }
178  } else {
179  $anon_ips[] = $this->link( $user );
180  }
181 
182  if ( $cnt === 0 ) {
183  break;
184  }
185  }
186 
187  $lang = $this->getLanguage();
188 
189  if ( count( $real_names ) ) {
190  $real = $lang->listToText( $real_names );
191  } else {
192  $real = false;
193  }
194 
195  # "ThisSite user(s) A, B and C"
196  if ( count( $user_names ) ) {
197  $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
198  count( $user_names ) )->escaped();
199  } else {
200  $user = false;
201  }
202 
203  if ( count( $anon_ips ) ) {
204  $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
205  count( $anon_ips ) )->escaped();
206  } else {
207  $anon = false;
208  }
209 
210  # This is the big list, all mooshed together. We sift for blank strings
211  $fullList = [];
212  foreach ( [ $real, $user, $anon, $others_link ] as $s ) {
213  if ( $s !== false ) {
214  $fullList[] = $s;
215  }
216  }
217 
218  $count = count( $fullList );
219 
220  # "Based on work by ..."
221  return $count
222  ? $this->msg( 'othercontribs' )->rawParams(
223  $lang->listToText( $fullList ) )->params( $count )->escaped()
224  : '';
225  }
226 
232  protected function link( User $user ) {
233  if ( $this->canShowRealUserName() && !$user->isAnon() ) {
234  $real = $user->getRealName();
235  if ( $real === '' ) {
236  $real = $user->getName();
237  }
238  } else {
239  $real = $user->getName();
240  }
241 
242  return Linker::userLink( $user->getId(), $user->getName(), $real );
243  }
244 
250  protected function userLink( User $user ) {
251  $link = $this->link( $user );
252  if ( $user->isAnon() ) {
253  return $this->msg( 'anonuser' )->rawParams( $link )->parse();
254  } elseif ( $this->canShowRealUserName() && $user->getRealName() ) {
255  return $link;
256  } else {
257  return $this->msg( 'siteuser' )->rawParams( $link )->params( $user->getName() )->escaped();
258  }
259  }
260 
265  protected function othersLink() {
266  return $this->linkRenderer->makeKnownLink(
267  $this->getTitle(),
268  $this->msg( 'others' )->text(),
269  [],
270  [ 'action' => 'credits' ]
271  );
272  }
273 }
getWikiPage()
Get a WikiPage object.
Definition: Action.php:200
IContextSource null $context
IContextSource if specified; otherwise we'll use the Context from the Page.
Definition: Action.php:58
getTitle()
Shortcut to get the Title object from the page.
Definition: Action.php:221
getOutput()
Get the OutputPage being used for this instance.
Definition: Action.php:151
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: Action.php:233
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition: Action.php:190
Legacy class representing an editable page and handling UI for some page actions.
Definition: Article.php:56
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)
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.
__construct(Article $article, IContextSource $context, LinkRenderer $linkRenderer, UserFactory $userFactory)
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.
This class is a collection of static functions that serve two purposes:
Definition: Html.php:55
Class that generates HTML for internal links.
Some internal bits split of from Skin.php.
Definition: Linker.php:67
A class containing constants representing the names of configuration variables.
Creates User objects.
Definition: UserFactory.php:42
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:71
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:1684
getRealName()
Get the user's real name.
Definition: User.php:2099
getId( $wikiId=self::LOCAL)
Get the user's ID.
Definition: User.php:1651
isAnon()
Get whether the user is anonymous.
Definition: User.php:2324
Interface for objects which can provide a MediaWiki context on request.
Shared interface for rigor levels when dealing with User methods.
if(!isset( $args[0])) $lang
$contributors