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