Merge "Move up devunt's name to Developers"
[lhc/web/wiklou.git] / includes / filerepo / file / UnregisteredLocalFile.php
1 <?php
2 /**
3 * File without associated database record.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup FileAbstraction
22 */
23
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.
27 *
28 * Read-only.
29 *
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.
33 *
34 * @ingroup FileAbstraction
35 */
36 class UnregisteredLocalFile extends File {
37 /** @var Title */
38 protected $title;
39
40 /** @var string */
41 protected $path;
42
43 /** @var bool|string */
44 protected $mime;
45
46 /** @var array Dimension data */
47 protected $dims;
48
49 /** @var bool|string Handler-specific metadata which will be saved in the img_metadata field */
50 protected $metadata;
51
52 /** @var MediaHandler */
53 public $handler;
54
55 /**
56 * @param string $path Storage path
57 * @param string $mime
58 * @return UnregisteredLocalFile
59 */
60 static function newFromPath( $path, $mime ) {
61 return new self( false, false, $path, $mime );
62 }
63
64 /**
65 * @param Title $title
66 * @param FileRepo $repo
67 * @return UnregisteredLocalFile
68 */
69 static function newFromTitle( $title, $repo ) {
70 return new self( $title, $repo, false, false );
71 }
72
73 /**
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.
76 *
77 * @throws MWException
78 * @param Title|bool $title
79 * @param FileRepo|bool $repo
80 * @param string|bool $path
81 * @param string|bool $mime
82 */
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' );
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->dims = [];
107 }
108
109 /**
110 * @param int $page
111 * @return bool
112 */
113 private function cachePageDimensions( $page = 1 ) {
114 if ( !isset( $this->dims[$page] ) ) {
115 if ( !$this->getHandler() ) {
116 return false;
117 }
118 $this->dims[$page] = $this->handler->getPageDimensions( $this, $page );
119 }
120
121 return $this->dims[$page];
122 }
123
124 /**
125 * @param int $page
126 * @return int
127 */
128 function getWidth( $page = 1 ) {
129 $dim = $this->cachePageDimensions( $page );
130
131 return $dim['width'];
132 }
133
134 /**
135 * @param int $page
136 * @return int
137 */
138 function getHeight( $page = 1 ) {
139 $dim = $this->cachePageDimensions( $page );
140
141 return $dim['height'];
142 }
143
144 /**
145 * @return bool|string
146 */
147 function getMimeType() {
148 if ( !isset( $this->mime ) ) {
149 $magic = MimeMagic::singleton();
150 $this->mime = $magic->guessMimeType( $this->getLocalRefPath() );
151 }
152
153 return $this->mime;
154 }
155
156 /**
157 * @param string $filename
158 * @return array|bool
159 */
160 function getImageSize( $filename ) {
161 if ( !$this->getHandler() ) {
162 return false;
163 }
164
165 return $this->handler->getImageSize( $this, $this->getLocalRefPath() );
166 }
167
168 /**
169 * @return int
170 */
171 function getBitDepth() {
172 $gis = $this->getImageSize( $this->getLocalRefPath() );
173
174 if ( !$gis || !isset( $gis['bits'] ) ) {
175 return 0;
176 }
177 return $gis['bits'];
178 }
179
180 /**
181 * @return bool
182 */
183 function getMetadata() {
184 if ( !isset( $this->metadata ) ) {
185 if ( !$this->getHandler() ) {
186 $this->metadata = false;
187 } else {
188 $this->metadata = $this->handler->getMetadata( $this, $this->getLocalRefPath() );
189 }
190 }
191
192 return $this->metadata;
193 }
194
195 /**
196 * @return bool|string
197 */
198 function getURL() {
199 if ( $this->repo ) {
200 return $this->repo->getZoneUrl( 'public' ) . '/' .
201 $this->repo->getHashPath( $this->name ) . rawurlencode( $this->name );
202 } else {
203 return false;
204 }
205 }
206
207 /**
208 * @return bool|int
209 */
210 function getSize() {
211 $this->assertRepoDefined();
212
213 return $this->repo->getFileSize( $this->path );
214 }
215
216 /**
217 * Optimize getLocalRefPath() by using an existing local reference.
218 * The file at the path of $fsFile should not be deleted (or at least
219 * not until the end of the request). This is mostly a performance hack.
220 *
221 * @param FSFile $fsFile
222 * @return void
223 */
224 public function setLocalReference( FSFile $fsFile ) {
225 $this->fsFile = $fsFile;
226 }
227 }