| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | * http://www.gnu.org/copyleft/gpl.html |
| 17 | * |
| 18 | * @file |
| 19 | */ |
| 20 | |
| 21 | use MediaWiki\SpecialPage\SpecialPage; |
| 22 | |
| 23 | /** |
| 24 | * @ingroup Upload |
| 25 | */ |
| 26 | class UploadStashFile extends UnregisteredLocalFile { |
| 27 | /** @var string */ |
| 28 | private $fileKey; |
| 29 | /** @var string|null Lazy set as in-memory cache */ |
| 30 | private $urlName; |
| 31 | /** @var string|null Lazy set as in-memory cache */ |
| 32 | protected $url; |
| 33 | /** @var string|null */ |
| 34 | private $sha1; |
| 35 | |
| 36 | /** |
| 37 | * A LocalFile wrapper around a file that has been temporarily stashed, |
| 38 | * so we can do things like create thumbnails for it. Arguably |
| 39 | * UnregisteredLocalFile should be handling its own file repo but that |
| 40 | * class is a bit retarded currently. |
| 41 | * |
| 42 | * @param FileRepo $repo Repository where we should find the path |
| 43 | * @param string $path Path to file |
| 44 | * @param string $key Key to store the path and any stashed data under |
| 45 | * @param string|null $sha1 SHA1 of file. Will calculate if not set |
| 46 | * @throws UploadStashBadPathException |
| 47 | * @throws UploadStashFileNotFoundException |
| 48 | */ |
| 49 | public function __construct( $repo, $path, $key, $sha1 = null ) { |
| 50 | $this->fileKey = $key; |
| 51 | $this->sha1 = $sha1; |
| 52 | |
| 53 | // resolve mwrepo:// urls |
| 54 | if ( FileRepo::isVirtualUrl( $path ) ) { |
| 55 | $path = $repo->resolveVirtualUrl( $path ); |
| 56 | } else { |
| 57 | // check if path appears to be correct, no parent traversals, |
| 58 | // and is in this repo's temp zone. |
| 59 | $repoTempPath = $repo->getZonePath( 'temp' ); |
| 60 | if ( ( !$repo->validateFilename( $path ) ) || |
| 61 | !str_starts_with( $path, $repoTempPath ) |
| 62 | ) { |
| 63 | wfDebug( "UploadStash: tried to construct an UploadStashFile " |
| 64 | . "from a file that should already exist at '$path', but path is not valid" ); |
| 65 | throw new UploadStashBadPathException( |
| 66 | wfMessage( 'uploadstash-bad-path-invalid' ) |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | // check if path exists! and is a plain file. |
| 71 | if ( !$repo->fileExists( $path ) ) { |
| 72 | wfDebug( "UploadStash: tried to construct an UploadStashFile from " |
| 73 | . "a file that should already exist at '$path', but path is not found" ); |
| 74 | throw new UploadStashFileNotFoundException( |
| 75 | wfMessage( 'uploadstash-file-not-found-not-exists' ) |
| 76 | ); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | parent::__construct( false, $repo, $path, false ); |
| 81 | |
| 82 | $this->name = basename( $this->path ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Get the SHA-1 base 36 hash |
| 87 | * |
| 88 | * This can be expensive on large files, so cache the value |
| 89 | * @return string|false |
| 90 | */ |
| 91 | public function getSha1() { |
| 92 | if ( !$this->sha1 ) { |
| 93 | $this->sha1 = parent::getSha1(); |
| 94 | } |
| 95 | return $this->sha1; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * A method needed by the file transforming and scaling routines in File.php |
| 100 | * We do not necessarily care about doing the description at this point |
| 101 | * However, we also can't return the empty string, as the rest of MediaWiki |
| 102 | * demands this (and calls to imagemagick convert require it to be there) |
| 103 | * |
| 104 | * @return string Dummy value |
| 105 | */ |
| 106 | public function getDescriptionUrl() { |
| 107 | return $this->getUrl(); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get the path for the thumbnail (actually any transformation of this file) |
| 112 | * The actual argument is the result of thumbName although we seem to have |
| 113 | * buggy code elsewhere that expects a boolean 'suffix' |
| 114 | * |
| 115 | * @param string|false $thumbName Name of thumbnail (e.g. "120px-123456.jpg" ), |
| 116 | * or false to just get the path |
| 117 | * @return string Path thumbnail should take on filesystem, or containing |
| 118 | * directory if thumbname is false |
| 119 | */ |
| 120 | public function getThumbPath( $thumbName = false ) { |
| 121 | $path = dirname( $this->path ); |
| 122 | if ( $thumbName !== false ) { |
| 123 | $path .= "/$thumbName"; |
| 124 | } |
| 125 | |
| 126 | return $path; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Return the file/url base name of a thumbnail with the specified parameters. |
| 131 | * We override this because we want to use the pretty url name instead of the |
| 132 | * ugly file name. |
| 133 | * |
| 134 | * @param array $params Handler-specific parameters |
| 135 | * @param int $flags Bitfield that supports THUMB_* constants |
| 136 | * @return string|null Base name for URL, like '120px-12345.jpg', or null if there is no handler |
| 137 | */ |
| 138 | public function thumbName( $params, $flags = 0 ) { |
| 139 | return $this->generateThumbName( $this->getUrlName(), $params ); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Helper function -- given a 'subpage', return the local URL, |
| 144 | * e.g. /wiki/Special:UploadStash/subpage |
| 145 | * @param string $subPage |
| 146 | * @return string Local URL for this subpage in the Special:UploadStash space. |
| 147 | */ |
| 148 | private function getSpecialUrl( $subPage ) { |
| 149 | return SpecialPage::getTitleFor( 'UploadStash', $subPage )->getLocalURL(); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Get a URL to access the thumbnail |
| 154 | * This is required because the model of how files work requires that |
| 155 | * the thumbnail urls be predictable. However, in our model the URL is |
| 156 | * not based on the filename (that's hidden in the db) |
| 157 | * |
| 158 | * @param string|false $thumbName Basename of thumbnail file -- however, we don't |
| 159 | * want to use the file exactly |
| 160 | * @return string URL to access thumbnail, or URL with partial path |
| 161 | */ |
| 162 | public function getThumbUrl( $thumbName = false ) { |
| 163 | wfDebug( __METHOD__ . " getting for $thumbName" ); |
| 164 | |
| 165 | return $this->getSpecialUrl( 'thumb/' . $this->getUrlName() . '/' . $thumbName ); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * The basename for the URL, which we want to not be related to the filename. |
| 170 | * Will also be used as the lookup key for a thumbnail file. |
| 171 | * |
| 172 | * @return string Base url name, like '120px-123456.jpg' |
| 173 | */ |
| 174 | public function getUrlName() { |
| 175 | if ( !$this->urlName ) { |
| 176 | $this->urlName = $this->fileKey; |
| 177 | } |
| 178 | |
| 179 | return $this->urlName; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Return the URL of the file, if for some reason we wanted to download it |
| 184 | * We tend not to do this for the original file, but we do want thumb icons |
| 185 | * |
| 186 | * @return string Url |
| 187 | */ |
| 188 | public function getUrl() { |
| 189 | if ( !isset( $this->url ) ) { |
| 190 | $this->url = $this->getSpecialUrl( 'file/' . $this->getUrlName() ); |
| 191 | } |
| 192 | |
| 193 | return $this->url; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Parent classes use this method, for no obvious reason, to return the path |
| 198 | * (relative to wiki root, I assume). But with this class, the URL is |
| 199 | * unrelated to the path. |
| 200 | * |
| 201 | * @return string Url |
| 202 | */ |
| 203 | public function getFullUrl() { |
| 204 | return $this->getUrl(); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Getter for file key (the unique id by which this file's location & |
| 209 | * metadata is stored in the db) |
| 210 | * |
| 211 | * @return string File key |
| 212 | */ |
| 213 | public function getFileKey() { |
| 214 | return $this->fileKey; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Remove the associated temporary file |
| 219 | * @return bool Success |
| 220 | */ |
| 221 | public function remove() { |
| 222 | if ( !$this->repo->fileExists( $this->path ) ) { |
| 223 | // Maybe the file's already been removed? This could totally happen in UploadBase. |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | return $this->repo->freeTemp( $this->path ); |
| 228 | } |
| 229 | |
| 230 | public function exists() { |
| 231 | return $this->repo->fileExists( $this->path ); |
| 232 | } |
| 233 | } |