X-Git-Url: https://git.cyclocoop.org/%27.WWW_URL.%27admin/?a=blobdiff_plain;f=includes%2FLogEventsList.php;h=f78da639e6942182fa9ab3c13bff463e7060db08;hb=450814de00419a2a4662e41ce57b1c4d61096c75;hp=922d524214575322aad6ace77ca4f37c29147ba7;hpb=7661bbeba58f4c69cd137ac4ad2a0f8d4a62faee;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/LogEventsList.php b/includes/LogEventsList.php index 922d524214..f78da639e6 100644 --- a/includes/LogEventsList.php +++ b/includes/LogEventsList.php @@ -1,29 +1,53 @@ , 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 , 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; + /** + * @var Skin + */ private $skin; + + /** + * @var OutputPage + */ private $out; public $flags; + /** + * @var Array + */ + protected $message; + + /** + * @var Array + */ + protected $mDefaultQuery; + public function __construct( $skin, $out, $flags = 0 ) { $this->skin = $skin; $this->out = $out; @@ -39,8 +63,8 @@ class LogEventsList { // Precache various messages if( !isset( $this->message ) ) { $messages = array( 'revertmerge', 'protect_change', 'unblocklink', 'change-blocklink', - 'revertmove', 'undeletelink', 'undeleteviewlink', 'revdel-restore', 'rev-delundel', 'hist', 'diff', - 'pipe-separator' ); + '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' ) ); } @@ -49,17 +73,25 @@ class LogEventsList { /** * Set page title and show header for this log type - * @param $type String + * @param $type Array + * @deprecated in 1.19 */ public function showHeader( $type ) { - if( LogPage::isLogType( $type ) ) { - $this->out->setPageTitle( LogPage::logName( $type ) ); - $this->out->addHTML( LogPage::logHeader( $type ) ); + wfDeprecated( __METHOD__ ); + // If only one log type is used, then show a special message... + $headerType = (count($type) == 1) ? $type[0] : ''; + if( LogPage::isLogType( $headerType ) ) { + $page = new LogPage( $headerType ); + $this->out->setPageTitle( $page->getName()->text() ); + $this->out->addHTML( $page->getDescription()->parseAsBlock() ); + } else { + $this->out->addHTML( wfMsgExt('alllogstext',array('parseinline')) ); } } /** * Show options for the log list + * * @param $types string or Array * @param $user String * @param $page String @@ -69,39 +101,63 @@ class LogEventsList { * @param $filter: array * @param $tagFilter: array? */ - public function showOptions( $types=array(), $user='', $page='', $pattern='', $year='', - $month = '', $filter = null, $tagFilter='' ) - { + 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() ); + $special = $title->getPrefixedDBkey(); + // For B/C, we take strings, but make sure they are converted... $types = ($types === '') ? array() : (array)$types; $tagSelector = ChangeTags::buildTagFilterSelector( $tagFilter ); - $this->out->addHTML( "
" . - Xml::element( 'legend', array(), wfMsg( 'log' ) ) . - Xml::hidden( 'title', $special ) . "\n" . - $this->getTypeMenu( $types ) . "\n" . - $this->getUserInput( $user ) . "\n" . - $this->getTitleInput( $page ) . "\n" . - ( !$wgMiserMode ? ($this->getTitlePattern( $pattern )."\n") : "" ) . - "

" . Xml::dateMenu( $year, $month ) . "\n" . - ( $tagSelector ? Xml::tags( 'p', null, implode( ' ', $tagSelector ) ) :'' ). "\n" . - ( $filter ? "

".$this->getFilterLinks( $filter )."\n" : "" ) . "\n" . - Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "

\n" . - "
" - ); + $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( ' ', $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; + global $wgLang; // show/hide links $messages = array( wfMsgHtml( 'show' ), wfMsgHtml( 'hide' ) ); // Option value -> message mapping @@ -112,12 +168,12 @@ class LogEventsList { // Then it would have to be copied. Not certain what is more expensive. $query = $this->getDefaultQuery(); $queryKey = "hide_{$type}_log"; - $query[$queryKey] = $hideVal; $hideVal = 1 - intval($val); + $query[$queryKey] = $hideVal; - $link = $this->skin->link( - $wgTitle, + $link = Linker::link( + $this->getDisplayTitle(), $messages[$hideVal], array(), $query, @@ -125,15 +181,17 @@ class LogEventsList { ); $links[$type] = wfMsgHtml( "log-show-hide-{$type}", $link ); - $hiddens .= Xml::hidden( "hide_{$type}_log", $val ) . "\n"; + $hiddens .= Html::hidden( "hide_{$type}_log", $val ) . "\n"; } // Build links return ''.$wgLang->pipeList( $links ) . '' . $hiddens; } - + private function getDefaultQuery() { + global $wgRequest; + if ( !isset( $this->mDefaultQuery ) ) { - $this->mDefaultQuery = $_GET; + $this->mDefaultQuery = $wgRequest->getQueryValues(); unset( $this->mDefaultQuery['title'] ); unset( $this->mDefaultQuery['dir'] ); unset( $this->mDefaultQuery['offset'] ); @@ -145,44 +203,64 @@ class LogEventsList { return $this->mDefaultQuery; } + /** + * Get the Title object of the page the links should point to. + * This is NOT the Title of the page the entries should be restricted to. + * + * @return Title object + */ + public function getDisplayTitle() { + return $this->out->getTitle(); + } + + public function getContext() { + return $this->out->getContext(); + } + /** * @param $queryTypes Array * @return String: Formatted HTML */ private function getTypeMenu( $queryTypes ) { - global $wgLogRestrictions, $wgUser; + $queryType = count($queryTypes) == 1 ? $queryTypes[0] : ''; + $selector = $this->getTypeSelector(); + $selector->setDefault( $queryType ); + return $selector->getHtml(); + } - $html = "'; - return $html; + return $select; } /** @@ -190,7 +268,9 @@ class LogEventsList { * @return String: Formatted HTML */ private function getUserInput( $user ) { - return Xml::inputLabel( wfMsg( 'specialloguserlabel' ), 'user', 'mw-log-user', 15, $user ); + return '' . + Xml::inputLabel( wfMsg( 'specialloguserlabel' ), 'user', 'mw-log-user', 15, $user ) . + ''; } /** @@ -198,11 +278,14 @@ class LogEventsList { * @return String: Formatted HTML */ private function getTitleInput( $title ) { - return Xml::inputLabel( wfMsg( 'speciallogtitlelabel' ), 'page', 'mw-log-page', 20, $title ); + return '' . + Xml::inputLabel( wfMsg( 'speciallogtitlelabel' ), 'page', 'mw-log-page', 20, $title ) . + ''; } /** - * @return boolean Checkbox + * @param $pattern + * @return string Checkbox */ private function getTitlePattern( $pattern ) { return '' . @@ -210,10 +293,34 @@ class LogEventsList { ''; } + /** + * @param $types + * @return string + */ + 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 ''; + } + + /** + * @return string + */ public function beginLogEventsList() { return "\n"; } @@ -223,59 +330,81 @@ class LogEventsList { * @return String: Formatted HTML list item */ public function logLine( $row ) { - global $wgLang, $wgUser, $wgContLang; - - $title = Title::makeTitle( $row->log_namespace, $row->log_title ); - $classes = array( "mw-logline-{$row->log_type}" ); - $time = $wgLang->timeanddate( wfTimestamp(TS_MW, $row->log_timestamp), true ); - // User links - if( self::isDeleted($row,LogPage::DELETED_USER) ) { - $userLink = '' . wfMsgHtml( 'rev-deleted-user' ) . ''; - } else { - $userLink = $this->skin->userLink( $row->log_user, $row->user_name ) . - $this->skin->userToolLinks( $row->log_user, $row->user_name, true, 0, $row->user_editcount ); - } - // Comment - if( self::isDeleted($row,LogPage::DELETED_COMMENT) ) { - $comment = '' . wfMsgHtml('rev-deleted-comment') . ''; - } else { - $comment = $wgContLang->getDirMark() . $this->skin->commentBlock( $row->log_comment ); - } + $entry = DatabaseLogEntry::newFromRow( $row ); + $formatter = LogFormatter::newFromEntry( $entry ); + $formatter->setShowUserToolLinks( !( $this->flags & self::NO_EXTRA_USER_LINKS ) ); + + $action = $formatter->getActionText(); + $comment = $formatter->getComment(); + + $classes = array( 'mw-logline-' . $entry->getType() ); + $title = $entry->getTarget(); + $time = $this->logTimestamp( $entry ); + // Extract extra parameters $paramArray = LogPage::extractParams( $row->log_params ); - $revert = $del = ''; + // Add review/revert links and such... + $revert = $this->logActionLinks( $row, $title, $paramArray, $comment ); + // Some user can hide log items and have review links - if( !($this->flags & self::NO_ACTION_LINK) && $wgUser->isAllowed( 'deleterevision' ) ) { - $del = $this->getShowHideLinks( $row ) . ' '; + $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 $action $comment $revert $tagDisplay" ) . "\n"; + } + + private function logTimestamp( LogEntry $entry ) { + global $wgLang; + $time = $wgLang->timeanddate( wfTimestamp( TS_MW, $entry->getTimestamp() ), true ); + return htmlspecialchars( $time ); + } + + /** + * @TODO: split up! + * + * @param $row + * @param Title $title + * @param Array $paramArray + * @param $comment + * @return String + */ + 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 ''; } - // Add review links and such... - if( ($this->flags & self::NO_ACTION_LINK) || ($row->log_deleted & LogPage::DELETED_ACTION) ) { - // Action text is suppressed... - } else if( self::typeAction($row,'move','move','move') && !empty($paramArray[0]) ) { + $revert = ''; + if( self::typeAction( $row, 'move', 'move', 'move' ) && !empty( $paramArray[0] ) ) { $destTitle = Title::newFromText( $paramArray[0] ); if( $destTitle ) { - $revert = '(' . $this->skin->link( + $revert = '(' . Linker::link( SpecialPage::getTitleFor( 'Movepage' ), $this->message['revertmove'], array(), array( 'wpOldTitle' => $destTitle->getPrefixedDBkey(), 'wpNewTitle' => $title->getPrefixedDBkey(), - 'wpReason' => wfMsgForContent( 'revertmove' ), + 'wpReason' => wfMsgForContent( 'revertmove' ), 'wpMovetalk' => 0 ), array( 'known', 'noclasses' ) ) . ')'; } // Show undelete link - } else if( self::typeAction($row,array('delete','suppress'),'delete','deletedhistory') ) { + } elseif( self::typeAction( $row, array( 'delete', 'suppress' ), 'delete', 'deletedhistory' ) ) { if( !$wgUser->isAllowed( 'undelete' ) ) { $viewdeleted = $this->message['undeleteviewlink']; } else { $viewdeleted = $this->message['undeletelink']; } - - $revert = '(' . $this->skin->link( + $revert = '(' . Linker::link( SpecialPage::getTitleFor( 'Undelete' ), $viewdeleted, array(), @@ -283,21 +412,18 @@ class LogEventsList { array( 'known', 'noclasses' ) ) . ')'; // Show unblock/change block link - } else if( self::typeAction($row,array('block','suppress'),array('block','reblock'),'block') ) { + } elseif( self::typeAction( $row, array( 'block', 'suppress' ), array( 'block', 'reblock' ), 'block' ) ) { $revert = '(' . - $this->skin->link( - SpecialPage::getTitleFor( 'Ipblocklist' ), + Linker::link( + SpecialPage::getTitleFor( 'Unblock', $row->log_title ), $this->message['unblocklink'], array(), - array( - 'action' => 'unblock', - 'ip' => $row->log_title - ), + array(), 'known' ) . $this->message['pipe-separator'] . - $this->skin->link( - SpecialPage::getTitleFor( 'Blockip', $row->log_title ), + Linker::link( + SpecialPage::getTitleFor( 'Block', $row->log_title ), $this->message['change-blocklink'], array(), array(), @@ -305,9 +431,9 @@ class LogEventsList { ) . ')'; // Show change protection link - } else if( self::typeAction( $row, 'protect', array( 'modify', 'protect', 'unprotect' ) ) ) { - $revert .= ' (' . - $this->skin->link( $title, + } elseif( self::typeAction( $row, 'protect', array( 'modify', 'protect', 'unprotect' ) ) ) { + $revert .= ' (' . + Linker::link( $title, $this->message['hist'], array(), array( @@ -317,7 +443,7 @@ class LogEventsList { ); if( $wgUser->isAllowed( 'protect' ) ) { $revert .= $this->message['pipe-separator'] . - $this->skin->link( $title, + Linker::link( $title, $this->message['protect_change'], array(), array( 'action' => 'protect' ), @@ -325,115 +451,50 @@ class LogEventsList { } $revert .= ')'; // Show unmerge link - } else if( self::typeAction($row,'merge','merge','mergehistory') ) { - $merge = SpecialPage::getTitleFor( 'Mergehistory' ); - $revert = '(' . $this->skin->link( - $merge, + } elseif( self::typeAction( $row, 'merge', 'merge', 'mergehistory' ) ) { + $revert = '(' . Linker::link( + SpecialPage::getTitleFor( 'MergeHistory' ), $this->message['revertmerge'], array(), array( 'target' => $paramArray[0], - 'dest' => $title->getPrefixedDBkey(), + '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','deleterevision') ) { - if( count($paramArray) >= 2 ) { - $revdel = SpecialPage::getTitleFor( 'Revisiondelete' ); - // Different revision types use different URL params... - $key = $paramArray[0]; - // $paramArray[1] is a CSV of the IDs - $Ids = explode( ',', $paramArray[1] ); - $query = $paramArray[1]; - $revert = array(); - // Diff link for single rev deletions - if( ( $key === 'oldid' || $key == 'revision' ) && count($Ids) == 1 ) { - $revert[] = $this->skin->link( - $title, - $this->message['diff'], - array(), - array( - 'diff' => intval( $Ids[0] ), - 'unhide' => 1 - ), - array( 'known', 'noclasses' ) - ); - } - // View/modify link... - $revert[] = $this->skin->link( - $revdel, - $this->message['revdel-restore'], - array(), - array( - 'target' => $title->getPrefixedUrl(), - 'type' => $key, - 'ids' => $query - ), - array( 'known', 'noclasses' ) - ); - // Pipe links - $revert = '(' . implode(' | ',$revert) . ')'; - } + } elseif( self::typeAction( $row, array( 'delete', 'suppress' ), 'revision', 'deletedhistory' ) ) { + $revert = RevisionDeleter::getLogLinks( $title, $paramArray, + $this->message ); // Hidden log items, give review link - } else if( self::typeAction($row,array('delete','suppress'),'event','deleterevision') ) { + } elseif( self::typeAction( $row, array( 'delete', 'suppress' ), 'event', 'deletedhistory' ) ) { if( count($paramArray) >= 1 ) { $revdel = SpecialPage::getTitleFor( 'Revisiondelete' ); // $paramArray[1] is a CSV of the IDs - $Ids = explode( ',', $paramArray[0] ); $query = $paramArray[0]; // Link to each hidden object ID, $paramArray[1] is the url param - $revert = '(' . $this->skin->link( + $revert = '(' . Linker::link( $revdel, - $this->message['revdel-restore'], + $this->message['revdel-restore'], array(), array( - 'target' => $title->getPrefixedUrl(), + '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() ); - } - if( $time < '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 { wfRunHooks( 'LogLine', array( $row->log_type, $row->log_action, $title, $paramArray, &$comment, &$revert, $row->log_timestamp ) ); } - // Event description - if( self::isDeleted($row,LogPage::DELETED_ACTION) ) { - $action = '' . wfMsgHtml('rev-deleted-event') . ''; - } else { - $action = LogPage::actionText( $row->log_type, $row->log_action, $title, - $this->skin, $paramArray, true ); - } - - // Any tags... - list($tagDisplay, $newClasses) = ChangeTags::formatSummaryRow( $row->ts_tags, 'logevent' ); - $classes = array_merge( $classes, $newClasses ); - if( $revert != '' ) { $revert = '' . $revert . ''; } - - $time = htmlspecialchars( $time ); - - return Xml::tags( 'li', array( "class" => implode( ' ', $classes ) ), - $del . $time . ' ' . $userLink . ' ' . $action . ' ' . $comment . ' ' . $revert . " $tagDisplay" ) . "\n"; + return $revert; } /** @@ -441,22 +502,30 @@ class LogEventsList { * @return string */ private function getShowHideLinks( $row ) { - // If event was hidden from sysops - if( !self::userCan( $row, LogPage::DELETED_RESTRICTED ) ) { - $del = Xml::tags( 'span', array( 'class'=>'mw-revdelundel-link' ), - '('.$this->message['rev-delundel'].')' ); - } else if( $row->log_type == 'suppress' ) { - $del = ''; // No one should be hiding from the oversight log - } else { - $target = SpecialPage::getTitleFor( 'Log', $row->log_type ); - $page = Title::makeTitle( $row->log_namespace, $row->log_title ); - $query = array( - 'target' => $target->getPrefixedDBkey(), - 'type' => 'logging', - 'ids' => $row->log_id, - ); - $del = $this->skin->revDeleteLink( $query, - self::isDeleted( $row, LogPage::DELETED_RESTRICTED ) ); + 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 = Linker::revDeleteLinkDisabled( $canHide ); + } else { + $target = SpecialPage::getTitleFor( 'Log', $row->log_type ); + $query = array( + 'target' => $target->getPrefixedDBkey(), + 'type' => 'logging', + 'ids' => $row->log_id, + ); + $del = Linker::revDeleteLink( $query, + self::isDeleted( $row, LogPage::DELETED_RESTRICTED ), $canHide ); + } + } } return $del; } @@ -466,14 +535,14 @@ class LogEventsList { * @param $type Mixed: string/array * @param $action Mixed: string/array * @param $right string - * @return bool + * @return Boolean */ public static function typeAction( $row, $type, $action, $right='' ) { $match = is_array($type) ? - in_array($row->log_type,$type) : $row->log_type == $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; + $match = is_array( $action ) ? + in_array( $row->log_action, $action ) : $row->log_action == $action; if( $match && $right ) { global $wgUser; $match = $wgUser->isAllowed( $right ); @@ -485,18 +554,38 @@ class LogEventsList { /** * 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 $field Integer + * @param $user User object to check, or null to use $wgUser + * @return Boolean + */ + public static function userCan( $row, $field, User $user = null ) { + return self::userCanBitfield( $row->log_deleted, $field, $user ); + } + + /** + * 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 + * @param $user User object to check, or null to use $wgUser * @return Boolean */ - public static function userCan( $row, $field ) { - if( ( $row->log_deleted & $field ) == $field ) { - global $wgUser; - $permission = ( $row->log_deleted & LogPage::DELETED_RESTRICTED ) == LogPage::DELETED_RESTRICTED - ? 'suppressrevision' - : 'deleterevision'; - wfDebug( "Checking for $permission due to $field match on $row->log_deleted\n" ); - return $wgUser->isAllowed( $permission ); + public static function userCanBitfield( $bitfield, $field, User $user = null ) { + if( $bitfield & $field ) { + if ( $bitfield & LogPage::DELETED_RESTRICTED ) { + $permission = 'suppressrevision'; + } else { + $permission = 'deletedhistory'; + } + wfDebug( "Checking for $permission due to $field match on $bitfield\n" ); + if ( $user === null ) { + global $wgUser; + $user = $wgUser; + } + return $user->isAllowed( $permission ); } else { return true; } @@ -508,42 +597,138 @@ class LogEventsList { * @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 $out OutputPage - * @param $types String or Array - * @param $page String - * @param $user String - * @param $lim Integer - * @param $conds Array + * Show log extract. Either with text and a box (set $msgKey) or without (don't set $msgKey) + * + * @param $out OutputPage|String-by-reference + * @param $types String|Array Log types to show + * @param $page String|Title 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 "
$1
"). + * - 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, $types=array(), $page='', $user='', $lim=0, $conds=array() ) { - global $wgUser; - # Insert list of top 50 or so items - $loglist = new LogEventsList( $wgUser->getSkin(), $out, 0 ); + public static function showLogExtract( + &$out, $types=array(), $page='', $user='', $param = array() + ) { + $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 ); + } + + if ( $out instanceof OutputPage ) { + $context = $out->getContext(); + } else { + $context = RequestContext::getMain(); + } + + # Insert list of top 50 (or top $lim) items + $loglist = new LogEventsList( $context->getSkin(), $context->getOutput(), $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 = '
'; + + 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 { - $out->addWikiMsg( 'logempty' ); + 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 instanceof Title ) { + $urlParam['page'] = $page->getPrefixedDBkey(); + } elseif ( $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 .= Linker::link( + SpecialPage::getTitleFor( 'Log' ), + wfMsgHtml( 'log-fulllog' ), + array(), + $urlParam + ); + } + if ( $logBody && $msgKey[0] ) { + $s .= '
'; + } + + if ( $wrap!='' ) { // Wrap message in html + $s = str_replace( '$1', $s, $wrap ); + } + + /* hook can return false, if we don't want the message to be emitted (Wikia BugId:7093) */ + if ( wfRunHooks( 'LogEventsListShowLogExtract', array( &$s, $types, $page, $user, $param ) ) ) { + // $out can be either an OutputPage object or a String-by-reference + if ( $out instanceof OutputPage ){ + $out->addHTML( $s ); + } else { + $out = $s; + } + } + return $pager->getNumRows(); } /** * SQL clause to skip forbidden log types for this user - * @param $db Database + * + * @param $db DatabaseBase * @param $audience string, public/user - * @return mixed (string or false) + * @return Mixed: string or false */ public static function getExcludeClause( $db, $audience = 'public' ) { global $wgLogRestrictions, $wgUser; @@ -563,37 +748,38 @@ class LogEventsList { } return false; } -} + } /** * @ingroup Pager */ class LogPager extends ReverseChronologicalPager { - private $types = array(), $user = '', $title = '', $pattern = ''; + private $types = array(), $performer = '', $title = '', $pattern = ''; private $typeCGI = ''; public $mLogEventsList; /** - * constructor + * Constructor + * * @param $list LogEventsList - * @param $types String or Array - * @param $user String - * @param $title String - * @param $pattern String - * @param $conds Array - * @param $year Integer - * @param $month Integer - */ - public function __construct( $list, $types = array(), $user = '', $title = '', $pattern = '', - $conds = array(), $year = false, $month = false, $tagFilter = '' ) - { - parent::__construct(); + * @param $types String or Array: log types to show + * @param $performer String: the user who made the log entries + * @param $title String|Title: 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(), $performer = '', $title = '', $pattern = '', + $conds = array(), $year = false, $month = false, $tagFilter = '' ) { + parent::__construct( $list->getContext() ); $this->mConds = $conds; $this->mLogEventsList = $list; $this->limitType( $types ); // also excludes hidden types - $this->limitUser( $user ); + $this->limitPerformer( $performer ); $this->limitTitle( $title, $pattern ); $this->getDateCond( $year, $month ); $this->mTagFilter = $tagFilter; @@ -602,7 +788,7 @@ class LogPager extends ReverseChronologicalPager { public function getDefaultQuery() { $query = parent::getDefaultQuery(); $query['type'] = $this->typeCGI; // arrays won't work here - $query['user'] = $this->user; + $query['user'] = $this->performer; $query['month'] = $this->mMonth; $query['year'] = $this->mYear; return $query; @@ -610,15 +796,15 @@ class LogPager extends ReverseChronologicalPager { // Call ONLY after calling $this->limitType() already! public function getFilterParams() { - global $wgFilterLogTypes, $wgUser, $wgRequest; + global $wgFilterLogTypes; $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 ); + if( $type !== 'patrol' || $this->getUser()->useNPPatrol() ) { + $hide = $this->getRequest()->getInt( "hide_{$type}_log", $default ); $filters[$type] = $hide; if( $hide ) $this->mConds[] = 'log_type != ' . $this->mDb->addQuotes( $type ); @@ -630,19 +816,23 @@ class LogPager extends ReverseChronologicalPager { /** * Set the log reader to return only entries of the given type. * Type restrictions enforced here + * * @param $types String or array: Log types ('upload', 'delete', etc); * empty string means no restriction */ private function limitType( $types ) { - global $wgLogRestrictions, $wgUser; + global $wgLogRestrictions; // 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... foreach ( $types as $type ) { - if( isset( $wgLogRestrictions[$type] ) && !$wgUser->isAllowed($wgLogRestrictions[$type]) ) { + if( isset( $wgLogRestrictions[$type] ) + && !$this->getUser()->isAllowed($wgLogRestrictions[$type]) + ) { $types = array_diff( $types, array( $type ) ); } } + $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'; @@ -651,7 +841,6 @@ class LogPager extends ReverseChronologicalPager { $this->mConds[] = $hideLogs; } if( count($types) ) { - $this->types = $types; $this->mConds['log_type'] = $types; // Set typeCGI; used in url param for paging if( count($types) == 1 ) $this->typeCGI = $types[0]; @@ -660,9 +849,10 @@ class LogPager extends ReverseChronologicalPager { /** * Set the log reader to return only entries by the given user. + * * @param $name String: (In)valid user name */ - private function limitUser( $name ) { + private function limitPerformer( $name ) { if( $name == '' ) { return false; } @@ -677,31 +867,42 @@ class LogPager extends ReverseChronologicalPager { 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( 'suppressrevision' ) ) { - $this->mConds[] = 'log_deleted & ' . LogPage::DELETED_USER . ' = 0'; + $user = $this->getUser(); + if( !$user->isAllowed( 'deletedhistory' ) ) { + $this->mConds[] = $this->mDb->bitAnd('log_deleted', LogPage::DELETED_USER) . ' = 0'; + } elseif( !$user->isAllowed( 'suppressrevision' ) ) { + $this->mConds[] = $this->mDb->bitAnd('log_deleted', LogPage::SUPPRESSED_USER) . + ' != ' . LogPage::SUPPRESSED_USER; } - $this->user = $usertitle->getText(); + $this->performer = $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 $page String: Title name as text + * + * @param $page String or Title object: Title name * @param $pattern String */ private function limitTitle( $page, $pattern ) { - global $wgMiserMode, $wgUser; + global $wgMiserMode; - $title = Title::newFromText( $page ); - if( strlen($page) == 0 || !$title instanceof Title ) - return false; + if ( $page instanceof Title ) { + $title = $page; + } else { + $title = Title::newFromText( $page ); + if( strlen( $page ) == 0 || !$title instanceof Title ) { + return false; + } + } $this->title = $title->getPrefixedText(); $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 @@ -714,60 +915,91 @@ class LogPager extends ReverseChronologicalPager { # 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( 'suppressrevision' ) ) { - $this->mConds[] = 'log_deleted & ' . LogPage::DELETED_ACTION . ' = 0'; + $user = $this->getUser(); + if( !$user->isAllowed( 'deletedhistory' ) ) { + $this->mConds[] = $db->bitAnd('log_deleted', LogPage::DELETED_ACTION) . ' = 0'; + } elseif( !$user->isAllowed( 'suppressrevision' ) ) { + $this->mConds[] = $db->bitAnd('log_deleted', LogPage::SUPPRESSED_ACTION) . + ' != ' . LogPage::SUPPRESSED_ACTION; } } + /** + * Constructs the most part of the query. Extra conditions are sprinkled in + * all over this class. + * @return array + */ public function getQueryInfo() { - $tables = array( 'logging', 'user' ); - $this->mConds[] = 'user_id = log_user'; - $groupBy = false; - # Add log_search table if there are conditions on it - if( array_key_exists('ls_field',$this->mConds) ) { + $basic = DatabaseLogEntry::getSelectQueryData(); + + $tables = $basic['tables']; + $fields = $basic['fields']; + $conds = $basic['conds']; + $options = $basic['options']; + $joins = $basic['join_conds']; + + $index = array(); + # Add log_search table if there are conditions on it. + # This filters the results to only include log rows that have + # log_search records with the specified ls_field and ls_value values. + if( array_key_exists( 'ls_field', $this->mConds ) ) { $tables[] = 'log_search'; - $index = array( 'log_search' => 'PRIMARY', 'logging' => 'PRIMARY' ); - $groupBy = 'ls_log_id'; - # Don't use the wrong logging index - } else if( $this->title || $this->pattern || $this->user ) { - $index = array( 'logging' => array('page_time','user_time') ); - } else if( $this->types ) { - $index = array( 'logging' => 'type_time' ); + $index['log_search'] = 'ls_field_val'; + $index['logging'] = 'PRIMARY'; + if ( !$this->hasEqualsClause( 'ls_field' ) + || !$this->hasEqualsClause( 'ls_value' ) ) + { + # Since (ls_field,ls_value,ls_logid) is unique, if the condition is + # to match a specific (ls_field,ls_value) tuple, then there will be + # no duplicate log rows. Otherwise, we need to remove the duplicates. + $options[] = 'DISTINCT'; + } + # Avoid usage of the wrong index by limiting + # the choices of available indexes. This mainly + # avoids site-breaking filesorts. + } elseif( $this->title || $this->pattern || $this->performer ) { + $index['logging'] = array( 'page_time', 'user_time' ); + if( count($this->types) == 1 ) { + $index['logging'][] = 'log_user_type_time'; + } + } elseif( count($this->types) == 1 ) { + $index['logging'] = 'type_time'; } else { - $index = array( 'logging' => 'times' ); + $index['logging'] = 'times'; } - $options = array( 'USE INDEX' => $index ); + $options['USE INDEX'] = $index; # Don't show duplicate rows when using log_search - if( $groupBy ) $options['GROUP BY'] = $groupBy; + $joins['log_search'] = array( 'INNER JOIN', 'ls_log_id=log_id' ); + $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, + 'fields' => $fields, + 'conds' => array_merge( $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' ) - ) + 'join_conds' => $joins, ); # Add ChangeTags filter query ChangeTags::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'], $info['join_conds'], $info['options'], $this->mTagFilter ); - return $info; } + // Checks if $this->mConds has $field matched to a *single* value + protected function hasEqualsClause( $field ) { + return ( + array_key_exists( $field, $this->mConds ) && + ( !is_array( $this->mConds[$field] ) || count( $this->mConds[$field] ) == 1 ) + ); + } + function getIndexField() { return 'log_timestamp'; } @@ -777,7 +1009,7 @@ class LogPager extends ReverseChronologicalPager { # Do a link batch query if( $this->getNumRows() > 0 ) { $lb = new LinkBatch; - while( $row = $this->mResult->fetchObject() ) { + 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 ) ); @@ -797,10 +1029,16 @@ class LogPager extends ReverseChronologicalPager { return $this->types; } - public function getUser() { - return $this->user; + /** + * @return string + */ + public function getPerformer() { + return $this->performer; } + /** + * @return string + */ public function getPage() { return $this->title; } @@ -829,112 +1067,3 @@ class LogPager extends ReverseChronologicalPager { } } -/** - * @deprecated - * @ingroup SpecialPage - */ -class LogReader { - var $pager; - /** - * @param $request WebRequest: for internal use use a FauxRequest object to pass arbitrary parameters. - */ - function __construct( $request ) { - global $wgUser, $wgOut; - wfDeprecated(__METHOD__); - # Get parameters - $type = $request->getVal( 'type' ); - $user = $request->getText( 'user' ); - $title = $request->getText( 'page' ); - $pattern = $request->getBool( '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 - */ - public function hasRows() { - return isset($this->pager) ? ($this->pager->getNumRows() > 0) : false; - } -} - -/** - * @deprecated - * @ingroup SpecialPage - */ -class LogViewer { - const NO_ACTION_LINK = 1; - - /** - * LogReader object - */ - var $reader; - - /** - * @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 ) { - wfDeprecated(__METHOD__); - $this->reader =& $reader; - $this->reader->pager->mLogEventsList->flags = $flags; - # Aliases for shorter code... - $this->pager =& $this->reader->pager; - $this->list =& $this->reader->pager->mLogEventsList; - } - - /** - * Take over the whole output page in $wgOut with the log display. - */ - public function show() { - # Set title and add header - $this->list->showHeader( $pager->getType() ); - # Show form options - $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->list->beginLogEventsList() . - $logBody . - $this->list->endLogEventsList() . - $this->pager->getNavigationBar() - ); - } else { - $wgOut->addWikiMsg( 'logempty' ); - } - } - - /** - * 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 $out OutputPage: where to send output - */ - public function showList( &$out ) { - $logBody = $this->pager->getBody(); - if( $logBody ) { - $out->addHTML( - $this->list->beginLogEventsList() . - $logBody . - $this->list->endLogEventsList() - ); - } else { - $out->addWikiMsg( 'logempty' ); - } - } -}