MediaWiki master
UploadStashFile.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Upload;
8
15
21 private $fileKey;
23 private $urlName;
25 protected $url;
27 private $sha1;
28
43 public function __construct( $repo, $path, $key, $sha1 = null, $mime = false ) {
44 $this->fileKey = $key;
45 $this->sha1 = $sha1;
46
47 // resolve mwrepo:// urls
48 if ( FileRepo::isVirtualUrl( $path ) ) {
50 } else {
51 // check if path appears to be correct, no parent traversals,
52 // and is in this repo's temp zone.
53 $repoTempPath = $repo->getZonePath( 'temp' );
54 if ( ( !$repo->validateFilename( $path ) ) ||
55 !str_starts_with( $path, $repoTempPath )
56 ) {
57 wfDebug( "UploadStash: tried to construct an UploadStashFile "
58 . "from a file that should already exist at '$path', but path is not valid" );
60 wfMessage( 'uploadstash-bad-path-invalid' )
61 );
62 }
63
64 // check if path exists! and is a plain file.
65 if ( !$repo->fileExists( $path ) ) {
66 wfDebug( "UploadStash: tried to construct an UploadStashFile from "
67 . "a file that should already exist at '$path', but path is not found" );
69 wfMessage( 'uploadstash-file-not-found-not-exists' )
70 );
71 }
72 }
73
74 parent::__construct( false, $repo, $path, $mime );
75
76 $this->name = basename( $this->path );
77 }
78
85 public function getSha1() {
86 if ( !$this->sha1 ) {
87 $this->sha1 = parent::getSha1();
88 }
89 return $this->sha1;
90 }
91
100 public function getDescriptionUrl() {
101 return $this->getUrl();
102 }
103
114 public function getThumbPath( $thumbName = false ) {
115 $path = dirname( $this->path );
116 if ( $thumbName !== false ) {
117 $path .= "/$thumbName";
118 }
119
120 return $path;
121 }
122
132 public function thumbName( $params, $flags = 0 ) {
133 return $this->generateThumbName( $this->getUrlName(), $params );
134 }
135
142 private function getSpecialUrl( $subPage ) {
143 return SpecialPage::getTitleFor( 'UploadStash', $subPage )->getLocalURL();
144 }
145
156 public function getThumbUrl( $thumbName = false ) {
157 wfDebug( __METHOD__ . " getting for $thumbName" );
158
159 return $this->getSpecialUrl( 'thumb/' . $this->getUrlName() . '/' . $thumbName );
160 }
161
168 public function getUrlName() {
169 if ( !$this->urlName ) {
170 $this->urlName = $this->fileKey;
171 }
172
173 return $this->urlName;
174 }
175
182 public function getUrl() {
183 if ( $this->url === null ) {
184 $this->url = $this->getSpecialUrl( 'file/' . $this->getUrlName() );
185 }
186
187 return $this->url;
188 }
189
197 public function getFullUrl() {
198 return $this->getUrl();
199 }
200
207 public function getFileKey() {
208 return $this->fileKey;
209 }
210
215 public function remove() {
216 if ( !$this->repo->fileExists( $this->path ) ) {
217 // Maybe the file's already been removed? This could totally happen in UploadBase.
218 return true;
219 }
220
221 return $this->repo->freeTemp( $this->path );
222 }
223
225 public function exists() {
226 return $this->repo->fileExists( $this->path );
227 }
228}
229
231class_alias( UploadStashFile::class, 'UploadStashFile' );
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Base class for file repositories.
Definition FileRepo.php:52
resolveVirtualUrl( $url)
Get the backend storage path corresponding to a virtual URL.
Definition FileRepo.php:358
fileExists( $file)
Checks existence of a file.
getZonePath( $zone)
Get the storage path corresponding to one of the zones.
Definition FileRepo.php:398
validateFilename( $filename)
Determine if a relative path is valid, i.e.
FileRepo LocalRepo ForeignAPIRepo false $repo
Some member variables can be lazy-initialised using __get().
Definition File.php:126
generateThumbName( $name, $params)
Generate a thumbnail file name from a name and specified parameters.
Definition File.php:1115
File without associated database record.
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
Definition LocalRepo.php:45
Parent class for all special pages.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
getUrlName()
The basename for the URL, which we want to not be related to the filename.
getThumbUrl( $thumbName=false)
Get a URL to access the thumbnail This is required because the model of how files work requires that ...
__construct( $repo, $path, $key, $sha1=null, $mime=false)
A LocalFile wrapper around a file that has been temporarily stashed, so we can do things like create ...
string null $url
Lazy set as in-memory cache.
exists()
Returns true if file exists in the repository.Overridden by LocalFile to avoid unnecessary stat calls...
getThumbPath( $thumbName=false)
Get the path for the thumbnail (actually any transformation of this file) The actual argument is the ...
thumbName( $params, $flags=0)
Return the file/url base name of a thumbnail with the specified parameters.
getSha1()
Get the SHA-1 base 36 hash.
getDescriptionUrl()
A method needed by the file transforming and scaling routines in File.php We do not necessarily care ...
getUrl()
Return the URL of the file, if for some reason we wanted to download it We tend not to do this for th...
getFullUrl()
Parent classes use this method, for no obvious reason, to return the path (relative to wiki root,...
getFileKey()
Getter for file key (the unique id by which this file's location & metadata is stored in the db)