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