* (bug 10636) Update Thai translation
[lhc/web/wiklou.git] / includes / SpecialLog.php
index c8b9a13..0ed417c 100644 (file)
@@ -19,8 +19,7 @@
 
 /**
  *
- * @package MediaWiki
- * @subpackage SpecialPage
+ * @addtogroup SpecialPage
  */
 
 /**
  */
 function wfSpecialLog( $par = '' ) {
        global $wgRequest;
-       $logReader =& new LogReader( $wgRequest );
+       $logReader = new LogReader( $wgRequest );
        if( $wgRequest->getVal( 'type' ) == '' && $par != '' ) {
                $logReader->limitType( $par );
        }
-       $logViewer =& new LogViewer( $logReader );
+       $logViewer = new LogViewer( $logReader );
        $logViewer->show();
 }
 
 /**
  *
- * @package MediaWiki
- * @subpackage SpecialPage
+ * @addtogroup SpecialPage
  */
 class LogReader {
        var $db, $joinClauses, $whereClauses;
-       var $type = '', $user = '', $title = null;
+       var $type = '', $user = '', $title = null, $pattern = false;
 
        /**
         * @param WebRequest $request For internal use use a FauxRequest object to pass arbitrary parameters.
         */
        function LogReader( $request ) {
-               $this->db =& wfGetDB( DB_SLAVE );
+               $this->db = wfGetDB( DB_SLAVE );
                $this->setupQuery( $request );
        }
 
        /**
         * Basic setup and applies the limiting factors from the WebRequest object.
         * @param WebRequest $request
-        * @access private
+        * @private
         */
        function setupQuery( $request ) {
                $page = $this->db->tableName( 'page' );
@@ -68,17 +66,22 @@ class LogReader {
 
                $this->limitType( $request->getVal( 'type' ) );
                $this->limitUser( $request->getText( 'user' ) );
-               $this->limitTitle( $request->getText( 'page' ) );
+               $this->limitTitle( $request->getText( 'page' ) , $request->getBool( 'pattern' ) );
                $this->limitTime( $request->getVal( 'from' ), '>=' );
                $this->limitTime( $request->getVal( 'until' ), '<=' );
 
                list( $this->limit, $this->offset ) = $request->getLimitOffset();
+               
+               // XXX This all needs to use Pager, ugly hack for now.
+               global $wgMiserMode;
+               if( $wgMiserMode )
+                       $this->offset = min( $this->offset, 10000 );
        }
 
        /**
         * Set the log reader to return only entries of the given type.
         * @param string $type A log type ('upload', 'delete', etc)
-        * @access private
+        * @private
         */
        function limitType( $type ) {
                if( empty( $type ) ) {
@@ -92,12 +95,12 @@ class LogReader {
        /**
         * Set the log reader to return only entries by the given user.
         * @param string $name (In)valid user name
-        * @access private
+        * @private
         */
        function limitUser( $name ) {
                if ( $name == '' )
                        return false;
-               $usertitle = Title::makeTitle( NS_USER, $name );
+               $usertitle = Title::makeTitleSafe( NS_USER, $name );
                if ( is_null( $usertitle ) )
                        return false;
                $this->user = $usertitle->getText();
@@ -116,24 +119,32 @@ class LogReader {
         * Set the log reader to return only entries affecting the given page.
         * (For the block and rights logs, this is a user page.)
         * @param string $page Title name as text
-        * @access private
+        * @private
         */
-       function limitTitle( $page ) {
+       function limitTitle( $page , $pattern ) {
+               global $wgMiserMode;
                $title = Title::newFromText( $page );
-               if( empty( $page ) || is_null( $title )  ) {
+               
+               if( strlen( $page ) == 0 || !$title instanceof Title )
                        return false;
-               }
+
                $this->title =& $title;
-               $safetitle = $this->db->strencode( $title->getDBkey() );
+               $this->pattern = $pattern;
                $ns = $title->getNamespace();
-               $this->whereClauses[] = "log_namespace=$ns AND log_title='$safetitle'";
+               if ( $pattern && !$wgMiserMode ) {
+                       $safetitle = $this->db->escapeLike( $title->getDBkey() ); // use escapeLike to avoid expensive search patterns like 't%st%'
+                       $this->whereClauses[] = "log_namespace=$ns AND log_title LIKE '$safetitle%'";
+               } else {
+                       $safetitle = $this->db->strencode( $title->getDBkey() );
+                       $this->whereClauses[] = "log_namespace=$ns AND log_title = '$safetitle'";
+               }
        }
 
        /**
         * Set the log reader to return only entries in a given time range.
         * @param string $time Timestamp of one endpoint
         * @param string $direction either ">=" or "<=" operators
-        * @access private
+        * @private
         */
        function limitTime( $time, $direction ) {
                # Direction should be a comparison operator
@@ -147,12 +158,11 @@ class LogReader {
        /**
         * Build an SQL query from all the set parameters.
         * @return string the SQL query
-        * @access private
+        * @private
         */
        function getQuery() {
                $logging = $this->db->tableName( "logging" );
-               $user = $this->db->tableName( 'user' );
-               $sql = "SELECT log_type, log_action, log_timestamp,
+               $sql = "SELECT /*! STRAIGHT_JOIN */ log_type, log_action, log_timestamp,
                        log_user, user_name,
                        log_namespace, log_title, page_id,
                        log_comment, log_params FROM $logging ";
@@ -190,6 +200,13 @@ class LogReader {
                return $this->user;
        }
 
+       /**
+        * @return boolean The checkbox, if titles should be searched by a pattern too
+        */
+       function queryPattern() {
+               return $this->pattern;
+       }
+
        /**
         * @return string The text of the title that this LogReader has been limited to.
         */
@@ -200,12 +217,28 @@ class LogReader {
                        return $this->title->getPrefixedText();
                }
        }
+       
+       /**
+        * Is there at least one row?
+        *
+        * @return bool
+        */
+       public function hasRows() {
+               # Little hack...
+               $limit = $this->limit;
+               $this->limit = 1;
+               $res = $this->db->query( $this->getQuery() );
+               $this->limit = $limit;
+               $ret = $this->db->numRows( $res ) > 0;
+               $this->db->freeResult( $res );
+               return $ret;
+       }
+       
 }
 
 /**
  *
- * @package MediaWiki
- * @subpackage SpecialPage
+ * @addtogroup SpecialPage
  */
 class LogViewer {
        /**
@@ -219,7 +252,7 @@ class LogViewer {
         */
        function LogViewer( &$reader ) {
                global $wgUser;
-               $this->skin =& $wgUser->getSkin();
+               $this->skin = $wgUser->getSkin();
                $this->reader =& $reader;
        }
 
@@ -231,9 +264,13 @@ class LogViewer {
                $this->showHeader( $wgOut );
                $this->showOptions( $wgOut );
                $result = $this->getLogRows();
-               $this->showPrevNext( $wgOut );
-               $this->doShowList( $wgOut, $result );
-               $this->showPrevNext( $wgOut );
+               if ( $this->numResults > 0 ) {
+                       $this->showPrevNext( $wgOut );
+                       $this->doShowList( $wgOut, $result );
+                       $this->showPrevNext( $wgOut );
+               } else {
+                       $this->showError( $wgOut );
+               }
        }
 
        /**
@@ -253,8 +290,8 @@ class LogViewer {
                $batch = new LinkBatch;
                while ( $s = $result->fetchObject() ) {
                        // User link
-                       $title = Title::makeTitleSafe( NS_USER, $s->user_name );
-                       $batch->addObj( $title );
+                       $batch->addObj( Title::makeTitleSafe( NS_USER, $s->user_name ) );
+                       $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $s->user_name ) );
 
                        // Move destination link
                        if ( $s->log_type == 'move' ) {
@@ -277,34 +314,39 @@ class LogViewer {
         * @param OutputPage $out where to send output
         */
        function showList( &$out ) {
-               $this->doShowList( $out, $this->getLogRows() );
+               $result = $this->getLogRows();
+               if ( $this->numResults > 0 ) {
+                       $this->doShowList( $out, $result );
+               } else {
+                       $this->showError( $out );
+               }
        }
 
        function doShowList( &$out, $result ) {
                // Rewind result pointer and go through it again, making the HTML
-               if ($this->numResults > 0) {
-                       $html = "\n<ul>\n";
-                       $result->seek( 0 );
-                       while( $s = $result->fetchObject() ) {
-                               $html .= $this->logLine( $s );
-                       }
-                       $html .= "\n</ul>\n";
-                       $out->addHTML( $html );
-               } else {
-                       $out->addWikiText( wfMsg( 'logempty' ) );
+               $html = "\n<ul>\n";
+               $result->seek( 0 );
+               while( $s = $result->fetchObject() ) {
+                       $html .= $this->logLine( $s );
                }
+               $html .= "\n</ul>\n";
+               $out->addHTML( $html );
                $result->free();
        }
 
+       function showError( &$out ) {
+               $out->addWikiText( wfMsg( 'logempty' ) );
+       }
+
        /**
         * @param Object $s a single row from the result set
         * @return string Formatted HTML list item
-        * @access private
+        * @private
         */
        function logLine( $s ) {
-               global $wgLang;
+               global $wgLang, $wgUser;;
+               $skin = $wgUser->getSkin();
                $title = Title::makeTitle( $s->log_namespace, $s->log_title );
-               $user = Title::makeTitleSafe( NS_USER, $s->user_name );
                $time = $wgLang->timeanddate( wfTimestamp(TS_MW, $s->log_timestamp), true );
 
                // Enter the existence or non-existence of this page into the link cache,
@@ -316,20 +358,46 @@ class LogViewer {
                        $linkCache->addBadLinkObj( $title );
                }
 
-               $userLink = $this->skin->makeLinkObj( $user, htmlspecialchars( $s->user_name ) );
+               $userLink = $this->skin->userLink( $s->log_user, $s->user_name ) . $this->skin->userToolLinksRedContribs( $s->log_user, $s->user_name );
                $comment = $this->skin->commentBlock( $s->log_comment );
                $paramArray = LogPage::extractParams( $s->log_params );
                $revert = '';
+               // show revertmove link
                if ( $s->log_type == 'move' && isset( $paramArray[0] ) ) {
-                       $specialTitle = Title::makeTitle( NS_SPECIAL, 'Movepage' );
                        $destTitle = Title::newFromText( $paramArray[0] );
                        if ( $destTitle ) {
-                               $revert = '(' . $this->skin->makeKnownLinkObj( $specialTitle, wfMsg( 'revertmove' ),
+                               $revert = '(' . $this->skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Movepage' ),
+                                       wfMsg( 'revertmove' ),
                                        'wpOldTitle=' . urlencode( $destTitle->getPrefixedDBkey() ) .
                                        '&wpNewTitle=' . urlencode( $title->getPrefixedDBkey() ) .
                                        '&wpReason=' . urlencode( wfMsgForContent( 'revertmove' ) ) .
                                        '&wpMovetalk=0' ) . ')';
                        }
+               // show undelete link
+               } elseif ( $s->log_action == 'delete' && $wgUser->isAllowed( 'delete' ) ) {
+                       $revert = '(' . $this->skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Undelete' ),
+                               wfMsg( 'undeletebtn' ) ,
+                               'target='. urlencode( $title->getPrefixedDBkey() ) ) . ')';
+               
+               // show unblock link
+               } elseif ( $s->log_action == 'block' && $wgUser->isAllowed( 'block' ) ) {
+                       $revert = '(' .  $skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Ipblocklist' ),
+                               wfMsg( 'unblocklink' ),
+                               'action=unblock&ip=' . urlencode( $s->log_title ) ) . ')';
+               // show change protection link
+               } elseif ( ( $s->log_action == 'protect' || $s->log_action == 'modify' ) && $wgUser->isAllowed( 'protect' ) ) {
+                       $revert = '(' .  $skin->makeKnownLinkObj( $title, wfMsg( 'protect_change' ), 'action=unprotect' ) . ')';
+               // show user tool links for self created users
+               // TODO: The extension should be handling this, get it out of core!
+               } elseif ( $s->log_action == 'create2' ) {
+                       if( isset( $paramArray[0] ) ) {
+                               $revert = $this->skin->userToolLinks( $paramArray[0], $s->log_title, true );
+                       } else {
+                               # Fall back to a blue contributions link
+                               $revert = $this->skin->userToolLinks( 1, $s->log_title );
+                       }
+                       # Suppress $comment from old entries, not needed and can contain incorrect links
+                       $comment = '';
                }
 
                $action = LogPage::actionText( $s->log_type, $s->log_action, $title, $this->skin, $paramArray, true, true );
@@ -339,7 +407,7 @@ class LogViewer {
 
        /**
         * @param OutputPage &$out where to send output
-        * @access private
+        * @private
         */
        function showHeader( &$out ) {
                $type = $this->reader->queryType();
@@ -351,58 +419,84 @@ class LogViewer {
 
        /**
         * @param OutputPage &$out where to send output
-        * @access private
+        * @private
         */
        function showOptions( &$out ) {
-               global $wgScript;
+               global $wgScript, $wgMiserMode;
                $action = htmlspecialchars( $wgScript );
-               $title = Title::makeTitle( NS_SPECIAL, 'Log' );
+               $title = SpecialPage::getTitleFor( 'Log' );
                $special = htmlspecialchars( $title->getPrefixedDBkey() );
                $out->addHTML( "<form action=\"$action\" method=\"get\">\n" .
-                       "<input type='hidden' name='title' value=\"$special\" />\n" .
-                       $this->getTypeMenu() .
-                       $this->getUserInput() .
-                       $this->getTitleInput() .
-                       "<input type='submit' value=\"" . wfMsg( 'allpagessubmit' ) . "\" />" .
-                       "</form>" );
+                       '<fieldset>' .
+                       Xml::element( 'legend', array(), wfMsg( 'log' ) ) .
+                       Xml::hidden( 'title', $special ) . "\n" .
+                       $this->getTypeMenu() . "\n" .
+                       $this->getUserInput() . "\n" .
+                       $this->getTitleInput() . "\n" .
+                       (!$wgMiserMode?($this->getTitlePattern()."\n"):"") .
+                       Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
+                       "</fieldset></form>" );
        }
 
        /**
         * @return string Formatted HTML
-        * @access private
+        * @private
         */
        function getTypeMenu() {
                $out = "<select name='type'>\n";
-               foreach( LogPage::validTypes() as $type ) {
-                       $text = htmlspecialchars( LogPage::logName( $type ) );
-                       $selected = ($type == $this->reader->queryType()) ? ' selected="selected"' : '';
-                       $out .= "<option value=\"$type\"$selected>$text</option>\n";
+
+               $validTypes = LogPage::validTypes();
+               $m = array(); // Temporary array
+
+               // First pass to load the log names
+               foreach( $validTypes as $type ) {
+                       $text = LogPage::logName( $type );
+                       $m[$text] = $type;
                }
-               $out .= "</select>\n";
+
+               // Second pass to sort by name
+               ksort($m);
+
+               // Third pass generates sorted XHTML content
+               foreach( $m as $text => $type ) {
+                       $selected = ($type == $this->reader->queryType());
+                       $out .= Xml::option( $text, $type, $selected ) . "\n";
+               }
+
+               $out .= '</select>';
                return $out;
        }
 
        /**
         * @return string Formatted HTML
-        * @access private
+        * @private
         */
        function getUserInput() {
-               $user = htmlspecialchars( $this->reader->queryUser() );
-               return wfMsg('specialloguserlabel') . "<input type='text' name='user' size='12' value=\"$user\" />\n";
+               $user =  $this->reader->queryUser();
+               return Xml::inputLabel( wfMsg( 'specialloguserlabel' ), 'user', 'user', 12, $user );
        }
 
        /**
         * @return string Formatted HTML
-        * @access private
+        * @private
         */
        function getTitleInput() {
-               $title = htmlspecialchars( $this->reader->queryTitle() );
-               return wfMsg('speciallogtitlelabel') . "<input type='text' name='page' size='20' value=\"$title\" />\n";
+               $title = $this->reader->queryTitle();
+               return Xml::inputLabel( wfMsg( 'speciallogtitlelabel' ), 'page', 'page', 20, $title );
+       }
+
+       /**
+        * @return boolean Checkbox
+        * @private
+        */
+       function getTitlePattern() {
+               $pattern = $this->reader->queryPattern();
+               return Xml::checkLabel( wfMsg( 'log-title-wildcard' ), 'pattern', 'pattern', $pattern );
        }
 
        /**
         * @param OutputPage &$out where to send output
-        * @access private
+        * @private
         */
        function showPrevNext( &$out ) {
                global $wgContLang,$wgRequest;
@@ -410,6 +504,7 @@ class LogViewer {
                $pieces[] = 'type=' . urlencode( $this->reader->queryType() );
                $pieces[] = 'user=' . urlencode( $this->reader->queryUser() );
                $pieces[] = 'page=' . urlencode( $this->reader->queryTitle() );
+               $pieces[] = 'pattern=' . urlencode( $this->reader->queryPattern() );
                $bits = implode( '&', $pieces );
                list( $limit, $offset ) = $wgRequest->getLimitOffset();
 
@@ -424,4 +519,4 @@ class LogViewer {
 }
 
 
-?>
+