Merge "Document the block duration tooltip"
[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, $path, $mime, $dims, $metadata;
38
39 /**
40 * @var MediaHandler
41 */
42 var $handler;
43
44 /**
45 * @param string $path Storage path
46 * @param $mime string
47 * @return UnregisteredLocalFile
48 */
49 static function newFromPath( $path, $mime ) {
50 return new self( false, false, $path, $mime );
51 }
52
53 /**
54 * @param $title
55 * @param $repo
56 * @return UnregisteredLocalFile
57 */
58 static function newFromTitle( $title, $repo ) {
59 return new self( $title, $repo, false, false );
60 }
61
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.
65 *
66 * @throws MWException
67 * @param $title Title|bool
68 * @param $repo FileRepo|bool
69 * @param $path string|bool
70 * @param $mime string|bool
71 */
72 function __construct( $title = false, $repo = false, $path = false, $mime = false ) {
73 if ( !( $title && $repo ) && !$path ) {
74 throw new MWException( __METHOD__ .
75 ': not enough parameters, must specify title and repo, or a full path' );
76 }
77 if ( $title instanceof Title ) {
78 $this->title = File::normalizeTitle( $title, 'exception' );
79 $this->name = $repo->getNameFromTitle( $title );
80 } else {
81 $this->name = basename( $path );
82 $this->title = File::normalizeTitle( $this->name, 'exception' );
83 }
84 $this->repo = $repo;
85 if ( $path ) {
86 $this->path = $path;
87 } else {
88 $this->assertRepoDefined();
89 $this->path = $repo->getRootDirectory() . '/' .
90 $repo->getHashPath( $this->name ) . $this->name;
91 }
92 if ( $mime ) {
93 $this->mime = $mime;
94 }
95 $this->dims = array();
96 }
97
98 /**
99 * @param $page int
100 * @return bool
101 */
102 private function cachePageDimensions( $page = 1 ) {
103 if ( !isset( $this->dims[$page] ) ) {
104 if ( !$this->getHandler() ) {
105 return false;
106 }
107 $this->dims[$page] = $this->handler->getPageDimensions( $this, $page );
108 }
109
110 return $this->dims[$page];
111 }
112
113 /**
114 * @param $page int
115 * @return number
116 */
117 function getWidth( $page = 1 ) {
118 $dim = $this->cachePageDimensions( $page );
119
120 return $dim['width'];
121 }
122
123 /**
124 * @param $page int
125 * @return number
126 */
127 function getHeight( $page = 1 ) {
128 $dim = $this->cachePageDimensions( $page );
129
130 return $dim['height'];
131 }
132
133 /**
134 * @return bool|string
135 */
136 function getMimeType() {
137 if ( !isset( $this->mime ) ) {
138 $magic = MimeMagic::singleton();
139 $this->mime = $magic->guessMimeType( $this->getLocalRefPath() );
140 }
141
142 return $this->mime;
143 }
144
145 /**
146 * @param $filename String
147 * @return Array|bool
148 */
149 function getImageSize( $filename ) {
150 if ( !$this->getHandler() ) {
151 return false;
152 }
153
154 return $this->handler->getImageSize( $this, $this->getLocalRefPath() );
155 }
156
157 /**
158 * @return bool
159 */
160 function getMetadata() {
161 if ( !isset( $this->metadata ) ) {
162 if ( !$this->getHandler() ) {
163 $this->metadata = false;
164 } else {
165 $this->metadata = $this->handler->getMetadata( $this, $this->getLocalRefPath() );
166 }
167 }
168
169 return $this->metadata;
170 }
171
172 /**
173 * @return bool|string
174 */
175 function getURL() {
176 if ( $this->repo ) {
177 return $this->repo->getZoneUrl( 'public' ) . '/' .
178 $this->repo->getHashPath( $this->name ) . rawurlencode( $this->name );
179 } else {
180 return false;
181 }
182 }
183
184 /**
185 * @return bool|int
186 */
187 function getSize() {
188 $this->assertRepoDefined();
189
190 return $this->repo->getFileSize( $this->path );
191 }
192
193 /**
194 * Optimize getLocalRefPath() by using an existing local reference.
195 * The file at the path of $fsFile should not be deleted (or at least
196 * not until the end of the request). This is mostly a performance hack.
197 *
198 * @param $fsFile FSFile
199 * @return void
200 */
201 public function setLocalReference( FSFile $fsFile ) {
202 $this->fsFile = $fsFile;
203 }
204 }