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