* File redirects are slow: 2 queries per image per repository on parse. We previously...
[lhc/web/wiklou.git] / includes / filerepo / UnregisteredLocalFile.php
1 <?php
2
3 /**
4 * A file object referring to either a standalone local file, or a file in a
5 * local repository with no database, for example an FSRepo repository.
6 *
7 * Read-only.
8 *
9 * TODO: Currently it doesn't really work in the repository role, there are
10 * lots of functions missing. It is used by the WebStore extension in the
11 * standalone role.
12 */
13 class UnregisteredLocalFile extends File {
14 var $title, $path, $mime, $handler, $dims;
15
16 static function newFromPath( $path, $mime ) {
17 return new UnregisteredLocalFile( false, false, $path, $mime );
18 }
19
20 static function newFromTitle( $title, $repo ) {
21 return new UnregisteredLocalFile( $title, $repo, false, false );
22 }
23
24 function __construct( $title = false, $repo = false, $path = false, $mime = false ) {
25 if ( !( $title && $repo ) && !$path ) {
26 throw new MWException( __METHOD__.': not enough parameters, must specify title and repo, or a full path' );
27 }
28 if ( $title ) {
29 $this->title = $title;
30 $this->name = $repo->getNameFromTitle( $title );
31 } else {
32 $this->name = basename( $path );
33 $this->title = Title::makeTitleSafe( NS_IMAGE, $this->name );
34 }
35 $this->repo = $repo;
36 if ( $path ) {
37 $this->path = $path;
38 } else {
39 $this->path = $repo->getRootDirectory() . '/' . $repo->getHashPath( $this->name ) . $this->name;
40 }
41 if ( $mime ) {
42 $this->mime = $mime;
43 }
44 $this->dims = array();
45 }
46
47 function getPageDimensions( $page = 1 ) {
48 if ( !isset( $this->dims[$page] ) ) {
49 if ( !$this->getHandler() ) {
50 return false;
51 }
52 $this->dims[$page] = $this->handler->getPageDimensions( $this, $page );
53 }
54 return $this->dims[$page];
55 }
56
57 function getWidth( $page = 1 ) {
58 $dim = $this->getPageDimensions( $page );
59 return $dim['width'];
60 }
61
62 function getHeight( $page = 1 ) {
63 $dim = $this->getPageDimensions( $page );
64 return $dim['height'];
65 }
66
67 function getMimeType() {
68 if ( !isset( $this->mime ) ) {
69 $magic = MimeMagic::singleton();
70 $this->mime = $magic->guessMimeType( $this->path );
71 }
72 return $this->mime;
73 }
74
75 function getImageSize( $filename ) {
76 if ( !$this->getHandler() ) {
77 return false;
78 }
79 return $this->handler->getImageSize( $this, $this->getPath() );
80 }
81
82 function getMetadata() {
83 if ( !isset( $this->metadata ) ) {
84 if ( !$this->getHandler() ) {
85 $this->metadata = false;
86 } else {
87 $this->metadata = $this->handler->getMetadata( $this, $this->getPath() );
88 }
89 }
90 return $this->metadata;
91 }
92
93 function getURL() {
94 if ( $this->repo ) {
95 return $this->repo->getZoneUrl( 'public' ) . '/' . $this->repo->getHashPath( $this->name ) . urlencode( $this->name );
96 } else {
97 return false;
98 }
99 }
100
101 function getSize() {
102 if ( file_exists( $this->path ) ) {
103 return filesize( $this->path );
104 } else {
105 return false;
106 }
107 }
108 }
109