summaryrefslogtreecommitdiff
path: root/includes/filerepo/UnregisteredLocalFile.php
blob: 2df9a9b5d258e91d5da58c5b26454644d061e4d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
 * File without associated database record
 *
 * @file
 * @ingroup FileRepo
 */

/**
 * A file object referring to either a standalone local file, or a file in a
 * local repository with no database, for example an FSRepo repository.
 *
 * Read-only.
 *
 * TODO: Currently it doesn't really work in the repository role, there are
 * lots of functions missing. It is used by the WebStore extension in the
 * standalone role.
 *
 * @ingroup FileRepo
 */
class UnregisteredLocalFile extends File {
	var $title, $path, $mime, $dims;

	/**
	 * @var MediaHandler
	 */
	var $handler;

	/**
	 * @param $path
	 * @param $mime
	 * @return UnregisteredLocalFile
	 */
	static function newFromPath( $path, $mime ) {
		return new UnregisteredLocalFile( false, false, $path, $mime );
	}

	/**
	 * @param $title
	 * @param $repo
	 * @return UnregisteredLocalFile
	 */
	static function newFromTitle( $title, $repo ) {
		return new UnregisteredLocalFile( $title, $repo, false, false );
	}

	/**
	 * @throws MWException
	 * @param $title string
	 * @param $repo FSRepo
	 * @param $path string
	 * @param $mime string
	 */
	function __construct( $title = false, $repo = false, $path = false, $mime = false ) {
		if ( !( $title && $repo ) && !$path ) {
			throw new MWException( __METHOD__.': not enough parameters, must specify title and repo, or a full path' );
		}
		if ( $title ) {
			$this->title = $title;
			$this->name = $repo->getNameFromTitle( $title );
		} else {
			$this->name = basename( $path );
			$this->title = Title::makeTitleSafe( NS_FILE, $this->name );
		}
		$this->repo = $repo;
		if ( $path ) {
			$this->path = $path;
		} else {
			$this->path = $repo->getRootDirectory() . '/' . $repo->getHashPath( $this->name ) . $this->name;
		}
		if ( $mime ) {
			$this->mime = $mime;
		}
		$this->dims = array();
	}

	function getPageDimensions( $page = 1 ) {
		if ( !isset( $this->dims[$page] ) ) {
			if ( !$this->getHandler() ) {
				return false;
			}
			$this->dims[$page] = $this->handler->getPageDimensions( $this, $page );
		}
		return $this->dims[$page];
	}

	function getWidth( $page = 1 ) {
		$dim = $this->getPageDimensions( $page );
		return $dim['width'];
	}

	function getHeight( $page = 1 ) {
		$dim = $this->getPageDimensions( $page );
		return $dim['height'];
	}

	function getMimeType() {
		if ( !isset( $this->mime ) ) {
			$magic = MimeMagic::singleton();
			$this->mime = $magic->guessMimeType( $this->path );
		}
		return $this->mime;
	}

	function getImageSize( $filename ) {
		if ( !$this->getHandler() ) {
			return false;
		}
		return $this->handler->getImageSize( $this, $this->getPath() );
	}

	function getMetadata() {
		if ( !isset( $this->metadata ) ) {
			if ( !$this->getHandler() ) {
				$this->metadata = false;
			} else {
				$this->metadata = $this->handler->getMetadata( $this, $this->getPath() );
			}
		}
		return $this->metadata;
	}

	function getURL() {
		if ( $this->repo ) {
			return $this->repo->getZoneUrl( 'public' ) . '/' . $this->repo->getHashPath( $this->name ) . rawurlencode( $this->name );
		} else {
			return false;
		}
	}

	function getSize() {
		if ( file_exists( $this->path ) ) {
			return filesize( $this->path );
		} else {
			return false;
		}
	}
}