CreditsAction.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * Formats credits for articles
  4. *
  5. * Copyright 2004, Evan Prodromou <evan@wikitravel.org>.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  20. *
  21. * @file
  22. * @ingroup Actions
  23. * @author <evan@wikitravel.org>
  24. */
  25. use MediaWiki\MediaWikiServices;
  26. /**
  27. * @ingroup Actions
  28. */
  29. class CreditsAction extends FormlessAction {
  30. public function getName() {
  31. return 'credits';
  32. }
  33. protected function getDescription() {
  34. return $this->msg( 'creditspage' )->escaped();
  35. }
  36. /**
  37. * This is largely cadged from PageHistory::history
  38. *
  39. * @return string HTML
  40. */
  41. public function onView() {
  42. if ( $this->page->getID() == 0 ) {
  43. $s = $this->msg( 'nocredits' )->parse();
  44. } else {
  45. $s = $this->getCredits( -1 );
  46. }
  47. return Html::rawElement( 'div', [ 'id' => 'mw-credits' ], $s );
  48. }
  49. /**
  50. * Get a list of contributors
  51. *
  52. * @param int $cnt Maximum list of contributors to show
  53. * @param bool $showIfMax Whether to contributors if there more than $cnt
  54. * @return string Html
  55. */
  56. public function getCredits( $cnt, $showIfMax = true ) {
  57. $s = '';
  58. if ( $cnt != 0 ) {
  59. $s = $this->getAuthor( $this->page );
  60. if ( $cnt > 1 || $cnt < 0 ) {
  61. $s .= ' ' . $this->getContributors( $cnt - 1, $showIfMax );
  62. }
  63. }
  64. return $s;
  65. }
  66. /**
  67. * Get the last author with the last modification time
  68. * @param Page $page
  69. * @return string HTML
  70. */
  71. protected function getAuthor( Page $page ) {
  72. $user = User::newFromName( $page->getUserText(), false );
  73. $timestamp = $page->getTimestamp();
  74. if ( $timestamp ) {
  75. $lang = $this->getLanguage();
  76. $d = $lang->date( $page->getTimestamp(), true );
  77. $t = $lang->time( $page->getTimestamp(), true );
  78. } else {
  79. $d = '';
  80. $t = '';
  81. }
  82. return $this->msg( 'lastmodifiedatby', $d, $t )->rawParams(
  83. $this->userLink( $user ) )->params( $user->getName() )->escaped();
  84. }
  85. /**
  86. * Whether we can display the user's real name (not a hidden pref)
  87. *
  88. * @since 1.24
  89. * @return bool
  90. */
  91. protected function canShowRealUserName() {
  92. $hiddenPrefs = $this->context->getConfig()->get( 'HiddenPrefs' );
  93. return !in_array( 'realname', $hiddenPrefs );
  94. }
  95. /**
  96. * Get a list of contributors of $article
  97. * @param int $cnt Maximum list of contributors to show
  98. * @param bool $showIfMax Whether to contributors if there more than $cnt
  99. * @return string Html
  100. */
  101. protected function getContributors( $cnt, $showIfMax ) {
  102. $contributors = $this->page->getContributors();
  103. $others_link = false;
  104. # Hmm... too many to fit!
  105. if ( $cnt > 0 && $contributors->count() > $cnt ) {
  106. $others_link = $this->othersLink();
  107. if ( !$showIfMax ) {
  108. return $this->msg( 'othercontribs' )->rawParams(
  109. $others_link )->params( $contributors->count() )->escaped();
  110. }
  111. }
  112. $real_names = [];
  113. $user_names = [];
  114. $anon_ips = [];
  115. # Sift for real versus user names
  116. /** @var User $user */
  117. foreach ( $contributors as $user ) {
  118. $cnt--;
  119. if ( $user->isLoggedIn() ) {
  120. $link = $this->link( $user );
  121. if ( $this->canShowRealUserName() && $user->getRealName() ) {
  122. $real_names[] = $link;
  123. } else {
  124. $user_names[] = $link;
  125. }
  126. } else {
  127. $anon_ips[] = $this->link( $user );
  128. }
  129. if ( $cnt == 0 ) {
  130. break;
  131. }
  132. }
  133. $lang = $this->getLanguage();
  134. if ( count( $real_names ) ) {
  135. $real = $lang->listToText( $real_names );
  136. } else {
  137. $real = false;
  138. }
  139. # "ThisSite user(s) A, B and C"
  140. if ( count( $user_names ) ) {
  141. $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
  142. count( $user_names ) )->escaped();
  143. } else {
  144. $user = false;
  145. }
  146. if ( count( $anon_ips ) ) {
  147. $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
  148. count( $anon_ips ) )->escaped();
  149. } else {
  150. $anon = false;
  151. }
  152. # This is the big list, all mooshed together. We sift for blank strings
  153. $fulllist = [];
  154. foreach ( [ $real, $user, $anon, $others_link ] as $s ) {
  155. if ( $s !== false ) {
  156. array_push( $fulllist, $s );
  157. }
  158. }
  159. $count = count( $fulllist );
  160. # "Based on work by ..."
  161. return $count
  162. ? $this->msg( 'othercontribs' )->rawParams(
  163. $lang->listToText( $fulllist ) )->params( $count )->escaped()
  164. : '';
  165. }
  166. /**
  167. * Get a link to $user's user page
  168. * @param User $user
  169. * @return string Html
  170. */
  171. protected function link( User $user ) {
  172. if ( $this->canShowRealUserName() && !$user->isAnon() ) {
  173. $real = $user->getRealName();
  174. if ( $real === '' ) {
  175. $real = $user->getName();
  176. }
  177. } else {
  178. $real = $user->getName();
  179. }
  180. $page = $user->isAnon()
  181. ? SpecialPage::getTitleFor( 'Contributions', $user->getName() )
  182. : $user->getUserPage();
  183. return MediaWikiServices::getInstance()
  184. ->getLinkRenderer()->makeLink( $page, $real );
  185. }
  186. /**
  187. * Get a link to $user's user page
  188. * @param User $user
  189. * @return string Html
  190. */
  191. protected function userLink( User $user ) {
  192. $link = $this->link( $user );
  193. if ( $user->isAnon() ) {
  194. return $this->msg( 'anonuser' )->rawParams( $link )->parse();
  195. } elseif ( $this->canShowRealUserName() && $user->getRealName() ) {
  196. return $link;
  197. } else {
  198. return $this->msg( 'siteuser' )->rawParams( $link )->params( $user->getName() )->escaped();
  199. }
  200. }
  201. /**
  202. * Get a link to action=credits of $article page
  203. * @return string HTML link
  204. */
  205. protected function othersLink() {
  206. return MediaWikiServices::getInstance()->getLinkRenderer()->makeKnownLink(
  207. $this->getTitle(),
  208. $this->msg( 'others' )->text(),
  209. [],
  210. [ 'action' => 'credits' ]
  211. );
  212. }
  213. }