From d1a3d98be94d19e3eaa706569a75ad0f0a2da965 Mon Sep 17 00:00:00 2001 From: Reedy Date: Sun, 14 Apr 2019 03:50:59 +0100 Subject: [PATCH] Move UploadStashFile to its own file Change-Id: I557bcd8369014bf2ea43c5eb8336562cb6295890 --- .phpcs.xml | 1 - autoload.php | 2 +- includes/upload/UploadStash.php | 192 ------------------------- includes/upload/UploadStashFile.php | 211 ++++++++++++++++++++++++++++ 4 files changed, 212 insertions(+), 194 deletions(-) create mode 100644 includes/upload/UploadStashFile.php diff --git a/.phpcs.xml b/.phpcs.xml index 0fb1587152..31ee7068b2 100644 --- a/.phpcs.xml +++ b/.phpcs.xml @@ -217,7 +217,6 @@ */includes/parser/Preprocessor\.php */includes/profiler/SectionProfiler\.php */includes/StubObject\.php - */includes/upload/UploadStash\.php */includes/utils/AutoloadGenerator\.php */maintenance/dumpIterator\.php */maintenance/Maintenance\.php diff --git a/autoload.php b/autoload.php index 727c21face..5ed3981cc8 100644 --- a/autoload.php +++ b/autoload.php @@ -1541,7 +1541,7 @@ $wgAutoloadLocalClasses = [ 'UploadStash' => __DIR__ . '/includes/upload/UploadStash.php', 'UploadStashBadPathException' => __DIR__ . '/includes/upload/exception/UploadStashBadPathException.php', 'UploadStashException' => __DIR__ . '/includes/upload/exception/UploadStashException.php', - 'UploadStashFile' => __DIR__ . '/includes/upload/UploadStash.php', + 'UploadStashFile' => __DIR__ . '/includes/upload/UploadStashFile.php', 'UploadStashFileException' => __DIR__ . '/includes/upload/exception/UploadStashFileException.php', 'UploadStashFileNotFoundException' => __DIR__ . '/includes/upload/exception/UploadStashFileNotFoundException.php', 'UploadStashNoSuchKeyException' => __DIR__ . '/includes/upload/exception/UploadStashNoSuchKeyException.php', diff --git a/includes/upload/UploadStash.php b/includes/upload/UploadStash.php index bc7b42b6ec..2c4bc11b71 100644 --- a/includes/upload/UploadStash.php +++ b/includes/upload/UploadStash.php @@ -572,195 +572,3 @@ class UploadStash { return true; } } - -/** - * @ingroup Upload - */ -class UploadStashFile extends UnregisteredLocalFile { - private $fileKey; - private $urlName; - protected $url; - - /** - * A LocalFile wrapper around a file that has been temporarily stashed, - * so we can do things like create thumbnails for it. Arguably - * UnregisteredLocalFile should be handling its own file repo but that - * class is a bit retarded currently. - * - * @param FileRepo $repo Repository where we should find the path - * @param string $path Path to file - * @param string $key Key to store the path and any stashed data under - * @throws UploadStashBadPathException - * @throws UploadStashFileNotFoundException - */ - public function __construct( $repo, $path, $key ) { - $this->fileKey = $key; - - // resolve mwrepo:// urls - if ( FileRepo::isVirtualUrl( $path ) ) { - $path = $repo->resolveVirtualUrl( $path ); - } else { - // check if path appears to be sane, no parent traversals, - // and is in this repo's temp zone. - $repoTempPath = $repo->getZonePath( 'temp' ); - if ( ( !$repo->validateFilename( $path ) ) || - ( strpos( $path, $repoTempPath ) !== 0 ) - ) { - 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( - 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( - wfMessage( 'uploadstash-file-not-found-not-exists' ) - ); - } - } - - parent::__construct( false, $repo, $path, false ); - - $this->name = basename( $this->path ); - } - - /** - * A method needed by the file transforming and scaling routines in File.php - * We do not necessarily care about doing the description at this point - * However, we also can't return the empty string, as the rest of MediaWiki - * demands this (and calls to imagemagick convert require it to be there) - * - * @return string Dummy value - */ - public function getDescriptionUrl() { - return $this->getUrl(); - } - - /** - * Get the path for the thumbnail (actually any transformation of this file) - * The actual argument is the result of thumbName although we seem to have - * buggy code elsewhere that expects a boolean 'suffix' - * - * @param string $thumbName Name of thumbnail (e.g. "120px-123456.jpg" ), - * or false to just get the path - * @return string Path thumbnail should take on filesystem, or containing - * directory if thumbname is false - */ - public function getThumbPath( $thumbName = false ) { - $path = dirname( $this->path ); - if ( $thumbName !== false ) { - $path .= "/$thumbName"; - } - - return $path; - } - - /** - * Return the file/url base name of a thumbnail with the specified parameters. - * We override this because we want to use the pretty url name instead of the - * ugly file name. - * - * @param array $params Handler-specific parameters - * @param int $flags Bitfield that supports THUMB_* constants - * @return string|null Base name for URL, like '120px-12345.jpg', or null if there is no handler - */ - function thumbName( $params, $flags = 0 ) { - return $this->generateThumbName( $this->getUrlName(), $params ); - } - - /** - * Helper function -- given a 'subpage', return the local URL, - * e.g. /wiki/Special:UploadStash/subpage - * @param string $subPage - * @return string Local URL for this subpage in the Special:UploadStash space. - */ - private function getSpecialUrl( $subPage ) { - return SpecialPage::getTitleFor( 'UploadStash', $subPage )->getLocalURL(); - } - - /** - * Get a URL to access the thumbnail - * This is required because the model of how files work requires that - * the thumbnail urls be predictable. However, in our model the URL is - * not based on the filename (that's hidden in the db) - * - * @param string $thumbName Basename of thumbnail file -- however, we don't - * want to use the file exactly - * @return string URL to access thumbnail, or URL with partial path - */ - public function getThumbUrl( $thumbName = false ) { - wfDebug( __METHOD__ . " getting for $thumbName \n" ); - - return $this->getSpecialUrl( 'thumb/' . $this->getUrlName() . '/' . $thumbName ); - } - - /** - * The basename for the URL, which we want to not be related to the filename. - * Will also be used as the lookup key for a thumbnail file. - * - * @return string Base url name, like '120px-123456.jpg' - */ - public function getUrlName() { - if ( !$this->urlName ) { - $this->urlName = $this->fileKey; - } - - return $this->urlName; - } - - /** - * Return the URL of the file, if for some reason we wanted to download it - * We tend not to do this for the original file, but we do want thumb icons - * - * @return string Url - */ - public function getUrl() { - if ( !isset( $this->url ) ) { - $this->url = $this->getSpecialUrl( 'file/' . $this->getUrlName() ); - } - - return $this->url; - } - - /** - * Parent classes use this method, for no obvious reason, to return the path - * (relative to wiki root, I assume). But with this class, the URL is - * unrelated to the path. - * - * @return string Url - */ - public function getFullUrl() { - return $this->getUrl(); - } - - /** - * Getter for file key (the unique id by which this file's location & - * metadata is stored in the db) - * - * @return string File key - */ - public function getFileKey() { - return $this->fileKey; - } - - /** - * Remove the associated temporary file - * @return bool Success - */ - public function remove() { - if ( !$this->repo->fileExists( $this->path ) ) { - // Maybe the file's already been removed? This could totally happen in UploadBase. - return true; - } - - return $this->repo->freeTemp( $this->path ); - } - - public function exists() { - return $this->repo->fileExists( $this->path ); - } -} diff --git a/includes/upload/UploadStashFile.php b/includes/upload/UploadStashFile.php new file mode 100644 index 0000000000..53edba5d9a --- /dev/null +++ b/includes/upload/UploadStashFile.php @@ -0,0 +1,211 @@ +fileKey = $key; + + // resolve mwrepo:// urls + if ( FileRepo::isVirtualUrl( $path ) ) { + $path = $repo->resolveVirtualUrl( $path ); + } else { + // check if path appears to be sane, no parent traversals, + // and is in this repo's temp zone. + $repoTempPath = $repo->getZonePath( 'temp' ); + if ( ( !$repo->validateFilename( $path ) ) || + ( strpos( $path, $repoTempPath ) !== 0 ) + ) { + 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( + 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( + wfMessage( 'uploadstash-file-not-found-not-exists' ) + ); + } + } + + parent::__construct( false, $repo, $path, false ); + + $this->name = basename( $this->path ); + } + + /** + * A method needed by the file transforming and scaling routines in File.php + * We do not necessarily care about doing the description at this point + * However, we also can't return the empty string, as the rest of MediaWiki + * demands this (and calls to imagemagick convert require it to be there) + * + * @return string Dummy value + */ + public function getDescriptionUrl() { + return $this->getUrl(); + } + + /** + * Get the path for the thumbnail (actually any transformation of this file) + * The actual argument is the result of thumbName although we seem to have + * buggy code elsewhere that expects a boolean 'suffix' + * + * @param string $thumbName Name of thumbnail (e.g. "120px-123456.jpg" ), + * or false to just get the path + * @return string Path thumbnail should take on filesystem, or containing + * directory if thumbname is false + */ + public function getThumbPath( $thumbName = false ) { + $path = dirname( $this->path ); + if ( $thumbName !== false ) { + $path .= "/$thumbName"; + } + + return $path; + } + + /** + * Return the file/url base name of a thumbnail with the specified parameters. + * We override this because we want to use the pretty url name instead of the + * ugly file name. + * + * @param array $params Handler-specific parameters + * @param int $flags Bitfield that supports THUMB_* constants + * @return string|null Base name for URL, like '120px-12345.jpg', or null if there is no handler + */ + function thumbName( $params, $flags = 0 ) { + return $this->generateThumbName( $this->getUrlName(), $params ); + } + + /** + * Helper function -- given a 'subpage', return the local URL, + * e.g. /wiki/Special:UploadStash/subpage + * @param string $subPage + * @return string Local URL for this subpage in the Special:UploadStash space. + */ + private function getSpecialUrl( $subPage ) { + return SpecialPage::getTitleFor( 'UploadStash', $subPage )->getLocalURL(); + } + + /** + * Get a URL to access the thumbnail + * This is required because the model of how files work requires that + * the thumbnail urls be predictable. However, in our model the URL is + * not based on the filename (that's hidden in the db) + * + * @param string $thumbName Basename of thumbnail file -- however, we don't + * want to use the file exactly + * @return string URL to access thumbnail, or URL with partial path + */ + public function getThumbUrl( $thumbName = false ) { + wfDebug( __METHOD__ . " getting for $thumbName \n" ); + + return $this->getSpecialUrl( 'thumb/' . $this->getUrlName() . '/' . $thumbName ); + } + + /** + * The basename for the URL, which we want to not be related to the filename. + * Will also be used as the lookup key for a thumbnail file. + * + * @return string Base url name, like '120px-123456.jpg' + */ + public function getUrlName() { + if ( !$this->urlName ) { + $this->urlName = $this->fileKey; + } + + return $this->urlName; + } + + /** + * Return the URL of the file, if for some reason we wanted to download it + * We tend not to do this for the original file, but we do want thumb icons + * + * @return string Url + */ + public function getUrl() { + if ( !isset( $this->url ) ) { + $this->url = $this->getSpecialUrl( 'file/' . $this->getUrlName() ); + } + + return $this->url; + } + + /** + * Parent classes use this method, for no obvious reason, to return the path + * (relative to wiki root, I assume). But with this class, the URL is + * unrelated to the path. + * + * @return string Url + */ + public function getFullUrl() { + return $this->getUrl(); + } + + /** + * Getter for file key (the unique id by which this file's location & + * metadata is stored in the db) + * + * @return string File key + */ + public function getFileKey() { + return $this->fileKey; + } + + /** + * Remove the associated temporary file + * @return bool Success + */ + public function remove() { + if ( !$this->repo->fileExists( $this->path ) ) { + // Maybe the file's already been removed? This could totally happen in UploadBase. + return true; + } + + return $this->repo->freeTemp( $this->path ); + } + + public function exists() { + return $this->repo->fileExists( $this->path ); + } +} -- 2.20.1