* http://www.gnu.org/copyleft/gpl.html
*
* @file
- * @ingroup Upload
*/
/**
*/
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] ) ) {
}
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]
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 )
+ );
}
}
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" );
$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" );
// 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.
*/
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" );
*/
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();
);
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 );
*/
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();
$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 );
$row = $dbr->selectRow(
'uploadstash',
- '*',
+ [
+ 'us_user', 'us_key', 'us_orig_path', 'us_path', 'us_props',
+ 'us_size', 'us_sha1', 'us_mime', 'us_media_type',
+ 'us_image_width', 'us_image_height', 'us_image_bits',
+ 'us_source_type', 'us_timestamp', 'us_status',
+ ],
[ 'us_key' => $key ],
__METHOD__
);
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;
}
}
+/**
+ * @ingroup Upload
+ */
class UploadStashFile extends UnregisteredLocalFile {
private $fileKey;
private $urlName;
) {
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' )
+ );
}
}
}
}
-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 {
}