Fix PHP warnings "preg_replace(): [...] invalid range in character class"
[lhc/web/wiklou.git] / includes / upload / UploadStash.php
index da896f9..e55ab1f 100644 (file)
@@ -18,7 +18,6 @@
  * http://www.gnu.org/copyleft/gpl.html
  *
  * @file
- * @ingroup Upload
  */
 
 /**
@@ -53,7 +52,7 @@
  */
 class UploadStash {
        // Format of the key for files -- has to be suitable as a filename itself (e.g. ab12cd34ef.jpg)
-       const KEY_FORMAT_REGEX = '/^[\w-\.]+\.\w*$/';
+       const KEY_FORMAT_REGEX = '/^[\w\-\.]+\.\w*$/';
        const MAX_US_PROPS_SIZE = 65535;
 
        /**
@@ -118,12 +117,15 @@ class UploadStash {
         */
        public function getFile( $key, $noAuth = false ) {
                if ( !preg_match( self::KEY_FORMAT_REGEX, $key ) ) {
-                       throw new UploadStashBadPathException( "key '$key' is not in a proper format" );
+                       throw new UploadStashBadPathException(
+                               wfMessage( 'uploadstash-bad-path-bad-format', $key )
+                       );
                }
 
                if ( !$noAuth && !$this->isLoggedIn ) {
-                       throw new UploadStashNotLoggedInException( __METHOD__ .
-                               ' No user is logged in, files must belong to users' );
+                       throw new UploadStashNotLoggedInException(
+                               wfMessage( 'uploadstash-not-logged-in' )
+                       );
                }
 
                if ( !isset( $this->fileMetadata[$key] ) ) {
@@ -134,7 +136,9 @@ class UploadStash {
                        }
 
                        if ( !isset( $this->fileMetadata[$key] ) ) {
-                               throw new UploadStashFileNotFoundException( "key '$key' not found in stash" );
+                               throw new UploadStashFileNotFoundException(
+                                       wfMessage( 'uploadstash-file-not-found', $key )
+                               );
                        }
 
                        // create $this->files[$key]
@@ -153,13 +157,16 @@ class UploadStash {
                if ( !$this->files[$key]->exists() ) {
                        wfDebug( __METHOD__ . " tried to get file at $key, but it doesn't exist\n" );
                        // @todo Is this not an UploadStashFileNotFoundException case?
-                       throw new UploadStashBadPathException( "path doesn't exist" );
+                       throw new UploadStashBadPathException(
+                               wfMessage( 'uploadstash-bad-path' )
+                       );
                }
 
                if ( !$noAuth ) {
                        if ( $this->fileMetadata[$key]['us_user'] != $this->userId ) {
-                               throw new UploadStashWrongOwnerException( "This file ($key) doesn't "
-                                       . "belong to the current user." );
+                               throw new UploadStashWrongOwnerException(
+                                       wfMessage( 'uploadstash-wrong-owner', $key )
+                               );
                        }
                }
 
@@ -205,10 +212,12 @@ class UploadStash {
        public function stashFile( $path, $sourceType = null ) {
                if ( !is_file( $path ) ) {
                        wfDebug( __METHOD__ . " tried to stash file at '$path', but it doesn't exist\n" );
-                       throw new UploadStashBadPathException( "path doesn't exist" );
+                       throw new UploadStashBadPathException(
+                               wfMessage( 'uploadstash-bad-path' )
+                       );
                }
 
-               $mwProps = new MWFileProps( MimeMagic::singleton() );
+               $mwProps = new MWFileProps( MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer() );
                $fileProps = $mwProps->getPropsFromPath( $path, true );
                wfDebug( __METHOD__ . " stashing file at '$path'\n" );
 
@@ -236,7 +245,9 @@ class UploadStash {
                $this->fileProps[$key] = $fileProps;
 
                if ( !preg_match( self::KEY_FORMAT_REGEX, $key ) ) {
-                       throw new UploadStashBadPathException( "key '$key' is not in a proper format" );
+                       throw new UploadStashBadPathException(
+                               wfMessage( 'uploadstash-bad-path-bad-format', $key )
+                       );
                }
 
                wfDebug( __METHOD__ . " key for '$path': $key\n" );
@@ -265,15 +276,15 @@ class UploadStash {
                        // At this point, $error should contain the single "most important"
                        // error, plus any parameters.
                        $errorMsg = array_shift( $error );
-                       throw new UploadStashFileException( "Error storing file in '$path': "
-                               . wfMessage( $errorMsg, $error )->text() );
+                       throw new UploadStashFileException( wfMessage( $errorMsg, $error ) );
                }
                $stashPath = $storeStatus->value;
 
                // fetch the current user ID
                if ( !$this->isLoggedIn ) {
-                       throw new UploadStashNotLoggedInException( __METHOD__
-                               . ' No user is logged in, files must belong to users' );
+                       throw new UploadStashNotLoggedInException(
+                               wfMessage( 'uploadstash-not-logged-in' )
+                       );
                }
 
                // insert the file metadata into the db.
@@ -332,8 +343,9 @@ class UploadStash {
         */
        public function clear() {
                if ( !$this->isLoggedIn ) {
-                       throw new UploadStashNotLoggedInException( __METHOD__
-                               . ' No user is logged in, files must belong to users' );
+                       throw new UploadStashNotLoggedInException(
+                               wfMessage( 'uploadstash-not-logged-in' )
+                       );
                }
 
                wfDebug( __METHOD__ . ' clearing all rows for user ' . $this->userId . "\n" );
@@ -361,8 +373,9 @@ class UploadStash {
         */
        public function removeFile( $key ) {
                if ( !$this->isLoggedIn ) {
-                       throw new UploadStashNotLoggedInException( __METHOD__
-                               . ' No user is logged in, files must belong to users' );
+                       throw new UploadStashNotLoggedInException(
+                               wfMessage( 'uploadstash-not-logged-in' )
+                       );
                }
 
                $dbw = $this->repo->getMasterDB();
@@ -377,12 +390,15 @@ class UploadStash {
                );
 
                if ( !$row ) {
-                       throw new UploadStashNoSuchKeyException( "No such key ($key), cannot remove" );
+                       throw new UploadStashNoSuchKeyException(
+                               wfMessage( 'uploadstash-no-such-key', $key )
+                       );
                }
 
                if ( $row->us_user != $this->userId ) {
-                       throw new UploadStashWrongOwnerException( "Can't delete: "
-                               . "the file ($key) doesn't belong to this user." );
+                       throw new UploadStashWrongOwnerException(
+                               wfMessage( 'uploadstash-wrong-owner', $key )
+                       );
                }
 
                return $this->removeFileNoAuth( $key );
@@ -427,8 +443,9 @@ class UploadStash {
         */
        public function listFiles() {
                if ( !$this->isLoggedIn ) {
-                       throw new UploadStashNotLoggedInException( __METHOD__
-                               . ' No user is logged in, files must belong to users' );
+                       throw new UploadStashNotLoggedInException(
+                               wfMessage( 'uploadstash-not-logged-in' )
+                       );
                }
 
                $dbr = $this->repo->getReplicaDB();
@@ -472,16 +489,18 @@ class UploadStash {
                        $extension = $n ? substr( $path, $n + 1 ) : '';
                } else {
                        // If not, assume that it should be related to the MIME type of the original file.
-                       $magic = MimeMagic::singleton();
+                       $magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
                        $mimeType = $magic->guessMimeType( $path );
-                       $extensions = explode( ' ', MimeMagic::singleton()->getExtensionsForType( $mimeType ) );
+                       $extensions = explode( ' ', $magic->getExtensionsForType( $mimeType ) );
                        if ( count( $extensions ) ) {
                                $extension = $extensions[0];
                        }
                }
 
                if ( is_null( $extension ) ) {
-                       throw new UploadStashFileException( "extension is null" );
+                       throw new UploadStashFileException(
+                               wfMessage( 'uploadstash-no-extension' )
+                       );
                }
 
                $extension = File::normalizeExtension( $extension );
@@ -546,7 +565,9 @@ class UploadStash {
        protected function initFile( $key ) {
                $file = new UploadStashFile( $this->repo, $this->fileMetadata[$key]['us_path'], $key );
                if ( $file->getSize() === 0 ) {
-                       throw new UploadStashZeroLengthFileException( "File is zero length" );
+                       throw new UploadStashZeroLengthFileException(
+                               wfMessage( 'uploadstash-zero-length' )
+                       );
                }
                $this->files[$key] = $file;
 
@@ -554,6 +575,9 @@ class UploadStash {
        }
 }
 
+/**
+ * @ingroup Upload
+ */
 class UploadStashFile extends UnregisteredLocalFile {
        private $fileKey;
        private $urlName;
@@ -586,14 +610,18 @@ class UploadStashFile extends UnregisteredLocalFile {
                        ) {
                                wfDebug( "UploadStash: tried to construct an UploadStashFile "
                                        . "from a file that should already exist at '$path', but path is not valid\n" );
-                               throw new UploadStashBadPathException( 'path is not valid' );
+                               throw new UploadStashBadPathException(
+                                       wfMessage( 'uploadstash-bad-path-invalid' )
+                               );
                        }
 
                        // check if path exists! and is a plain file.
                        if ( !$repo->fileExists( $path ) ) {
                                wfDebug( "UploadStash: tried to construct an UploadStashFile from "
                                        . "a file that should already exist at '$path', but path is not found\n" );
-                               throw new UploadStashFileNotFoundException( 'cannot find path, or not a plain file' );
+                               throw new UploadStashFileNotFoundException(
+                                       wfMessage( 'uploadstash-file-not-found-not-exists' )
+                               );
                        }
                }
 
@@ -739,26 +767,70 @@ class UploadStashFile extends UnregisteredLocalFile {
        }
 }
 
-class UploadStashException extends MWException {
+/**
+ * @ingroup Upload
+ */
+class UploadStashException extends MWException implements ILocalizedException {
+       /** @var string|array|MessageSpecifier */
+       protected $messageSpec;
+
+       /**
+        * @param string|array|MessageSpecifier $messageSpec See Message::newFromSpecifier
+        * @param int $code Exception code
+        * @param Exception|Throwable $previous The previous exception used for the exception chaining.
+        */
+       public function __construct( $messageSpec, $code = 0, $previous = null ) {
+               $this->messageSpec = $messageSpec;
+
+               $msg = $this->getMessageObject()->text();
+               $msg = preg_replace( '!</?(var|kbd|samp|code)>!', '"', $msg );
+               $msg = Sanitizer::stripAllTags( $msg );
+               parent::__construct( $msg, $code, $previous );
+       }
+
+       public function getMessageObject() {
+               return Message::newFromSpecifier( $this->messageSpec );
+       }
 }
 
+/**
+ * @ingroup Upload
+ */
 class UploadStashFileNotFoundException extends UploadStashException {
 }
 
+/**
+ * @ingroup Upload
+ */
 class UploadStashBadPathException extends UploadStashException {
 }
 
+/**
+ * @ingroup Upload
+ */
 class UploadStashFileException extends UploadStashException {
 }
 
+/**
+ * @ingroup Upload
+ */
 class UploadStashZeroLengthFileException extends UploadStashException {
 }
 
+/**
+ * @ingroup Upload
+ */
 class UploadStashNotLoggedInException extends UploadStashException {
 }
 
+/**
+ * @ingroup Upload
+ */
 class UploadStashWrongOwnerException extends UploadStashException {
 }
 
+/**
+ * @ingroup Upload
+ */
 class UploadStashNoSuchKeyException extends UploadStashException {
 }