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