| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\MediaWikiServices; |
| 22 |
use MediaWiki\Title\Title; |
| 23 |
use Wikimedia\FileBackend\FSFile\FSFile; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class UnregisteredLocalFile extends File { |
| 40 |
|
| 41 |
protected $title; |
| 42 |
|
| 43 |
|
| 44 |
protected $path; |
| 45 |
|
| 46 |
|
| 47 |
protected $mime; |
| 48 |
|
| 49 |
|
| 50 |
protected $pageDims; |
| 51 |
|
| 52 |
|
| 53 |
protected $sizeAndMetadata; |
| 54 |
|
| 55 |
|
| 56 |
public $handler; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public static function newFromPath( $path, $mime ) { |
| 64 |
return new static( false, false, $path, $mime ); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public static function newFromTitle( $title, $repo ) { |
| 73 |
return new static( $title, $repo, false, false ); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public function __construct( $title = false, $repo = false, $path = false, $mime = false ) { |
| 86 |
if ( !( $title && $repo ) && !$path ) { |
| 87 |
throw new BadMethodCallException( __METHOD__ . |
| 88 |
': not enough parameters, must specify title and repo, or a full path' ); |
| 89 |
} |
| 90 |
if ( $title instanceof Title ) { |
| 91 |
$this->title = File::normalizeTitle( $title, 'exception' ); |
| 92 |
$this->name = $repo->getNameFromTitle( $title ); |
| 93 |
} else { |
| 94 |
$this->name = basename( $path ); |
| 95 |
$this->title = File::normalizeTitle( $this->name, 'exception' ); |
| 96 |
} |
| 97 |
$this->repo = $repo; |
| 98 |
if ( $path ) { |
| 99 |
$this->path = $path; |
| 100 |
} else { |
| 101 |
$this->assertRepoDefined(); |
| 102 |
$this->path = $repo->getRootDirectory() . '/' . |
| 103 |
$repo->getHashPath( $this->name ) . $this->name; |
| 104 |
} |
| 105 |
if ( $mime ) { |
| 106 |
$this->mime = $mime; |
| 107 |
} |
| 108 |
$this->pageDims = []; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
private function cachePageDimensions( $page = 1 ) { |
| 116 |
$page = (int)$page; |
| 117 |
if ( $page < 1 ) { |
| 118 |
$page = 1; |
| 119 |
} |
| 120 |
|
| 121 |
if ( !isset( $this->pageDims[$page] ) ) { |
| 122 |
if ( !$this->getHandler() ) { |
| 123 |
return false; |
| 124 |
} |
| 125 |
if ( $this->getHandler()->isMultiPage( $this ) ) { |
| 126 |
$this->pageDims[$page] = $this->handler->getPageDimensions( $this, $page ); |
| 127 |
} else { |
| 128 |
$info = $this->getSizeAndMetadata(); |
| 129 |
return [ |
| 130 |
'width' => $info['width'], |
| 131 |
'height' => $info['height'] |
| 132 |
]; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
return $this->pageDims[$page]; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public function getWidth( $page = 1 ) { |
| 144 |
$dim = $this->cachePageDimensions( $page ); |
| 145 |
|
| 146 |
return $dim['width'] ?? 0; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public function getHeight( $page = 1 ) { |
| 154 |
$dim = $this->cachePageDimensions( $page ); |
| 155 |
|
| 156 |
return $dim['height'] ?? 0; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public function getMimeType() { |
| 163 |
if ( !isset( $this->mime ) ) { |
| 164 |
$refPath = $this->getLocalRefPath(); |
| 165 |
if ( $refPath !== false ) { |
| 166 |
$magic = MediaWikiServices::getInstance()->getMimeAnalyzer(); |
| 167 |
$this->mime = $magic->guessMimeType( $refPath ); |
| 168 |
} else { |
| 169 |
$this->mime = false; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
return $this->mime; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public function getBitDepth() { |
| 180 |
$info = $this->getSizeAndMetadata(); |
| 181 |
return $info['bits'] ?? 0; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public function getMetadata() { |
| 188 |
$info = $this->getSizeAndMetadata(); |
| 189 |
return $info['metadata'] ? serialize( $info['metadata'] ) : false; |
| 190 |
} |
| 191 |
|
| 192 |
public function getMetadataArray(): array { |
| 193 |
$info = $this->getSizeAndMetadata(); |
| 194 |
return $info['metadata']; |
| 195 |
} |
| 196 |
|
| 197 |
private function getSizeAndMetadata() { |
| 198 |
if ( $this->sizeAndMetadata === null ) { |
| 199 |
if ( !$this->getHandler() ) { |
| 200 |
$this->sizeAndMetadata = [ 'width' => 0, 'height' => 0, 'metadata' => [] ]; |
| 201 |
} else { |
| 202 |
$this->sizeAndMetadata = $this->getHandler()->getSizeAndMetadataWithFallback( |
| 203 |
$this, $this->getLocalRefPath() ); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
return $this->sizeAndMetadata; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public function getURL() { |
| 214 |
if ( $this->repo ) { |
| 215 |
return $this->repo->getZoneUrl( 'public' ) . '/' . |
| 216 |
$this->repo->getHashPath( $this->name ) . rawurlencode( $this->name ); |
| 217 |
} else { |
| 218 |
return false; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public function getSize() { |
| 226 |
$this->assertRepoDefined(); |
| 227 |
|
| 228 |
return $this->repo->getFileSize( $this->path ); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public function setLocalReference( FSFile $fsFile ) { |
| 240 |
$this->fsFile = $fsFile; |
| 241 |
} |
| 242 |
} |