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  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
135 
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 }
CreditsAction\canShowRealUserName
canShowRealUserName()
Whether we can display the user's real name (not a hidden pref)
Definition: CreditsAction.php:105
User\isAnon
isAnon()
Get whether the user is anonymous.
Definition: User.php:2922
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:155
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
CreditsAction\getName
getName()
Return the name of the action this object responds to.
Definition: CreditsAction.php:33
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:538
User\getUserPage
getUserPage()
Get this user's personal page title.
Definition: User.php:3639
$s
$s
Definition: mergeMessageFileList.php:185
SpecialPage\getTitleFor
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,...
Definition: SpecialPage.php:92
WikiPage\getUserText
getUserText( $audience=RevisionRecord::FOR_PUBLIC, User $user=null)
Definition: WikiPage.php:857
CreditsAction\othersLink
othersLink()
Get a link to action=credits of $article page.
Definition: CreditsAction.php:237
$contributors
$contributors
Definition: updateCredits.php:42
CreditsAction\getCredits
getCredits( $cnt, $showIfMax=true)
Get a list of contributors.
Definition: CreditsAction.php:63
Action\getWikiPage
getWikiPage()
Get a WikiPage object.
Definition: Action.php:278
CreditsAction\link
link(User $user)
Get a link to $user's user page.
Definition: CreditsAction.php:199
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:2473
CreditsAction\getAuthor
getAuthor()
Get the last author with the last modification time.
Definition: CreditsAction.php:81
Action\getTitle
getTitle()
Shortcut to get the Title object from the page.
Definition: Action.php:299
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:311
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
Action\getLanguage
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition: Action.php:268
CreditsAction\getContributors
getContributors( $cnt, $showIfMax)
Get a list of contributors of $article.
Definition: CreditsAction.php:116
CreditsAction
Definition: CreditsAction.php:31
$t
$t
Definition: testCompression.php:74
WikiPage\getTimestamp
getTimestamp()
Definition: WikiPage.php:776
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:55
User\getName
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:1954
CreditsAction\userLink
userLink(User $user)
Get a link to $user's user page.
Definition: CreditsAction.php:222