lessphp: Update to upstream 6e8e724fc7
[mediawiki.git] / includes / filerepo / file / UnregisteredLocalFile.php
blob47ba6d6b293b085eec8f2858b61cb072ef41b1ca
1 <?php
2 /**
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
20 * @file
21 * @ingroup FileAbstraction
24 /**
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.
28 * Read-only.
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
32 * standalone role.
34 * @ingroup FileAbstraction
36 class UnregisteredLocalFile extends File {
37 var $title, $path, $mime, $dims, $metadata;
39 /**
40 * @var MediaHandler
42 var $handler;
44 /**
45 * @param string $path Storage path
46 * @param $mime string
47 * @return UnregisteredLocalFile
49 static function newFromPath( $path, $mime ) {
50 return new self( false, false, $path, $mime );
53 /**
54 * @param $title
55 * @param $repo
56 * @return UnregisteredLocalFile
58 static function newFromTitle( $title, $repo ) {
59 return new self( $title, $repo, false, false );
62 /**
63 * Create an UnregisteredLocalFile based on a path or a (title,repo) pair.
64 * A FileRepo object is not required here, unlike most other File classes.
66 * @throws MWException
67 * @param $title Title|bool
68 * @param $repo FileRepo|bool
69 * @param $path string|bool
70 * @param $mime string|bool
72 function __construct( $title = false, $repo = false, $path = false, $mime = false ) {
73 if ( !( $title && $repo ) && !$path ) {
74 throw new MWException( __METHOD__ . ': not enough parameters, must specify title and repo, or a full path' );
76 if ( $title instanceof Title ) {
77 $this->title = File::normalizeTitle( $title, 'exception' );
78 $this->name = $repo->getNameFromTitle( $title );
79 } else {
80 $this->name = basename( $path );
81 $this->title = File::normalizeTitle( $this->name, 'exception' );
83 $this->repo = $repo;
84 if ( $path ) {
85 $this->path = $path;
86 } else {
87 $this->assertRepoDefined();
88 $this->path = $repo->getRootDirectory() . '/' .
89 $repo->getHashPath( $this->name ) . $this->name;
91 if ( $mime ) {
92 $this->mime = $mime;
94 $this->dims = array();
97 /**
98 * @param $page int
99 * @return bool
101 private function cachePageDimensions( $page = 1 ) {
102 if ( !isset( $this->dims[$page] ) ) {
103 if ( !$this->getHandler() ) {
104 return false;
106 $this->dims[$page] = $this->handler->getPageDimensions( $this, $page );
108 return $this->dims[$page];
112 * @param $page int
113 * @return number
115 function getWidth( $page = 1 ) {
116 $dim = $this->cachePageDimensions( $page );
117 return $dim['width'];
121 * @param $page int
122 * @return number
124 function getHeight( $page = 1 ) {
125 $dim = $this->cachePageDimensions( $page );
126 return $dim['height'];
130 * @return bool|string
132 function getMimeType() {
133 if ( !isset( $this->mime ) ) {
134 $magic = MimeMagic::singleton();
135 $this->mime = $magic->guessMimeType( $this->getLocalRefPath() );
137 return $this->mime;
141 * @param $filename String
142 * @return Array|bool
144 function getImageSize( $filename ) {
145 if ( !$this->getHandler() ) {
146 return false;
148 return $this->handler->getImageSize( $this, $this->getLocalRefPath() );
152 * @return bool
154 function getMetadata() {
155 if ( !isset( $this->metadata ) ) {
156 if ( !$this->getHandler() ) {
157 $this->metadata = false;
158 } else {
159 $this->metadata = $this->handler->getMetadata( $this, $this->getLocalRefPath() );
162 return $this->metadata;
166 * @return bool|string
168 function getURL() {
169 if ( $this->repo ) {
170 return $this->repo->getZoneUrl( 'public' ) . '/' .
171 $this->repo->getHashPath( $this->name ) . rawurlencode( $this->name );
172 } else {
173 return false;
178 * @return bool|int
180 function getSize() {
181 $this->assertRepoDefined();
182 return $this->repo->getFileSize( $this->path );
186 * Optimize getLocalRefPath() by using an existing local reference.
187 * The file at the path of $fsFile should not be deleted (or at least
188 * not until the end of the request). This is mostly a performance hack.
190 * @param $fsFile FSFile
191 * @return void
193 public function setLocalReference( FSFile $fsFile ) {
194 $this->fsFile = $fsFile;