MediaWiki REL1_37
UnregisteredLocalFile.php
Go to the documentation of this file.
1<?php
38 protected $title;
39
41 protected $path;
42
44 protected $mime;
45
47 protected $pageDims;
48
51
53 public $handler;
54
60 public static function newFromPath( $path, $mime ) {
61 return new static( false, false, $path, $mime );
62 }
63
69 public static function newFromTitle( $title, $repo ) {
70 return new static( $title, $repo, false, false );
71 }
72
83 public function __construct( $title = false, $repo = false, $path = false, $mime = false ) {
84 if ( !( $title && $repo ) && !$path ) {
85 throw new MWException( __METHOD__ .
86 ': not enough parameters, must specify title and repo, or a full path' );
87 }
88 if ( $title instanceof Title ) {
89 $this->title = File::normalizeTitle( $title, 'exception' );
90 $this->name = $repo->getNameFromTitle( $title );
91 } else {
92 $this->name = basename( $path );
93 $this->title = File::normalizeTitle( $this->name, 'exception' );
94 }
95 $this->repo = $repo;
96 if ( $path ) {
97 $this->path = $path;
98 } else {
99 $this->assertRepoDefined();
100 $this->path = $repo->getRootDirectory() . '/' .
101 $repo->getHashPath( $this->name ) . $this->name;
102 }
103 if ( $mime ) {
104 $this->mime = $mime;
105 }
106 $this->pageDims = [];
107 }
108
113 private function cachePageDimensions( $page = 1 ) {
114 $page = (int)$page;
115 if ( $page < 1 ) {
116 $page = 1;
117 }
118
119 if ( !isset( $this->pageDims[$page] ) ) {
120 if ( !$this->getHandler() ) {
121 return false;
122 }
123 if ( $this->getHandler()->isMultiPage( $this ) ) {
124 $this->pageDims[$page] = $this->handler->getPageDimensions( $this, $page );
125 } else {
126 $info = $this->getSizeAndMetadata();
127 return [
128 'width' => $info['width'],
129 'height' => $info['height']
130 ];
131 }
132 }
133
134 return $this->pageDims[$page];
135 }
136
141 public function getWidth( $page = 1 ) {
142 $dim = $this->cachePageDimensions( $page );
143
144 return $dim['width'] ?? 0;
145 }
146
151 public function getHeight( $page = 1 ) {
152 $dim = $this->cachePageDimensions( $page );
153
154 return $dim['height'] ?? 0;
155 }
156
160 public function getMimeType() {
161 if ( !isset( $this->mime ) ) {
162 $magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
163 $this->mime = $magic->guessMimeType( $this->getLocalRefPath() );
164 }
165
166 return $this->mime;
167 }
168
172 public function getBitDepth() {
173 $info = $this->getSizeAndMetadata();
174 return $info['bits'] ?? 0;
175 }
176
180 public function getMetadata() {
181 $info = $this->getSizeAndMetadata();
182 return $info['metadata'] ? serialize( $info['metadata'] ) : false;
183 }
184
185 public function getMetadataArray(): array {
186 $info = $this->getSizeAndMetadata();
187 return $info['metadata'];
188 }
189
190 private function getSizeAndMetadata() {
191 if ( $this->sizeAndMetadata === null ) {
192 if ( !$this->getHandler() ) {
193 $this->sizeAndMetadata = [ 'width' => 0, 'height' => 0, 'metadata' => [] ];
194 } else {
195 $this->sizeAndMetadata = $this->getHandler()->getSizeAndMetadataWithFallback(
196 $this, $this->getLocalRefPath() );
197 }
198 }
199
200 return $this->sizeAndMetadata;
201 }
202
206 public function getURL() {
207 if ( $this->repo ) {
208 return $this->repo->getZoneUrl( 'public' ) . '/' .
209 $this->repo->getHashPath( $this->name ) . rawurlencode( $this->name );
210 } else {
211 return false;
212 }
213 }
214
218 public function getSize() {
219 $this->assertRepoDefined();
220
221 return $this->repo->getFileSize( $this->path );
222 }
223
232 public function setLocalReference( FSFile $fsFile ) {
233 $this->fsFile = $fsFile;
234 }
235}
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:739
getNameFromTitle( $title)
Get the name of a file from its title.
Definition FileRepo.php:718
getHashPath( $name)
Get a relative path including trailing slash, e.g.
Definition FileRepo.php:750
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:66
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:205
assertRepoDefined()
Assert that $this->repo is set to a valid FileRepo instance.
Definition File.php:2470
getLocalRefPath()
Get an FS copy or original of this file and return the path.
Definition File.php:472
string $name
The name of a file from its title object.
Definition File.php:140
FileRepo LocalRepo ForeignAPIRepo bool $repo
Some member variables can be lazy-initialised using __get().
Definition File.php:113
getHandler()
Get a MediaHandler instance for this file.
Definition File.php:1545
MediaWiki exception.
Base media handler class.
Represents a title within MediaWiki.
Definition Title.php:48
A file object referring to either a standalone local file, or a file in a local repository with no da...
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)