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