MediaWiki  master
UnregisteredLocalFile.php
Go to the documentation of this file.
1 <?php
23 
38 class UnregisteredLocalFile extends File {
40  protected $title;
41 
43  protected $path;
44 
46  protected $mime;
47 
49  protected $pageDims;
50 
52  protected $sizeAndMetadata;
53 
55  public $handler;
56 
62  public static function newFromPath( $path, $mime ) {
63  return new static( false, false, $path, $mime );
64  }
65 
71  public static function newFromTitle( $title, $repo ) {
72  return new static( $title, $repo, false, false );
73  }
74 
85  public function __construct( $title = false, $repo = false, $path = false, $mime = false ) {
86  if ( !( $title && $repo ) && !$path ) {
87  throw new MWException( __METHOD__ .
88  ': not enough parameters, must specify title and repo, or a full path' );
89  }
90  if ( $title instanceof Title ) {
91  $this->title = File::normalizeTitle( $title, 'exception' );
92  $this->name = $repo->getNameFromTitle( $title );
93  } else {
94  $this->name = basename( $path );
95  $this->title = File::normalizeTitle( $this->name, 'exception' );
96  }
97  $this->repo = $repo;
98  if ( $path ) {
99  $this->path = $path;
100  } else {
101  $this->assertRepoDefined();
102  $this->path = $repo->getRootDirectory() . '/' .
103  $repo->getHashPath( $this->name ) . $this->name;
104  }
105  if ( $mime ) {
106  $this->mime = $mime;
107  }
108  $this->pageDims = [];
109  }
110 
115  private function cachePageDimensions( $page = 1 ) {
116  $page = (int)$page;
117  if ( $page < 1 ) {
118  $page = 1;
119  }
120 
121  if ( !isset( $this->pageDims[$page] ) ) {
122  if ( !$this->getHandler() ) {
123  return false;
124  }
125  if ( $this->getHandler()->isMultiPage( $this ) ) {
126  $this->pageDims[$page] = $this->handler->getPageDimensions( $this, $page );
127  } else {
128  $info = $this->getSizeAndMetadata();
129  return [
130  'width' => $info['width'],
131  'height' => $info['height']
132  ];
133  }
134  }
135 
136  return $this->pageDims[$page];
137  }
138 
143  public function getWidth( $page = 1 ) {
144  $dim = $this->cachePageDimensions( $page );
145 
146  return $dim['width'] ?? 0;
147  }
148 
153  public function getHeight( $page = 1 ) {
154  $dim = $this->cachePageDimensions( $page );
155 
156  return $dim['height'] ?? 0;
157  }
158 
162  public function getMimeType() {
163  if ( !isset( $this->mime ) ) {
164  $refPath = $this->getLocalRefPath();
165  if ( $refPath !== false ) {
166  $magic = MediaWikiServices::getInstance()->getMimeAnalyzer();
167  $this->mime = $magic->guessMimeType( $refPath );
168  } else {
169  $this->mime = false;
170  }
171  }
172 
173  return $this->mime;
174  }
175 
179  public function getBitDepth() {
180  $info = $this->getSizeAndMetadata();
181  return $info['bits'] ?? 0;
182  }
183 
187  public function getMetadata() {
188  $info = $this->getSizeAndMetadata();
189  return $info['metadata'] ? serialize( $info['metadata'] ) : false;
190  }
191 
192  public function getMetadataArray(): array {
193  $info = $this->getSizeAndMetadata();
194  return $info['metadata'];
195  }
196 
197  private function getSizeAndMetadata() {
198  if ( $this->sizeAndMetadata === null ) {
199  if ( !$this->getHandler() ) {
200  $this->sizeAndMetadata = [ 'width' => 0, 'height' => 0, 'metadata' => [] ];
201  } else {
202  $this->sizeAndMetadata = $this->getHandler()->getSizeAndMetadataWithFallback(
203  $this, $this->getLocalRefPath() );
204  }
205  }
206 
207  return $this->sizeAndMetadata;
208  }
209 
213  public function getURL() {
214  if ( $this->repo ) {
215  return $this->repo->getZoneUrl( 'public' ) . '/' .
216  $this->repo->getHashPath( $this->name ) . rawurlencode( $this->name );
217  } else {
218  return false;
219  }
220  }
221 
225  public function getSize() {
226  $this->assertRepoDefined();
227 
228  return $this->repo->getFileSize( $this->path );
229  }
230 
239  public function setLocalReference( FSFile $fsFile ) {
240  $this->fsFile = $fsFile;
241  }
242 }
Class representing a non-directory file on the file system.
Definition: FSFile.php:32
getRootDirectory()
Get the public zone root storage directory of the repository.
Definition: FileRepo.php:737
getNameFromTitle( $title)
Get the name of a file from its title.
Definition: FileRepo.php:716
getHashPath( $name)
Get a relative path including trailing slash, e.g.
Definition: FileRepo.php:748
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:70
static normalizeTitle( $title, $exception=false)
Given a string or Title object return either a valid Title object with namespace NS_FILE or null.
Definition: File.php:209
assertRepoDefined()
Assert that $this->repo is set to a valid FileRepo instance.
Definition: File.php:2460
getLocalRefPath()
Get an FS copy or original of this file and return the path.
Definition: File.php:480
FileRepo LocalRepo ForeignAPIRepo false $repo
Some member variables can be lazy-initialised using __get().
Definition: File.php:117
getHandler()
Get a MediaHandler instance for this file.
Definition: File.php:1541
string null $name
The name of a file from its title object.
Definition: File.php:144
MediaWiki exception.
Definition: MWException.php:33
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition: Title.php:76
File without associated database record.
setLocalReference(FSFile $fsFile)
Optimize getLocalRefPath() by using an existing local reference.
array[] bool[] $pageDims
Dimension data.
__construct( $title=false, $repo=false, $path=false, $mime=false)
Create an UnregisteredLocalFile based on a path or a (title,repo) pair.
getMetadataArray()
Get the unserialized handler-specific metadata STUB.
static newFromPath( $path, $mime)
static newFromTitle( $title, $repo)