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