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