bugfix: if the upload path is absolute, don't prepend the server URL
[lhc/web/wiklou.git] / includes / SpecialContributions.php
index 11f2366..1750b4d 100644 (file)
 <?php
-
-function wfSpecialContributions( $par = "" )
-{
-       global $wgUser, $wgOut, $wgLang, $wgRequest;
-       $fname = "wfSpecialContributions";
-       $sysop = $wgUser->isSysop();
-
-       if( $par )
-               $target = $par;
-       else
-               $target = $wgRequest->getVal( 'target' );
-
-       if ( "" == $target ) {
-               $wgOut->errorpage( "notargettitle", "notargettext" );
+/**
+ * @package MediaWiki
+ * @subpackage SpecialPage
+ */
+
+/**
+ * Special page "user contributions".
+ * Shows a list of the contributions of a user.
+ *
+ * @return     none
+ * @param      string  $par    (optional) user name of the user for which to show the contributions
+ */
+function wfSpecialContributions( $par = null ) {
+       global $wgUser, $wgOut, $wgLang, $wgContLang, $wgRequest;
+       $fname = 'wfSpecialContributions';
+
+       // GET values
+       $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
+       $namespace = $wgRequest->getVal( 'namespace', '' );
+       $namespace = $namespace === '' ? NULL : $namespace;
+       $invert = $wgRequest->getBool( 'invert' );
+       $hideminor = ($wgRequest->getBool( 'hideminor' ) ? true : false);
+       
+       if ( '' == $target ) {
+               $wgOut->errorpage( 'notargettitle', 'notargettext' );
                return;
        }
-       
+
        # FIXME: Change from numeric offsets to date offsets
-       list( $limit, $offset ) = wfCheckLimits( 50, "" );
-       $offlimit = $limit + $offset;
-       $querylimit = $offlimit + 1;
-       $hideminor = ($wgRequest->getVal( 'hideminor' ) ? 1 : 0);
+       list( $limit, $offset ) = wfCheckLimits( 50, '' );
+       $querylimit = $limit + 1;
+       $sk = $wgUser->getSkin();
+       $dbr =& wfGetDB( DB_SLAVE );
+       $userCond = "";
 
        $nt = Title::newFromURL( $target );
-       $nt->setNamespace( Namespace::getUser() );
+       if ( !$nt ) {
+               $wgOut->errorpage( 'notargettitle', 'notargettext' );
+               return;
+       }
+       $nt =& Title::makeTitle( NS_USER, $nt->getDBkey() );
 
-       $sk = $wgUser->getSkin();
        $id = User::idFromName( $nt->getText() );
 
        if ( 0 == $id ) {
                $ul = $nt->getText();
        } else {
-               $ul = $sk->makeLinkObj( $nt, $nt->getText() );
+               $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
+               $userCond = '=' . $id;
        }
        $talk = $nt->getTalkPage();
-       if( $talk )
-               $ul .= " (" . $sk->makeLinkObj( $talk, $wgLang->getNsText(Namespace::getTalk(0)) ) . ")";
-       else
-               $ul .= "brrrp";
-       $wgOut->setSubtitle( wfMsg( "contribsub", $ul ) );
+       if( $talk ) {
+               $ul .= ' (' . $sk->makeLinkObj( $talk, $wgLang->getNsText( NS_TALK ) ) . ')';
+       }
+
+
+       if ( $target == 'newbies' ) {
+               # View the contributions of all recently created accounts
+               $max = $dbr->selectField( 'user', 'max(user_id)', false, $fname );
+               $userCond = '>' . ($max - $max / 100);
+               $ul = wfMsg ( 'newbies' );
+               $id = 0;
+       }
+
+       $wgOut->setSubtitle( wfMsg( 'contribsub', $ul ) );
 
        if ( $hideminor ) {
-               $cmq = "AND cur_minor_edit=0";
-               $omq = "AND old_minor_edit=0";
-               $mlink = $sk->makeKnownLink( $wgLang->specialPage( "Contributions" ),
-                 WfMsg( "show" ), "target=" . wfEscapeHTML( $nt->getPrefixedURL() ) .
-                 "&offset={$offset}&limit={$limit}&hideminor=0" );
+               $minorQuery = "AND rev_minor_edit=0";
+               $mlink = $sk->makeKnownLink( $wgContLang->specialPage( "Contributions" ),
+                 WfMsg( "show" ), "target=" . htmlspecialchars( $nt->getPrefixedURL() ) .
+                 "&offset={$offset}&limit={$limit}&hideminor=0&namespace={$namespace}" );
        } else {
-               $cmq = $omq = "";
-               $mlink = $sk->makeKnownLink( $wgLang->specialPage( "Contributions" ),
-                 WfMsg( "hide" ), "target=" . wfEscapeHTML( $nt->getPrefixedURL() ) .
-                 "&offset={$offset}&limit={$limit}&hideminor=1" );
+               $minorQuery = "";
+               $mlink = $sk->makeKnownLink( $wgContLang->specialPage( "Contributions" ),
+                 WfMsg( 'hide' ), 'target=' . htmlspecialchars( $nt->getPrefixedURL() ) .
+                 "&offset={$offset}&limit={$limit}&hideminor=1&namespace={$namespace}" );
        }
-
-       if ( 0 == $id ) {
-               $sql = "SELECT cur_namespace,cur_title,cur_timestamp,cur_comment,cur_minor_edit FROM cur " .
-                 "WHERE cur_user_text='" . wfStrencode( $nt->getText() ) . "' {$cmq} " .
-                 "ORDER BY inverse_timestamp LIMIT {$querylimit}";
-               $res1 = wfQuery( $sql, DB_READ, $fname );
-
-               $sql = "SELECT old_namespace,old_title,old_timestamp,old_comment,old_minor_edit FROM old " .
-                 "WHERE old_user_text='" . wfStrencode( $nt->getText() ) . "' {$omq} " .
-                 "ORDER BY inverse_timestamp LIMIT {$querylimit}";
-               $res2 = wfQuery( $sql, DB_READ, $fname );
+       
+       if( !is_null($namespace) ) {
+               $minorQuery .= ' AND page_namespace ' . ($invert ? '!' : '') . "= {$namespace}";
+       }
+       
+       extract( $dbr->tableNames( 'page', 'revision' ) );
+       if ( $userCond == "" ) {
+               $condition = "rev_user_text=" . $dbr->addQuotes( $nt->getText() );
+               $index = 'usertext_timestamp';
        } else {
-               $sql = "SELECT cur_namespace,cur_title,cur_timestamp,cur_comment,cur_minor_edit FROM cur " .
-                 "WHERE cur_user={$id} {$cmq} ORDER BY inverse_timestamp LIMIT {$querylimit}";
-               $res1 = wfQuery( $sql, DB_READ, $fname );
-
-               $sql = "SELECT old_namespace,old_title,old_timestamp,old_comment,old_minor_edit FROM old " .
-                 "WHERE old_user={$id} {$omq} ORDER BY inverse_timestamp LIMIT {$querylimit}";
-               $res2 = wfQuery( $sql, DB_READ, $fname );
+               $condition = "rev_user {$userCond}";
+               $index = 'user_timestamp';
        }
-       $nCur = wfNumRows( $res1 );
-       $nOld = wfNumRows( $res2 );
+
+
+       $use_index = $dbr->useIndexClause( $index );
+       $sql = "SELECT
+               page_namespace,page_title,page_is_new,page_latest,
+               rev_id,rev_timestamp,rev_comment,rev_minor_edit,rev_user_text,
+               rev_deleted
+               FROM $page,$revision $use_index
+               WHERE page_id=rev_page AND $condition $minorQuery " .
+         "ORDER BY rev_timestamp DESC " . $dbr->limitResult( $querylimit, $offset );
+       $res = $dbr->query( $sql, $fname );
+       $numRows = $dbr->numRows( $res );
+
+       $wgOut->addHTML( ucNamespaceForm( $target, $hideminor, $namespace, $invert ) );
 
        $top = wfShowingResults( $offset, $limit );
        $wgOut->addHTML( "<p>{$top}\n" );
 
        $sl = wfViewPrevNext( $offset, $limit,
-         $wgLang->specialpage( "Contributions" ),
-         "hideminor={$hideminor}&target=" . wfUrlEncode( $target ),
-         ($nCur + $nOld) <= $offlimit);
+         $wgContLang->specialpage( "Contributions" ),
+         "hideminor={$hideminor}&namespace={$namespace}&target=" . wfUrlEncode( $target ),
+         ($numRows) <= $limit);
+
+       $shm = wfMsg( "showhideminor", $mlink );
+       $wgOut->addHTML( "<br />{$sl} ($shm)</p>\n");
 
-        $shm = wfMsg( "showhideminor", $mlink );
-       $wgOut->addHTML( "<br>{$sl} ($shm) \n");
-       
 
-       if ( 0 == $nCur && 0 == $nOld ) {
+       if ( 0 == $numRows ) {
                $wgOut->addHTML( "\n<p>" . wfMsg( "nocontribs" ) . "</p>\n" );
                return;
        }
-       if ( 0 != $nCur ) { $obj1 = wfFetchObject( $res1 ); }
-       if ( 0 != $nOld ) { $obj2 = wfFetchObject( $res2 ); }
 
        $wgOut->addHTML( "<ul>\n" );
-       for( $n = 0; $n < $offlimit; $n++ ) {
-               if ( 0 == $nCur && 0 == $nOld ) { break; }
-
-               if ( ( 0 == $nOld ) ||
-                 ( ( 0 != $nCur ) &&
-                 ( $obj1->cur_timestamp >= $obj2->old_timestamp ) ) ) {
-                       $ns = $obj1->cur_namespace;
-                       $t = $obj1->cur_title;
-                       $ts = $obj1->cur_timestamp;
-                       $comment =$obj1->cur_comment;
-                       $me = $obj1->cur_minor_edit;
-
-                       $obj1 = wfFetchObject( $res1 );
-                       $topmark = true;                        
-                       --$nCur;
-               } else {
-                       $ns = $obj2->old_namespace;
-                       $t = $obj2->old_title;
-                       $ts = $obj2->old_timestamp;
-                       $comment =$obj2->old_comment;
-                       $me = $obj2->old_minor_edit;
-
-                       $obj2 = wfFetchObject( $res2 );
-                       $topmark = false;
-                       --$nOld;
+       $n = 0;
+       while( $obj = $dbr->fetchObject( $res ) ) {
+               if( ++$n > $limit ) {
+                       // Extra row for determining 'next'ability, don't display
+                       break;
                }
-               if( $n >= $offset )
-                       ucListEdit( $sk, $ns, $t, $ts, $topmark, $comment, ( $me > 0) );
+               $wgOut->addHTML( ucListEdit( $sk, $obj ) );
        }
+       $dbr->freeResult( $res );
        $wgOut->addHTML( "</ul>\n" );
+
+       $wgOut->addHTML( "<br />{$sl} ($shm)\n");
 }
 
-function ucListEdit( $sk, $ns, $t, $ts, $topmark, $comment, $isminor )
-{
-       global $wgLang, $wgOut, $wgUser, $target;
-       $page = Title::makeName( $ns, $t );
-       $link = $sk->makeKnownLink( $page, "" );
-       $topmarktext = $topmark ? wfMsg ( "uctop" ) : "";
-       $sysop = $wgUser->isSysop();
-
-       $extraRollback = $_REQUEST['bot'] ? '&bot=1' : '';      
-       if($sysop && $topmark ) {
-               $topmarktext .= " [". $sk->makeKnownLink( $page,
-                 wfMsg( "rollbacklink" ), 
-                 "action=rollback&from=" . urlencode( $target ) . $extraRollback ) ."]";
-       }
-       if($comment) {
-       
-               $comment="<em>(". htmlspecialchars( $comment ) .")</em> ";
+
+/**
+ * Generates each row in the contributions list.
+ *
+ * Contributions which are marked "top" are currently on top of the history.
+ * For these contributions, a [rollback] link is shown for users with sysop
+ * privileges. The rollback link restores the most recent version that was not
+ * written by the target user.
+ * 
+ * If the contributions page is called with the parameter &bot=1, all rollback
+ * links also get that parameter. It causes the edit itself and the rollback
+ * to be marked as "bot" edits. Bot edits are hidden by default from recent
+ * changes, so this allows sysops to combat a busy vandal without bothering
+ * other users.
+ * 
+ * @todo This would probably look a lot nicer in a table.
+ */
+function ucListEdit( $sk, $row ) {
+       $fname = 'ucListEdit';
+       wfProfileIn( $fname );
        
+       global $wgLang, $wgOut, $wgUser, $wgRequest;
+       static $messages;
+       if( !isset( $messages ) ) {
+               foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
+                       $messages[$msg] = wfMsg( $msg );
+               }
        }
-       $d = $wgLang->timeanddate( $ts, true );
+       
+       $page =& Title::makeTitle( $row->page_namespace, $row->page_title );
+       $link = $sk->makeKnownLinkObj( $page, '' );
+       $difftext = $topmarktext = '';
+       if( $row->rev_id == $row->page_latest ) {
+               $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
+               if( !$row->page_is_new ) {
+                       $difftext .= $sk->makeKnownLinkObj( $page, '(' . $messages['diff'] . ')', 'diff=0' );
+               } else {
+                       $difftext .= $messages['newarticle'];
+               }
+               
+               if( $wgUser->isAllowed('rollback') ) {
+                       $extraRollback = $wgRequest->getBool( 'bot' ) ? '&bot=1' : '';
+                       $extraRollback .= '&token=' . urlencode(
+                               $wgUser->editToken( array( $page->getPrefixedText(), $row->rev_user_text ) ) );
+                       $topmarktext .= ' ['. $sk->makeKnownLinkObj( $page,
+                               $messages['rollbacklink'],
+                               'action=rollback&from=' . urlencode( $row->rev_user_text ) . $extraRollback ) .']';
+               }
 
-        if ($isminor) {
-          $mflag = "<strong>" . wfMsg( "minoreditletter" ) . "</strong> ";
-        }
+       }
+       if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
+               $difftext = '(' . $messages['diff'] . ')';
+       } else {
+               $difftext = $sk->makeKnownLinkObj( $page, '(' . $messages['diff'].')', 'diff=prev&oldid='.$row->rev_id );
+       }
+       $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
 
-       $wgOut->addHTML( "<li>{$d} {$mflag}{$link} {$comment}{$topmarktext}</li>\n" );
-}
+       $comment = $sk->commentBlock( $row->rev_comment, $page );
+       $d = $wgLang->timeanddate( $row->rev_timestamp, true );
 
-function ucCountLink( $lim, $d )
-{
-       global $wgUser, $wgLang, $target;
+       if( $row->rev_minor_edit ) {
+               $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
+       } else {
+               $mflag = '';
+       }
 
-       $sk = $wgUser->getSkin();
-       $s = $sk->makeKnownLink( $wgLang->specialPage( "Contributions" ),
-         "{$lim}", "target={$target}&days={$d}&limit={$lim}" );
-       return $s;
+       $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
+       if( $row->rev_deleted ) {
+               $ret = '<span class="deleted">' . $ret . '</span> ' . htmlspecialchars( wfMsg( 'deletedrev' ) );
+       }
+       $ret = "<li>$ret</li>\n";
+       wfProfileOut( $fname );
+       return $ret;
 }
 
-function ucDaysLink( $lim, $d )
-{
-       global $wgUser, $wgLang, $target;
-
-       $sk = $wgUser->getSkin();
-       $s = $sk->makeKnownLink( $wgLang->specialPage( "Contributions" ),
-         "{$d}", "target={$target}&days={$d}&limit={$lim}" );
-       return $s;
+/**
+ * Generates a form used to restrict display of contributions
+ * to a specific namespace
+ *
+ * @return     none
+ * @param      string  $target target user to show contributions for
+ * @param      string  $hideminor whether minor contributions are hidden
+ * @param      string  $namespace currently selected namespace, NULL for show all
+ * @param      bool    $invert  inverts the namespace selection on true (default null)
+ */
+function ucNamespaceForm ( $target, $hideminor, $namespace, $invert ) {
+       global $wgContLang, $wgScript;
+
+       $namespaceselect = "<select name='namespace' id='nsselectbox'>";
+       $namespaceselect .= '<option value="" '.(is_null($namespace) ? ' selected="selected"' : '').'>'.wfMsg( 'contributionsall' ).'</option>';
+       $arr = $wgContLang->getFormattedNamespaces();
+       foreach( $arr as $ns => $name ) {
+               if( $ns < NS_MAIN )
+                       continue;
+               $n = $ns === NS_MAIN ? wfMsg ( 'blanknamespace' ) : $name;
+               $sel = $namespace == $ns ? ' selected="selected"' : '';
+               $namespaceselect .= "<option value='$ns'$sel>$n</option>";
+       }
+       $namespaceselect .= '</select>';
+
+       $out = "<div class='namespaceselector'><form method='get' action='{$wgScript}'>";
+       $out .= '<input type="hidden" name="title" value="'.$wgContLang->specialpage( 'Contributions' ).'" />';
+       $out .= '<input type="hidden" name="target" value="'.htmlspecialchars( $target ).'" />';
+       $out .= '<input type="hidden" name="hideminor" value="'.$hideminor.'" />';      
+       $out .= "
+<div id='nsselect' class='contributions'>
+       <label for='nsselectbox'>" . wfMsg('namespace') . "</label>
+       $namespaceselect
+       <input type='submit' value='" . wfMsg( 'allpagessubmit' ) . "' />
+       <input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $invert ? ' checked="checked"' : '' ) . " />
+       <label for='nsinvert'>" . wfMsg('invert') . "</label>
+</div>";
+       $out .= '</form></div>';
+       return $out;
 }
 ?>