Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / filerepo / file / UnregisteredLocalFile.php
blob34fbe6e162e57e59d95321718f429e8c1150c7a9
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 use MediaWiki\MediaWikiServices;
22 use MediaWiki\Title\Title;
23 use Wikimedia\FileBackend\FSFile\FSFile;
25 /**
26 * File without associated database record.
28 * Represents a standalone local file, or a file in a local repository
29 * with no database, for example a FileRepo repository.
31 * Read-only.
33 * @todo Currently it doesn't really work in the repository role, there are
34 * lots of functions missing. It is used by the WebStore extension in the
35 * standalone role.
37 * @ingroup FileAbstraction
39 class UnregisteredLocalFile extends File {
40 /** @var Title */
41 protected $title;
43 /** @var string */
44 protected $path;
46 /** @var string|false|null */
47 protected $mime;
49 /** @var array[]|bool[] Dimension data */
50 protected $pageDims;
52 /** @var array|null */
53 protected $sizeAndMetadata;
55 /** @var MediaHandler */
56 public $handler;
58 /**
59 * @param string $path Storage path
60 * @param string $mime
61 * @return static
63 public static function newFromPath( $path, $mime ) {
64 return new static( false, false, $path, $mime );
67 /**
68 * @param Title $title
69 * @param FileRepo $repo
70 * @return static
72 public static function newFromTitle( $title, $repo ) {
73 return new static( $title, $repo, false, false );
76 /**
77 * Create an UnregisteredLocalFile based on a path or a (title,repo) pair.
78 * A FileRepo object is not required here, unlike most other File classes.
80 * @param Title|false $title
81 * @param FileRepo|false $repo
82 * @param string|false $path
83 * @param string|false $mime
85 public function __construct( $title = false, $repo = false, $path = false, $mime = false ) {
86 if ( !( $title && $repo ) && !$path ) {
87 throw new BadMethodCallException( __METHOD__ .
88 ': not enough parameters, must specify title and repo, or a full path' );
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' );
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;
105 if ( $mime ) {
106 $this->mime = $mime;
108 $this->pageDims = [];
112 * @param int $page
113 * @return array|false
115 private function cachePageDimensions( $page = 1 ) {
116 $page = (int)$page;
117 if ( $page < 1 ) {
118 $page = 1;
121 if ( !isset( $this->pageDims[$page] ) ) {
122 if ( !$this->getHandler() ) {
123 return false;
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']
136 return $this->pageDims[$page];
140 * @param int $page
141 * @return int
143 public function getWidth( $page = 1 ) {
144 $dim = $this->cachePageDimensions( $page );
146 return $dim['width'] ?? 0;
150 * @param int $page
151 * @return int
153 public function getHeight( $page = 1 ) {
154 $dim = $this->cachePageDimensions( $page );
156 return $dim['height'] ?? 0;
160 * @return string|false
162 public function getMimeType() {
163 if ( $this->mime === null ) {
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;
173 return $this->mime;
177 * @return int
179 public function getBitDepth() {
180 $info = $this->getSizeAndMetadata();
181 return $info['bits'] ?? 0;
185 * @return string|false
187 public function getMetadata() {
188 $info = $this->getSizeAndMetadata();
189 return $info['metadata'] ? serialize( $info['metadata'] ) : false;
192 public function getMetadataArray(): array {
193 $info = $this->getSizeAndMetadata();
194 return $info['metadata'];
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() );
207 return $this->sizeAndMetadata;
211 * @return string|false
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;
223 * @return false|int
225 public function getSize() {
226 $this->assertRepoDefined();
228 return $this->repo->getFileSize( $this->path );
232 * Optimize getLocalRefPath() by using an existing local reference.
233 * The file at the path of $fsFile should not be deleted (or at least
234 * not until the end of the request). This is mostly a performance hack.
236 * @param FSFile $fsFile
237 * @return void
239 public function setLocalReference( FSFile $fsFile ) {
240 $this->fsFile = $fsFile;