MediaWiki  1.34.0
MediaTransformOutput.php
Go to the documentation of this file.
1 <?php
29 abstract class MediaTransformOutput {
33  public $responsiveUrls = [];
34 
36  protected $file;
37 
39  protected $width;
40 
42  protected $height;
43 
45  protected $url;
46 
48  protected $page;
49 
51  protected $path;
52 
54  protected $lang;
55 
57  protected $storagePath = false;
58 
62  public function getWidth() {
63  return $this->width;
64  }
65 
69  public function getHeight() {
70  return $this->height;
71  }
72 
76  public function getFile() {
77  return $this->file;
78  }
79 
85  public function getExtension() {
86  return $this->path ? FileBackend::extensionFromPath( $this->path ) : false;
87  }
88 
92  public function getUrl() {
93  return $this->url;
94  }
95 
99  public function getStoragePath() {
100  return $this->storagePath;
101  }
102 
107  public function setStoragePath( $storagePath ) {
108  $this->storagePath = $storagePath;
109  if ( $this->path === false ) {
110  $this->path = $storagePath;
111  }
112  }
113 
134  abstract public function toHtml( $options = [] );
135 
140  public function isError() {
141  return false;
142  }
143 
155  public function hasFile() {
156  // If TRANSFORM_LATER, $this->path will be false.
157  // Note: a null path means "use the source file".
158  return ( !$this->isError() && ( $this->path || $this->path === null ) );
159  }
160 
167  public function fileIsSource() {
168  return ( !$this->isError() && $this->path === null );
169  }
170 
177  public function getLocalCopyPath() {
178  if ( $this->isError() ) {
179  return false;
180  } elseif ( $this->path === null ) {
181  return $this->file->getLocalRefPath(); // assume thumb was not scaled
182  } elseif ( FileBackend::isStoragePath( $this->path ) ) {
183  $be = $this->file->getRepo()->getBackend();
184  // The temp file will be process cached by FileBackend
185  $fsFile = $be->getLocalReference( [ 'src' => $this->path ] );
186 
187  return $fsFile ? $fsFile->getPath() : false;
188  } else {
189  return $this->path; // may return false
190  }
191  }
192 
200  public function streamFileWithStatus( $headers = [] ) {
201  if ( !$this->path ) {
202  return Status::newFatal( 'backend-fail-stream', '<no path>' );
203  } elseif ( FileBackend::isStoragePath( $this->path ) ) {
204  $be = $this->file->getRepo()->getBackend();
205  return Status::wrap(
206  $be->streamFile( [ 'src' => $this->path, 'headers' => $headers ] ) );
207  } else { // FS-file
208  $success = StreamFile::stream( $this->getLocalCopyPath(), $headers );
209  return $success ? Status::newGood() : Status::newFatal( 'backend-fail-stream', $this->path );
210  }
211  }
212 
220  public function streamFile( $headers = [] ) {
221  return $this->streamFileWithStatus( $headers )->isOK();
222  }
223 
231  protected function linkWrap( $linkAttribs, $contents ) {
232  if ( $linkAttribs ) {
233  return Xml::tags( 'a', $linkAttribs, $contents );
234  } else {
235  return $contents;
236  }
237  }
238 
244  public function getDescLinkAttribs( $title = null, $params = [] ) {
245  if ( is_array( $params ) ) {
246  $query = $params;
247  } else {
248  $query = [];
249  }
250  if ( $this->page && $this->page !== 1 ) {
251  $query['page'] = $this->page;
252  }
253  if ( $this->lang ) {
254  $query['lang'] = $this->lang;
255  }
256 
257  if ( is_string( $params ) && $params !== '' ) {
258  $query = $params . '&' . wfArrayToCgi( $query );
259  }
260 
261  $attribs = [
262  'href' => $this->file->getTitle()->getLocalURL( $query ),
263  'class' => 'image',
264  ];
265  if ( $title ) {
266  $attribs['title'] = $title;
267  }
268 
269  return $attribs;
270  }
271 }
MediaTransformOutput\$responsiveUrls
array $responsiveUrls
Associative array mapping optional supplementary image files from pixel density (eg 1....
Definition: MediaTransformOutput.php:33
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
MediaTransformOutput\getFile
getFile()
Definition: MediaTransformOutput.php:76
MediaTransformOutput\isError
isError()
This will be overridden to return true in error classes.
Definition: MediaTransformOutput.php:140
MediaTransformOutput\getStoragePath
getStoragePath()
Definition: MediaTransformOutput.php:99
MediaTransformOutput\$file
File $file
Definition: MediaTransformOutput.php:36
MediaTransformOutput\toHtml
toHtml( $options=[])
Fetch HTML for this transform output.
FileBackend\extensionFromPath
static extensionFromPath( $path, $case='lowercase')
Get the final extension from a storage or FS path.
Definition: FileBackend.php:1579
$success
$success
Definition: NoLocalSettings.php:42
MediaTransformOutput\getWidth
getWidth()
Definition: MediaTransformOutput.php:62
MediaTransformOutput\$url
string $url
URL path to the thumb.
Definition: MediaTransformOutput.php:45
StreamFile\stream
static stream( $fname, $headers=[], $sendErrors=true, $optHeaders=[], $flags=0)
Stream a file to the browser, adding all the headings and fun stuff.
Definition: StreamFile.php:47
MediaTransformOutput\hasFile
hasFile()
Check if an output thumbnail file actually exists.
Definition: MediaTransformOutput.php:155
MediaTransformOutput\getUrl
getUrl()
Definition: MediaTransformOutput.php:92
MediaTransformOutput\streamFile
streamFile( $headers=[])
Stream the file if there were no errors.
Definition: MediaTransformOutput.php:220
MediaTransformOutput\$lang
bool string $lang
Language code, false if not set.
Definition: MediaTransformOutput.php:54
MediaTransformOutput\linkWrap
linkWrap( $linkAttribs, $contents)
Wrap some XHTML text in an anchor tag with the given attributes.
Definition: MediaTransformOutput.php:231
MediaTransformOutput\getExtension
getExtension()
Get the final extension of the thumbnail.
Definition: MediaTransformOutput.php:85
MediaTransformOutput\fileIsSource
fileIsSource()
Check if the output thumbnail is the same as the source.
Definition: MediaTransformOutput.php:167
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:61
MediaTransformOutput\setStoragePath
setStoragePath( $storagePath)
Definition: MediaTransformOutput.php:107
FileBackend\isStoragePath
static isStoragePath( $path)
Check if a given path is a "mwstore://" path.
Definition: FileBackend.php:1508
Status\wrap
static wrap( $sv)
Succinct helper method to wrap a StatusValue.
Definition: Status.php:55
MediaTransformOutput\streamFileWithStatus
streamFileWithStatus( $headers=[])
Stream the file if there were no errors.
Definition: MediaTransformOutput.php:200
$title
$title
Definition: testCompression.php:34
MediaTransformOutput\getDescLinkAttribs
getDescLinkAttribs( $title=null, $params=[])
Definition: MediaTransformOutput.php:244
MediaTransformOutput\$height
int $height
Image height.
Definition: MediaTransformOutput.php:42
MediaTransformOutput\$storagePath
bool string $storagePath
Permanent storage path.
Definition: MediaTransformOutput.php:57
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:81
Xml\tags
static tags( $element, $attribs, $contents)
Same as Xml::element(), but does not escape contents.
Definition: Xml.php:130
MediaTransformOutput\getLocalCopyPath
getLocalCopyPath()
Get the path of a file system copy of the thumbnail.
Definition: MediaTransformOutput.php:177
MediaTransformOutput
Base class for the output of MediaHandler::doTransform() and File::transform().
Definition: MediaTransformOutput.php:29
MediaTransformOutput\$path
bool string $path
Filesystem path to the thumb.
Definition: MediaTransformOutput.php:51
MediaTransformOutput\getHeight
getHeight()
Definition: MediaTransformOutput.php:69
MediaTransformOutput\$width
int $width
Image width.
Definition: MediaTransformOutput.php:39
MediaTransformOutput\$page
bool string $page
Definition: MediaTransformOutput.php:48
wfArrayToCgi
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e....
Definition: GlobalFunctions.php:347