MediaWiki master
UnregisteredLocalFile.php
Go to the documentation of this file.
1<?php
22
23use BadMethodCallException;
24use MediaHandler;
29
46 protected $title;
47
49 protected $path;
50
52 protected $mime;
53
55 protected $pageDims;
56
59
61 public $handler;
62
68 public static function newFromPath( $path, $mime ) {
69 return new static( false, false, $path, $mime );
70 }
71
77 public static function newFromTitle( $title, $repo ) {
78 return new static( $title, $repo, false, false );
79 }
80
90 public function __construct( $title = false, $repo = false, $path = false, $mime = false ) {
91 if ( !( $title && $repo ) && !$path ) {
92 throw new BadMethodCallException( __METHOD__ .
93 ': not enough parameters, must specify title and repo, or a full path' );
94 }
95 if ( $title instanceof Title ) {
96 $this->title = File::normalizeTitle( $title, 'exception' );
97 $this->name = $repo->getNameFromTitle( $title );
98 } else {
99 $this->name = basename( $path );
100 $this->title = File::normalizeTitle( $this->name, 'exception' );
101 }
102 $this->repo = $repo;
103 if ( $path ) {
104 $this->path = $path;
105 } else {
106 $this->assertRepoDefined();
107 $this->path = $repo->getRootDirectory() . '/' .
108 $repo->getHashPath( $this->name ) . $this->name;
109 }
110 if ( $mime ) {
111 $this->mime = $mime;
112 }
113 $this->pageDims = [];
114 }
115
120 private function cachePageDimensions( $page = 1 ) {
121 $page = (int)$page;
122 if ( $page < 1 ) {
123 $page = 1;
124 }
125
126 if ( !isset( $this->pageDims[$page] ) ) {
127 if ( !$this->getHandler() ) {
128 return false;
129 }
130 if ( $this->getHandler()->isMultiPage( $this ) ) {
131 $this->pageDims[$page] = $this->handler->getPageDimensions( $this, $page );
132 } else {
133 $info = $this->getSizeAndMetadata();
134 return [
135 'width' => $info['width'],
136 'height' => $info['height']
137 ];
138 }
139 }
140
141 return $this->pageDims[$page];
142 }
143
148 public function getWidth( $page = 1 ) {
149 $dim = $this->cachePageDimensions( $page );
150
151 return $dim['width'] ?? 0;
152 }
153
158 public function getHeight( $page = 1 ) {
159 $dim = $this->cachePageDimensions( $page );
160
161 return $dim['height'] ?? 0;
162 }
163
167 public function getMimeType() {
168 if ( $this->mime === null ) {
169 $refPath = $this->getLocalRefPath();
170 if ( $refPath !== false ) {
171 $magic = MediaWikiServices::getInstance()->getMimeAnalyzer();
172 $this->mime = $magic->guessMimeType( $refPath );
173 } else {
174 $this->mime = false;
175 }
176 }
177
178 return $this->mime;
179 }
180
184 public function getBitDepth() {
185 $info = $this->getSizeAndMetadata();
186 return $info['bits'] ?? 0;
187 }
188
192 public function getMetadata() {
193 $info = $this->getSizeAndMetadata();
194 return $info['metadata'] ? serialize( $info['metadata'] ) : false;
195 }
196
197 public function getMetadataArray(): array {
198 $info = $this->getSizeAndMetadata();
199 return $info['metadata'];
200 }
201
202 private function getSizeAndMetadata(): array {
203 if ( $this->sizeAndMetadata === null ) {
204 if ( !$this->getHandler() ) {
205 $this->sizeAndMetadata = [ 'width' => 0, 'height' => 0, 'metadata' => [] ];
206 } else {
207 $this->sizeAndMetadata = $this->getHandler()->getSizeAndMetadataWithFallback(
208 $this, $this->getLocalRefPath() );
209 }
210 }
211
212 return $this->sizeAndMetadata;
213 }
214
218 public function getURL() {
219 if ( $this->repo ) {
220 return $this->repo->getZoneUrl( 'public' ) . '/' .
221 $this->repo->getHashPath( $this->name ) . rawurlencode( $this->name );
222 } else {
223 return false;
224 }
225 }
226
230 public function getSize() {
231 $this->assertRepoDefined();
232
233 return $this->repo->getFileSize( $this->path );
234 }
235
244 public function setLocalReference( FSFile $fsFile ) {
245 $this->fsFile = $fsFile;
246 }
247}
248
250class_alias( UnregisteredLocalFile::class, 'UnregisteredLocalFile' );
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:82
Base media handler class.
Base class for file repositories.
Definition FileRepo.php:68
getNameFromTitle( $title)
Get the name of a file from its title.
Definition FileRepo.php:732
getRootDirectory()
Get the public zone root storage directory of the repository.
Definition FileRepo.php:753
getHashPath( $name)
Get a relative path including trailing slash, e.g.
Definition FileRepo.php:764
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:93
FileRepo LocalRepo ForeignAPIRepo false $repo
Some member variables can be lazy-initialised using __get().
Definition File.php:140
getLocalRefPath()
Get an FS copy or original of this file and return the path.
Definition File.php:505
string null $name
The name of a file from its title object.
Definition File.php:170
assertRepoDefined()
Assert that $this->repo is set to a valid FileRepo instance.
Definition File.php:2554
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:234
getHandler()
Get a MediaHandler instance for this file.
Definition File.php:1636
File without associated database record.
setLocalReference(FSFile $fsFile)
Optimize getLocalRefPath() by using an existing local reference.
getMetadataArray()
Get the unserialized handler-specific metadata STUB.
__construct( $title=false, $repo=false, $path=false, $mime=false)
Create an UnregisteredLocalFile based on a path or a (title,repo) pair.
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Represents a title within MediaWiki.
Definition Title.php:78
Class representing a non-directory file on the file system.
Definition FSFile.php:34