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