74a6a95f1e0fe1a96dd5a2e2785e472e090a963c
[lhc/web/wiklou.git] / includes / SpecialContributions.php
1 <?php
2 /**
3 * Special:Contributions, show user contributions in a paged list
4 * @addtogroup SpecialPage
5 */
6
7 class ContribsPager extends IndexPager {
8 public $mDefaultDirection = true;
9 var $messages, $target;
10 var $namespace = '', $mDb;
11
12 function __construct( $target, $namespace = false ) {
13 parent::__construct();
14 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
15 $this->messages[$msg] = wfMsgExt( $msg, array( 'escape') );
16 }
17 $this->target = $target;
18 $this->namespace = $namespace;
19 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
20 }
21
22 function getDefaultQuery() {
23 $query = parent::getDefaultQuery();
24 $query['target'] = $this->target;
25 return $query;
26 }
27
28 function getQueryInfo() {
29 list( $index, $userCond ) = $this->getUserCond();
30 $conds = array_merge( array( 'page_id=rev_page' ), $userCond, $this->getNamespaceCond() );
31
32 return array(
33 'tables' => array( 'page', 'revision' ),
34 'fields' => array(
35 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'rev_id', 'rev_page',
36 'rev_text_id', 'rev_timestamp', 'rev_comment', 'rev_minor_edit', 'rev_user',
37 'rev_user_text', 'rev_deleted'
38 ),
39 'conds' => $conds,
40 'options' => array( 'FORCE INDEX' => $index )
41 );
42 }
43
44 function getUserCond() {
45 $condition = array();
46
47 if ( $this->target == 'newbies' ) {
48 $max = $this->mDb->selectField( 'user', 'max(user_id)', false, __METHOD__ );
49 $condition[] = 'rev_user >' . (int)($max - $max / 100);
50 $index = 'user_timestamp';
51 } else {
52 $condition['rev_user_text'] = $this->target;
53 $index = 'usertext_timestamp';
54 }
55 return array( $index, $condition );
56 }
57
58 function getNamespaceCond() {
59 if ( $this->namespace !== '' ) {
60 return array( 'page_namespace' => (int)$this->namespace );
61 } else {
62 return array();
63 }
64 }
65
66 function getIndexField() {
67 return 'rev_timestamp';
68 }
69
70 function getStartBody() {
71 return "<ul>\n";
72 }
73
74 function getEndBody() {
75 return "</ul>\n";
76 }
77
78 function getNavigationBar() {
79 if ( isset( $this->mNavigationBar ) ) {
80 return $this->mNavigationBar;
81 }
82 $linkTexts = array(
83 'prev' => wfMsgHtml( "sp-contributions-newer", $this->mLimit ),
84 'next' => wfMsgHtml( 'sp-contributions-older', $this->mLimit ),
85 'first' => wfMsgHtml('sp-contributions-newest'),
86 'last' => wfMsgHtml( 'sp-contributions-oldest' )
87 );
88
89 $pagingLinks = $this->getPagingLinks( $linkTexts );
90 $limitLinks = $this->getLimitLinks();
91 $limits = implode( ' | ', $limitLinks );
92
93 $this->mNavigationBar = "({$pagingLinks['first']} | {$pagingLinks['last']}) " .
94 wfMsgHtml("viewprevnext", $pagingLinks['prev'], $pagingLinks['next'], $limits);
95 return $this->mNavigationBar;
96 }
97
98 /**
99 * Generates each row in the contributions list.
100 *
101 * Contributions which are marked "top" are currently on top of the history.
102 * For these contributions, a [rollback] link is shown for users with sysop
103 * privileges. The rollback link restores the most recent version that was not
104 * written by the target user.
105 *
106 * @todo This would probably look a lot nicer in a table.
107 */
108 function formatRow( $row ) {
109 wfProfileIn( __METHOD__ );
110
111 global $wgLang, $wgUser;
112
113 $sk = $this->getSkin();
114 $rev = new Revision( $row );
115
116 $page = Title::makeTitle( $row->page_namespace, $row->page_title );
117 $link = $sk->makeKnownLinkObj( $page );
118 $difftext = $topmarktext = '';
119 if( $row->rev_id == $row->page_latest ) {
120 $topmarktext .= '<strong>' . $this->messages['uctop'] . '</strong>';
121 if( !$row->page_is_new ) {
122 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $this->messages['diff'], 'diff=0' ) . ')';
123 } else {
124 $difftext .= $this->messages['newarticle'];
125 }
126
127 if( $wgUser->isAllowed( 'rollback' ) ) {
128 $topmarktext .= ' '.$sk->generateRollback( $rev );
129 }
130
131 }
132 if( $rev->userCan( Revision::DELETED_TEXT ) ) {
133 $difftext = '(' . $sk->makeKnownLinkObj( $page, $this->messages['diff'], 'diff=prev&oldid='.$row->rev_id ) . ')';
134 } else {
135 $difftext = '(' . $this->messages['diff'] . ')';
136 }
137 $histlink='('.$sk->makeKnownLinkObj( $page, $this->messages['hist'], 'action=history' ) . ')';
138
139 $comment = $sk->revComment( $rev );
140 $d = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->rev_timestamp ), true );
141
142 if( $this->target == 'newbies' ) {
143 $userlink = ' . . ' . $sk->userLink( $row->rev_user, $row->rev_user_text );
144 $userlink .= ' (' . $sk->userTalkLink( $row->rev_user, $row->rev_user_text ) . ') ';
145 } else {
146 $userlink = '';
147 }
148
149 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
150 $d = '<span class="history-deleted">' . $d . '</span>';
151 }
152
153 if( $row->rev_minor_edit ) {
154 $mflag = '<span class="minor">' . $this->messages['minoreditletter'] . '</span> ';
155 } else {
156 $mflag = '';
157 }
158
159 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link}{$userlink}{$comment} {$topmarktext}";
160 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
161 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
162 }
163 $ret = "<li>$ret</li>\n";
164 wfProfileOut( __METHOD__ );
165 return $ret;
166 }
167
168 /**
169 * Get the Database object in use
170 *
171 * @return Database
172 */
173 public function getDatabase() {
174 return $this->mDb;
175 }
176
177 }
178
179 /**
180 * Special page "user contributions".
181 * Shows a list of the contributions of a user.
182 *
183 * @return none
184 * @param $par String: (optional) user name of the user for which to show the contributions
185 */
186 function wfSpecialContributions( $par = null ) {
187 global $wgUser, $wgOut, $wgLang, $wgRequest;
188
189 $options = array();
190
191 if ( isset( $par ) && $par == 'newbies' ) {
192 $target = 'newbies';
193 $options['contribs'] = 'newbie';
194 } elseif ( isset( $par ) ) {
195 $target = $par;
196 } else {
197 $target = $wgRequest->getVal( 'target' );
198 }
199
200 // check for radiobox
201 if ( $wgRequest->getVal( 'contribs' ) == 'newbie' ) {
202 $target = 'newbies';
203 $options['contribs'] = 'newbie';
204 }
205
206 if ( !strlen( $target ) ) {
207 $wgOut->addHTML( contributionsForm( '' ) );
208 return;
209 }
210
211 $options['limit'] = $wgRequest->getInt( 'limit', 50 );
212 $options['target'] = $target;
213
214 $nt = Title::makeTitleSafe( NS_USER, $target );
215 if ( !$nt ) {
216 $wgOut->addHTML( contributionsForm( '' ) );
217 return;
218 }
219 $id = User::idFromName( $nt->getText() );
220
221 if ( $target != 'newbies' ) {
222 $target = $nt->getText();
223 $wgOut->setSubtitle( contributionsSub( $nt, $id ) );
224 } else {
225 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
226 }
227
228 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
229 $options['namespace'] = intval( $ns );
230 } else {
231 $options['namespace'] = '';
232 }
233 if ( $wgUser->isAllowed( 'rollback' ) && $wgRequest->getBool( 'bot' ) ) {
234 $options['bot'] = '1';
235 }
236
237 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
238
239 $wgOut->addHTML( contributionsForm( $options ) );
240
241 $pager = new ContribsPager( $target, $options['namespace'] );
242 if ( !$pager->getNumRows() ) {
243 $wgOut->addWikiText( wfMsg( 'nocontribs' ) );
244 return;
245 }
246
247 # Show a message about slave lag, if applicable
248 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
249 $wgOut->showLagWarning( $lag );
250
251 $wgOut->addHTML(
252 '<p>' . $pager->getNavigationBar() . '</p>' .
253 $pager->getBody() .
254 '<p>' . $pager->getNavigationBar() . '</p>' );
255
256 # If there were contributions, and it was a valid user or IP, show
257 # the appropriate "footer" message - WHOIS tools, etc.
258 if( $target != 'newbies' ) {
259 $message = IP::isIPAddress( $target )
260 ? 'sp-contributions-footer-anon'
261 : 'sp-contributions-footer';
262
263
264 $text = wfMsg( $message, $target );
265 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
266 $wgOut->addHtml( '<div class="mw-contributions-footer">' );
267 $wgOut->addWikiText( $text );
268 $wgOut->addHtml( '</div>' );
269 }
270 }
271 }
272
273 /**
274 * Generates the subheading with links
275 * @param Title $nt Title object for the target
276 * @param integer $id User ID for the target
277 * @return String: appropriately-escaped HTML to be output literally
278 */
279 function contributionsSub( $nt, $id ) {
280 global $wgSysopUserBans, $wgLang, $wgUser;
281
282 $sk = $wgUser->getSkin();
283
284 if ( 0 == $id ) {
285 $user = $nt->getText();
286 } else {
287 $user = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
288 }
289 $talk = $nt->getTalkPage();
290 if( $talk ) {
291 # Talk page link
292 $tools[] = $sk->makeLinkObj( $talk, wfMsgHtml( 'talkpagelinktext' ) );
293 if( ( $id != 0 && $wgSysopUserBans ) || ( $id == 0 && User::isIP( $nt->getText() ) ) ) {
294 # Block link
295 if( $wgUser->isAllowed( 'block' ) )
296 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Blockip', $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
297 # Block log link
298 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ), wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
299 }
300 # Other logs link
301 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ), wfMsgHtml( 'log' ), 'user=' . $nt->getPartialUrl() );
302 $links = implode( ' | ', $tools );
303 }
304
305 // Old message 'contribsub' had one parameter, but that doesn't work for
306 // languages that want to put the "for" bit right after $user but before
307 // $links. If 'contribsub' is around, use it for reverse compatibility,
308 // otherwise use 'contribsub2'.
309 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
310 return wfMsgHtml( 'contribsub2', $user, $links );
311 } else {
312 return wfMsgHtml( 'contribsub', "$user ($links)" );
313 }
314 }
315
316 /**
317 * Generates the namespace selector form with hidden attributes.
318 * @param $options Array: the options to be included.
319 */
320 function contributionsForm( $options ) {
321 global $wgScript, $wgTitle, $wgRequest;
322
323 $options['title'] = $wgTitle->getPrefixedText();
324 if ( !isset( $options['target'] ) ) {
325 $options['target'] = '';
326 } else {
327 $options['target'] = str_replace( '_' , ' ' , $options['target'] );
328 }
329
330 if ( !isset( $options['namespace'] ) ) {
331 $options['namespace'] = '';
332 }
333
334 if ( !isset( $options['contribs'] ) ) {
335 $options['contribs'] = 'user';
336 }
337
338 if ( $options['contribs'] == 'newbie' ) {
339 $options['target'] = '';
340 }
341
342 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
343
344 foreach ( $options as $name => $value ) {
345 if ( in_array( $name, array( 'namespace', 'target', 'contribs' ) ) ) {
346 continue;
347 }
348 $f .= "\t" . Xml::hidden( $name, $value ) . "\n";
349 }
350
351 $f .= '<fieldset>' .
352 Xml::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
353 Xml::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ), 'contribs' , 'newbie' , 'newbie', $options['contribs'] == 'newbie' ? true : false ) . '<br />' .
354 Xml::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parseinline' ) ), 'contribs' , 'user', 'user', $options['contribs'] == 'user' ? true : false ) . ' ' .
355 Xml::input( 'target', 20, $options['target']) . ' '.
356 Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
357 Xml::namespaceSelector( $options['namespace'], '' ) .
358 Xml::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
359 '</fieldset>' .
360 Xml::closeElement( 'form' );
361 return $f;
362 }
363
364
365 ?>