MediaWiki master
UnregisteredLocalFile.php
Go to the documentation of this file.
1<?php
23
40 protected $title;
41
43 protected $path;
44
46 protected $mime;
47
49 protected $pageDims;
50
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
84 public function __construct( $title = false, $repo = false, $path = false, $mime = false ) {
85 if ( !( $title && $repo ) && !$path ) {
86 throw new BadMethodCallException( __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 $refPath = $this->getLocalRefPath();
164 if ( $refPath !== false ) {
165 $magic = MediaWikiServices::getInstance()->getMimeAnalyzer();
166 $this->mime = $magic->guessMimeType( $refPath );
167 } else {
168 $this->mime = false;
169 }
170 }
171
172 return $this->mime;
173 }
174
178 public function getBitDepth() {
179 $info = $this->getSizeAndMetadata();
180 return $info['bits'] ?? 0;
181 }
182
186 public function getMetadata() {
187 $info = $this->getSizeAndMetadata();
188 return $info['metadata'] ? serialize( $info['metadata'] ) : false;
189 }
190
191 public function getMetadataArray(): array {
192 $info = $this->getSizeAndMetadata();
193 return $info['metadata'];
194 }
195
196 private function getSizeAndMetadata() {
197 if ( $this->sizeAndMetadata === null ) {
198 if ( !$this->getHandler() ) {
199 $this->sizeAndMetadata = [ 'width' => 0, 'height' => 0, 'metadata' => [] ];
200 } else {
201 $this->sizeAndMetadata = $this->getHandler()->getSizeAndMetadataWithFallback(
202 $this, $this->getLocalRefPath() );
203 }
204 }
205
206 return $this->sizeAndMetadata;
207 }
208
212 public function getURL() {
213 if ( $this->repo ) {
214 return $this->repo->getZoneUrl( 'public' ) . '/' .
215 $this->repo->getHashPath( $this->name ) . rawurlencode( $this->name );
216 } else {
217 return false;
218 }
219 }
220
224 public function getSize() {
225 $this->assertRepoDefined();
226
227 return $this->repo->getFileSize( $this->path );
228 }
229
238 public function setLocalReference( FSFile $fsFile ) {
239 $this->fsFile = $fsFile;
240 }
241}
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:736
getNameFromTitle( $title)
Get the name of a file from its title.
Definition FileRepo.php:715
getHashPath( $name)
Get a relative path including trailing slash, e.g.
Definition FileRepo.php:747
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:73
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:214
assertRepoDefined()
Assert that $this->repo is set to a valid FileRepo instance.
Definition File.php:2466
getLocalRefPath()
Get an FS copy or original of this file and return the path.
Definition File.php:485
FileRepo LocalRepo ForeignAPIRepo false $repo
Some member variables can be lazy-initialised using __get().
Definition File.php:120
getHandler()
Get a MediaHandler instance for this file.
Definition File.php:1548
Base media handler class.
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition Title.php:78
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)