Merge "Made WikiPage recall the source of the data used to load its state."
[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;
38
39 /**
40 * @var MediaHandler
41 */
42 var $handler;
43
44 /**
45 * @param $path string 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
69 * @param $path string
70 * @param $mime string
71 */
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' );
75 }
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' );
82 }
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;
90 }
91 if ( $mime ) {
92 $this->mime = $mime;
93 }
94 $this->dims = array();
95 }
96
97 private function cachePageDimensions( $page = 1 ) {
98 if ( !isset( $this->dims[$page] ) ) {
99 if ( !$this->getHandler() ) {
100 return false;
101 }
102 $this->dims[$page] = $this->handler->getPageDimensions( $this, $page );
103 }
104 return $this->dims[$page];
105 }
106
107 function getWidth( $page = 1 ) {
108 $dim = $this->cachePageDimensions( $page );
109 return $dim['width'];
110 }
111
112 function getHeight( $page = 1 ) {
113 $dim = $this->cachePageDimensions( $page );
114 return $dim['height'];
115 }
116
117 function getMimeType() {
118 if ( !isset( $this->mime ) ) {
119 $magic = MimeMagic::singleton();
120 $this->mime = $magic->guessMimeType( $this->getLocalRefPath() );
121 }
122 return $this->mime;
123 }
124
125 function getImageSize( $filename ) {
126 if ( !$this->getHandler() ) {
127 return false;
128 }
129 return $this->handler->getImageSize( $this, $this->getLocalRefPath() );
130 }
131
132 function getMetadata() {
133 if ( !isset( $this->metadata ) ) {
134 if ( !$this->getHandler() ) {
135 $this->metadata = false;
136 } else {
137 $this->metadata = $this->handler->getMetadata( $this, $this->getLocalRefPath() );
138 }
139 }
140 return $this->metadata;
141 }
142
143 function getURL() {
144 if ( $this->repo ) {
145 return $this->repo->getZoneUrl( 'public' ) . '/' .
146 $this->repo->getHashPath( $this->name ) . rawurlencode( $this->name );
147 } else {
148 return false;
149 }
150 }
151
152 function getSize() {
153 $this->assertRepoDefined();
154 $props = $this->repo->getFileProps( $this->path );
155 if ( isset( $props['size'] ) ) {
156 return $props['size'];
157 }
158 return false; // doesn't exist
159 }
160 }