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