Fixed undefined defines warnings introduced in change 5131
[mediawiki.git] / includes / filerepo / file / UnregisteredLocalFile.php
blob7c5eab03ddb65ded1f810af4d6c54e829b94c5c1
1 <?php
2 /**
3 * File without associated database record
5 * @file
6 * @ingroup FileAbstraction
7 */
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.
13 * Read-only.
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.
19 * @ingroup FileAbstraction
21 class UnregisteredLocalFile extends File {
22 var $title, $path, $mime, $dims;
24 /**
25 * @var MediaHandler
27 var $handler;
29 /**
30 * @param $path string Storage path
31 * @param $mime string
32 * @return UnregisteredLocalFile
34 static function newFromPath( $path, $mime ) {
35 return new self( false, false, $path, $mime );
38 /**
39 * @param $title
40 * @param $repo
41 * @return UnregisteredLocalFile
43 static function newFromTitle( $title, $repo ) {
44 return new self( $title, $repo, false, false );
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.
51 * @throws MWException
52 * @param $title Title|bool
53 * @param $repo FileRepo
54 * @param $path string
55 * @param $mime string
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' );
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' );
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;
76 if ( $mime ) {
77 $this->mime = $mime;
79 $this->dims = array();
82 private function cachePageDimensions( $page = 1 ) {
83 if ( !isset( $this->dims[$page] ) ) {
84 if ( !$this->getHandler() ) {
85 return false;
87 $this->dims[$page] = $this->handler->getPageDimensions( $this, $page );
89 return $this->dims[$page];
92 function getWidth( $page = 1 ) {
93 $dim = $this->cachePageDimensions( $page );
94 return $dim['width'];
97 function getHeight( $page = 1 ) {
98 $dim = $this->cachePageDimensions( $page );
99 return $dim['height'];
102 function getMimeType() {
103 if ( !isset( $this->mime ) ) {
104 $magic = MimeMagic::singleton();
105 $this->mime = $magic->guessMimeType( $this->getLocalRefPath() );
107 return $this->mime;
110 function getImageSize( $filename ) {
111 if ( !$this->getHandler() ) {
112 return false;
114 return $this->handler->getImageSize( $this, $this->getLocalRefPath() );
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() );
125 return $this->metadata;
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;
137 function getSize() {
138 $this->assertRepoDefined();
139 $props = $this->repo->getFileProps( $this->path );
140 if ( isset( $props['size'] ) ) {
141 return $props['size'];
143 return false; // doesn't exist