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