enhance filerepo doc structure
[lhc/web/wiklou.git] / includes / filerepo / file / UnregisteredLocalFile.php
1 <?php
2 /**
3 * File without associated database record
4 *
5 * @file
6 * @ingroup FileAbstraction
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 FileRepo 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 FileAbstraction
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 string Storage path
31 * @param $mime string
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 FileRepo
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->assertRepoDefined();
73 $this->path = $repo->getRootDirectory() . '/' .
74 $repo->getHashPath( $this->name ) . $this->name;
75 }
76 if ( $mime ) {
77 $this->mime = $mime;
78 }
79 $this->dims = array();
80 }
81
82 private function cachePageDimensions( $page = 1 ) {
83 if ( !isset( $this->dims[$page] ) ) {
84 if ( !$this->getHandler() ) {
85 return false;
86 }
87 $this->dims[$page] = $this->handler->getPageDimensions( $this, $page );
88 }
89 return $this->dims[$page];
90 }
91
92 function getWidth( $page = 1 ) {
93 $dim = $this->cachePageDimensions( $page );
94 return $dim['width'];
95 }
96
97 function getHeight( $page = 1 ) {
98 $dim = $this->cachePageDimensions( $page );
99 return $dim['height'];
100 }
101
102 function getMimeType() {
103 if ( !isset( $this->mime ) ) {
104 $magic = MimeMagic::singleton();
105 $this->mime = $magic->guessMimeType( $this->getLocalRefPath() );
106 }
107 return $this->mime;
108 }
109
110 function getImageSize( $filename ) {
111 if ( !$this->getHandler() ) {
112 return false;
113 }
114 return $this->handler->getImageSize( $this, $this->getLocalRefPath() );
115 }
116
117 function getMetadata() {
118 if ( !isset( $this->metadata ) ) {
119 if ( !$this->getHandler() ) {
120 $this->metadata = false;
121 } else {
122 $this->metadata = $this->handler->getMetadata( $this, $this->getLocalRefPath() );
123 }
124 }
125 return $this->metadata;
126 }
127
128 function getURL() {
129 if ( $this->repo ) {
130 return $this->repo->getZoneUrl( 'public' ) . '/' .
131 $this->repo->getHashPath( $this->name ) . rawurlencode( $this->name );
132 } else {
133 return false;
134 }
135 }
136
137 function getSize() {
138 $this->assertRepoDefined();
139 $props = $this->repo->getFileProps( $this->path );
140 if ( isset( $props['size'] ) ) {
141 return $props['size'];
142 }
143 return false; // doesn't exist
144 }
145 }