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