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