Merge "Revert "Restore search box tabindex""
[mediawiki.git] / includes / actions / CreditsAction.php
blob2bb1be44c5a71ef85f2a9a684f1cf42f2b53cfd2
1 <?php
2 /**
3 * Formats credits for articles
5 * Copyright 2004, Evan Prodromou <evan@wikitravel.org>.
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.
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.
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
21 * @file
22 * @ingroup Actions
23 * @author <evan@wikitravel.org>
26 /**
27 * @ingroup Actions
29 class CreditsAction extends FormlessAction {
31 public function getName() {
32 return 'credits';
35 protected function getDescription() {
36 return $this->msg( 'creditspage' )->escaped();
39 /**
40 * This is largely cadged from PageHistory::history
42 * @return string HTML
44 public function onView() {
45 wfProfileIn( __METHOD__ );
47 if ( $this->page->getID() == 0 ) {
48 $s = $this->msg( 'nocredits' )->parse();
49 } else {
50 $s = $this->getCredits( -1 );
53 wfProfileOut( __METHOD__ );
55 return Html::rawElement( 'div', array( 'id' => 'mw-credits' ), $s );
58 /**
59 * Get a list of contributors
61 * @param int $cnt Maximum list of contributors to show
62 * @param bool $showIfMax Whether to contributors if there more than $cnt
63 * @return string html
65 public function getCredits( $cnt, $showIfMax = true ) {
66 wfProfileIn( __METHOD__ );
67 $s = '';
69 if ( $cnt != 0 ) {
70 $s = $this->getAuthor( $this->page );
71 if ( $cnt > 1 || $cnt < 0 ) {
72 $s .= ' ' . $this->getContributors( $cnt - 1, $showIfMax );
76 wfProfileOut( __METHOD__ );
78 return $s;
81 /**
82 * Get the last author with the last modification time
83 * @param Page $page
84 * @return string HTML
86 protected function getAuthor( Page $page ) {
87 $user = User::newFromName( $page->getUserText(), false );
89 $timestamp = $page->getTimestamp();
90 if ( $timestamp ) {
91 $lang = $this->getLanguage();
92 $d = $lang->date( $page->getTimestamp(), true );
93 $t = $lang->time( $page->getTimestamp(), true );
94 } else {
95 $d = '';
96 $t = '';
99 return $this->msg( 'lastmodifiedatby', $d, $t )->rawParams(
100 $this->userLink( $user ) )->params( $user->getName() )->escaped();
104 * Get a list of contributors of $article
105 * @param int $cnt Maximum list of contributors to show
106 * @param bool $showIfMax Whether to contributors if there more than $cnt
107 * @return string html
109 protected function getContributors( $cnt, $showIfMax ) {
110 global $wgHiddenPrefs;
112 $contributors = $this->page->getContributors();
114 $others_link = false;
116 # Hmm... too many to fit!
117 if ( $cnt > 0 && $contributors->count() > $cnt ) {
118 $others_link = $this->othersLink();
119 if ( !$showIfMax ) {
120 return $this->msg( 'othercontribs' )->rawParams(
121 $others_link )->params( $contributors->count() )->escaped();
125 $real_names = array();
126 $user_names = array();
127 $anon_ips = array();
129 # Sift for real versus user names
130 /** @var $user User */
131 foreach ( $contributors as $user ) {
132 $cnt--;
133 if ( $user->isLoggedIn() ) {
134 $link = $this->link( $user );
135 if ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
136 $real_names[] = $link;
137 } else {
138 $user_names[] = $link;
140 } else {
141 $anon_ips[] = $this->link( $user );
144 if ( $cnt == 0 ) {
145 break;
149 $lang = $this->getLanguage();
151 if ( count( $real_names ) ) {
152 $real = $lang->listToText( $real_names );
153 } else {
154 $real = false;
157 # "ThisSite user(s) A, B and C"
158 if ( count( $user_names ) ) {
159 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
160 count( $user_names ) )->escaped();
161 } else {
162 $user = false;
165 if ( count( $anon_ips ) ) {
166 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
167 count( $anon_ips ) )->escaped();
168 } else {
169 $anon = false;
172 # This is the big list, all mooshed together. We sift for blank strings
173 $fulllist = array();
174 foreach ( array( $real, $user, $anon, $others_link ) as $s ) {
175 if ( $s !== false ) {
176 array_push( $fulllist, $s );
180 $count = count( $fulllist );
182 # "Based on work by ..."
183 return $count
184 ? $this->msg( 'othercontribs' )->rawParams(
185 $lang->listToText( $fulllist ) )->params( $count )->escaped()
186 : '';
190 * Get a link to $user's user page
191 * @param User $user
192 * @return string Html
194 protected function link( User $user ) {
195 global $wgHiddenPrefs;
196 if ( !in_array( 'realname', $wgHiddenPrefs ) && !$user->isAnon() ) {
197 $real = $user->getRealName();
198 } else {
199 $real = false;
202 $page = $user->isAnon()
203 ? SpecialPage::getTitleFor( 'Contributions', $user->getName() )
204 : $user->getUserPage();
206 return Linker::link( $page, htmlspecialchars( $real ? $real : $user->getName() ) );
210 * Get a link to $user's user page
211 * @param User $user
212 * @return string Html
214 protected function userLink( User $user ) {
215 $link = $this->link( $user );
216 if ( $user->isAnon() ) {
217 return $this->msg( 'anonuser' )->rawParams( $link )->parse();
218 } else {
219 global $wgHiddenPrefs;
220 if ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
221 return $link;
222 } else {
223 return $this->msg( 'siteuser' )->rawParams( $link )->params( $user->getName() )->escaped();
229 * Get a link to action=credits of $article page
230 * @return string HTML link
232 protected function othersLink() {
233 return Linker::linkKnown(
234 $this->getTitle(),
235 $this->msg( 'others' )->escaped(),
236 array(),
237 array( 'action' => 'credits' )