MediaWiki  1.34.0
UploadStashFile.php
Go to the documentation of this file.
1 <?php
25  private $fileKey;
26  private $urlName;
27  protected $url;
28 
41  public function __construct( $repo, $path, $key ) {
42  $this->fileKey = $key;
43 
44  // resolve mwrepo:// urls
45  if ( FileRepo::isVirtualUrl( $path ) ) {
47  } else {
48  // check if path appears to be sane, no parent traversals,
49  // and is in this repo's temp zone.
50  $repoTempPath = $repo->getZonePath( 'temp' );
51  if ( ( !$repo->validateFilename( $path ) ) ||
52  ( strpos( $path, $repoTempPath ) !== 0 )
53  ) {
54  wfDebug( "UploadStash: tried to construct an UploadStashFile "
55  . "from a file that should already exist at '$path', but path is not valid\n" );
57  wfMessage( 'uploadstash-bad-path-invalid' )
58  );
59  }
60 
61  // check if path exists! and is a plain file.
62  if ( !$repo->fileExists( $path ) ) {
63  wfDebug( "UploadStash: tried to construct an UploadStashFile from "
64  . "a file that should already exist at '$path', but path is not found\n" );
66  wfMessage( 'uploadstash-file-not-found-not-exists' )
67  );
68  }
69  }
70 
71  parent::__construct( false, $repo, $path, false );
72 
73  $this->name = basename( $this->path );
74  }
75 
84  public function getDescriptionUrl() {
85  return $this->getUrl();
86  }
87 
98  public function getThumbPath( $thumbName = false ) {
99  $path = dirname( $this->path );
100  if ( $thumbName !== false ) {
101  $path .= "/$thumbName";
102  }
103 
104  return $path;
105  }
106 
116  function thumbName( $params, $flags = 0 ) {
117  return $this->generateThumbName( $this->getUrlName(), $params );
118  }
119 
126  private function getSpecialUrl( $subPage ) {
127  return SpecialPage::getTitleFor( 'UploadStash', $subPage )->getLocalURL();
128  }
129 
140  public function getThumbUrl( $thumbName = false ) {
141  wfDebug( __METHOD__ . " getting for $thumbName \n" );
142 
143  return $this->getSpecialUrl( 'thumb/' . $this->getUrlName() . '/' . $thumbName );
144  }
145 
152  public function getUrlName() {
153  if ( !$this->urlName ) {
154  $this->urlName = $this->fileKey;
155  }
156 
157  return $this->urlName;
158  }
159 
166  public function getUrl() {
167  if ( !isset( $this->url ) ) {
168  $this->url = $this->getSpecialUrl( 'file/' . $this->getUrlName() );
169  }
170 
171  return $this->url;
172  }
173 
181  public function getFullUrl() {
182  return $this->getUrl();
183  }
184 
191  public function getFileKey() {
192  return $this->fileKey;
193  }
194 
199  public function remove() {
200  if ( !$this->repo->fileExists( $this->path ) ) {
201  // Maybe the file's already been removed? This could totally happen in UploadBase.
202  return true;
203  }
204 
205  return $this->repo->freeTemp( $this->path );
206  }
207 
208  public function exists() {
209  return $this->repo->fileExists( $this->path );
210  }
211 }
File\$repo
FileRepo LocalRepo ForeignAPIRepo bool $repo
Some member variables can be lazy-initialised using __get().
Definition: File.php:106
UploadStashFile\getUrlName
getUrlName()
The basename for the URL, which we want to not be related to the filename.
Definition: UploadStashFile.php:152
UploadStashBadPathException
Definition: UploadStashBadPathException.php:26
FileRepo\validateFilename
validateFilename( $filename)
Determine if a relative path is valid, i.e.
Definition: FileRepo.php:1696
UnregisteredLocalFile
A file object referring to either a standalone local file, or a file in a local repository with no da...
Definition: UnregisteredLocalFile.php:36
UploadStashFile
Definition: UploadStashFile.php:24
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
FileRepo\getZonePath
getZonePath( $zone)
Get the storage path corresponding to one of the zones.
Definition: FileRepo.php:372
SpecialPage\getTitleFor
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,...
Definition: SpecialPage.php:83
UnregisteredLocalFile\$path
string $path
Definition: UnregisteredLocalFile.php:41
UploadStashFile\getThumbPath
getThumbPath( $thumbName=false)
Get the path for the thumbnail (actually any transformation of this file) The actual argument is the ...
Definition: UploadStashFile.php:98
UploadStashFile\$urlName
$urlName
Definition: UploadStashFile.php:26
UploadStashFile\getFullUrl
getFullUrl()
Parent classes use this method, for no obvious reason, to return the path (relative to wiki root,...
Definition: UploadStashFile.php:181
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:913
FileRepo\fileExists
fileExists( $file)
Checks existence of a file.
Definition: FileRepo.php:1384
UploadStashFile\$fileKey
$fileKey
Definition: UploadStashFile.php:25
UploadStashFile\$url
$url
Definition: UploadStashFile.php:27
File\generateThumbName
generateThumbName( $name, $params)
Generate a thumbnail file name from a name and specified parameters.
Definition: File.php:977
UploadStashFile\thumbName
thumbName( $params, $flags=0)
Return the file/url base name of a thumbnail with the specified parameters.
Definition: UploadStashFile.php:116
UploadStashFileNotFoundException
Definition: UploadStashFileNotFoundException.php:26
UploadStashFile\getDescriptionUrl
getDescriptionUrl()
A method needed by the file transforming and scaling routines in File.php We do not necessarily care ...
Definition: UploadStashFile.php:84
UploadStashFile\getSpecialUrl
getSpecialUrl( $subPage)
Helper function – given a 'subpage', return the local URL, e.g.
Definition: UploadStashFile.php:126
UploadStashFile\getFileKey
getFileKey()
Getter for file key (the unique id by which this file's location & metadata is stored in the db)
Definition: UploadStashFile.php:191
UploadStashFile\__construct
__construct( $repo, $path, $key)
A LocalFile wrapper around a file that has been temporarily stashed, so we can do things like create ...
Definition: UploadStashFile.php:41
UploadStashFile\exists
exists()
Returns true if file exists in the repository.
Definition: UploadStashFile.php:208
UploadStashFile\getUrl
getUrl()
Return the URL of the file, if for some reason we wanted to download it We tend not to do this for th...
Definition: UploadStashFile.php:166
FileRepo\isVirtualUrl
static isVirtualUrl( $url)
Determine if a string is an mwrepo:// URL.
Definition: FileRepo.php:263
FileRepo\resolveVirtualUrl
resolveVirtualUrl( $url)
Get the backend storage path corresponding to a virtual URL.
Definition: FileRepo.php:332
UploadStashFile\getThumbUrl
getThumbUrl( $thumbName=false)
Get a URL to access the thumbnail This is required because the model of how files work requires that ...
Definition: UploadStashFile.php:140