MediaWiki REL1_37
MediaTransformOutput.php
Go to the documentation of this file.
1<?php
30abstract class MediaTransformOutput {
34 public $responsiveUrls = [];
35
37 protected $file;
38
40 protected $width;
41
43 protected $height;
44
46 protected $url;
47
49 protected $page;
50
52 protected $path;
53
55 protected $lang;
56
58 protected $storagePath = false;
59
63 public function getWidth() {
64 return $this->width;
65 }
66
70 public function getHeight() {
71 return $this->height;
72 }
73
77 public function getFile() {
78 return $this->file;
79 }
80
88 public function getExtension() {
89 return $this->path ? FileBackend::extensionFromPath( $this->path ) : false;
90 }
91
97 public function getUrl() {
98 return $this->url;
99 }
100
106 public function getStoragePath() {
107 return $this->storagePath;
108 }
109
116 public function setStoragePath( $storagePath ) {
117 $this->storagePath = $storagePath;
118 if ( $this->path === false ) {
119 $this->path = $storagePath;
120 }
121 }
122
143 abstract public function toHtml( $options = [] );
144
149 public function isError() {
150 return false;
151 }
152
164 public function hasFile() {
165 // If TRANSFORM_LATER, $this->path will be false.
166 // Note: a null path means "use the source file".
167 return ( !$this->isError() && ( $this->path || $this->path === null ) );
168 }
169
176 public function fileIsSource() {
177 return ( !$this->isError() && $this->path === null );
178 }
179
186 public function getLocalCopyPath() {
187 if ( $this->isError() ) {
188 return false;
189 } elseif ( $this->path === null ) {
190 return $this->file->getLocalRefPath(); // assume thumb was not scaled
191 } elseif ( FileBackend::isStoragePath( $this->path ) ) {
192 $be = $this->file->getRepo()->getBackend();
193 // The temp file will be process cached by FileBackend
194 $fsFile = $be->getLocalReference( [ 'src' => $this->path ] );
195
196 return $fsFile ? $fsFile->getPath() : false;
197 } else {
198 return $this->path; // may return false
199 }
200 }
201
209 public function streamFileWithStatus( $headers = [] ) {
210 if ( !$this->path ) {
211 return Status::newFatal( 'backend-fail-stream', '<no path>' );
212 } elseif ( FileBackend::isStoragePath( $this->path ) ) {
213 $be = $this->file->getRepo()->getBackend();
214 return Status::wrap(
215 $be->streamFile( [ 'src' => $this->path, 'headers' => $headers ] ) );
216 } else { // FS-file
217 $success = StreamFile::stream( $this->getLocalCopyPath(), $headers );
218 return $success ? Status::newGood() : Status::newFatal( 'backend-fail-stream', $this->path );
219 }
220 }
221
229 public function streamFile( $headers = [] ) {
230 return $this->streamFileWithStatus( $headers )->isOK();
231 }
232
241 protected function linkWrap( $linkAttribs, $contents ) {
242 if ( $linkAttribs ) {
243 return Xml::tags( 'a', $linkAttribs, $contents );
244 } else {
247 return $contents;
248 } else {
249 return Xml::tags( 'span', null, $contents );
250 }
251 }
252 }
253
259 public function getDescLinkAttribs( $title = null, $params = [] ) {
260 if ( is_array( $params ) ) {
261 $query = $params;
262 } else {
263 $query = [];
264 }
265 if ( $this->page && $this->page !== 1 ) {
266 $query['page'] = $this->page;
267 }
268 if ( $this->lang ) {
269 $query['lang'] = $this->lang;
270 }
271
272 if ( is_string( $params ) && $params !== '' ) {
273 $query = $params . '&' . wfArrayToCgi( $query );
274 }
275
276 $attribs = [
277 'href' => $this->file->getTitle()->getLocalURL( $query ),
278 ];
279
282 $attribs['class'] = 'image';
283 }
284
285 if ( $title ) {
286 $attribs['title'] = $title;
287 }
288
289 return $attribs;
290 }
291}
$wgParserEnableLegacyMediaDOM
Enable legacy media HTML structure in the output from the Parser.
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e....
static isStoragePath( $path)
Check if a given path is a "mwstore://" path.
static extensionFromPath( $path, $case='lowercase')
Get the final extension from a storage or FS path.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:66
Base class for the output of MediaHandler::doTransform() and File::transform().
streamFileWithStatus( $headers=[])
Stream the file if there were no errors.
string $url
URL path to the thumb.
hasFile()
Check if an output thumbnail file actually exists.
getLocalCopyPath()
Get the path of a file system copy of the thumbnail.
bool string $lang
Language code, false if not set.
fileIsSource()
Check if the output thumbnail is the same as the source.
linkWrap( $linkAttribs, $contents)
Wrap some XHTML text in an anchor tag with the given attributes or, fallback to a span in the absence...
getExtension()
Get the final extension of the thumbnail.
streamFile( $headers=[])
Stream the file if there were no errors.
array $responsiveUrls
Associative array mapping optional supplementary image files from pixel density (eg 1....
bool string $path
Filesystem path to the thumb.
isError()
This will be overridden to return true in error classes.
getDescLinkAttribs( $title=null, $params=[])
bool string $storagePath
Permanent storage path.
toHtml( $options=[])
Fetch HTML for this transform output.
static stream( $fname, $headers=[], $sendErrors=true, $optHeaders=[], $flags=0)
Stream a file to the browser, adding all the headings and fun stuff.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42
if(!isset( $args[0])) $lang