And even more documentation
[lhc/web/wiklou.git] / includes / actions / CreditsAction.php
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
26 class CreditsAction extends FormlessAction {
27
28 public function getName() {
29 return 'credits';
30 }
31
32 public function getRestriction() {
33 return null;
34 }
35
36 /**
37 * This is largely cadged from PageHistory::history
38 */
39 public function onView() {
40 wfProfileIn( __METHOD__ );
41
42 if ( $this->page->getID() == 0 ) {
43 $s = wfMsg( 'nocredits' );
44 } else {
45 $s = $this->getCredits( -1 );
46 }
47
48 wfProfileOut( __METHOD__ );
49
50 return $s;
51 }
52
53 /**
54 * Get a list of contributors
55 *
56 * @param $cnt Int: maximum list of contributors to show
57 * @param $showIfMax Bool: whether to contributors if there more than $cnt
58 * @return String: html
59 */
60 public function getCredits( $cnt, $showIfMax = true ) {
61 wfProfileIn( __METHOD__ );
62 $s = '';
63
64 if ( isset( $cnt ) && $cnt != 0 ) {
65 $s = self::getAuthor( $this->page );
66 if ( $cnt > 1 || $cnt < 0 ) {
67 $s .= ' ' . $this->getContributors( $cnt - 1, $showIfMax );
68 }
69 }
70
71 wfProfileOut( __METHOD__ );
72 return $s;
73 }
74
75 /**
76 * Get the last author with the last modification time
77 * @param $article Article object
78 */
79 protected static function getAuthor( Article $article ) {
80 global $wgLang;
81
82 $user = User::newFromId( $article->getUser() );
83
84 $timestamp = $article->getTimestamp();
85 if ( $timestamp ) {
86 $d = $wgLang->date( $article->getTimestamp(), true );
87 $t = $wgLang->time( $article->getTimestamp(), true );
88 } else {
89 $d = '';
90 $t = '';
91 }
92 return wfMessage( 'lastmodifiedatby', $d, $t )->rawParams( self::userLink( $user ) )->params( $user->getName() )->escaped();
93 }
94
95 /**
96 * Get a list of contributors of $article
97 * @param $article Article object
98 * @param $cnt Int: maximum list of contributors to show
99 * @param $showIfMax Bool: whether to contributors if there more than $cnt
100 * @return String: html
101 */
102 protected function getContributors( $cnt, $showIfMax ) {
103 global $wgLang, $wgHiddenPrefs;
104
105 $contributors = $this->page->getContributors();
106
107 $others_link = false;
108
109 # Hmm... too many to fit!
110 if ( $cnt > 0 && $contributors->count() > $cnt ) {
111 $others_link = $this->othersLink();
112 if ( !$showIfMax )
113 return wfMessage( 'othercontribs' )->rawParams( $others_link )->params( $contributors->count() )->escaped();
114 }
115
116 $real_names = array();
117 $user_names = array();
118 $anon_ips = array();
119
120 # Sift for real versus user names
121 foreach ( $contributors as $user ) {
122 $cnt--;
123 if ( $user->isLoggedIn() ) {
124 $link = self::link( $user );
125 if ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
126 $real_names[] = $link;
127 } else {
128 $user_names[] = $link;
129 }
130 } else {
131 $anon_ips[] = self::link( $user );
132 }
133
134 if ( $cnt == 0 ) {
135 break;
136 }
137 }
138
139 if ( count( $real_names ) ) {
140 $real = $wgLang->listToText( $real_names );
141 } else {
142 $real = false;
143 }
144
145 # "ThisSite user(s) A, B and C"
146 if ( count( $user_names ) ) {
147 $user = wfMessage( 'siteusers' )->rawParams( $wgLang->listToText( $user_names ) )->params(
148 count( $user_names ) )->escaped();
149 } else {
150 $user = false;
151 }
152
153 if ( count( $anon_ips ) ) {
154 $anon = wfMessage( 'anonusers' )->rawParams( $wgLang->listToText( $anon_ips ) )->params(
155 count( $anon_ips ) )->escaped();
156 } else {
157 $anon = false;
158 }
159
160 # This is the big list, all mooshed together. We sift for blank strings
161 $fulllist = array();
162 foreach ( array( $real, $user, $anon, $others_link ) as $s ) {
163 if ( $s !== false ) {
164 array_push( $fulllist, $s );
165 }
166 }
167
168 $count = count( $fulllist );
169 # "Based on work by ..."
170 return $count
171 ? wfMessage( 'othercontribs' )->rawParams(
172 $wgLang->listToText( $fulllist ) )->params( $count )->escaped()
173 : '';
174 }
175
176 /**
177 * Get a link to $user's user page
178 * @param $user User object
179 * @return String: html
180 */
181 protected static function link( User $user ) {
182 global $wgHiddenPrefs;
183 if ( !in_array( 'realname', $wgHiddenPrefs ) && !$user->isAnon() ) {
184 $real = $user->getRealName();
185 } else {
186 $real = false;
187 }
188
189 $page = $user->isAnon()
190 ? SpecialPage::getTitleFor( 'Contributions', $user->getName() )
191 : $user->getUserPage();
192
193 return Linker::link( $page, htmlspecialchars( $real ? $real : $user->getName() ) );
194 }
195
196 /**
197 * Get a link to $user's user page
198 * @param $user User object
199 * @return String: html
200 */
201 protected static function userLink( User $user ) {
202 $link = self::link( $user );
203 if ( $user->isAnon() ) {
204 return wfMsgExt( 'anonuser', array( 'parseinline', 'replaceafter' ), $link );
205 } else {
206 global $wgHiddenPrefs;
207 if ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
208 return $link;
209 } else {
210 return wfMessage( 'siteuser' )->rawParams( $link )->params( $user->getName() )->escaped();
211 }
212 }
213 }
214
215 /**
216 * Get a link to action=credits of $article page
217 * @param $article Article object
218 * @return String: html
219 */
220 protected function othersLink() {
221 return Linker::link(
222 $this->getTitle(),
223 wfMsgHtml( 'others' ),
224 array(),
225 array( 'action' => 'credits' ),
226 array( 'known' )
227 );
228 }
229 }