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