No current need for an alias to getPath()
[lhc/web/wiklou.git] / includes / filerepo / 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 static function newFromPath( $path, $mime ) {
30 return new UnregisteredLocalFile( false, false, $path, $mime );
31 }
32
33 static function newFromTitle( $title, $repo ) {
34 return new UnregisteredLocalFile( $title, $repo, false, false );
35 }
36
37 /**
38 * @throws MWException
39 * @param bool $title
40 * @param $repo FSRepo
41 * @param bool $path
42 * @param bool $mime
43 */
44 function __construct( $title = false, $repo = false, $path = false, $mime = false ) {
45 if ( !( $title && $repo ) && !$path ) {
46 throw new MWException( __METHOD__.': not enough parameters, must specify title and repo, or a full path' );
47 }
48 if ( $title ) {
49 $this->title = $title;
50 $this->name = $repo->getNameFromTitle( $title );
51 } else {
52 $this->name = basename( $path );
53 $this->title = Title::makeTitleSafe( NS_FILE, $this->name );
54 }
55 $this->repo = $repo;
56 if ( $path ) {
57 $this->path = $path;
58 } else {
59 $this->path = $repo->getRootDirectory() . '/' . $repo->getHashPath( $this->name ) . $this->name;
60 }
61 if ( $mime ) {
62 $this->mime = $mime;
63 }
64 $this->dims = array();
65 }
66
67 function getPageDimensions( $page = 1 ) {
68 if ( !isset( $this->dims[$page] ) ) {
69 if ( !$this->getHandler() ) {
70 return false;
71 }
72 $this->dims[$page] = $this->handler->getPageDimensions( $this, $page );
73 }
74 return $this->dims[$page];
75 }
76
77 function getWidth( $page = 1 ) {
78 $dim = $this->getPageDimensions( $page );
79 return $dim['width'];
80 }
81
82 function getHeight( $page = 1 ) {
83 $dim = $this->getPageDimensions( $page );
84 return $dim['height'];
85 }
86
87 function getMimeType() {
88 if ( !isset( $this->mime ) ) {
89 $magic = MimeMagic::singleton();
90 $this->mime = $magic->guessMimeType( $this->path );
91 }
92 return $this->mime;
93 }
94
95 function getImageSize( $filename ) {
96 if ( !$this->getHandler() ) {
97 return false;
98 }
99 return $this->handler->getImageSize( $this, $this->getPath() );
100 }
101
102 function getMetadata() {
103 if ( !isset( $this->metadata ) ) {
104 if ( !$this->getHandler() ) {
105 $this->metadata = false;
106 } else {
107 $this->metadata = $this->handler->getMetadata( $this, $this->getPath() );
108 }
109 }
110 return $this->metadata;
111 }
112
113 function getURL() {
114 if ( $this->repo ) {
115 return $this->repo->getZoneUrl( 'public' ) . '/' . $this->repo->getHashPath( $this->name ) . rawurlencode( $this->name );
116 } else {
117 return false;
118 }
119 }
120
121 function getSize() {
122 if ( file_exists( $this->path ) ) {
123 return filesize( $this->path );
124 } else {
125 return false;
126 }
127 }
128 }