summaryrefslogtreecommitdiff
path: root/includes/filerepo/file/UnregisteredLocalFile.php
blob: 5a3e4e9c7788c4c9502044652ede554a4498bf6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
 * File without associated database record.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup FileAbstraction
 */

/**
 * A file object referring to either a standalone local file, or a file in a
 * local repository with no database, for example an FileRepo repository.
 *
 * Read-only.
 *
 * @todo Currently it doesn't really work in the repository role, there are
 * lots of functions missing. It is used by the WebStore extension in the
 * standalone role.
 *
 * @ingroup FileAbstraction
 */
class UnregisteredLocalFile extends File {
	/** @var Title */
	protected $title;

	/** @var string */
	protected $path;

	/** @var bool|string */
	protected $mime;

	/** @var array Dimension data */
	protected $dims;

	/** @var bool|string Handler-specific metadata which will be saved in the img_metadata field */
	protected $metadata;

	/** @var MediaHandler */
	public $handler;

	/**
	 * @param string $path Storage path
	 * @param string $mime
	 * @return UnregisteredLocalFile
	 */
	static function newFromPath( $path, $mime ) {
		return new self( false, false, $path, $mime );
	}

	/**
	 * @param Title $title
	 * @param FileRepo $repo
	 * @return UnregisteredLocalFile
	 */
	static function newFromTitle( $title, $repo ) {
		return new self( $title, $repo, false, false );
	}

	/**
	 * Create an UnregisteredLocalFile based on a path or a (title,repo) pair.
	 * A FileRepo object is not required here, unlike most other File classes.
	 *
	 * @throws MWException
	 * @param Title|bool $title
	 * @param FileRepo|bool $repo
	 * @param string|bool $path
	 * @param string|bool $mime
	 */
	function __construct( $title = false, $repo = false, $path = false, $mime = false ) {
		if ( !( $title && $repo ) && !$path ) {
			throw new MWException( __METHOD__ .
				': not enough parameters, must specify title and repo, or a full path' );
		}
		if ( $title instanceof Title ) {
			$this->title = File::normalizeTitle( $title, 'exception' );
			$this->name = $repo->getNameFromTitle( $title );
		} else {
			$this->name = basename( $path );
			$this->title = File::normalizeTitle( $this->name, 'exception' );
		}
		$this->repo = $repo;
		if ( $path ) {
			$this->path = $path;
		} else {
			$this->assertRepoDefined();
			$this->path = $repo->getRootDirectory() . '/' .
				$repo->getHashPath( $this->name ) . $this->name;
		}
		if ( $mime ) {
			$this->mime = $mime;
		}
		$this->dims = array();
	}

	/**
	 * @param int $page
	 * @return bool
	 */
	private function cachePageDimensions( $page = 1 ) {
		if ( !isset( $this->dims[$page] ) ) {
			if ( !$this->getHandler() ) {
				return false;
			}
			$this->dims[$page] = $this->handler->getPageDimensions( $this, $page );
		}

		return $this->dims[$page];
	}

	/**
	 * @param int $page
	 * @return int
	 */
	function getWidth( $page = 1 ) {
		$dim = $this->cachePageDimensions( $page );

		return $dim['width'];
	}

	/**
	 * @param int $page
	 * @return int
	 */
	function getHeight( $page = 1 ) {
		$dim = $this->cachePageDimensions( $page );

		return $dim['height'];
	}

	/**
	 * @return bool|string
	 */
	function getMimeType() {
		if ( !isset( $this->mime ) ) {
			$magic = MimeMagic::singleton();
			$this->mime = $magic->guessMimeType( $this->getLocalRefPath() );
		}

		return $this->mime;
	}

	/**
	 * @param string $filename
	 * @return array|bool
	 */
	function getImageSize( $filename ) {
		if ( !$this->getHandler() ) {
			return false;
		}

		return $this->handler->getImageSize( $this, $this->getLocalRefPath() );
	}

	/**
	 * @return bool
	 */
	function getMetadata() {
		if ( !isset( $this->metadata ) ) {
			if ( !$this->getHandler() ) {
				$this->metadata = false;
			} else {
				$this->metadata = $this->handler->getMetadata( $this, $this->getLocalRefPath() );
			}
		}

		return $this->metadata;
	}

	/**
	 * @return bool|string
	 */
	function getURL() {
		if ( $this->repo ) {
			return $this->repo->getZoneUrl( 'public' ) . '/' .
				$this->repo->getHashPath( $this->name ) . rawurlencode( $this->name );
		} else {
			return false;
		}
	}

	/**
	 * @return bool|int
	 */
	function getSize() {
		$this->assertRepoDefined();

		return $this->repo->getFileSize( $this->path );
	}

	/**
	 * Optimize getLocalRefPath() by using an existing local reference.
	 * The file at the path of $fsFile should not be deleted (or at least
	 * not until the end of the request). This is mostly a performance hack.
	 *
	 * @param FSFile $fsFile
	 * @return void
	 */
	public function setLocalReference( FSFile $fsFile ) {
		$this->fsFile = $fsFile;
	}
}