Replace usages of deprecated User::isAllowed. Step 2.
[lhc/web/wiklou.git] / includes / logging / LogFormatter.php
index b9bb70c..b4a6825 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * Contains classes for formatting log entries
+ * Contains a class for formatting log entries
  *
  * 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
@@ -153,6 +153,21 @@ class LogFormatter {
                        : self::FOR_PUBLIC;
        }
 
+       /**
+        * Check if a log item type can be displayed
+        * @return bool
+        */
+       public function canViewLogType() {
+               // If the user doesn't have the right permission to view the specific
+               // log type, return false
+               $logRestrictions = $this->context->getConfig()->get( 'LogRestrictions' );
+               $type = $this->entry->getType();
+               return !isset( $logRestrictions[$type] )
+                       || MediaWikiServices::getInstance()
+                                  ->getPermissionManager()
+                                  ->userHasRight( $this->context->getUser(), $logRestrictions[$type] );
+       }
+
        /**
         * Check if a log item can be displayed
         * @param int $field LogPage::DELETED_* constant
@@ -161,9 +176,10 @@ class LogFormatter {
        protected function canView( $field ) {
                if ( $this->audience == self::FOR_THIS_USER ) {
                        return LogEventsList::userCanBitfield(
-                               $this->entry->getDeleted(), $field, $this->context->getUser() );
+                               $this->entry->getDeleted(), $field, $this->context->getUser() ) &&
+                               self::canViewLogType();
                } else {
-                       return !$this->entry->isDeleted( $field );
+                       return !$this->entry->isDeleted( $field ) && self::canViewLogType();
                }
        }
 
@@ -613,9 +629,13 @@ class LogFormatter {
                                $this->setShowUserToolLinks( false );
 
                                $user = User::newFromName( $value );
-                               $value = Message::rawParam( $this->makeUserLink( $user ) );
 
-                               $this->setShowUserToolLinks( $saveLinkFlood );
+                               if ( !$user ) {
+                                       $value = $this->msg( 'empty-username' )->text();
+                               } else {
+                                       $value = Message::rawParam( $this->makeUserLink( $user ) );
+                                       $this->setShowUserToolLinks( $saveLinkFlood );
+                               }
                                break;
                        case 'title':
                                $title = Title::newFromText( $value );
@@ -910,106 +930,3 @@ class LogFormatter {
                return [ $name => $value ];
        }
 }
-
-/**
- * This class formats all log entries for log types
- * which have not been converted to the new system.
- * This is not about old log entries which store
- * parameters in a different format - the new
- * LogFormatter classes have code to support formatting
- * those too.
- * @since 1.19
- */
-class LegacyLogFormatter extends LogFormatter {
-       /**
-        * Backward compatibility for extension changing the comment from
-        * the LogLine hook. This will be set by the first call on getComment(),
-        * then it might be modified by the hook when calling getActionLinks(),
-        * so that the modified value will be returned when calling getComment()
-        * a second time.
-        *
-        * @var string|null
-        */
-       private $comment = null;
-
-       /**
-        * Cache for the result of getActionLinks() so that it does not need to
-        * run multiple times depending on the order that getComment() and
-        * getActionLinks() are called.
-        *
-        * @var string|null
-        */
-       private $revert = null;
-
-       public function getComment() {
-               if ( $this->comment === null ) {
-                       $this->comment = parent::getComment();
-               }
-
-               // Make sure we execute the LogLine hook so that we immediately return
-               // the correct value.
-               if ( $this->revert === null ) {
-                       $this->getActionLinks();
-               }
-
-               return $this->comment;
-       }
-
-       /**
-        * @return string
-        * @return-taint onlysafefor_html
-        */
-       protected function getActionMessage() {
-               $entry = $this->entry;
-               $action = LogPage::actionText(
-                       $entry->getType(),
-                       $entry->getSubtype(),
-                       $entry->getTarget(),
-                       $this->plaintext ? null : $this->context->getSkin(),
-                       (array)$entry->getParameters(),
-                       !$this->plaintext // whether to filter [[]] links
-               );
-
-               $performer = $this->getPerformerElement();
-               if ( !$this->irctext ) {
-                       $sep = $this->msg( 'word-separator' );
-                       $sep = $this->plaintext ? $sep->text() : $sep->escaped();
-                       $action = $performer . $sep . $action;
-               }
-
-               return $action;
-       }
-
-       public function getActionLinks() {
-               if ( $this->revert !== null ) {
-                       return $this->revert;
-               }
-
-               if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) ) {
-                       $this->revert = '';
-                       return $this->revert;
-               }
-
-               $title = $this->entry->getTarget();
-               $type = $this->entry->getType();
-               $subtype = $this->entry->getSubtype();
-
-               // Do nothing. The implementation is handled by the hook modifiying the
-               // passed-by-ref parameters. This also changes the default value so that
-               // getComment() and getActionLinks() do not call them indefinitely.
-               $this->revert = '';
-
-               // This is to populate the $comment member of this instance so that it
-               // can be modified when calling the hook just below.
-               if ( $this->comment === null ) {
-                       $this->getComment();
-               }
-
-               $params = $this->entry->getParameters();
-
-               Hooks::run( 'LogLine', [ $type, $subtype, $title, $params,
-                       &$this->comment, &$this->revert, $this->entry->getTimestamp() ] );
-
-               return $this->revert;
-       }
-}