3 * File without associated database record.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup FileAbstraction
25 * A file object referring to either a standalone local file, or a file in a
26 * local repository with no database, for example an FileRepo repository.
30 * @todo Currently it doesn't really work in the repository role, there are
31 * lots of functions missing. It is used by the WebStore extension in the
34 * @ingroup FileAbstraction
36 class UnregisteredLocalFile
extends File
{
43 /** @var bool|string */
46 /** @var array Dimension data */
49 /** @var bool|string Handler-specific metadata which will be saved in the img_metadata field */
52 /** @var MediaHandler */
56 * @param string $path Storage path
58 * @return UnregisteredLocalFile
60 static function newFromPath( $path, $mime ) {
61 return new self( false, false, $path, $mime );
66 * @param FileRepo $repo
67 * @return UnregisteredLocalFile
69 static function newFromTitle( $title, $repo ) {
70 return new self( $title, $repo, false, false );
74 * Create an UnregisteredLocalFile based on a path or a (title,repo) pair.
75 * A FileRepo object is not required here, unlike most other File classes.
78 * @param Title|bool $title
79 * @param FileRepo|bool $repo
80 * @param string|bool $path
81 * @param string|bool $mime
83 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' );
88 if ( $title instanceof Title
) {
89 $this->title
= File
::normalizeTitle( $title, 'exception' );
90 $this->name
= $repo->getNameFromTitle( $title );
92 $this->name
= basename( $path );
93 $this->title
= File
::normalizeTitle( $this->name
, 'exception' );
99 $this->assertRepoDefined();
100 $this->path
= $repo->getRootDirectory() . '/' .
101 $repo->getHashPath( $this->name
) . $this->name
;
113 private function cachePageDimensions( $page = 1 ) {
119 if ( !isset( $this->dims
[$page] ) ) {
120 if ( !$this->getHandler() ) {
123 $this->dims
[$page] = $this->handler
->getPageDimensions( $this, $page );
126 return $this->dims
[$page];
133 function getWidth( $page = 1 ) {
134 $dim = $this->cachePageDimensions( $page );
136 return $dim['width'];
143 function getHeight( $page = 1 ) {
144 $dim = $this->cachePageDimensions( $page );
146 return $dim['height'];
150 * @return bool|string
152 function getMimeType() {
153 if ( !isset( $this->mime
) ) {
154 $magic = MediaWiki\MediaWikiServices
::getInstance()->getMimeAnalyzer();
155 $this->mime
= $magic->guessMimeType( $this->getLocalRefPath() );
162 * @param string $filename
165 function getImageSize( $filename ) {
166 if ( !$this->getHandler() ) {
170 return $this->handler
->getImageSize( $this, $this->getLocalRefPath() );
176 function getBitDepth() {
177 $gis = $this->getImageSize( $this->getLocalRefPath() );
179 if ( !$gis ||
!isset( $gis['bits'] ) ) {
188 function getMetadata() {
189 if ( !isset( $this->metadata
) ) {
190 if ( !$this->getHandler() ) {
191 $this->metadata
= false;
193 $this->metadata
= $this->handler
->getMetadata( $this, $this->getLocalRefPath() );
197 return $this->metadata
;
201 * @return bool|string
205 return $this->repo
->getZoneUrl( 'public' ) . '/' .
206 $this->repo
->getHashPath( $this->name
) . rawurlencode( $this->name
);
216 $this->assertRepoDefined();
218 return $this->repo
->getFileSize( $this->path
);
222 * Optimize getLocalRefPath() by using an existing local reference.
223 * The file at the path of $fsFile should not be deleted (or at least
224 * not until the end of the request). This is mostly a performance hack.
226 * @param FSFile $fsFile
229 public function setLocalReference( FSFile
$fsFile ) {
230 $this->fsFile
= $fsFile;