]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/filerepo/UnregisteredLocalFile.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.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, $handler, $dims;
23
24         static function newFromPath( $path, $mime ) {
25                 return new UnregisteredLocalFile( false, false, $path, $mime );
26         }
27
28         static function newFromTitle( $title, $repo ) {
29                 return new UnregisteredLocalFile( $title, $repo, false, false );
30         }
31
32         function __construct( $title = false, $repo = false, $path = false, $mime = false ) {
33                 if ( !( $title && $repo ) && !$path ) {
34                         throw new MWException( __METHOD__.': not enough parameters, must specify title and repo, or a full path' );
35                 }
36                 if ( $title ) {
37                         $this->title = $title;
38                         $this->name = $repo->getNameFromTitle( $title );
39                 } else {
40                         $this->name = basename( $path );
41                         $this->title = Title::makeTitleSafe( NS_FILE, $this->name );
42                 }
43                 $this->repo = $repo;
44                 if ( $path ) {
45                         $this->path = $path;
46                 } else {
47                         $this->path = $repo->getRootDirectory() . '/' . $repo->getHashPath( $this->name ) . $this->name;
48                 }
49                 if ( $mime ) {
50                         $this->mime = $mime;
51                 }
52                 $this->dims = array();
53         }
54
55         function getPageDimensions( $page = 1 ) {
56                 if ( !isset( $this->dims[$page] ) ) {
57                         if ( !$this->getHandler() ) {
58                                 return false;
59                         }
60                         $this->dims[$page] = $this->handler->getPageDimensions( $this, $page );
61                 }
62                 return $this->dims[$page];
63         }
64
65         function getWidth( $page = 1 ) {
66                 $dim = $this->getPageDimensions( $page );
67                 return $dim['width'];
68         }
69
70         function getHeight( $page = 1 ) {
71                 $dim = $this->getPageDimensions( $page );
72                 return $dim['height'];
73         }
74
75         function getMimeType() {
76                 if ( !isset( $this->mime ) ) {
77                         $magic = MimeMagic::singleton();
78                         $this->mime = $magic->guessMimeType( $this->path );
79                 }
80                 return $this->mime;
81         }
82
83         function getImageSize( $filename ) {
84                 if ( !$this->getHandler() ) {
85                         return false;
86                 }
87                 return $this->handler->getImageSize( $this, $this->getPath() );
88         }
89
90         function getMetadata() {
91                 if ( !isset( $this->metadata ) ) {
92                         if ( !$this->getHandler() ) {
93                                 $this->metadata = false;
94                         } else {
95                                 $this->metadata = $this->handler->getMetadata( $this, $this->getPath() );
96                         }
97                 }
98                 return $this->metadata;
99         }
100
101         function getURL() {
102                 if ( $this->repo ) {
103                         return $this->repo->getZoneUrl( 'public' ) . '/' . $this->repo->getHashPath( $this->name ) . rawurlencode( $this->name );
104                 } else {
105                         return false;
106                 }
107         }
108
109         function getSize() {
110                 if ( file_exists( $this->path ) ) {
111                         return filesize( $this->path );
112                 } else {
113                         return false;
114                 }
115         }
116 }