Changing lines like this: "extract( $dbw->tableNames( 'page', 'archive' ) );" to...
[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. */
189 if ( !strlen( $options['offset'] ) || !preg_match( '/^[0-9]+$/', $options['offset'] ) )
190 $options['offset'] = '';
191
192 $title = SpecialPage::getTitleFor( 'Contributions' );
193 $options['target'] = $target;
194
195 $nt =& Title::makeTitle( NS_USER, $nt->getDBkey() );
196 $finder = new ContribsFinder( ( $target == 'newbies' ) ? 'newbies' : $nt->getText() );
197 $finder->setLimit( $options['limit'] );
198 $finder->setOffset( $options['offset'] );
199
200 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
201 $options['namespace'] = intval( $ns );
202 $finder->setNamespace( $options['namespace'] );
203 } else {
204 $options['namespace'] = '';
205 }
206
207 if ( $wgUser->isAllowed( 'rollback' ) && $wgRequest->getBool( 'bot' ) ) {
208 $options['bot'] = '1';
209 }
210
211 if ( $wgRequest->getText( 'go' ) == 'prev' ) {
212 $offset = $finder->getPreviousOffsetForPaging();
213 if ( $offset !== false ) {
214 $options['offset'] = $offset;
215 $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
216 $wgOut->redirect( $prevurl );
217 return;
218 }
219 }
220
221 if ( $wgRequest->getText( 'go' ) == 'first' && $target != 'newbies') {
222 $offset = $finder->getFirstOffsetForPaging();
223 if ( $offset !== false ) {
224 $options['offset'] = $offset;
225 $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
226 $wgOut->redirect( $prevurl );
227 return;
228 }
229 }
230
231 if ( $target == 'newbies' ) {
232 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
233 } else {
234 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', contributionsSub( $nt ) ) );
235 }
236
237 $id = User::idFromName( $nt->getText() );
238 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
239
240 $wgOut->addHTML( contributionsForm( $options) );
241
242 $contribs = $finder->find();
243
244 if ( count( $contribs ) == 0) {
245 $wgOut->addWikiText( wfMsg( 'nocontribs' ) );
246 return;
247 }
248
249 list( $early, $late ) = $finder->getEditLimits();
250 $lastts = count( $contribs ) ? $contribs[count( $contribs ) - 1]->rev_timestamp : 0;
251 $atstart = ( !count( $contribs ) || $late == $contribs[0]->rev_timestamp );
252 $atend = ( !count( $contribs ) || $early == $lastts );
253
254 // These four are defaults
255 $newestlink = wfMsgHtml( 'sp-contributions-newest' );
256 $oldestlink = wfMsgHtml( 'sp-contributions-oldest' );
257 $newerlink = wfMsgHtml( 'sp-contributions-newer', $options['limit'] );
258 $olderlink = wfMsgHtml( 'sp-contributions-older', $options['limit'] );
259
260 if ( !$atstart ) {
261 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => '' ), $options ) );
262 $newestlink = "<a href=\"$stuff\">$newestlink</a>";
263 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'prev' ), $options ) );
264 $newerlink = "<a href=\"$stuff\">$newerlink</a>";
265 }
266
267 if ( !$atend ) {
268 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'first' ), $options ) );
269 $oldestlink = "<a href=\"$stuff\">$oldestlink</a>";
270 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => $lastts ), $options ) );
271 $olderlink = "<a href=\"$stuff\">$olderlink</a>";
272 }
273
274 if ( $target == 'newbies' ) {
275 $firstlast ="($newestlink)";
276 } else {
277 $firstlast = "($newestlink | $oldestlink)";
278 }
279
280 $urls = array();
281 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
282 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'limit' => $num ), $options ) );
283 $urls[] = "<a href=\"$stuff\">".$wgLang->formatNum( $num )."</a>";
284 }
285 $bits = implode( $urls, ' | ' );
286
287 $prevnextbits = $firstlast .' '. wfMsgHtml( 'viewprevnext', $newerlink, $olderlink, $bits );
288
289 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
290
291 $wgOut->addHTML( "<ul>\n" );
292
293 $sk = $wgUser->getSkin();
294 foreach ( $contribs as $contrib )
295 $wgOut->addHTML( ucListEdit( $sk, $contrib ) );
296
297 $wgOut->addHTML( "</ul>\n" );
298 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
299 }
300
301 /**
302 * Generates the subheading with links
303 * @param $nt @see Title object for the target
304 */
305 function contributionsSub( $nt ) {
306 global $wgSysopUserBans, $wgLang, $wgUser;
307
308 $sk = $wgUser->getSkin();
309 $id = User::idFromName( $nt->getText() );
310
311 if ( 0 == $id ) {
312 $ul = $nt->getText();
313 } else {
314 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
315 }
316 $talk = $nt->getTalkPage();
317 if( $talk ) {
318 # Talk page link
319 $tools[] = $sk->makeLinkObj( $talk, $wgLang->getNsText( NS_TALK ) );
320 if( ( $id != 0 && $wgSysopUserBans ) || ( $id == 0 && User::isIP( $nt->getText() ) ) ) {
321 # Block link
322 if( $wgUser->isAllowed( 'block' ) )
323 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Blockip', $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
324 # Block log link
325 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ), htmlspecialchars( LogPage::logName( 'block' ) ), 'type=block&page=' . $nt->getPrefixedUrl() );
326 }
327 # Other logs link
328 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ), wfMsgHtml( 'log' ), 'user=' . $nt->getPartialUrl() );
329 $ul .= ' (' . implode( ' | ', $tools ) . ')';
330 }
331 return $ul;
332 }
333
334 /**
335 * Generates the namespace selector form with hidden attributes.
336 * @param $options Array: the options to be included.
337 */
338 function contributionsForm( $options ) {
339 global $wgScript, $wgTitle;
340
341 $options['title'] = $wgTitle->getPrefixedText();
342
343 $f = "<form method='get' action=\"$wgScript\">\n";
344 foreach ( $options as $name => $value ) {
345 if( $name === 'namespace') continue;
346 $f .= "\t" . wfElement( 'input', array(
347 'name' => $name,
348 'type' => 'hidden',
349 'value' => $value ) ) . "\n";
350 }
351
352 $f .= '<p>' . wfMsgHtml( 'namespace' ) . ' ' .
353 HTMLnamespaceselector( $options['namespace'], '' ) .
354 wfElement( 'input', array(
355 'type' => 'submit',
356 'value' => wfMsg( 'allpagessubmit' ) )
357 ) .
358 "</p></form>\n";
359
360 return $f;
361 }
362
363 /**
364 * Generates each row in the contributions list.
365 *
366 * Contributions which are marked "top" are currently on top of the history.
367 * For these contributions, a [rollback] link is shown for users with sysop
368 * privileges. The rollback link restores the most recent version that was not
369 * written by the target user.
370 *
371 * @todo This would probably look a lot nicer in a table.
372 */
373 function ucListEdit( $sk, $row ) {
374 $fname = 'ucListEdit';
375 wfProfileIn( $fname );
376
377 global $wgLang, $wgUser, $wgRequest;
378 static $messages;
379 if( !isset( $messages ) ) {
380 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
381 $messages[$msg] = wfMsgExt( $msg, array( 'escape') );
382 }
383 }
384
385 $rev = new Revision( $row );
386
387 $page = Title::makeTitle( $row->page_namespace, $row->page_title );
388 $link = $sk->makeKnownLinkObj( $page );
389 $difftext = $topmarktext = '';
390 if( $row->rev_id == $row->page_latest ) {
391 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
392 if( !$row->page_is_new ) {
393 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=0' ) . ')';
394 } else {
395 $difftext .= $messages['newarticle'];
396 }
397
398 if( $wgUser->isAllowed( 'rollback' ) ) {
399 $topmarktext .= ' '.$sk->generateRollback( $rev );
400 }
401
402 }
403 if( $rev->userCan( Revision::DELETED_TEXT ) ) {
404 $difftext = '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=prev&oldid='.$row->rev_id ) . ')';
405 } else {
406 $difftext = '(' . $messages['diff'] . ')';
407 }
408 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
409
410 $comment = $sk->revComment( $rev );
411 $d = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->rev_timestamp ), true );
412
413 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
414 $d = '<span class="history-deleted">' . $d . '</span>';
415 }
416
417 if( $row->rev_minor_edit ) {
418 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
419 } else {
420 $mflag = '';
421 }
422
423 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
424 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
425 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
426 }
427 $ret = "<li>$ret</li>\n";
428 wfProfileOut( $fname );
429 return $ret;
430 }
431
432 ?>