Restored r98710 but with a 'forRefresh' option (not used yet)
[lhc/web/wiklou.git] / includes / filerepo / file / UnregisteredLocalFile.php
1 <?php
2 /**
3 * File without associated database record
4 *
5 * @file
6 * @ingroup FileRepo
7 */
8
9 /**
10 * A file object referring to either a standalone local file, or a file in a
11 * local repository with no database, for example an FSRepo repository.
12 *
13 * Read-only.
14 *
15 * TODO: Currently it doesn't really work in the repository role, there are
16 * lots of functions missing. It is used by the WebStore extension in the
17 * standalone role.
18 *
19 * @ingroup FileRepo
20 */
21 class UnregisteredLocalFile extends File {
22 var $title, $path, $mime, $dims;
23
24 /**
25 * @var MediaHandler
26 */
27 var $handler;
28
29 /**
30 * @param $path
31 * @param $mime
32 * @return UnregisteredLocalFile
33 */
34 static function newFromPath( $path, $mime ) {
35 return new self( false, false, $path, $mime );
36 }
37
38 /**
39 * @param $title
40 * @param $repo
41 * @return UnregisteredLocalFile
42 */
43 static function newFromTitle( $title, $repo ) {
44 return new self( $title, $repo, false, false );
45 }
46
47 /**
48 * Create an UnregisteredLocalFile based on a path or a (title,repo) pair.
49 * A FileRepo object is not required here, unlike most other File classes.
50 *
51 * @throws MWException
52 * @param $title Title|false
53 * @param $repo FSRepo
54 * @param $path string
55 * @param $mime string
56 */
57 function __construct( $title = false, $repo = false, $path = false, $mime = false ) {
58 if ( !( $title && $repo ) && !$path ) {
59 throw new MWException( __METHOD__.': not enough parameters, must specify title and repo, or a full path' );
60 }
61 if ( $title instanceof Title ) {
62 $this->title = File::normalizeTitle( $title, 'exception' );
63 $this->name = $repo->getNameFromTitle( $title );
64 } else {
65 $this->name = basename( $path );
66 $this->title = File::normalizeTitle( $this->name, 'exception' );
67 }
68 $this->repo = $repo;
69 if ( $path ) {
70 $this->path = $path;
71 } else {
72 $this->path = $repo->getRootDirectory() . '/' .
73 $repo->getHashPath( $this->name ) . $this->name;
74 }
75 if ( $mime ) {
76 $this->mime = $mime;
77 }
78 $this->dims = array();
79 }
80
81 private function cachePageDimensions( $page = 1 ) {
82 if ( !isset( $this->dims[$page] ) ) {
83 if ( !$this->getHandler() ) {
84 return false;
85 }
86 $this->dims[$page] = $this->handler->getPageDimensions( $this, $page );
87 }
88 return $this->dims[$page];
89 }
90
91 function getWidth( $page = 1 ) {
92 $dim = $this->cachePageDimensions( $page );
93 return $dim['width'];
94 }
95
96 function getHeight( $page = 1 ) {
97 $dim = $this->cachePageDimensions( $page );
98 return $dim['height'];
99 }
100
101 function getMimeType() {
102 if ( !isset( $this->mime ) ) {
103 $magic = MimeMagic::singleton();
104 $this->mime = $magic->guessMimeType( $this->getPath() );
105 }
106 return $this->mime;
107 }
108
109 function getImageSize( $filename ) {
110 if ( !$this->getHandler() ) {
111 return false;
112 }
113 return $this->handler->getImageSize( $this, $this->getPath() );
114 }
115
116 function getMetadata() {
117 if ( !isset( $this->metadata ) ) {
118 if ( !$this->getHandler() ) {
119 $this->metadata = false;
120 } else {
121 $this->metadata = $this->handler->getMetadata( $this, $this->getPath() );
122 }
123 }
124 return $this->metadata;
125 }
126
127 function getURL() {
128 if ( $this->repo ) {
129 return $this->repo->getZoneUrl( 'public' ) . '/' .
130 $this->repo->getHashPath( $this->name ) . rawurlencode( $this->name );
131 } else {
132 return false;
133 }
134 }
135
136 function getSize() {
137 if ( file_exists( $this->path ) ) {
138 return filesize( $this->path );
139 } else {
140 return false;
141 }
142 }
143 }