f5d3a9796212076e211650472537d34eacf5f2b5
[lhc/web/wiklou.git] / includes / SpecialContributions.php
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage SpecialPage
5 */
6
7 /** @package MediaWiki */
8 class ContribsFinder {
9 var $username, $offset, $limit, $namespace;
10 var $dbr;
11
12 /**
13 * Constructor
14 * @param $username Username as a string
15 */
16 function ContribsFinder( $username ) {
17 $this->username = $username;
18 $this->namespace = false;
19 $this->dbr =& wfGetDB( DB_SLAVE );
20 }
21
22 function setNamespace( $ns ) {
23 $this->namespace = $ns;
24 }
25
26 function setLimit( $limit ) {
27 $this->limit = $limit;
28 }
29
30 function setOffset( $offset ) {
31 $this->offset = $offset;
32 }
33
34 /**
35 * Get timestamp of either first or last contribution made by the user.
36 * @todo Maybe it should be private ?
37 * @param $dir string 'ASC' or 'DESC'.
38 * @return Revision timestamp (rev_timestamp).
39 */
40 function getEditLimit( $dir ) {
41 list( $index, $usercond ) = $this->getUserCond();
42 $nscond = $this->getNamespaceCond();
43 $use_index = $this->dbr->useIndexClause( $index );
44 list( $revision, $page) = $this->dbr->tableNamesN( 'revision', 'page' );
45 $sql = "SELECT rev_timestamp " .
46 " FROM $page,$revision $use_index " .
47 " WHERE rev_page=page_id AND $usercond $nscond" .
48 " ORDER BY rev_timestamp $dir LIMIT 1";
49
50 $res = $this->dbr->query( $sql, __METHOD__ );
51 $row = $this->dbr->fetchObject( $res );
52 if ( $row ) {
53 return $row->rev_timestamp;
54 } else {
55 return false;
56 }
57 }
58
59 /**
60 * Get timestamps of first and last contributions made by the user.
61 * @return Array containing first rev_timestamp and last rev_timestamp.
62 */
63 function getEditLimits() {
64 return array(
65 $this->getEditLimit( "ASC" ),
66 $this->getEditLimit( "DESC" )
67 );
68 }
69
70 function getUserCond() {
71 $condition = '';
72
73 if ( $this->username == 'newbies' ) {
74 $max = $this->dbr->selectField( 'user', 'max(user_id)', false, 'make_sql' );
75 $condition = '>' . (int)($max - $max / 100);
76 }
77
78 if ( $condition == '' ) {
79 $condition = ' rev_user_text=' . $this->dbr->addQuotes( $this->username );
80 $index = 'usertext_timestamp';
81 } else {
82 $condition = ' rev_user '.$condition ;
83 $index = 'user_timestamp';
84 }
85 return array( $index, $condition );
86 }
87
88 function getNamespaceCond() {
89 if ( $this->namespace !== false )
90 return ' AND page_namespace = ' . (int)$this->namespace;
91 return '';
92 }
93
94 /**
95 * @return Timestamp of first entry in previous page.
96 */
97 function getPreviousOffsetForPaging() {
98 list( $index, $usercond ) = $this->getUserCond();
99 $nscond = $this->getNamespaceCond();
100
101 $use_index = $this->dbr->useIndexClause( $index );
102 list( $page, $revision ) = $this->dbr->tableNamesN( 'page', 'revision' );
103
104 $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " .
105 "WHERE page_id = rev_page AND rev_timestamp > '" . $this->offset . "' AND " .
106 $usercond . $nscond;
107 $sql .= " ORDER BY rev_timestamp ASC";
108 $sql = $this->dbr->limitResult( $sql, $this->limit, 0 );
109 $res = $this->dbr->query( $sql );
110
111 $numRows = $this->dbr->numRows( $res );
112 if ( $numRows ) {
113 $this->dbr->dataSeek( $res, $numRows - 1 );
114 $row = $this->dbr->fetchObject( $res );
115 $offset = $row->rev_timestamp;
116 } else {
117 $offset = false;
118 }
119 $this->dbr->freeResult( $res );
120 return $offset;
121 }
122
123 /**
124 * @return Timestamp of first entry in next page.
125 */
126 function getFirstOffsetForPaging() {
127 list( $index, $usercond ) = $this->getUserCond();
128 $use_index = $this->dbr->useIndexClause( $index );
129 list( $page, $revision ) = $this->dbr->tableNamesN( 'page', 'revision' );
130 $nscond = $this->getNamespaceCond();
131 $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " .
132 "WHERE page_id = rev_page AND " .
133 $usercond . $nscond;
134 $sql .= " ORDER BY rev_timestamp ASC";
135 $sql = $this->dbr->limitResult( $sql, $this->limit, 0 );
136 $res = $this->dbr->query( $sql );
137
138 $numRows = $this->dbr->numRows( $res );
139 if ( $numRows ) {
140 $this->dbr->dataSeek( $res, $numRows - 1 );
141 $row = $this->dbr->fetchObject( $res );
142 $offset = $row->rev_timestamp;
143 } else {
144 $offset = false;
145 }
146 $this->dbr->freeResult( $res );
147 return $offset;
148 }
149
150 /* private */ function makeSql() {
151 $offsetQuery = '';
152
153 list( $page, $revision ) = $this->dbr->tableNamesN( 'page', 'revision' );
154 list( $index, $userCond ) = $this->getUserCond();
155
156 if ( $this->offset )
157 $offsetQuery = "AND rev_timestamp < '{$this->offset}'";
158
159 $nscond = $this->getNamespaceCond();
160 $use_index = $this->dbr->useIndexClause( $index );
161 $sql = 'SELECT ' .
162 'page_namespace,page_title,page_is_new,page_latest,'.
163 'rev_id,rev_page,rev_text_id,rev_timestamp,rev_comment,rev_minor_edit,rev_user,rev_user_text,'.
164 'rev_deleted ' .
165 "FROM $page,$revision $use_index " .
166 "WHERE page_id=rev_page AND $userCond $nscond $offsetQuery " .
167 'ORDER BY rev_timestamp DESC';
168 $sql = $this->dbr->limitResult( $sql, $this->limit, 0 );
169 return $sql;
170 }
171
172 /**
173 * This do the search for the user given when creating the object.
174 * It should probably be the only public function in this class.
175 * @return Array of contributions.
176 */
177 function find() {
178 $contribs = array();
179 $res = $this->dbr->query( $this->makeSql(), __METHOD__ );
180 while ( $c = $this->dbr->fetchObject( $res ) )
181 $contribs[] = $c;
182 $this->dbr->freeResult( $res );
183 return $contribs;
184 }
185 };
186
187 /**
188 * Special page "user contributions".
189 * Shows a list of the contributions of a user.
190 *
191 * @return none
192 * @param $par String: (optional) user name of the user for which to show the contributions
193 */
194 function wfSpecialContributions( $par = null ) {
195 global $wgUser, $wgOut, $wgLang, $wgRequest;
196
197 $target = isset( $par ) ? $par : $wgRequest->getVal( 'target' );
198 if ( !strlen( $target ) ) {
199 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
200 return;
201 }
202
203 $nt = Title::newFromURL( $target );
204 if ( !$nt ) {
205 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
206 return;
207 }
208
209 $options = array();
210
211 list( $options['limit'], $options['offset']) = wfCheckLimits();
212 $options['offset'] = $wgRequest->getVal( 'offset' );
213 /* Offset must be an integral. */
214 if ( !strlen( $options['offset'] ) || !preg_match( '/^[0-9]+$/', $options['offset'] ) )
215 $options['offset'] = '';
216
217 $title = SpecialPage::getTitleFor( 'Contributions' );
218 $options['target'] = $target;
219
220 $nt =& Title::makeTitle( NS_USER, $nt->getDBkey() );
221 $finder = new ContribsFinder( ( $target == 'newbies' ) ? 'newbies' : $nt->getText() );
222 $finder->setLimit( $options['limit'] );
223 $finder->setOffset( $options['offset'] );
224
225 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
226 $options['namespace'] = intval( $ns );
227 $finder->setNamespace( $options['namespace'] );
228 } else {
229 $options['namespace'] = '';
230 }
231
232 if ( $wgUser->isAllowed( 'rollback' ) && $wgRequest->getBool( 'bot' ) ) {
233 $options['bot'] = '1';
234 }
235
236 if ( $wgRequest->getText( 'go' ) == 'prev' ) {
237 $offset = $finder->getPreviousOffsetForPaging();
238 if ( $offset !== false ) {
239 $options['offset'] = $offset;
240 $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
241 $wgOut->redirect( $prevurl );
242 return;
243 }
244 }
245
246 if ( $wgRequest->getText( 'go' ) == 'first' && $target != 'newbies') {
247 $offset = $finder->getFirstOffsetForPaging();
248 if ( $offset !== false ) {
249 $options['offset'] = $offset;
250 $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
251 $wgOut->redirect( $prevurl );
252 return;
253 }
254 }
255
256 if ( $target == 'newbies' ) {
257 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
258 } else {
259 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', contributionsSub( $nt ) ) );
260 }
261
262 $id = User::idFromName( $nt->getText() );
263 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
264
265 $wgOut->addHTML( contributionsForm( $options) );
266
267 $contribs = $finder->find();
268
269 if ( count( $contribs ) == 0) {
270 $wgOut->addWikiText( wfMsg( 'nocontribs' ) );
271 return;
272 }
273
274 list( $early, $late ) = $finder->getEditLimits();
275 $lastts = count( $contribs ) ? $contribs[count( $contribs ) - 1]->rev_timestamp : 0;
276 $atstart = ( !count( $contribs ) || $late == $contribs[0]->rev_timestamp );
277 $atend = ( !count( $contribs ) || $early == $lastts );
278
279 // These four are defaults
280 $newestlink = wfMsgHtml( 'sp-contributions-newest' );
281 $oldestlink = wfMsgHtml( 'sp-contributions-oldest' );
282 $newerlink = wfMsgHtml( 'sp-contributions-newer', $options['limit'] );
283 $olderlink = wfMsgHtml( 'sp-contributions-older', $options['limit'] );
284
285 if ( !$atstart ) {
286 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => '' ), $options ) );
287 $newestlink = "<a href=\"$stuff\">$newestlink</a>";
288 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'prev' ), $options ) );
289 $newerlink = "<a href=\"$stuff\">$newerlink</a>";
290 }
291
292 if ( !$atend ) {
293 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'first' ), $options ) );
294 $oldestlink = "<a href=\"$stuff\">$oldestlink</a>";
295 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => $lastts ), $options ) );
296 $olderlink = "<a href=\"$stuff\">$olderlink</a>";
297 }
298
299 if ( $target == 'newbies' ) {
300 $firstlast ="($newestlink)";
301 } else {
302 $firstlast = "($newestlink | $oldestlink)";
303 }
304
305 $urls = array();
306 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
307 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'limit' => $num ), $options ) );
308 $urls[] = "<a href=\"$stuff\">".$wgLang->formatNum( $num )."</a>";
309 }
310 $bits = implode( $urls, ' | ' );
311
312 $prevnextbits = $firstlast .' '. wfMsgHtml( 'viewprevnext', $newerlink, $olderlink, $bits );
313
314 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
315
316 $wgOut->addHTML( "<ul>\n" );
317
318 $sk = $wgUser->getSkin();
319 foreach ( $contribs as $contrib )
320 $wgOut->addHTML( ucListEdit( $sk, $contrib ) );
321
322 $wgOut->addHTML( "</ul>\n" );
323 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
324 }
325
326 /**
327 * Generates the subheading with links
328 * @param $nt @see Title object for the target
329 */
330 function contributionsSub( $nt ) {
331 global $wgSysopUserBans, $wgLang, $wgUser;
332
333 $sk = $wgUser->getSkin();
334 $id = User::idFromName( $nt->getText() );
335
336 if ( 0 == $id ) {
337 $ul = $nt->getText();
338 } else {
339 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
340 }
341 $talk = $nt->getTalkPage();
342 if( $talk ) {
343 # Talk page link
344 $tools[] = $sk->makeLinkObj( $talk, $wgLang->getNsText( NS_TALK ) );
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 $ul .= ' (' . implode( ' | ', $tools ) . ')';
355 }
356 return $ul;
357 }
358
359 /**
360 * Generates the namespace selector form with hidden attributes.
361 * @param $options Array: the options to be included.
362 */
363 function contributionsForm( $options ) {
364 global $wgScript, $wgTitle;
365
366 $options['title'] = $wgTitle->getPrefixedText();
367
368 $f = "<form method='get' action=\"$wgScript\">\n";
369 foreach ( $options as $name => $value ) {
370 if( $name === 'namespace') continue;
371 $f .= "\t" . wfElement( 'input', array(
372 'name' => $name,
373 'type' => 'hidden',
374 'value' => $value ) ) . "\n";
375 }
376
377 $f .= '<p>' . wfMsgHtml( 'namespace' ) . ' ' .
378 HTMLnamespaceselector( $options['namespace'], '' ) .
379 wfElement( 'input', array(
380 'type' => 'submit',
381 'value' => wfMsg( 'allpagessubmit' ) )
382 ) .
383 "</p></form>\n";
384
385 return $f;
386 }
387
388 /**
389 * Generates each row in the contributions list.
390 *
391 * Contributions which are marked "top" are currently on top of the history.
392 * For these contributions, a [rollback] link is shown for users with sysop
393 * privileges. The rollback link restores the most recent version that was not
394 * written by the target user.
395 *
396 * @todo This would probably look a lot nicer in a table.
397 */
398 function ucListEdit( $sk, $row ) {
399 $fname = 'ucListEdit';
400 wfProfileIn( $fname );
401
402 global $wgLang, $wgUser, $wgRequest;
403 static $messages;
404 if( !isset( $messages ) ) {
405 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
406 $messages[$msg] = wfMsgExt( $msg, array( 'escape') );
407 }
408 }
409
410 $rev = new Revision( $row );
411
412 $page = Title::makeTitle( $row->page_namespace, $row->page_title );
413 $link = $sk->makeKnownLinkObj( $page );
414 $difftext = $topmarktext = '';
415 if( $row->rev_id == $row->page_latest ) {
416 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
417 if( !$row->page_is_new ) {
418 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=0' ) . ')';
419 } else {
420 $difftext .= $messages['newarticle'];
421 }
422
423 if( $wgUser->isAllowed( 'rollback' ) ) {
424 $topmarktext .= ' '.$sk->generateRollback( $rev );
425 }
426
427 }
428 if( $rev->userCan( Revision::DELETED_TEXT ) ) {
429 $difftext = '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=prev&oldid='.$row->rev_id ) . ')';
430 } else {
431 $difftext = '(' . $messages['diff'] . ')';
432 }
433 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
434
435 $comment = $sk->revComment( $rev );
436 $d = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->rev_timestamp ), true );
437
438 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
439 $d = '<span class="history-deleted">' . $d . '</span>';
440 }
441
442 if( $row->rev_minor_edit ) {
443 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
444 } else {
445 $mflag = '';
446 }
447
448 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
449 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
450 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
451 }
452 $ret = "<li>$ret</li>\n";
453 wfProfileOut( $fname );
454 return $ret;
455 }
456
457 ?>