Fix r80982, re-add accidental removed AND during debugging
[lhc/web/wiklou.git] / includes / LogEventsList.php
index 715d894..820793a 100644 (file)
@@ -1,30 +1,39 @@
 <?php
-# Copyright (C) 2004 Brion Vibber <brion@pobox.com>, 2008 Aaron Schulz
-# 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
-# (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.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-# http://www.gnu.org/copyleft/gpl.html
+/**
+ * Contain classes to list log entries
+ *
+ * Copyright © 2004 Brion Vibber <brion@pobox.com>, 2008 Aaron Schulz
+ * 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
+ * (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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
 
 class LogEventsList {
        const NO_ACTION_LINK = 1;
-       
+       const NO_EXTRA_USER_LINKS = 2;
+
        private $skin;
+       private $out;
        public $flags;
 
-       function __construct( &$skin, $flags = 0 ) {
-               $this->skin =& $skin;
+       public function __construct( $skin, $out, $flags = 0 ) {
+               $this->skin = $skin;
+               $this->out = $out;
                $this->flags = $flags;
                $this->preCacheMessages();
        }
@@ -36,294 +45,545 @@ class LogEventsList {
        private function preCacheMessages() {
                // Precache various messages
                if( !isset( $this->message ) ) {
-                       $messages = 'revertmerge protect_change unblocklink revertmove undeletelink revdel-restore rev-delundel';
-                       foreach( explode(' ', $messages ) as $msg ) {
-                               $this->message[$msg] = wfMsgExt( $msg, array( 'escape') );
+                       $messages = array( 'revertmerge', 'protect_change', 'unblocklink', 'change-blocklink',
+                               'revertmove', 'undeletelink', 'undeleteviewlink', 'revdel-restore', 'hist', 'diff',
+                               'pipe-separator', 'revdel-restore-deleted', 'revdel-restore-visible' );
+                       foreach( $messages as $msg ) {
+                               $this->message[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
                        }
                }
        }
-       
+
        /**
         * Set page title and show header for this log type
-        * @param OutputPage $out where to send output
-        * @param strin $type
+        * @param $type Array
         */
-       public function showHeader( $out, $type ) {
-               if( LogPage::isLogType( $type ) ) {
-                       $out->setPageTitle( LogPage::logName( $type ) );
-                       $out->addWikiText( LogPage::logHeader( $type ) );
+       public function showHeader( $type ) {
+               // If only one log type is used, then show a special message...
+               $headerType = (count($type) == 1) ? $type[0] : '';
+               if( LogPage::isLogType( $headerType ) ) {
+                       $this->out->setPageTitle( LogPage::logName( $headerType ) );
+                       $this->out->addHTML( LogPage::logHeader( $headerType ) );
+               } else {
+                       $this->out->addHTML( wfMsgExt('alllogstext',array('parseinline')) );
                }
        }
 
        /**
         * Show options for the log list
-        * @param OutputPage $out where to send output
-        * @param string $type,
-        * @param string $user,
-        * @param string $page,
-        * @param string $pattern
+        *
+        * @param $types string or Array
+        * @param $user String
+        * @param $page String
+        * @param $pattern String
+        * @param $year Integer: year
+        * @param $month Integer: month
+        * @param $filter: array
+        * @param $tagFilter: array?
         */
-       public function showOptions( $out, $type, $user, $page, $pattern ) {
+       public function showOptions( $types=array(), $user='', $page='', $pattern='', $year='', 
+               $month = '', $filter = null, $tagFilter='' )
+       {
                global $wgScript, $wgMiserMode;
-               $action = htmlspecialchars( $wgScript );
+
+               $action = $wgScript;
                $title = SpecialPage::getTitleFor( 'Log' );
-               $special = htmlspecialchars( $title->getPrefixedDBkey() );
-               $out->addHTML( "<form action=\"$action\" method=\"get\"><fieldset>" .
-                       Xml::element( 'legend', array(), wfMsg( 'log' ) ) .
-                       Xml::hidden( 'title', $special ) . "\n" .
-                       $this->getTypeMenu( $type ) . "\n" .
-                       $this->getUserInput( $user ) . "\n" .
-                       $this->getTitleInput( $page ) . "\n" .
-                       ( !$wgMiserMode ? ($this->getTitlePattern( $pattern )."\n") : "" ) .
-                       Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
-                       "</fieldset></form>" );
+               $special = $title->getPrefixedDBkey();
+
+               // For B/C, we take strings, but make sure they are converted...
+               $types = ($types === '') ? array() : (array)$types;
+
+               $tagSelector = ChangeTags::buildTagFilterSelector( $tagFilter );
+
+               $html = Html::hidden( 'title', $special );
+
+               // Basic selectors
+               $html .= $this->getTypeMenu( $types ) . "\n";
+               $html .= $this->getUserInput( $user ) . "\n";
+               $html .= $this->getTitleInput( $page ) . "\n";
+               $html .= $this->getExtraInputs( $types ) . "\n";
+
+               // Title pattern, if allowed
+               if (!$wgMiserMode) {
+                       $html .= $this->getTitlePattern( $pattern ) . "\n";
+               }
+
+               // date menu
+               $html .= Xml::tags( 'p', null, Xml::dateMenu( $year, $month ) );
+
+               // Tag filter
+               if ($tagSelector) {
+                       $html .= Xml::tags( 'p', null, implode( '&#160;', $tagSelector ) );
+               }
+
+               // Filter links
+               if ($filter) {
+                       $html .= Xml::tags( 'p', null, $this->getFilterLinks( $filter ) );
+               }
+
+               // Submit button
+               $html .= Xml::submitButton( wfMsg( 'allpagessubmit' ) );
+
+               // Fieldset
+               $html = Xml::fieldset( wfMsg( 'log' ), $html );
+
+               // Form wrapping
+               $html = Xml::tags( 'form', array( 'action' => $action, 'method' => 'get' ), $html );
+
+               $this->out->addHTML( $html );
+       }
+
+       /**
+        * @param $filter Array
+        * @return String: Formatted HTML
+        */
+       private function getFilterLinks( $filter ) {
+               global $wgTitle, $wgLang;
+               // show/hide links
+               $messages = array( wfMsgHtml( 'show' ), wfMsgHtml( 'hide' ) );
+               // Option value -> message mapping
+               $links = array();
+               $hiddens = ''; // keep track for "go" button
+               foreach( $filter as $type => $val ) {
+                       // Should the below assignment be outside the foreach?
+                       // Then it would have to be copied. Not certain what is more expensive.
+                       $query = $this->getDefaultQuery();
+                       $queryKey = "hide_{$type}_log";
+
+                       $hideVal = 1 - intval($val);
+                       $query[$queryKey] = $hideVal;
+
+                       $link = $this->skin->link(
+                               $wgTitle,
+                               $messages[$hideVal],
+                               array(),
+                               $query,
+                               array( 'known', 'noclasses' )
+                       );
+
+                       $links[$type] = wfMsgHtml( "log-show-hide-{$type}", $link );
+                       $hiddens .= Html::hidden( "hide_{$type}_log", $val ) . "\n";
+               }
+               // Build links
+               return '<small>'.$wgLang->pipeList( $links ) . '</small>' . $hiddens;
+       }
+
+       private function getDefaultQuery() {
+               if ( !isset( $this->mDefaultQuery ) ) {
+                       $this->mDefaultQuery = $_GET;
+                       unset( $this->mDefaultQuery['title'] );
+                       unset( $this->mDefaultQuery['dir'] );
+                       unset( $this->mDefaultQuery['offset'] );
+                       unset( $this->mDefaultQuery['limit'] );
+                       unset( $this->mDefaultQuery['order'] );
+                       unset( $this->mDefaultQuery['month'] );
+                       unset( $this->mDefaultQuery['year'] );
+               }
+               return $this->mDefaultQuery;
        }
 
        /**
-        * @return string Formatted HTML
+        * @param $queryTypes Array
+        * @return String: Formatted HTML
         */
-       private function getTypeMenu( $queryType ) {
+       private function getTypeMenu( $queryTypes ) {
                global $wgLogRestrictions, $wgUser;
-       
-               $out = "<select name='type'>\n";
+
+               $html = "<select name='type'>\n";
 
                $validTypes = LogPage::validTypes();
-               $m = array(); // Temporary array
+               $typesByName = array(); // Temporary array
 
                // First pass to load the log names
                foreach( $validTypes as $type ) {
                        $text = LogPage::logName( $type );
-                       $m[$text] = $type;
+                       $typesByName[$type] = $text;
                }
 
                // Second pass to sort by name
-               ksort($m);
+               asort($typesByName);
+
+               // Note the query type
+               $queryType = count($queryTypes) == 1 ? $queryTypes[0] : '';
+
+               // Always put "All public logs" on top
+               if ( isset( $typesByName[''] ) ) {
+                       $all = $typesByName[''];
+                       unset( $typesByName[''] );
+                       $typesByName = array( '' => $all ) + $typesByName;
+               }
 
                // Third pass generates sorted XHTML content
-               foreach( $m as $text => $type ) {
+               foreach( $typesByName as $type => $text ) {
                        $selected = ($type == $queryType);
                        // Restricted types
                        if ( isset($wgLogRestrictions[$type]) ) {
                                if ( $wgUser->isAllowed( $wgLogRestrictions[$type] ) ) {
-                                       $out .= Xml::option( $text, $type, $selected ) . "\n";
+                                       $html .= Xml::option( $text, $type, $selected ) . "\n";
                                }
                        } else {
-                               $out .= Xml::option( $text, $type, $selected ) . "\n";
+                               $html .= Xml::option( $text, $type, $selected ) . "\n";
                        }
                }
 
-               $out .= '</select>';
-               return $out;
+               $html .= '</select>';
+               return $html;
        }
 
        /**
-        * @return string Formatted HTML
+        * @param $user String
+        * @return String: Formatted HTML
         */
        private function getUserInput( $user ) {
-               return Xml::inputLabel( wfMsg( 'specialloguserlabel' ), 'user', 'user', 12, $user );
+               return '<span style="white-space: nowrap">' .
+                       Xml::inputLabel( wfMsg( 'specialloguserlabel' ), 'user', 'mw-log-user', 15, $user ) .
+                       '</span>';
        }
 
        /**
-        * @return string Formatted HTML
+        * @param $title String
+        * @return String: Formatted HTML
         */
        private function getTitleInput( $title ) {
-               return Xml::inputLabel( wfMsg( 'speciallogtitlelabel' ), 'page', 'page', 20, $title );
+               return '<span style="white-space: nowrap">' .
+                       Xml::inputLabel( wfMsg( 'speciallogtitlelabel' ), 'page', 'mw-log-page', 20, $title ) .
+                       '</span>';
        }
 
        /**
         * @return boolean Checkbox
         */
        private function getTitlePattern( $pattern ) {
-               return Xml::checkLabel( wfMsg( 'log-title-wildcard' ), 'pattern', 'pattern', $pattern );
+               return '<span style="white-space: nowrap">' .
+                       Xml::checkLabel( wfMsg( 'log-title-wildcard' ), 'pattern', 'pattern', $pattern ) .
+                       '</span>';
        }
 
-
-       protected function getWrapperElement() {
-               global $wgDateGroupedLogs;
-               return $wgDateGroupedLogs ? 'div' : 'ul';
+       private function getExtraInputs( $types ) {
+               global $wgRequest;
+               $offender = $wgRequest->getVal('offender');
+               $user = User::newFromName( $offender, false );
+               if( !$user || ($user->getId() == 0 && !IP::isIPAddress($offender) ) ) {
+                       $offender = ''; // Blank field if invalid
+               }
+               if( count($types) == 1 && $types[0] == 'suppress' ) {
+                       return Xml::inputLabel( wfMsg('revdelete-offender'), 'offender',
+                               'mw-log-offender', 20, $offender );
+               }
+               return '';
        }
-       
+
        public function beginLogEventsList() {
-               return Xml::openElement( $this->getWrapperElement() ) . "\n";
+               return "<ul>\n";
        }
-       
+
        public function endLogEventsList() {
-               return Xml::closeElement( $this->getWrapperElement() ) . "\n";
+               return "</ul>\n";
        }
-       
-               /**
-        * @param Row $row a single row from the result set
-        * @return string Formatted HTML list item
-        * @private
+
+       /**
+        * @param $row Row: a single row from the result set
+        * @return String: Formatted HTML list item
         */
        public function logLine( $row ) {
-               global $wgLang, $wgUser, $wgContLang;
-               $skin = $wgUser->getSkin();
+               $classes = array( 'mw-logline-' . $row->log_type );
                $title = Title::makeTitle( $row->log_namespace, $row->log_title );
-               $time = $wgLang->timeanddate( wfTimestamp(TS_MW, $row->log_timestamp), true );
-               // Enter the existence or non-existence of this page into the link cache,
-               // for faster makeLinkObj() in LogPage::actionText()
-               $linkCache =& LinkCache::singleton();
-               $linkCache->addLinkObj( $title );
+               // Log time
+               $time = $this->logTimestamp( $row );
                // User links
-               if( self::isDeleted($row,LogPage::DELETED_USER) ) {
-                       $userLink = '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
-               } else {
-                       $userLink = $this->skin->userLink( $row->log_user, $row->user_name ) . 
-                               $this->skin->userToolLinksRedContribs( $row->log_user, $row->user_name );
-               }
-               // Comment
-               if( self::isDeleted($row,LogPage::DELETED_COMMENT) ) {
-                       $comment = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-comment') . '</span>';
-               } else {
-                       $comment = $wgContLang->getDirMark() . $this->skin->commentBlock( $row->log_comment );
-               }
+               $userLink = $this->logUserLinks( $row );
                // Extract extra parameters
                $paramArray = LogPage::extractParams( $row->log_params );
-               $revert = $del = '';
+               // Event description
+               $action = $this->logAction( $row, $title, $paramArray );
+               // Log comment
+               $comment = $this->logComment( $row );
+               // Add review/revert links and such...
+               $revert = $this->logActionLinks( $row, $title, $paramArray, $comment );
+
                // Some user can hide log items and have review links
-               if( $wgUser->isAllowed( 'deleterevision' ) ) {
-                       $del = $this->showhideLinks( $row ) . ' ';
-               }
-               // Add review links and such...
-               if( !($this->flags & self::NO_ACTION_LINK) && !($row->log_deleted & LogPage::DELETED_ACTION) ) {
-                       if( $row->log_type == 'move' && isset( $paramArray[0] ) && $wgUser->isAllowed( 'move' ) ) {
-                               $destTitle = Title::newFromText( $paramArray[0] );
-                               if( $destTitle ) {
-                                       $revert = '(' . $this->skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Movepage' ),
-                                               $this->message['revertmove'],
-                                               'wpOldTitle=' . urlencode( $destTitle->getPrefixedDBkey() ) .
-                                               '&wpNewTitle=' . urlencode( $title->getPrefixedDBkey() ) .
-                                               '&wpReason=' . urlencode( wfMsgForContent( 'revertmove' ) ) .
-                                               '&wpMovetalk=0' ) . ')';
-                               }
-                       // Show undelete link
-                       } else if( $row->log_action == 'delete' && $wgUser->isAllowed( 'delete' ) ) {
-                               $revert = '(' . $this->skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Undelete' ),
-                                       $this->message['undeletelink'], 'target='. urlencode( $title->getPrefixedDBkey() ) ) . ')';
-                       // Show unblock link
-                       } else if( $row->log_action == 'block' && $wgUser->isAllowed( 'block' ) ) {
-                               $revert = '(' .  $this->skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Ipblocklist' ),
-                                       $this->message['unblocklink'],
-                                       'action=unblock&ip=' . urlencode( $row->log_title ) ) . ')';
-                       // Show change protection link
-                       } else if( ( $row->log_action == 'protect' || $row->log_action == 'modify' ) && $wgUser->isAllowed( 'protect' ) ) {
-                               $revert = '(' .  $this->skin->makeKnownLinkObj( $title, $this->message['protect_change'], 'action=unprotect' ) . ')';
-                       // Show unmerge link
-                       } else if ( $row->log_action == 'merge' ) {
-                               $merge = SpecialPage::getTitleFor( 'Mergehistory' );
-                               $revert = '(' .  $this->skin->makeKnownLinkObj( $merge, $this->message['revertmerge'],
-                                       wfArrayToCGI( 
-                                               array('target' => $paramArray[0], 'dest' => $title->getPrefixedText(), 'mergepoint' => $paramArray[1] ) 
-                                       ) 
-                               ) . ')';
-                       // If an edit was hidden from a page give a review link to the history
-                       } else if( $row->log_action == 'revision' && $wgUser->isAllowed( 'deleterevision' ) && isset($paramArray[2]) ) {
-                               $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
-                               // Different revision types use different URL params...
-                               $subtype = isset($paramArray[2]) ? $paramArray[1] : '';
-                               // Link to each hidden object ID, $paramArray[1] is the url param. List if several...
-                               $Ids = explode( ',', $paramArray[2] );
-                               if( count($Ids) == 1 ) {
-                                       $revert = $this->skin->makeKnownLinkObj( $revdel, $this->message['revdel-restore'],
-                                               wfArrayToCGI( array('target' => $paramArray[0], $paramArray[1] => $Ids[0] ) ) );
-                               } else {
-                                       $revert .= $this->message['revdel-restore'].':';
-                                       foreach( $Ids as $n => $id ) {
-                                               $revert .= ' '.$this->skin->makeKnownLinkObj( $revdel, '#'.($n+1),
-                                                       wfArrayToCGI( array('target' => $paramArray[0], $paramArray[1] => $id ) ) );
-                                       }
-                               }
-                               $revert = "($revert)";
-                       // Hidden log items, give review link
-                       } else if( $row->log_action == 'event' && $wgUser->isAllowed( 'deleterevision' ) && isset($paramArray[0]) ) {
-                               $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
-                               $revert .= $this->message['revdel-restore'];
-                               $Ids = explode( ',', $paramArray[0] );
-                               // Link to each hidden object ID, $paramArray[1] is the url param. List if several...
-                               if( count($Ids) == 1 ) {
-                                       $revert = $this->skin->makeKnownLinkObj( $revdel, $this->message['revdel-restore'],
-                                               wfArrayToCGI( array('logid' => $Ids[0] ) ) );
-                               } else {
-                                       foreach( $Ids as $n => $id ) {
-                                               $revert .= $this->skin->makeKnownLinkObj( $revdel, '#'.($n+1),
-                                                       wfArrayToCGI( array('logid' => $id ) ) );
-                                       }
-                               }
-                               $revert = "($revert)";
-                       } else {
-                               wfRunHooks( 'LogLine', array( $row->log_type, $row->log_action, $title, $paramArray, 
-                                       &$comment, &$revert, $row->log_timestamp ) );
-                               // wfDebug( "Invoked LogLine hook for " $row->log_type . ", " . $row->log_action . "\n" );
-                               // Do nothing. The implementation is handled by the hook modifiying the passed-by-ref parameters.
+               $del = $this->getShowHideLinks( $row );
+               if( $del != '' ) $del .= ' ';
+
+               // Any tags...
+               list( $tagDisplay, $newClasses ) = ChangeTags::formatSummaryRow( $row->ts_tags, 'logevent' );
+               $classes = array_merge( $classes, $newClasses );
+
+               return Xml::tags( 'li', array( "class" => implode( ' ', $classes ) ),
+                       $del . "$time $userLink $action $comment $revert $tagDisplay" ) . "\n";
+       }
+       
+       private function logTimestamp( $row ) {
+               global $wgLang;
+               $time = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->log_timestamp ), true );
+               return htmlspecialchars( $time );
+       }
+
+       private function logUserLinks( $row ) {
+               if( self::isDeleted( $row, LogPage::DELETED_USER ) ) {
+                       $userLinks = '<span class="history-deleted">' .
+                               wfMsgHtml( 'rev-deleted-user' ) . '</span>';
+               } else {
+                       $userLinks = $this->skin->userLink( $row->log_user, $row->user_name );
+                       // Talk|Contribs links...
+                       if( !( $this->flags & self::NO_EXTRA_USER_LINKS ) ) {
+                               $userLinks .= $this->skin->userToolLinks(
+                                       $row->log_user, $row->user_name, true, 0, $row->user_editcount );
                        }
                }
-               // Event description
-               if( self::isDeleted($row,LogPage::DELETED_ACTION) ) {
-                       $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
+               return $userLinks;
+       }
+
+       private function logAction( $row, $title, $paramArray ) {
+               if( self::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
+                       $action = '<span class="history-deleted">' .
+                               wfMsgHtml( 'rev-deleted-event' ) . '</span>';
                } else {
-                       $action = LogPage::actionText( $row->log_type, $row->log_action, $title, $this->skin, $paramArray, true );
+                       $action = LogPage::actionText(
+                               $row->log_type, $row->log_action, $title, $this->skin, $paramArray, true );
                }
+               return $action;
+       }
+       
+       private function logComment( $row ) {
+               global $wgContLang;
+               if( self::isDeleted( $row, LogPage::DELETED_COMMENT ) ) {
+                       $comment = '<span class="history-deleted">' .
+                               wfMsgHtml( 'rev-deleted-comment' ) . '</span>';
+               } else {
+                       $comment = $wgContLang->getDirMark() .
+                               $this->skin->commentBlock( $row->log_comment );
+               }
+               return $comment;
+       }
 
-               global $wgDateGroupedLogs;
-               if ( $wgDateGroupedLogs ) {
-                       $time = $wgLang->time( wfTimestamp(TS_MW, $row->log_timestamp), true );
-                       $date = $wgLang->date( wfTimestamp(TS_MW, $row->log_timestamp), true );
-                       $line = Xml::tags('div', array( 'class' => 'mw-log-entry' ), "$del$time $userLink $action $comment $revert" );
-
-                       static $lastdate = false;
-                       if ( $date !== $lastdate ) {
-                               $lastdate = $date;
-                               return Xml::element('h4', null, $date) . "\n" . $line;
+       // @TODO: split up!
+       private function logActionLinks( $row, $title, $paramArray, &$comment ) {
+               global $wgUser;
+               if( ( $this->flags & self::NO_ACTION_LINK ) // we don't want to see the action
+                       || self::isDeleted( $row, LogPage::DELETED_ACTION ) ) // action is hidden
+               {
+                       return '';
+               }
+               $revert = '';
+               if( self::typeAction( $row, 'move', 'move', 'move' ) && !empty( $paramArray[0] ) ) {
+                       $destTitle = Title::newFromText( $paramArray[0] );
+                       if( $destTitle ) {
+                               $revert = '(' . $this->skin->link(
+                                       SpecialPage::getTitleFor( 'Movepage' ),
+                                       $this->message['revertmove'],
+                                       array(),
+                                       array(
+                                               'wpOldTitle' => $destTitle->getPrefixedDBkey(),
+                                               'wpNewTitle' => $title->getPrefixedDBkey(),
+                                               'wpReason'   => wfMsgForContent( 'revertmove' ),
+                                               'wpMovetalk' => 0
+                                       ),
+                                       array( 'known', 'noclasses' )
+                               ) . ')';
+                       }
+               // Show undelete link
+               } else if( self::typeAction( $row, array( 'delete', 'suppress' ), 'delete', 'deletedhistory' ) ) {
+                       if( !$wgUser->isAllowed( 'undelete' ) ) {
+                               $viewdeleted = $this->message['undeleteviewlink'];
                        } else {
-                               return $line;
+                               $viewdeleted = $this->message['undeletelink'];
                        }
+                       $revert = '(' . $this->skin->link(
+                               SpecialPage::getTitleFor( 'Undelete' ),
+                               $viewdeleted,
+                               array(),
+                               array( 'target' => $title->getPrefixedDBkey() ),
+                               array( 'known', 'noclasses' )
+                        ) . ')';
+               // Show unblock/change block link
+               } else if( self::typeAction( $row, array( 'block', 'suppress' ), array( 'block', 'reblock' ), 'block' ) ) {
+                       $revert = '(' .
+                               $this->skin->link(
+                                       SpecialPage::getTitleFor( 'Ipblocklist' ),
+                                       $this->message['unblocklink'],
+                                       array(),
+                                       array(
+                                               'action' => 'unblock',
+                                               'ip' => $row->log_title
+                                       ),
+                                       'known'
+                               ) .
+                               $this->message['pipe-separator'] .
+                               $this->skin->link(
+                                       SpecialPage::getTitleFor( 'Blockip', $row->log_title ),
+                                       $this->message['change-blocklink'],
+                                       array(),
+                                       array(),
+                                       'known'
+                               ) .
+                               ')';
+               // Show change protection link
+               } else if( self::typeAction( $row, 'protect', array( 'modify', 'protect', 'unprotect' ) ) ) {
+                       $revert .= ' (' .
+                               $this->skin->link( $title,
+                                       $this->message['hist'],
+                                       array(),
+                                       array(
+                                               'action' => 'history',
+                                               'offset' => $row->log_timestamp
+                                       )
+                               );
+                       if( $wgUser->isAllowed( 'protect' ) ) {
+                               $revert .= $this->message['pipe-separator'] .
+                                       $this->skin->link( $title,
+                                               $this->message['protect_change'],
+                                               array(),
+                                               array( 'action' => 'protect' ),
+                                               'known' );
+                       }
+                       $revert .= ')';
+               // Show unmerge link
+               } else if( self::typeAction( $row, 'merge', 'merge', 'mergehistory' ) ) {
+                       $revert = '(' . $this->skin->link(
+                               SpecialPage::getTitleFor( 'MergeHistory' ),
+                               $this->message['revertmerge'],
+                               array(),
+                               array(
+                                       'target' => $paramArray[0],
+                                       'dest' => $title->getPrefixedDBkey(),
+                                       'mergepoint' => $paramArray[1]
+                               ),
+                               array( 'known', 'noclasses' )
+                       ) . ')';
+               // If an edit was hidden from a page give a review link to the history
+               } else if( self::typeAction( $row, array( 'delete', 'suppress' ), 'revision', 'deletedhistory' ) ) {
+                       $revert = RevisionDeleter::getLogLinks( $title, $paramArray,
+                                                               $this->skin, $this->message );
+               // Hidden log items, give review link
+               } else if( self::typeAction( $row, array( 'delete', 'suppress' ), 'event', 'deletedhistory' ) ) {
+                       if( count($paramArray) >= 1 ) {
+                               $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
+                               // $paramArray[1] is a CSV of the IDs
+                               $query = $paramArray[0];
+                               // Link to each hidden object ID, $paramArray[1] is the url param
+                               $revert = '(' . $this->skin->link(
+                                       $revdel,
+                                       $this->message['revdel-restore'], 
+                                       array(),
+                                       array(
+                                               'target' => $title->getPrefixedText(),
+                                               'type' => 'logging',
+                                               'ids' => $query
+                                       ),
+                                       array( 'known', 'noclasses' )
+                               ) . ')';
+                       }
+               // Self-created users
+               } else if( self::typeAction( $row, 'newusers', 'create2' ) ) {
+                       if( isset( $paramArray[0] ) ) {
+                               $revert = $this->skin->userToolLinks( $paramArray[0], $title->getDBkey(), true );
+                       } else {
+                               # Fall back to a blue contributions link
+                               $revert = $this->skin->userToolLinks( 1, $title->getDBkey() );
+                       }
+                       $ts = wfTimestamp( TS_UNIX, $row->log_timestamp );
+                       if( $ts < '20080129000000' ) {
+                               # Suppress $comment from old entries (before 2008-01-29),
+                               # not needed and can contain incorrect links
+                               $comment = '';
+                       }
+               // Do nothing. The implementation is handled by the hook modifiying the passed-by-ref parameters.
                } else {
-                       return Xml::tags('li', null, "$del$time $userLink $action $comment $revert" );
+                       wfRunHooks( 'LogLine', array( $row->log_type, $row->log_action, $title, $paramArray,
+                               &$comment, &$revert, $row->log_timestamp ) );
+               }
+               if( $revert != '' ) {
+                       $revert = '<span class="mw-logevent-actionlink">' . $revert . '</span>';
                }
+               return $revert;
+       }
 
+       /**
+        * @param $row Row
+        * @return string
+        */
+       private function getShowHideLinks( $row ) {
+               global $wgUser;
+               if( ( $this->flags & self::NO_ACTION_LINK ) // we don't want to see the links
+                       || $row->log_type == 'suppress' ) // no one can hide items from the suppress log
+               {
+                       return '';
+               }
+               $del = '';
+               // Don't show useless link to people who cannot hide revisions
+               if( $wgUser->isAllowed( 'deletedhistory' ) ) {
+                       if( $row->log_deleted || $wgUser->isAllowed( 'deleterevision' ) ) {
+                               $canHide = $wgUser->isAllowed( 'deleterevision' );
+                               // If event was hidden from sysops
+                               if( !self::userCan( $row, LogPage::DELETED_RESTRICTED ) ) {
+                                       $del = $this->skin->revDeleteLinkDisabled( $canHide );
+                               } else {
+                                       $target = SpecialPage::getTitleFor( 'Log', $row->log_type );
+                                       $query = array(
+                                               'target' => $target->getPrefixedDBkey(),
+                                               'type'   => 'logging',
+                                               'ids'    => $row->log_id,
+                                       );
+                                       $del = $this->skin->revDeleteLink( $query,
+                                               self::isDeleted( $row, LogPage::DELETED_RESTRICTED ), $canHide );
+                               }
+                       }
+               }
+               return $del;
        }
-       
+
        /**
-        * @param Row $row
-        * @private
+        * @param $row Row
+        * @param $type Mixed: string/array
+        * @param $action Mixed: string/array
+        * @param $right string
+        * @return Boolean
         */
-       private function showhideLinks( $row ) {
-               global $wgAllowLogDeletion;
-               
-               if( !$wgAllowLogDeletion )
-                       return "";
-       
-               $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
-               // If event was hidden from sysops
-               if( !self::userCan( $row, LogPage::DELETED_RESTRICTED ) ) {
-                       $del = $this->message['rev-delundel'];
-               } else if( $row->log_type == 'suppress' ) {
-                       // No one should be hiding from the oversight log
-                       $del = $this->message['rev-delundel'];
-               } else {
-                       $del = $this->skin->makeKnownLinkObj( $revdel, $this->message['rev-delundel'], 'logid='.$row->log_id );
-                       // Bolden oversighted content
-                       if( self::isDeleted( $row, LogPage::DELETED_RESTRICTED ) )
-                               $del = "<strong>$del</strong>";
+       public static function typeAction( $row, $type, $action, $right='' ) {
+               $match = is_array($type) ?
+                       in_array( $row->log_type, $type ) : $row->log_type == $type;
+               if( $match ) {
+                       $match = is_array( $action ) ?
+                               in_array( $row->log_action, $action ) : $row->log_action == $action;
+                       if( $match && $right ) {
+                               global $wgUser;
+                               $match = $wgUser->isAllowed( $right );
+                       }
                }
-               return "<tt>(<small>$del</small>)</tt>";
+               return $match;
        }
-       
+
        /**
         * Determine if the current user is allowed to view a particular
         * field of this log row, if it's marked as deleted.
-        * @param Row $row
-        * @param int $field
-        * @return bool
+        *
+        * @param $row Row
+        * @param $field Integer
+        * @return Boolean
         */
        public static function userCan( $row, $field ) {
-               if( ( $row->log_deleted & $field ) == $field ) {
+               return self::userCanBitfield( $row->log_deleted, $field );
+       }
+
+       /**
+        * Determine if the current user is allowed to view a particular
+        * field of this log row, if it's marked as deleted.
+        *
+        * @param $bitfield Integer (current field)
+        * @param $field Integer
+        * @return Boolean
+        */
+       public static function userCanBitfield( $bitfield, $field ) {
+               if( $bitfield & $field ) {
                        global $wgUser;
-                       $permission = ( $row->log_deleted & LogPage::DELETED_RESTRICTED ) == LogPage::DELETED_RESTRICTED
-                               ? 'hiderevision'
-                               : 'deleterevision';
-                       wfDebug( "Checking for $permission due to $field match on $row->log_deleted\n" );
+
+                       if ( $bitfield & LogPage::DELETED_RESTRICTED ) {
+                               $permission = 'suppressrevision';
+                       } else {
+                               $permission = 'deletedhistory';
+                       }
+                       wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
                        return $wgUser->isAllowed( $permission );
                } else {
                        return true;
@@ -331,117 +591,255 @@ class LogEventsList {
        }
 
        /**
-        * @param Row $row
-        * @param int $field one of DELETED_* bitfield constants
-        * @return bool
+        * @param $row Row
+        * @param $field Integer: one of DELETED_* bitfield constants
+        * @return Boolean
         */
        public static function isDeleted( $row, $field ) {
-               return ($row->log_deleted & $field) == $field;
+               return ( $row->log_deleted & $field ) == $field;
        }
-       
+
        /**
-        * Quick function to show a short log extract
-        * @param OutputPage $out
-        * @param string $type
-        * @param string $page
-        * @param string $user
+        * Show log extract. Either with text and a box (set $msgKey) or without (don't set $msgKey)
+        *
+        * @param $out OutputPage or String-by-reference
+        * @param $types String or Array
+        * @param $page String The page title to show log entries for
+        * @param $user String The user who made the log entries
+        * @param $param Associative Array with the following additional options:
+        * - lim Integer Limit of items to show, default is 50
+        * - conds Array Extra conditions for the query (e.g. "log_action != 'revision'")
+        * - showIfEmpty boolean Set to false if you don't want any output in case the loglist is empty
+        *   if set to true (default), "No matching items in log" is displayed if loglist is empty
+        * - msgKey Array If you want a nice box with a message, set this to the key of the message.
+        *   First element is the message key, additional optional elements are parameters for the key
+        *   that are processed with wgMsgExt and option 'parse'
+        * - offset Set to overwrite offset parameter in $wgRequest
+        *   set to '' to unset offset
+        * - wrap String Wrap the message in html (usually something like "<div ...>$1</div>").
+        * - flags Integer display flags (NO_ACTION_LINK,NO_EXTRA_USER_LINKS)
+        * @return Integer Number of total log items (not limited by $lim)
         */
-        public static function showLogExtract( $out, $type='', $page='', $user='' ) {
-               global $wgUser;
-               # Insert list of top 50 or so items
-               $loglist = new LogEventsList( $wgUser->getSkin() );
-               $pager = new LogPager( $loglist, $type, $user, $page, '' );
+       public static function showLogExtract(
+               &$out, $types=array(), $page='', $user='', $param = array()
+       ) {
+               global $wgUser, $wgOut;
+               $defaultParameters = array(
+                       'lim' => 25,
+                       'conds' => array(),
+                       'showIfEmpty' => true,
+                       'msgKey' => array(''),
+                       'wrap' => "$1",
+                       'flags' => 0
+               );
+               # The + operator appends elements of remaining keys from the right
+               # handed array to the left handed, whereas duplicated keys are NOT overwritten.
+               $param += $defaultParameters;
+               # Convert $param array to individual variables
+               $lim = $param['lim'];
+               $conds = $param['conds'];
+               $showIfEmpty = $param['showIfEmpty'];
+               $msgKey = $param['msgKey'];
+               $wrap = $param['wrap'];
+               $flags = $param['flags'];
+               if ( !is_array( $msgKey ) ) {
+                       $msgKey = array( $msgKey );
+               }
+               # Insert list of top 50 (or top $lim) items
+               $loglist = new LogEventsList( $wgUser->getSkin(), $wgOut, $flags );
+               $pager = new LogPager( $loglist, $types, $user, $page, '', $conds );
+               if ( isset( $param['offset'] ) ) { # Tell pager to ignore $wgRequest offset
+                       $pager->setOffset( $param['offset'] );
+               }
+               if( $lim > 0 ) $pager->mLimit = $lim;
                $logBody = $pager->getBody();
+               $s = '';
                if( $logBody ) {
-                       $out->addHTML(
-                               $loglist->beginLogEventsList() .
-                               $logBody .
-                               $loglist->endLogEventsList()
+                       if ( $msgKey[0] ) {
+                               $s = '<div class="mw-warning-with-logexcerpt">';
+
+                               if ( count( $msgKey ) == 1 ) {
+                                       $s .= wfMsgExt( $msgKey[0], array( 'parse' ) );
+                               } else { // Process additional arguments
+                                       $args = $msgKey;
+                                       array_shift( $args );
+                                       $s .= wfMsgExt( $msgKey[0], array( 'parse' ), $args );
+                               }
+                       }
+                       $s .= $loglist->beginLogEventsList() .
+                                $logBody .
+                                $loglist->endLogEventsList();
+               } else {
+                       if ( $showIfEmpty )
+                               $s = Html::rawElement( 'div', array( 'class' => 'mw-warning-logempty' ),
+                                       wfMsgExt( 'logempty', array( 'parseinline' ) ) );
+               }
+               if( $pager->getNumRows() > $pager->mLimit ) { # Show "Full log" link
+                       $urlParam = array();
+                       if ( $page != '')
+                               $urlParam['page'] = $page;
+                       if ( $user != '')
+                               $urlParam['user'] = $user;
+                       if ( !is_array( $types ) ) # Make it an array, if it isn't
+                               $types = array( $types );
+                       # If there is exactly one log type, we can link to Special:Log?type=foo
+                       if ( count( $types ) == 1 )
+                               $urlParam['type'] = $types[0];
+                       $s .= $wgUser->getSkin()->link(
+                               SpecialPage::getTitleFor( 'Log' ),
+                               wfMsgHtml( 'log-fulllog' ),
+                               array(),
+                               $urlParam
                        );
+               }
+               if ( $logBody && $msgKey[0] ) {
+                       $s .= '</div>';
+               }
+
+               if ( $wrap!='' ) { // Wrap message in html
+                       $s = str_replace( '$1', $s, $wrap );
+               }
+
+               // $out can be either an OutputPage object or a String-by-reference
+               if( $out instanceof OutputPage ){
+                       $out->addHTML( $s );
                } else {
-                       $out->addWikiMsg( 'logempty' );
+                       $out = $s;
                }
-        }
-        
-        /**
+               return $pager->getNumRows();
+       }
+
+       /**
         * SQL clause to skip forbidden log types for this user
-        * @param Database $db
-        * @returns mixed (string or false)
+        *
+        * @param $db Database
+        * @param $audience string, public/user
+        * @return Mixed: string or false
         */
-       public static function getExcludeClause( $db ) {
+       public static function getExcludeClause( $db, $audience = 'public' ) {
                global $wgLogRestrictions, $wgUser;
                // Reset the array, clears extra "where" clauses when $par is used
                $hiddenLogs = array();
-               // Don't show private logs to unpriviledged users
-               foreach( $wgLogRestrictions as $logtype => $right ) {
-                       if( !$wgUser->isAllowed($right) ) {
-                               $safetype = $db->strencode( $logtype );
-                               $hiddenLogs[] = "'$safetype'";
+               // Don't show private logs to unprivileged users
+               foreach( $wgLogRestrictions as $logType => $right ) {
+                       if( $audience == 'public' || !$wgUser->isAllowed($right) ) {
+                               $safeType = $db->strencode( $logType );
+                               $hiddenLogs[] = $safeType;
                        }
                }
-               if( !empty($hiddenLogs) ) {
-                       return 'log_type NOT IN(' . implode(',',$hiddenLogs) . ')';
+               if( count($hiddenLogs) == 1 ) {
+                       return 'log_type != ' . $db->addQuotes( $hiddenLogs[0] );
+               } elseif( $hiddenLogs ) {
+                       return 'log_type NOT IN (' . $db->makeList($hiddenLogs) . ')';
                }
                return false;
        }
 }
 
 /**
- * @addtogroup Pager
+ * @ingroup Pager
  */
 class LogPager extends ReverseChronologicalPager {
-       private $type = '', $user = '', $title = '', $pattern = '';
+       private $types = array(), $user = '', $title = '', $pattern = '';
+       private $typeCGI = '';
        public $mLogEventsList;
+
        /**
-       * constructor
-       * @param LogEventsList $loglist,
-       * @param string $type,
-       * @param string $user,
-       * @param string $page,
-       * @param string $pattern
-       * @param array $conds
-       */
-       function __construct( $loglist, $type='', $user='', $title='', $pattern='', $conds = array() ) {
+        * Constructor
+        *
+        * @param $list LogEventsList
+        * @param $types String or Array: log types to show
+        * @param $user String: the user who made the log entries
+        * @param $title String: the page title the log entries are for
+        * @param $pattern String: do a prefix search rather than an exact title match
+        * @param $conds Array: extra conditions for the query
+        * @param $year Integer: the year to start from
+        * @param $month Integer: the month to start from
+        * @param $tagFilter String: tag
+        */
+       public function __construct( $list, $types = array(), $user = '', $title = '', $pattern = '',
+               $conds = array(), $year = false, $month = false, $tagFilter = '' ) 
+       {
                parent::__construct();
                $this->mConds = $conds;
-               
-               $this->mLogEventsList = $loglist;
-               
-               $this->limitType( $type );
+
+               $this->mLogEventsList = $list;
+
+               $this->limitType( $types ); // also excludes hidden types
                $this->limitUser( $user );
                $this->limitTitle( $title, $pattern );
+               $this->getDateCond( $year, $month );
+               $this->mTagFilter = $tagFilter;
        }
-       
+
+       public function getDefaultQuery() {
+               $query = parent::getDefaultQuery();
+               $query['type'] = $this->typeCGI; // arrays won't work here
+               $query['user'] = $this->user;
+               $query['month'] = $this->mMonth;
+               $query['year'] = $this->mYear;
+               return $query;
+       }
+
+       // Call ONLY after calling $this->limitType() already!
+       public function getFilterParams() {
+               global $wgFilterLogTypes, $wgUser, $wgRequest;
+               $filters = array();
+               if( count($this->types) ) {
+                       return $filters;
+               }
+               foreach( $wgFilterLogTypes as $type => $default ) {
+                       // Avoid silly filtering
+                       if( $type !== 'patrol' || $wgUser->useNPPatrol() ) {
+                               $hide = $wgRequest->getInt( "hide_{$type}_log", $default );
+                               $filters[$type] = $hide;
+                               if( $hide )
+                                       $this->mConds[] = 'log_type != ' . $this->mDb->addQuotes( $type );
+                       }
+               }
+               return $filters;
+       }
+
        /**
         * Set the log reader to return only entries of the given type.
         * Type restrictions enforced here
-        * @param string $type A log type ('upload', 'delete', etc)
-        * @private
+        *
+        * @param $types String or array: Log types ('upload', 'delete', etc);
+        *   empty string means no restriction
         */
-       private function limitType( $type ) {
+       private function limitType( $types ) {
                global $wgLogRestrictions, $wgUser;
+               // If $types is not an array, make it an array
+               $types = ($types === '') ? array() : (array)$types;
                // Don't even show header for private logs; don't recognize it...
-               if( isset($wgLogRestrictions[$type]) && !$wgUser->isAllowed($wgLogRestrictions[$type]) ) {
-                       $type = '';
+               foreach ( $types as $type ) {
+                       if( isset( $wgLogRestrictions[$type] )
+                               && !$wgUser->isAllowed($wgLogRestrictions[$type])
+                       ) {
+                               $types = array_diff( $types, array( $type ) );
+                       }
                }
-               // Don't show private logs to unpriviledged users
-               $hideLogs = LogEventsList::getExcludeClause( $this->mDb );
+               $this->types = $types;
+               // Don't show private logs to unprivileged users.
+               // Also, only show them upon specific request to avoid suprises.
+               $audience = $types ? 'user' : 'public';
+               $hideLogs = LogEventsList::getExcludeClause( $this->mDb, $audience );
                if( $hideLogs !== false ) {
                        $this->mConds[] = $hideLogs;
                }
-               if( empty($type) ) {
-                       return false;
+               if( count($types) ) {
+                       $this->mConds['log_type'] = $types;
+                       // Set typeCGI; used in url param for paging
+                       if( count($types) == 1 ) $this->typeCGI = $types[0];
                }
-               $this->type = $type;
-               $this->mConds['log_type'] = $type;
        }
-       
+
        /**
         * Set the log reader to return only entries by the given user.
-        * @param string $name (In)valid user name
-        * @private
+        *
+        * @param $name String: (In)valid user name
         */
-       function limitUser( $name ) {
+       private function limitUser( $name ) {
                if( $name == '' ) {
                        return false;
                }
@@ -449,105 +847,209 @@ class LogPager extends ReverseChronologicalPager {
                if( is_null($usertitle) ) {
                        return false;
                }
-               $this->user = $usertitle->getText();
                /* Fetch userid at first, if known, provides awesome query plan afterwards */
-               $userid = User::idFromName( $this->user );
+               $userid = User::idFromName( $name );
                if( !$userid ) {
-                       /* It should be nicer to abort query at all, 
+                       /* It should be nicer to abort query at all,
                           but for now it won't pass anywhere behind the optimizer */
                        $this->mConds[] = "NULL";
                } else {
+                       global $wgUser;
                        $this->mConds['log_user'] = $userid;
+                       // Paranoia: avoid brute force searches (bug 17342)
+                       if( !$wgUser->isAllowed( 'deletedhistory' ) ) {
+                               $this->mConds[] = $this->mDb->bitAnd('log_deleted', LogPage::DELETED_USER) . ' = 0';
+                       } else if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
+                               $this->mConds[] = $this->mDb->bitAnd('log_deleted', LogPage::SUPPRESSED_USER) .
+                                       ' != ' . LogPage::SUPPRESSED_USER;
+                       }
+                       $this->user = $usertitle->getText();
                }
        }
 
        /**
         * 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
-        * @private
+        *
+        * @param $page String: Title name as text
+        * @param $pattern String
         */
-       function limitTitle( $page, $pattern ) {
-               global $wgMiserMode;
-               
+       private function limitTitle( $page, $pattern ) {
+               global $wgMiserMode, $wgUser;
+
                $title = Title::newFromText( $page );
-               if( strlen($page) == 0 || !$title instanceof Title )
+               if( strlen( $page ) == 0 || !$title instanceof Title )
                        return false;
-               
+
                $this->title = $title->getPrefixedText();
-               $this->pattern = $pattern;
                $ns = $title->getNamespace();
+               $db = $this->mDb;
+
+               # Using the (log_namespace, log_title, log_timestamp) index with a
+               # range scan (LIKE) on the first two parts, instead of simple equality,
+               # makes it unusable for sorting.  Sorted retrieval using another index
+               # would be possible, but then we might have to scan arbitrarily many
+               # nodes of that index. Therefore, we need to avoid this if $wgMiserMode
+               # is on.
+               #
+               # This is not a problem with simple title matches, because then we can
+               # use the page_time index.  That should have no more than a few hundred
+               # log entries for even the busiest pages, so it can be safely scanned
+               # in full to satisfy an impossible condition on user or similar.
                if( $pattern && !$wgMiserMode ) {
-                       # use escapeLike to avoid expensive search patterns like 't%st%'
-                       $safetitle = $this->mDb->escapeLike( $title->getDBkey() );
                        $this->mConds['log_namespace'] = $ns;
-                       $this->mConds[] = "log_title LIKE '$safetitle%'";
+                       $this->mConds[] = 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() );
+                       $this->pattern = $pattern;
                } else {
                        $this->mConds['log_namespace'] = $ns;
                        $this->mConds['log_title'] = $title->getDBkey();
                }
+               // Paranoia: avoid brute force searches (bug 17342)
+               if( !$wgUser->isAllowed( 'deletedhistory' ) ) {
+                       $this->mConds[] = $db->bitAnd('log_deleted', LogPage::DELETED_ACTION) . ' = 0';
+               } else if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
+                       $this->mConds[] = $db->bitAnd('log_deleted', LogPage::SUPPRESSED_ACTION) .
+                               ' != ' . LogPage::SUPPRESSED_ACTION;
+               }
        }
 
-       function getQueryInfo() {
+       public function getQueryInfo() {
+               $tables = array( 'logging', 'user' );
                $this->mConds[] = 'user_id = log_user';
-               # Hack this until live
-               global $wgAllowLogDeletion;
-               $log_id = $wgAllowLogDeletion ? 'log_id' : '0 AS log_id';
-               return array(
-                       'tables' => array( 'logging', 'user' ),
-                       'fields' => array( 'log_type', 'log_action', 'log_user', 'log_namespace', 'log_title', 
-                               'log_params', 'log_comment', $log_id, 'log_deleted', 'log_timestamp', 'user_name' ),
-                       'conds' => $this->mConds,
-                       'options' => array()
+               $index = array();
+               $options = array();
+               # Add log_search table if there are conditions on it
+               if( array_key_exists('ls_field',$this->mConds) ) {
+                       $tables[] = 'log_search';
+                       $index['log_search'] = 'ls_field_val';
+                       $index['logging'] = 'PRIMARY';
+                       $options[] = 'DISTINCT';
+               # Avoid usage of the wrong index by limiting
+               # the choices of available indexes. This mainly
+               # avoids site-breaking filesorts.
+               } else if( $this->title || $this->pattern || $this->user ) {
+                       $index['logging'] = array( 'page_time', 'user_time' );
+                       if( count($this->types) == 1 ) {
+                               $index['logging'][] = 'log_user_type_time';
+                       }
+               } else if( count($this->types) == 1 ) {
+                       $index['logging'] = 'type_time';
+               } else {
+                       $index['logging'] = 'times';
+               }
+               $options['USE INDEX'] = $index;
+               # Don't show duplicate rows when using log_search
+               $info = array(
+                       'tables'     => $tables,
+                       'fields'     => array( 'log_type', 'log_action', 'log_user', 'log_namespace',
+                               'log_title', 'log_params', 'log_comment', 'log_id', 'log_deleted',
+                               'log_timestamp', 'user_name', 'user_editcount' ),
+                       'conds'      => $this->mConds,
+                       'options'    => $options,
+                       'join_conds' => array(
+                               'user' => array( 'INNER JOIN', 'user_id=log_user' ),
+                               'log_search' => array( 'INNER JOIN', 'ls_log_id=log_id' )
+                       )
                );
+               # Add ChangeTags filter query
+               ChangeTags::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'],
+                       $info['join_conds'], $info['options'], $this->mTagFilter );
+               return $info;
        }
 
        function getIndexField() {
                return 'log_timestamp';
        }
 
-       function formatRow( $row ) {
+       public function getStartBody() {
+               wfProfileIn( __METHOD__ );
+               # Do a link batch query
+               if( $this->getNumRows() > 0 ) {
+                       $lb = new LinkBatch;
+                       foreach ( $this->mResult as $row ) {
+                               $lb->add( $row->log_namespace, $row->log_title );
+                               $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
+                               $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
+                       }
+                       $lb->execute();
+                       $this->mResult->seek( 0 );
+               }
+               wfProfileOut( __METHOD__ );
+               return '';
+       }
+
+       public function formatRow( $row ) {
                return $this->mLogEventsList->logLine( $row );
        }
-       
+
        public function getType() {
-               return $this->type;
+               return $this->types;
        }
-       
+
        public function getUser() {
                return $this->user;
        }
-       
+
        public function getPage() {
                return $this->title;
        }
-       
+
        public function getPattern() {
                return $this->pattern;
        }
+
+       public function getYear() {
+               return $this->mYear;
+       }
+
+       public function getMonth() {
+               return $this->mMonth;
+       }
+
+       public function getTagFilter() {
+               return $this->mTagFilter;
+       }
+
+       public function doQuery() {
+               // Workaround MySQL optimizer bug
+               $this->mDb->setBigSelects();
+               parent::doQuery();
+               $this->mDb->setBigSelects( 'default' );
+       }
 }
 
 /**
- *
- * @addtogroup SpecialPage
+ * @deprecated
+ * @ingroup SpecialPage
  */
 class LogReader {
        var $pager;
+
        /**
-        * @param WebRequest $request For internal use use a FauxRequest object to pass arbitrary parameters.
+        * @param $request WebRequest: for internal use use a FauxRequest object to pass arbitrary parameters.
         */
        function __construct( $request ) {
-               global $wgUser;
+               global $wgUser, $wgOut;
+               wfDeprecated(__METHOD__);
                # Get parameters
                $type = $request->getVal( 'type' );
                $user = $request->getText( 'user' );
                $title = $request->getText( 'page' );
                $pattern = $request->getBool( 'pattern' );
-               
-               $loglist = new LogEventsList( $wgUser->getSkin() );
-               $this->pager = new LogPager( $loglist, $type, $user, $title, $pattern );
+               $year = $request->getIntOrNull( 'year' );
+               $month = $request->getIntOrNull( 'month' );
+               $tagFilter = $request->getVal( 'tagfilter' );
+               # Don't let the user get stuck with a certain date
+               $skip = $request->getText( 'offset' ) || $request->getText( 'dir' ) == 'prev';
+               if( $skip ) {
+                       $year = '';
+                       $month = '';
+               }
+               # Use new list class to output results
+               $loglist = new LogEventsList( $wgUser->getSkin(), $wgOut, 0 );
+               $this->pager = new LogPager( $loglist, $type, $user, $title, $pattern, $year, $month, $tagFilter );
        }
-       
+
        /**
        * Is there at least one row?
        * @return bool
@@ -558,27 +1060,29 @@ class LogReader {
 }
 
 /**
- *
- * @addtogroup SpecialPage
+ * @deprecated
+ * @ingroup SpecialPage
  */
 class LogViewer {
        const NO_ACTION_LINK = 1;
+
        /**
-        * @var LogReader $reader
+        * LogReader object
         */
        var $reader;
+
        /**
-        * @param LogReader &$reader where to get our data from
-        * @param integer $flags Bitwise combination of flags:
+        * @param &$reader LogReader: where to get our data from
+        * @param $flags Integer: Bitwise combination of flags:
         *     LogEventsList::NO_ACTION_LINK   Don't show restore/unblock/block links
         */
        function __construct( &$reader, $flags = 0 ) {
-               global $wgUser;
+               wfDeprecated(__METHOD__);
                $this->reader =& $reader;
                $this->reader->pager->mLogEventsList->flags = $flags;
                # Aliases for shorter code...
                $this->pager =& $this->reader->pager;
-               $this->logEventsList =& $this->reader->pager->mLogEventsList;
+               $this->list =& $this->reader->pager->mLogEventsList;
        }
 
        /**
@@ -587,18 +1091,18 @@ class LogViewer {
        public function show() {
                global $wgOut;
                # Set title and add header
-               $this->logEventsList->showHeader( $wgOut, $pager->getType() );
+               $this->list->showHeader( $pager->getType() );
                # Show form options
-               $this->logEventsList->showOptions( $wgOut, $this->pager->getType(), $this->pager->getUser(), 
-                       $this->pager->getPage(), $this->pager->getPattern() );
+               $this->list->showOptions( $this->pager->getType(), $this->pager->getUser(), $this->pager->getPage(),
+                       $this->pager->getPattern(), $this->pager->getYear(), $this->pager->getMonth() );
                # Insert list
                $logBody = $this->pager->getBody();
                if( $logBody ) {
                        $wgOut->addHTML(
-                               $this->pager->getNavigationBar() . 
-                               $this->logEventsList->beginLogEventsList() .
+                               $this->pager->getNavigationBar() .
+                               $this->list->beginLogEventsList() .
                                $logBody .
-                               $this->logEventsList->endLogEventsList() .
+                               $this->list->endLogEventsList() .
                                $this->pager->getNavigationBar()
                        );
                } else {
@@ -610,19 +1114,19 @@ class LogViewer {
         * Output just the list of entries given by the linked LogReader,
         * with extraneous UI elements. Use for displaying log fragments in
         * another page (eg at Special:Undelete)
-        * @param OutputPage $out where to send output
+        *
+        * @param $out OutputPage: where to send output
         */
        public function showList( &$out ) {
                $logBody = $this->pager->getBody();
                if( $logBody ) {
                        $out->addHTML(
-                               $this->logEventsList->beginLogEventsList() .
+                               $this->list->beginLogEventsList() .
                                $logBody .
-                               $this->logEventsList->endLogEventsList()
+                               $this->list->endLogEventsList()
                        );
                } else {
                        $out->addWikiMsg( 'logempty' );
                }
        }
 }
-