Lazy initialisation of wgProxyList, no flip required
[lhc/web/wiklou.git] / includes / SpecialLog.php
index e3c0b5a..65de257 100644 (file)
@@ -1,17 +1,17 @@
 <?php
 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
 # http://www.mediawiki.org/
-# 
+#
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or 
+# the Free Software Foundation; either version 2 of the License, or
 # (at your option) any later version.
-# 
+#
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 # GNU General Public License for more details.
-# 
+#
 # You should have received a copy of the GNU General Public License along
 # with this program; if not, write to the Free Software Foundation, Inc.,
 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
@@ -29,7 +29,7 @@
 function wfSpecialLog( $par = '' ) {
        global $wgRequest;
        $logReader =& new LogReader( $wgRequest );
-       if( '' == $wgRequest->getVal( 'type' ) && !empty( $par ) ) {
+       if( $wgRequest->getVal( 'type' ) == '' && $par != '' ) {
                $logReader->limitType( $par );
        }
        $logViewer =& new LogViewer( $logReader );
@@ -44,7 +44,7 @@ function wfSpecialLog( $par = '' ) {
 class LogReader {
        var $db, $joinClauses, $whereClauses;
        var $type = '', $user = '', $title = null;
-       
+
        /**
         * @param WebRequest $request For internal use use a FauxRequest object to pass arbitrary parameters.
         */
@@ -52,27 +52,27 @@ class LogReader {
                $this->db =& wfGetDB( DB_SLAVE );
                $this->setupQuery( $request );
        }
-       
+
        /**
         * Basic setup and applies the limiting factors from the WebRequest object.
         * @param WebRequest $request
         * @private
         */
        function setupQuery( $request ) {
-               $cur = $this->db->tableName( 'cur' );
+               $page = $this->db->tableName( 'page' );
                $user = $this->db->tableName( 'user' );
-               $this->joinClauses = array( "LEFT OUTER JOIN $cur ON log_namespace=cur_namespace AND log_title=cur_title" );
+               $this->joinClauses = array( "LEFT OUTER JOIN $page ON log_namespace=page_namespace AND log_title=page_title" );
                $this->whereClauses = array( 'user_id=log_user' );
-               
+
                $this->limitType( $request->getVal( 'type' ) );
                $this->limitUser( $request->getText( 'user' ) );
                $this->limitTitle( $request->getText( 'page' ) );
                $this->limitTime( $request->getVal( 'from' ), '>=' );
                $this->limitTime( $request->getVal( 'until' ), '<=' );
-               
+
                list( $this->limit, $this->offset ) = $request->getLimitOffset();
        }
-       
+
        /**
         * Set the log reader to return only entries of the given type.
         * @param string $type A log type ('upload', 'delete', etc)
@@ -86,23 +86,24 @@ class LogReader {
                $safetype = $this->db->strencode( $type );
                $this->whereClauses[] = "log_type='$safetype'";
        }
-       
+
        /**
         * Set the log reader to return only entries by the given user.
-        * @param string $name Valid user name
+        * @param string $name (In)valid user name
         * @private
         */
        function limitUser( $name ) {
+               if ( $name == '' )
+                       return false;
                $title = Title::makeTitle( NS_USER, $name );
-               if( empty( $name ) || is_null( $title ) ) {
+               if ( is_null( $title ) )
                        return false;
-               }
-               $this->user = str_replace( '_', ' ', $title->getDBkey() );
+               $this->user = $title->getText();
                $safename = $this->db->strencode( $this->user );
                $user = $this->db->tableName( 'user' );
                $this->whereClauses[] = "user_name='$safename'";
        }
-       
+
        /**
         * Set the log reader to return only entries affecting the given page.
         * (For the block and rights logs, this is a user page.)
@@ -119,7 +120,7 @@ class LogReader {
                $ns = $title->getNamespace();
                $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
@@ -134,7 +135,7 @@ class LogReader {
                $safetime = $this->db->strencode( wfTimestamp( TS_MW, $time ) );
                $this->whereClauses[] = "log_timestamp $direction '$safetime'";
        }
-       
+
        /**
         * Build an SQL query from all the set parameters.
         * @return string the SQL query
@@ -145,8 +146,8 @@ class LogReader {
                $user = $this->db->tableName( 'user' );
                $sql = "SELECT log_type, log_action, log_timestamp,
                        log_user, user_name,
-                       log_namespace, log_title, cur_id,
-                       log_comment FROM $user, $logging ";
+                       log_namespace, log_title, page_id,
+                       log_comment, log_params FROM $user, $logging ";
                if( !empty( $this->joinClauses ) ) {
                        $sql .= implode( ',', $this->joinClauses );
                }
@@ -154,32 +155,33 @@ class LogReader {
                        $sql .= " WHERE " . implode( ' AND ', $this->whereClauses );
                }
                $sql .= " ORDER BY log_timestamp DESC ";
-               $sql .= $this->db->limitResult( $this->limit, $this->offset );
+               $sql = $this->db->limitResult($sql, $this->limit, $this->offset );
                return $sql;
        }
-       
+
        /**
         * Execute the query and start returning results.
         * @return ResultWrapper result object to return the relevant rows
         */
        function getRows() {
-               return $this->db->resultObject( $this->db->query( $this->getQuery() ) );
+               $res = $this->db->query( $this->getQuery() );
+               return $this->db->resultObject( $res );
        }
-       
+
        /**
         * @return string The query type that this LogReader has been limited to.
         */
        function queryType() {
                return $this->type;
        }
-       
+
        /**
         * @return string The username type that this LogReader has been limited to, if any.
         */
        function queryUser() {
                return $this->user;
        }
-       
+
        /**
         * @return string The text of the title that this LogReader has been limited to.
         */
@@ -202,7 +204,8 @@ class LogViewer {
         * @var LogReader $reader
         */
        var $reader;
-       
+       var $numResults = 0;
+
        /**
         * @param LogReader &$reader where to get our data from
         */
@@ -211,7 +214,7 @@ class LogViewer {
                $this->skin =& $wgUser->getSkin();
                $this->reader =& $reader;
        }
-       
+
        /**
         * Take over the whole output page in $wgOut with the log display.
         */
@@ -219,11 +222,47 @@ class LogViewer {
                global $wgOut;
                $this->showHeader( $wgOut );
                $this->showOptions( $wgOut );
+               $result = $this->getLogRows();
                $this->showPrevNext( $wgOut );
-               $this->showList( $wgOut );
+               $this->doShowList( $wgOut, $result );
                $this->showPrevNext( $wgOut );
        }
-       
+
+       /**
+        * Load the data from the linked LogReader
+        * Preload the link cache
+        * Initialise numResults
+        *
+        * Must be called before calling showPrevNext
+        *
+        * @return object database result set
+        */
+       function getLogRows() {
+               global $wgLinkCache;
+               $result = $this->reader->getRows();
+               $this->numResults = 0;
+
+               // Fetch results and form a batch link existence query
+               $batch = new LinkBatch;
+               while ( $s = $result->fetchObject() ) {
+                       // User link
+                       $title = Title::makeTitleSafe( NS_USER, $s->user_name );
+                       $batch->addObj( $title );
+
+                       // Move destination link
+                       if ( $s->log_type == 'move' ) {
+                               $paramArray = LogPage::extractParams( $s->log_params );
+                               $title = Title::newFromText( $paramArray[0] );
+                               $batch->addObj( $title );
+                       }
+                       ++$this->numResults;
+               }
+               $batch->execute( $wgLinkCache );
+
+               return $result;
+       }
+
+
        /**
         * Output just the list of entries given by the linked LogReader,
         * with extraneous UI elements. Use for displaying log fragments in
@@ -231,42 +270,64 @@ class LogViewer {
         * @param OutputPage $out where to send output
         */
        function showList( &$out ) {
-               $html = "";
-               $result = $this->reader->getRows();
-               while( $s = $result->fetchObject() ) {
-                       $html .= $this->logLine( $s );
+               $this->doShowList( $out, $this->getLogRows() );
+       }
+
+       function doShowList( &$out, $result ) {
+               // Rewind result pointer and go through it again, making the HTML
+               $html='';
+               if ($this->numResults > 0) {
+                       $html = "\n<ul>\n";
+                       $result->seek( 0 );
+                       while( $s = $result->fetchObject() ) {
+                               $html .= $this->logLine( $s );
+                       }
+                       $html .= "\n</ul>\n";
                }
                $result->free();
                $out->addHTML( $html );
        }
-       
+
        /**
         * @param Object $s a single row from the result set
         * @return string Formatted HTML list item
         * @private
         */
        function logLine( $s ) {
-               global $wgLang;
+               global $wgLang, $wgLinkCache;
                $title = Title::makeTitle( $s->log_namespace, $s->log_title );
                $user = Title::makeTitleSafe( NS_USER, $s->user_name );
-               $time = $wgLang->timeanddate( $s->log_timestamp );
-               if( $s->cur_id ) {
-                       $titleLink = $this->skin->makeKnownLinkObj( $title );
+               $time = $wgLang->timeanddate( wfTimestamp(TS_MW, $s->log_timestamp), true );
+
+               // Enter the existence or non-existence of this page into the link cache,
+               // for faster makeLinkObj() in LogPage::actionText()
+               if( $s->page_id ) {
+                       $wgLinkCache->addGoodLinkObj( $s->page_id, $title );
                } else {
-                       $titleLink = $this->skin->makeBrokenLinkObj( $title );
+                       $wgLinkCache->addBadLinkObj( $title );
                }
+
                $userLink = $this->skin->makeLinkObj( $user, htmlspecialchars( $s->user_name ) );
-               if( '' === $s->log_comment ) {
-                       $comment = '';
-               } else {
-                       $comment = '(<em>' . $this->skin->formatComment( $s->log_comment ) . '</em>)';
+               $comment = $this->skin->commentBlock( $s->log_comment );
+               $paramArray = LogPage::extractParams( $s->log_params );
+               $revert = '';
+               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' ),
+                                       'wpOldTitle=' . urlencode( $destTitle->getPrefixedDBkey() ) .
+                                       '&wpNewTitle=' . urlencode( $title->getPrefixedDBkey() ) .
+                                       '&wpReason=' . urlencode( wfMsgForContent( 'revertmove' ) ) .
+                                       '&wpMovetalk=0' ) . ')';
+                       }
                }
-               
-               $action = LogPage::actionText( $s->log_type, $s->log_action, $titleLink );
-               $out = "<li>$time $userLink $action $comment</li>\n";
+
+               $action = LogPage::actionText( $s->log_type, $s->log_action, $title, $this->skin, $paramArray, true, true );
+               $out = "<li>$time $userLink $action $comment $revert</li>\n";
                return $out;
        }
-       
+
        /**
         * @param OutputPage &$out where to send output
         * @private
@@ -278,7 +339,7 @@ class LogViewer {
                        $out->addWikiText( LogPage::logHeader( $type ) );
                }
        }
-       
+
        /**
         * @param OutputPage &$out where to send output
         * @private
@@ -293,10 +354,10 @@ class LogViewer {
                        $this->getTypeMenu() .
                        $this->getUserInput() .
                        $this->getTitleInput() .
-                       "<input type='submit' />" .
+                       "<input type='submit' value=\"" . wfMsg( 'allpagessubmit' ) . "\" />" .
                        "</form>" );
        }
-       
+
        /**
         * @return string Formatted HTML
         * @private
@@ -311,44 +372,44 @@ class LogViewer {
                $out .= "</select>\n";
                return $out;
        }
-       
+
        /**
         * @return string Formatted HTML
         * @private
         */
        function getUserInput() {
                $user = htmlspecialchars( $this->reader->queryUser() );
-               return "User: <input type='text' name='user' size='12' value=\"$user\" />\n";
+               return wfMsg('specialloguserlabel') . "<input type='text' name='user' size='12' value=\"$user\" />\n";
        }
-       
+
        /**
         * @return string Formatted HTML
         * @private
         */
        function getTitleInput() {
                $title = htmlspecialchars( $this->reader->queryTitle() );
-               return "Title: <input type='text' name='page' size='20' value=\"$title\" />\n";
+               return wfMsg('speciallogtitlelabel') . "<input type='text' name='page' size='20' value=\"$title\" />\n";
        }
-       
+
        /**
         * @param OutputPage &$out where to send output
         * @private
         */
        function showPrevNext( &$out ) {
-               global $wgContLang;
+               global $wgContLang,$wgRequest;
                $pieces = array();
-               $pieces[] = 'type=' . htmlspecialchars( $this->reader->queryType() );
-               $pieces[] = 'user=' . htmlspecialchars( $this->reader->queryUser() );
-               $pieces[] = 'page=' . htmlspecialchars( $this->reader->queryTitle() );
+               $pieces[] = 'type=' . urlencode( $this->reader->queryType() );
+               $pieces[] = 'user=' . urlencode( $this->reader->queryUser() );
+               $pieces[] = 'page=' . urlencode( $this->reader->queryTitle() );
                $bits = implode( '&', $pieces );
-               $offset = 0; $limit = 50;
-               
+               list( $limit, $offset ) = $wgRequest->getLimitOffset();
+
                # TODO: use timestamps instead of offsets to make it more natural
                # to go huge distances in time
                $html = wfViewPrevNext( $offset, $limit,
                        $wgContLang->specialpage( 'Log' ),
                        $bits,
-                       false);
+                       $this->numResults < $limit);
                $out->addHTML( '<p>' . $html . '</p>' );
        }
 }