MediaWiki  master
MediaTransformOutput.php
Go to the documentation of this file.
1 <?php
30 abstract 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 
240  protected function linkWrap( $linkAttribs, $contents ) {
241  if ( $linkAttribs ) {
242  return Xml::tags( 'a', $linkAttribs, $contents );
243  } else {
244  return $contents;
245  }
246  }
247 
253  public function getDescLinkAttribs( $title = null, $params = [] ) {
254  if ( is_array( $params ) ) {
255  $query = $params;
256  } else {
257  $query = [];
258  }
259  if ( $this->page && $this->page !== 1 ) {
260  $query['page'] = $this->page;
261  }
262  if ( $this->lang ) {
263  $query['lang'] = $this->lang;
264  }
265 
266  if ( is_string( $params ) && $params !== '' ) {
267  $query = $params . '&' . wfArrayToCgi( $query );
268  }
269 
270  $attribs = [
271  'href' => $this->file->getTitle()->getLocalURL( $query ),
272  'class' => 'image',
273  ];
274  if ( $title ) {
275  $attribs['title'] = $title;
276  }
277 
278  return $attribs;
279  }
280 }
MediaTransformOutput\$responsiveUrls
array $responsiveUrls
Associative array mapping optional supplementary image files from pixel density (eg 1....
Definition: MediaTransformOutput.php:34
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:70
MediaTransformOutput\getFile
getFile()
Definition: MediaTransformOutput.php:77
MediaTransformOutput\isError
isError()
This will be overridden to return true in error classes.
Definition: MediaTransformOutput.php:149
MediaTransformOutput\getStoragePath
getStoragePath()
Stable to override.
Definition: MediaTransformOutput.php:106
MediaTransformOutput\$file
File $file
Definition: MediaTransformOutput.php:37
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:1602
$success
$success
Definition: NoLocalSettings.php:42
MediaTransformOutput\getWidth
getWidth()
Definition: MediaTransformOutput.php:63
MediaTransformOutput\$url
string $url
URL path to the thumb.
Definition: MediaTransformOutput.php:46
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:164
MediaTransformOutput\getUrl
getUrl()
Stable to override.
Definition: MediaTransformOutput.php:97
MediaTransformOutput\streamFile
streamFile( $headers=[])
Stream the file if there were no errors.
Definition: MediaTransformOutput.php:229
MediaTransformOutput\$lang
bool string $lang
Language code, false if not set.
Definition: MediaTransformOutput.php:55
MediaTransformOutput\linkWrap
linkWrap( $linkAttribs, $contents)
Wrap some XHTML text in an anchor tag with the given attributes.
Definition: MediaTransformOutput.php:240
MediaTransformOutput\getExtension
getExtension()
Get the final extension of the thumbnail.
Definition: MediaTransformOutput.php:88
MediaTransformOutput\fileIsSource
fileIsSource()
Check if the output thumbnail is the same as the source.
Definition: MediaTransformOutput.php:176
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:63
MediaTransformOutput\setStoragePath
setStoragePath( $storagePath)
Stable to override.
Definition: MediaTransformOutput.php:116
FileBackend\isStoragePath
static isStoragePath( $path)
Check if a given path is a "mwstore://" path.
Definition: FileBackend.php:1528
Status\wrap
static wrap( $sv)
Succinct helper method to wrap a StatusValue.
Definition: Status.php:62
MediaTransformOutput\streamFileWithStatus
streamFileWithStatus( $headers=[])
Stream the file if there were no errors.
Definition: MediaTransformOutput.php:209
$title
$title
Definition: testCompression.php:38
MediaTransformOutput\getDescLinkAttribs
getDescLinkAttribs( $title=null, $params=[])
Definition: MediaTransformOutput.php:253
MediaTransformOutput\$height
int $height
Image height.
Definition: MediaTransformOutput.php:43
MediaTransformOutput\$storagePath
bool string $storagePath
Permanent storage path.
Definition: MediaTransformOutput.php:58
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
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:186
MediaTransformOutput
Base class for the output of MediaHandler::doTransform() and File::transform().
Definition: MediaTransformOutput.php:30
MediaTransformOutput\$path
bool string $path
Filesystem path to the thumb.
Definition: MediaTransformOutput.php:52
MediaTransformOutput\getHeight
getHeight()
Definition: MediaTransformOutput.php:70
MediaTransformOutput\$width
int $width
Image width.
Definition: MediaTransformOutput.php:40
MediaTransformOutput\$page
bool string $page
Definition: MediaTransformOutput.php:49
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:346