MediaWiki master
UploadStashFile.php
Go to the documentation of this file.
1<?php
24
30 private $fileKey;
32 private $urlName;
34 protected $url;
36 private $sha1;
37
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 ) ) {
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" );
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" );
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
94 public function getSha1() {
95 if ( !$this->sha1 ) {
96 $this->sha1 = parent::getSha1();
97 }
98 return $this->sha1;
99 }
100
109 public function getDescriptionUrl() {
110 return $this->getUrl();
111 }
112
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
141 public function thumbName( $params, $flags = 0 ) {
142 return $this->generateThumbName( $this->getUrlName(), $params );
143 }
144
151 private function getSpecialUrl( $subPage ) {
152 return SpecialPage::getTitleFor( 'UploadStash', $subPage )->getLocalURL();
153 }
154
165 public function getThumbUrl( $thumbName = false ) {
166 wfDebug( __METHOD__ . " getting for $thumbName" );
167
168 return $this->getSpecialUrl( 'thumb/' . $this->getUrlName() . '/' . $thumbName );
169 }
170
177 public function getUrlName() {
178 if ( !$this->urlName ) {
179 $this->urlName = $this->fileKey;
180 }
181
182 return $this->urlName;
183 }
184
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
206 public function getFullUrl() {
207 return $this->getUrl();
208 }
209
216 public function getFileKey() {
217 return $this->fileKey;
218 }
219
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}
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:68
resolveVirtualUrl( $url)
Get the backend storage path corresponding to a virtual URL.
Definition FileRepo.php:374
fileExists( $file)
Checks existence of a file.
getZonePath( $zone)
Get the storage path corresponding to one of the zones.
Definition FileRepo.php:414
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:140
generateThumbName( $name, $params)
Generate a thumbnail file name from a name and specified parameters.
Definition File.php:1128
File without associated database record.
Parent class for all special pages.
__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 ...
exists()
Returns true if file exists in the repository.
getThumbPath( $thumbName=false)
Get the path for the thumbnail (actually any transformation of this file) The actual argument is the ...
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...
string null $url
Lazy set as in-memory cache.
getThumbUrl( $thumbName=false)
Get a URL to access the thumbnail This is required because the model of how files work requires that ...
getFullUrl()
Parent classes use this method, for no obvious reason, to return the path (relative to wiki root,...
getUrlName()
The basename for the URL, which we want to not be related to the filename.
getSha1()
Get the SHA-1 base 36 hash.
getFileKey()
Getter for file key (the unique id by which this file's location & metadata is stored in the db)
thumbName( $params, $flags=0)
Return the file/url base name of a thumbnail with the specified parameters.