MediaWiki  master
UnregisteredLocalFile.php
Go to the documentation of this file.
1 <?php
22 
37 class UnregisteredLocalFile extends File {
39  protected $title;
40 
42  protected $path;
43 
45  protected $mime;
46 
48  protected $pageDims;
49 
51  protected $sizeAndMetadata;
52 
54  public $handler;
55 
61  public static function newFromPath( $path, $mime ) {
62  return new static( false, false, $path, $mime );
63  }
64 
70  public static function newFromTitle( $title, $repo ) {
71  return new static( $title, $repo, false, false );
72  }
73 
84  public function __construct( $title = false, $repo = false, $path = false, $mime = false ) {
85  if ( !( $title && $repo ) && !$path ) {
86  throw new MWException( __METHOD__ .
87  ': not enough parameters, must specify title and repo, or a full path' );
88  }
89  if ( $title instanceof Title ) {
90  $this->title = File::normalizeTitle( $title, 'exception' );
91  $this->name = $repo->getNameFromTitle( $title );
92  } else {
93  $this->name = basename( $path );
94  $this->title = File::normalizeTitle( $this->name, 'exception' );
95  }
96  $this->repo = $repo;
97  if ( $path ) {
98  $this->path = $path;
99  } else {
100  $this->assertRepoDefined();
101  $this->path = $repo->getRootDirectory() . '/' .
102  $repo->getHashPath( $this->name ) . $this->name;
103  }
104  if ( $mime ) {
105  $this->mime = $mime;
106  }
107  $this->pageDims = [];
108  }
109 
114  private function cachePageDimensions( $page = 1 ) {
115  $page = (int)$page;
116  if ( $page < 1 ) {
117  $page = 1;
118  }
119 
120  if ( !isset( $this->pageDims[$page] ) ) {
121  if ( !$this->getHandler() ) {
122  return false;
123  }
124  if ( $this->getHandler()->isMultiPage( $this ) ) {
125  $this->pageDims[$page] = $this->handler->getPageDimensions( $this, $page );
126  } else {
127  $info = $this->getSizeAndMetadata();
128  return [
129  'width' => $info['width'],
130  'height' => $info['height']
131  ];
132  }
133  }
134 
135  return $this->pageDims[$page];
136  }
137 
142  public function getWidth( $page = 1 ) {
143  $dim = $this->cachePageDimensions( $page );
144 
145  return $dim['width'] ?? 0;
146  }
147 
152  public function getHeight( $page = 1 ) {
153  $dim = $this->cachePageDimensions( $page );
154 
155  return $dim['height'] ?? 0;
156  }
157 
161  public function getMimeType() {
162  if ( !isset( $this->mime ) ) {
163  $magic = MediaWikiServices::getInstance()->getMimeAnalyzer();
164  $this->mime = $magic->guessMimeType( $this->getLocalRefPath() );
165  }
166 
167  return $this->mime;
168  }
169 
173  public function getBitDepth() {
174  $info = $this->getSizeAndMetadata();
175  return $info['bits'] ?? 0;
176  }
177 
181  public function getMetadata() {
182  $info = $this->getSizeAndMetadata();
183  return $info['metadata'] ? serialize( $info['metadata'] ) : false;
184  }
185 
186  public function getMetadataArray(): array {
187  $info = $this->getSizeAndMetadata();
188  return $info['metadata'];
189  }
190 
191  private function getSizeAndMetadata() {
192  if ( $this->sizeAndMetadata === null ) {
193  if ( !$this->getHandler() ) {
194  $this->sizeAndMetadata = [ 'width' => 0, 'height' => 0, 'metadata' => [] ];
195  } else {
196  $this->sizeAndMetadata = $this->getHandler()->getSizeAndMetadataWithFallback(
197  $this, $this->getLocalRefPath() );
198  }
199  }
200 
201  return $this->sizeAndMetadata;
202  }
203 
207  public function getURL() {
208  if ( $this->repo ) {
209  return $this->repo->getZoneUrl( 'public' ) . '/' .
210  $this->repo->getHashPath( $this->name ) . rawurlencode( $this->name );
211  } else {
212  return false;
213  }
214  }
215 
219  public function getSize() {
220  $this->assertRepoDefined();
221 
222  return $this->repo->getFileSize( $this->path );
223  }
224 
233  public function setLocalReference( FSFile $fsFile ) {
234  $this->fsFile = $fsFile;
235  }
236 }
serialize()
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:68
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:207
assertRepoDefined()
Assert that $this->repo is set to a valid FileRepo instance.
Definition: File.php:2462
getLocalRefPath()
Get an FS copy or original of this file and return the path.
Definition: File.php:474
FileRepo LocalRepo ForeignAPIRepo bool $repo
Some member variables can be lazy-initialised using __get().
Definition: File.php:115
getHandler()
Get a MediaHandler instance for this file.
Definition: File.php:1540
string null $name
The name of a file from its title object.
Definition: File.php:142
MediaWiki exception.
Definition: MWException.php:29
MediaWikiServices is the service locator for the application scope of MediaWiki.
Represents a title within MediaWiki.
Definition: Title.php:49
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)