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