| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
use MediaWiki\MainConfigNames; |
| 26 |
use MediaWiki\MediaWikiServices; |
| 27 |
use MediaWiki\Status\Status; |
| 28 |
use MediaWiki\Xml\Xml; |
| 29 |
use Wikimedia\FileBackend\FileBackend; |
| 30 |
use Wikimedia\FileBackend\HTTPFileStreamer; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
abstract class MediaTransformOutput { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public $responsiveUrls = []; |
| 43 |
|
| 44 |
|
| 45 |
protected $file; |
| 46 |
|
| 47 |
|
| 48 |
protected $width; |
| 49 |
|
| 50 |
|
| 51 |
protected $height; |
| 52 |
|
| 53 |
|
| 54 |
protected $url; |
| 55 |
|
| 56 |
|
| 57 |
protected $page; |
| 58 |
|
| 59 |
|
| 60 |
protected $path; |
| 61 |
|
| 62 |
|
| 63 |
protected $lang; |
| 64 |
|
| 65 |
|
| 66 |
protected $storagePath = false; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function getWidth() { |
| 72 |
return $this->width; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public function getHeight() { |
| 79 |
return $this->height; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public function getFile() { |
| 86 |
return $this->file; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public function getExtension() { |
| 97 |
return $this->path ? FileBackend::extensionFromPath( $this->path ) : false; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public function getUrl() { |
| 106 |
return $this->url; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public function getStoragePath() { |
| 115 |
return $this->storagePath; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public function setStoragePath( $storagePath ) { |
| 125 |
$this->storagePath = $storagePath; |
| 126 |
if ( $this->path === false ) { |
| 127 |
$this->path = $storagePath; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
abstract public function toHtml( $options = [] ); |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public function isError() { |
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public function hasFile() { |
| 173 |
|
| 174 |
|
| 175 |
return ( !$this->isError() && ( $this->path || $this->path === null ) ); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public function fileIsSource() { |
| 185 |
return ( !$this->isError() && $this->path === null ); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public function getLocalCopyPath() { |
| 195 |
if ( $this->isError() ) { |
| 196 |
return false; |
| 197 |
} |
| 198 |
|
| 199 |
if ( $this->path === null ) { |
| 200 |
return $this->file->getLocalRefPath(); |
| 201 |
} |
| 202 |
if ( FileBackend::isStoragePath( $this->path ) ) { |
| 203 |
$be = $this->file->getRepo()->getBackend(); |
| 204 |
|
| 205 |
$fsFile = $be->getLocalReference( [ 'src' => $this->path ] ); |
| 206 |
|
| 207 |
return $fsFile ? $fsFile->getPath() : false; |
| 208 |
} |
| 209 |
return $this->path; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public function streamFileWithStatus( $headers = [] ) { |
| 220 |
if ( !$this->path ) { |
| 221 |
return Status::newFatal( 'backend-fail-stream', '<no path>' ); |
| 222 |
} |
| 223 |
|
| 224 |
$repo = $this->file->getRepo(); |
| 225 |
|
| 226 |
if ( $repo && FileBackend::isStoragePath( $this->path ) ) { |
| 227 |
return Status::wrap( |
| 228 |
$repo->getBackend()->streamFile( |
| 229 |
[ 'src' => $this->path, 'headers' => $headers, ] |
| 230 |
) |
| 231 |
); |
| 232 |
} else { |
| 233 |
$streamer = new HTTPFileStreamer( |
| 234 |
$this->getLocalCopyPath(), |
| 235 |
$repo ? $repo->getBackend()->getStreamerOptions() : [] |
| 236 |
); |
| 237 |
|
| 238 |
$success = $streamer->stream( $headers ); |
| 239 |
return $success ? Status::newGood() |
| 240 |
: Status::newFatal( 'backend-fail-stream', $this->path ); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public function streamFile( $headers = [] ) { |
| 252 |
return $this->streamFileWithStatus( $headers )->isOK(); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
protected function linkWrap( $linkAttribs, $contents ) { |
| 264 |
if ( isset( $linkAttribs['href'] ) ) { |
| 265 |
return Xml::tags( 'a', $linkAttribs, $contents ); |
| 266 |
} |
| 267 |
$parserEnableLegacyMediaDOM = MediaWikiServices::getInstance() |
| 268 |
->getMainConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM ); |
| 269 |
if ( $parserEnableLegacyMediaDOM ) { |
| 270 |
return $contents; |
| 271 |
} |
| 272 |
return Xml::tags( 'span', $linkAttribs ?: null, $contents ); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
public function getDescLinkAttribs( $title = null, $params = [] ) { |
| 281 |
if ( is_array( $params ) ) { |
| 282 |
$query = $params; |
| 283 |
} else { |
| 284 |
$query = []; |
| 285 |
} |
| 286 |
if ( $this->page && $this->page !== 1 ) { |
| 287 |
$query['page'] = $this->page; |
| 288 |
} |
| 289 |
if ( $this->lang ) { |
| 290 |
$query['lang'] = $this->lang; |
| 291 |
} |
| 292 |
|
| 293 |
if ( is_string( $params ) && $params !== '' ) { |
| 294 |
$query = $params . '&' . wfArrayToCgi( $query ); |
| 295 |
} |
| 296 |
|
| 297 |
$attribs = [ |
| 298 |
'href' => $this->file->getTitle()->getLocalURL( $query ), |
| 299 |
]; |
| 300 |
|
| 301 |
$parserEnableLegacyMediaDOM = MediaWikiServices::getInstance() |
| 302 |
->getMainConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM ); |
| 303 |
if ( $parserEnableLegacyMediaDOM ) { |
| 304 |
$attribs['class'] = 'image'; |
| 305 |
} else { |
| 306 |
$attribs['class'] = 'mw-file-description'; |
| 307 |
} |
| 308 |
|
| 309 |
if ( $title ) { |
| 310 |
$attribs['title'] = $title; |
| 311 |
} |
| 312 |
|
| 313 |
return $attribs; |
| 314 |
} |
| 315 |
} |