Credits.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * Credits.php -- formats credits for articles
  4. * Copyright 2004, Evan Prodromou <evan@wikitravel.org>.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  19. *
  20. * @author <evan@wikitravel.org>
  21. */
  22. class Credits {
  23. /**
  24. * This is largely cadged from PageHistory::history
  25. * @param $article Article object
  26. */
  27. public static function showPage( Article $article ) {
  28. global $wgOut;
  29. wfProfileIn( __METHOD__ );
  30. $wgOut->setPageTitle( $article->mTitle->getPrefixedText() );
  31. $wgOut->setSubtitle( wfMsg( 'creditspage' ) );
  32. $wgOut->setArticleFlag( false );
  33. $wgOut->setArticleRelated( true );
  34. $wgOut->setRobotPolicy( 'noindex,nofollow' );
  35. if( $article->mTitle->getArticleID() == 0 ) {
  36. $s = wfMsg( 'nocredits' );
  37. } else {
  38. $s = self::getCredits($article, -1 );
  39. }
  40. $wgOut->addHTML( $s );
  41. wfProfileOut( __METHOD__ );
  42. }
  43. /**
  44. * Get a list of contributors of $article
  45. * @param $article Article object
  46. * @param $cnt Int: maximum list of contributors to show
  47. * @param $showIfMax Bool: whether to contributors if there more than $cnt
  48. * @return String: html
  49. */
  50. public static function getCredits($article, $cnt, $showIfMax=true) {
  51. wfProfileIn( __METHOD__ );
  52. $s = '';
  53. if( isset( $cnt ) && $cnt != 0 ){
  54. $s = self::getAuthor( $article );
  55. if ($cnt > 1 || $cnt < 0) {
  56. $s .= ' ' . self::getContributors( $article, $cnt - 1, $showIfMax );
  57. }
  58. }
  59. wfProfileOut( __METHOD__ );
  60. return $s;
  61. }
  62. /**
  63. * Get the last author with the last modification time
  64. * @param $article Article object
  65. */
  66. protected static function getAuthor( Article $article ){
  67. global $wgLang, $wgAllowRealName;
  68. $user = User::newFromId( $article->getUser() );
  69. $timestamp = $article->getTimestamp();
  70. if( $timestamp ){
  71. $d = $wgLang->date( $article->getTimestamp(), true );
  72. $t = $wgLang->time( $article->getTimestamp(), true );
  73. } else {
  74. $d = '';
  75. $t = '';
  76. }
  77. return wfMsg( 'lastmodifiedatby', $d, $t, self::userLink( $user ) );
  78. }
  79. /**
  80. * Get a list of contributors of $article
  81. * @param $article Article object
  82. * @param $cnt Int: maximum list of contributors to show
  83. * @param $showIfMax Bool: whether to contributors if there more than $cnt
  84. * @return String: html
  85. */
  86. protected static function getContributors( Article $article, $cnt, $showIfMax ) {
  87. global $wgLang, $wgAllowRealName;
  88. $contributors = $article->getContributors();
  89. $others_link = '';
  90. # Hmm... too many to fit!
  91. if( $cnt > 0 && $contributors->count() > $cnt ){
  92. $others_link = self::othersLink( $article );
  93. if( !$showIfMax )
  94. return wfMsg( 'othercontribs', $others_link );
  95. }
  96. $real_names = array();
  97. $user_names = array();
  98. $anon = 0;
  99. # Sift for real versus user names
  100. foreach( $contributors as $user ) {
  101. $cnt--;
  102. if( $user->isLoggedIn() ){
  103. $link = self::link( $user );
  104. if( $wgAllowRealName && $user->getRealName() )
  105. $real_names[] = $link;
  106. else
  107. $user_names[] = $link;
  108. } else {
  109. $anon++;
  110. }
  111. if( $cnt == 0 ) break;
  112. }
  113. # Two strings: real names, and user names
  114. $real = $wgLang->listToText( $real_names );
  115. $user = $wgLang->listToText( $user_names );
  116. if( $anon )
  117. $anon = wfMsgExt( 'anonymous', array( 'parseinline' ), $anon );
  118. # "ThisSite user(s) A, B and C"
  119. if( !empty( $user ) ){
  120. $user = wfMsgExt( 'siteusers', array( 'parsemag' ), $user, count( $user_names ) );
  121. }
  122. # This is the big list, all mooshed together. We sift for blank strings
  123. $fulllist = array();
  124. foreach( array( $real, $user, $anon, $others_link ) as $s ){
  125. if( !empty( $s ) ){
  126. array_push( $fulllist, $s );
  127. }
  128. }
  129. # Make the list into text...
  130. $creds = $wgLang->listToText( $fulllist );
  131. # "Based on work by ..."
  132. return empty( $creds ) ? '' : wfMsg( 'othercontribs', $creds );
  133. }
  134. /**
  135. * Get a link to $user_name page
  136. * @param $user User object
  137. * @return String: html
  138. */
  139. protected static function link( User $user ) {
  140. global $wgUser, $wgAllowRealName;
  141. if( $wgAllowRealName )
  142. $real = $user->getRealName();
  143. else
  144. $real = false;
  145. $skin = $wgUser->getSkin();
  146. $page = $user->getUserPage();
  147. return $skin->link( $page, htmlspecialchars( $real ? $real : $user->getName() ) );
  148. }
  149. /**
  150. * Get a link to $user_name page
  151. * @param $user_name String: user name
  152. * @param $linkText String: optional display
  153. * @return String: html
  154. */
  155. protected static function userLink( User $user ) {
  156. global $wgUser, $wgAllowRealName;
  157. if( $user->isAnon() ){
  158. return wfMsgExt( 'anonymous', array( 'parseinline' ), 1 );
  159. } else {
  160. $link = self::link( $user );
  161. if( $wgAllowRealName && $user->getRealName() )
  162. return $link;
  163. else
  164. return wfMsgExt( 'siteuser', array( 'parseinline', 'replaceafter' ), $link );
  165. }
  166. }
  167. /**
  168. * Get a link to action=credits of $article page
  169. * @param $article Article object
  170. * @return String: html
  171. */
  172. protected static function othersLink( Article $article ) {
  173. global $wgUser;
  174. $skin = $wgUser->getSkin();
  175. return $skin->link( $article->getTitle(), wfMsgHtml( 'others' ), array(), array( 'action' => 'credits' ), array( 'known' ) );
  176. }
  177. }