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