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