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
21 use MediaWiki\MediaWikiServices
;
22 use MediaWiki\Title\Title
;
23 use Wikimedia\FileBackend\FSFile\FSFile
;
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.
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
37 * @ingroup FileAbstraction
39 class UnregisteredLocalFile
extends File
{
46 /** @var string|false|null */
49 /** @var array[]|bool[] Dimension data */
52 /** @var array|null */
53 protected $sizeAndMetadata;
55 /** @var MediaHandler */
59 * @param string $path Storage path
63 public static function newFromPath( $path, $mime ) {
64 return new static( false, false, $path, $mime );
69 * @param FileRepo $repo
72 public static function newFromTitle( $title, $repo ) {
73 return new static( $title, $repo, false, false );
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 );
94 $this->name
= basename( $path );
95 $this->title
= File
::normalizeTitle( $this->name
, 'exception' );
101 $this->assertRepoDefined();
102 $this->path
= $repo->getRootDirectory() . '/' .
103 $repo->getHashPath( $this->name
) . $this->name
;
108 $this->pageDims
= [];
113 * @return array|false
115 private function cachePageDimensions( $page = 1 ) {
121 if ( !isset( $this->pageDims
[$page] ) ) {
122 if ( !$this->getHandler() ) {
125 if ( $this->getHandler()->isMultiPage( $this ) ) {
126 $this->pageDims
[$page] = $this->handler
->getPageDimensions( $this, $page );
128 $info = $this->getSizeAndMetadata();
130 'width' => $info['width'],
131 'height' => $info['height']
136 return $this->pageDims
[$page];
143 public function getWidth( $page = 1 ) {
144 $dim = $this->cachePageDimensions( $page );
146 return $dim['width'] ??
0;
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 );
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' => [] ];
202 $this->sizeAndMetadata
= $this->getHandler()->getSizeAndMetadataWithFallback(
203 $this, $this->getLocalRefPath() );
207 return $this->sizeAndMetadata
;
211 * @return string|false
213 public function getURL() {
215 return $this->repo
->getZoneUrl( 'public' ) . '/' .
216 $this->repo
->getHashPath( $this->name
) . rawurlencode( $this->name
);
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
239 public function setLocalReference( FSFile
$fsFile ) {
240 $this->fsFile
= $fsFile;