Replace usages of deprecated User::isAllowed. Step 2.
[lhc/web/wiklou.git] / includes / upload / UploadBase.php
index 5b15e82..d7dfffa 100644 (file)
  * @file
  * @ingroup Upload
  */
+
+use MediaWiki\MediaWikiServices;
 use MediaWiki\Shell\Shell;
+use MediaWiki\User\UserIdentity;
 
 /**
  * @defgroup Upload Upload related
@@ -145,12 +148,13 @@ abstract class UploadBase {
         * identifying the missing permission.
         * Can be overridden by subclasses.
         *
-        * @param User $user
+        * @param UserIdentity $user
         * @return bool|string
         */
-       public static function isAllowed( $user ) {
+       public static function isAllowed( UserIdentity $user ) {
+               $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
                foreach ( [ 'upload', 'edit' ] as $permission ) {
-                       if ( !$user->isAllowed( $permission ) ) {
+                       if ( !$permissionManager->userHasRight( $user, $permission ) ) {
                                return $permission;
                        }
                }
@@ -704,6 +708,33 @@ abstract class UploadBase {
                return $warnings;
        }
 
+       /**
+        * Convert the warnings array returned by checkWarnings() to something that
+        * can be serialized. File objects will be converted to an associative array
+        * with the following keys:
+        *
+        *   - fileName: The name of the file
+        *   - timestamp: The upload timestamp
+        *
+        * @param mixed[] $warnings
+        * @return mixed[]
+        */
+       public static function makeWarningsSerializable( $warnings ) {
+               array_walk_recursive( $warnings, function ( &$param, $key ) {
+                       if ( $param instanceof File ) {
+                               $param = [
+                                       'fileName' => $param->getName(),
+                                       'timestamp' => $param->getTimestamp()
+                               ];
+                       } elseif ( is_object( $param ) ) {
+                               throw new InvalidArgumentException(
+                                       'UploadBase::makeWarningsSerializable: ' .
+                                       'Unexpected object of class ' . get_class( $param ) );
+                       }
+               } );
+               return $warnings;
+       }
+
        /**
         * Check whether the resulting filename is different from the desired one,
         * but ignore things like ucfirst() and spaces/underscore things
@@ -1736,7 +1767,6 @@ abstract class UploadBase {
         * Check a block of CSS or CSS fragment for anything that looks like
         * it is bringing in remote code.
         * @param string $value a string of CSS
-        * @param bool $propOnly only check css properties (start regex with :)
         * @return bool true if the CSS contains an illegal string, false if otherwise
         */
        private static function checkCssFragment( $value ) {
@@ -1928,7 +1958,10 @@ abstract class UploadBase {
                 * wfFindFile finds a file, it exists in a shared repository.
                 */
                $file = wfFindFile( $this->getTitle(), [ 'latest' => true ] );
-               if ( $file && !$user->isAllowed( 'reupload-shared' ) ) {
+               if ( $file && !MediaWikiServices::getInstance()
+                               ->getPermissionManager()
+                               ->userHasRight( $user, 'reupload-shared' )
+               ) {
                        return [ 'fileexists-shared-forbidden', $file->getName() ];
                }
 
@@ -1943,9 +1976,10 @@ abstract class UploadBase {
         * @return bool
         */
        public static function userCanReUpload( User $user, File $img ) {
-               if ( $user->isAllowed( 'reupload' ) ) {
+               $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
+               if ( $permissionManager->userHasRight( $user, 'reupload' ) ) {
                        return true; // non-conditional
-               } elseif ( !$user->isAllowed( 'reupload-own' ) ) {
+               } elseif ( !$permissionManager->userHasRight( $user, 'reupload-own' ) ) {
                        return false;
                }