MediaWiki  master
MediaTransformOutput.php
Go to the documentation of this file.
1 <?php
2 
26 
33 abstract class MediaTransformOutput {
37  public $responsiveUrls = [];
38 
40  protected $file;
41 
43  protected $width;
44 
46  protected $height;
47 
49  protected $url;
50 
52  protected $page;
53 
55  protected $path;
56 
58  protected $lang;
59 
61  protected $storagePath = false;
62 
66  public function getWidth() {
67  return $this->width;
68  }
69 
73  public function getHeight() {
74  return $this->height;
75  }
76 
80  public function getFile() {
81  return $this->file;
82  }
83 
91  public function getExtension() {
92  return $this->path ? FileBackend::extensionFromPath( $this->path ) : false;
93  }
94 
100  public function getUrl() {
101  return $this->url;
102  }
103 
109  public function getStoragePath() {
110  return $this->storagePath;
111  }
112 
119  public function setStoragePath( $storagePath ) {
120  $this->storagePath = $storagePath;
121  if ( $this->path === false ) {
122  $this->path = $storagePath;
123  }
124  }
125 
146  abstract public function toHtml( $options = [] );
147 
152  public function isError() {
153  return false;
154  }
155 
167  public function hasFile() {
168  // If TRANSFORM_LATER, $this->path will be false.
169  // Note: a null path means "use the source file".
170  return ( !$this->isError() && ( $this->path || $this->path === null ) );
171  }
172 
179  public function fileIsSource() {
180  return ( !$this->isError() && $this->path === null );
181  }
182 
189  public function getLocalCopyPath() {
190  if ( $this->isError() ) {
191  return false;
192  } elseif ( $this->path === null ) {
193  return $this->file->getLocalRefPath(); // assume thumb was not scaled
194  } elseif ( FileBackend::isStoragePath( $this->path ) ) {
195  $be = $this->file->getRepo()->getBackend();
196  // The temp file will be process cached by FileBackend
197  $fsFile = $be->getLocalReference( [ 'src' => $this->path ] );
198 
199  return $fsFile ? $fsFile->getPath() : false;
200  } else {
201  return $this->path; // may return false
202  }
203  }
204 
212  public function streamFileWithStatus( $headers = [] ) {
213  if ( !$this->path ) {
214  return Status::newFatal( 'backend-fail-stream', '<no path>' );
215  } elseif ( FileBackend::isStoragePath( $this->path ) ) {
216  $be = $this->file->getRepo()->getBackend();
217  return Status::wrap(
218  $be->streamFile( [ 'src' => $this->path, 'headers' => $headers ] ) );
219  } else { // FS-file
220  $success = StreamFile::stream( $this->getLocalCopyPath(), $headers );
221  return $success ? Status::newGood() : Status::newFatal( 'backend-fail-stream', $this->path );
222  }
223  }
224 
232  public function streamFile( $headers = [] ) {
233  return $this->streamFileWithStatus( $headers )->isOK();
234  }
235 
244  protected function linkWrap( $linkAttribs, $contents ) {
245  if ( $linkAttribs ) {
246  return Xml::tags( 'a', $linkAttribs, $contents );
247  } else {
248  $parserEnableLegacyMediaDOM = MediaWikiServices::getInstance()
249  ->getMainConfig()->get( 'ParserEnableLegacyMediaDOM' );
250  if ( $parserEnableLegacyMediaDOM ) {
251  return $contents;
252  } else {
253  return Xml::tags( 'span', null, $contents );
254  }
255  }
256  }
257 
263  public function getDescLinkAttribs( $title = null, $params = [] ) {
264  if ( is_array( $params ) ) {
265  $query = $params;
266  } else {
267  $query = [];
268  }
269  if ( $this->page && $this->page !== 1 ) {
270  $query['page'] = $this->page;
271  }
272  if ( $this->lang ) {
273  $query['lang'] = $this->lang;
274  }
275 
276  if ( is_string( $params ) && $params !== '' ) {
277  $query = $params . '&' . wfArrayToCgi( $query );
278  }
279 
280  $attribs = [
281  'href' => $this->file->getTitle()->getLocalURL( $query ),
282  ];
283 
284  $parserEnableLegacyMediaDOM = MediaWikiServices::getInstance()
285  ->getMainConfig()->get( 'ParserEnableLegacyMediaDOM' );
286  if ( $parserEnableLegacyMediaDOM ) {
287  $attribs['class'] = 'image';
288  }
289 
290  if ( $title ) {
291  $attribs['title'] = $title;
292  }
293 
294  return $attribs;
295  }
296 }
MediaTransformOutput\$responsiveUrls
array $responsiveUrls
Associative array mapping optional supplementary image files from pixel density (eg 1....
Definition: MediaTransformOutput.php:37
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:70
MediaTransformOutput\getFile
getFile()
Definition: MediaTransformOutput.php:80
MediaTransformOutput\isError
isError()
This will be overridden to return true in error classes.
Definition: MediaTransformOutput.php:152
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:203
MediaTransformOutput\getStoragePath
getStoragePath()
Definition: MediaTransformOutput.php:109
MediaTransformOutput\$file
File $file
Definition: MediaTransformOutput.php:40
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:1584
$success
$success
Definition: NoLocalSettings.php:42
MediaTransformOutput\getWidth
getWidth()
Definition: MediaTransformOutput.php:66
MediaTransformOutput\$url
string $url
URL path to the thumb.
Definition: MediaTransformOutput.php:49
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:50
MediaTransformOutput\hasFile
hasFile()
Check if an output thumbnail file actually exists.
Definition: MediaTransformOutput.php:167
MediaTransformOutput\getUrl
getUrl()
Definition: MediaTransformOutput.php:100
MediaTransformOutput\streamFile
streamFile( $headers=[])
Stream the file if there were no errors.
Definition: MediaTransformOutput.php:232
MediaTransformOutput\$lang
bool string $lang
Language code, false if not set.
Definition: MediaTransformOutput.php:58
MediaTransformOutput\linkWrap
linkWrap( $linkAttribs, $contents)
Wrap some XHTML text in an anchor tag with the given attributes or, fallback to a span in the absence...
Definition: MediaTransformOutput.php:244
MediaTransformOutput\getExtension
getExtension()
Get the final extension of the thumbnail.
Definition: MediaTransformOutput.php:91
MediaTransformOutput\fileIsSource
fileIsSource()
Check if the output thumbnail is the same as the source.
Definition: MediaTransformOutput.php:179
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:67
MediaTransformOutput\setStoragePath
setStoragePath( $storagePath)
Definition: MediaTransformOutput.php:119
FileBackend\isStoragePath
static isStoragePath( $path)
Check if a given path is a "mwstore://" path.
Definition: FileBackend.php:1510
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:212
$title
$title
Definition: testCompression.php:38
MediaTransformOutput\getDescLinkAttribs
getDescLinkAttribs( $title=null, $params=[])
Definition: MediaTransformOutput.php:263
MediaTransformOutput\$height
int $height
Image height.
Definition: MediaTransformOutput.php:46
MediaTransformOutput\$storagePath
bool string $storagePath
Permanent storage path.
Definition: MediaTransformOutput.php:61
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:133
MediaTransformOutput\getLocalCopyPath
getLocalCopyPath()
Get the path of a file system copy of the thumbnail.
Definition: MediaTransformOutput.php:189
MediaTransformOutput
Base class for the output of MediaHandler::doTransform() and File::transform().
Definition: MediaTransformOutput.php:33
MediaTransformOutput\$path
bool string $path
Filesystem path to the thumb.
Definition: MediaTransformOutput.php:55
MediaTransformOutput\getHeight
getHeight()
Definition: MediaTransformOutput.php:73
MediaTransformOutput\$width
int $width
Image width.
Definition: MediaTransformOutput.php:43
MediaTransformOutput\$page
bool string $page
Definition: MediaTransformOutput.php:52
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:330