MediaWiki  master
MediaTransformOutput.php
Go to the documentation of this file.
1 <?php
2 
27 
34 abstract class MediaTransformOutput {
38  public $responsiveUrls = [];
39 
41  protected $file;
42 
44  protected $width;
45 
47  protected $height;
48 
50  protected $url;
51 
53  protected $page;
54 
56  protected $path;
57 
59  protected $lang;
60 
62  protected $storagePath = false;
63 
67  public function getWidth() {
68  return $this->width;
69  }
70 
74  public function getHeight() {
75  return $this->height;
76  }
77 
81  public function getFile() {
82  return $this->file;
83  }
84 
92  public function getExtension() {
93  return $this->path ? FileBackend::extensionFromPath( $this->path ) : false;
94  }
95 
101  public function getUrl() {
102  return $this->url;
103  }
104 
110  public function getStoragePath() {
111  return $this->storagePath;
112  }
113 
120  public function setStoragePath( $storagePath ) {
121  $this->storagePath = $storagePath;
122  if ( $this->path === false ) {
123  $this->path = $storagePath;
124  }
125  }
126 
147  abstract public function toHtml( $options = [] );
148 
153  public function isError() {
154  return false;
155  }
156 
168  public function hasFile() {
169  // If TRANSFORM_LATER, $this->path will be false.
170  // Note: a null path means "use the source file".
171  return ( !$this->isError() && ( $this->path || $this->path === null ) );
172  }
173 
180  public function fileIsSource() {
181  return ( !$this->isError() && $this->path === null );
182  }
183 
190  public function getLocalCopyPath() {
191  if ( $this->isError() ) {
192  return false;
193  }
194 
195  if ( $this->path === null ) {
196  return $this->file->getLocalRefPath(); // assume thumb was not scaled
197  }
198  if ( FileBackend::isStoragePath( $this->path ) ) {
199  $be = $this->file->getRepo()->getBackend();
200  // The temp file will be process cached by FileBackend
201  $fsFile = $be->getLocalReference( [ 'src' => $this->path ] );
202 
203  return $fsFile ? $fsFile->getPath() : false;
204  }
205  return $this->path; // may return false
206  }
207 
215  public function streamFileWithStatus( $headers = [] ) {
216  if ( !$this->path ) {
217  return Status::newFatal( 'backend-fail-stream', '<no path>' );
218  }
219  if ( FileBackend::isStoragePath( $this->path ) ) {
220  $be = $this->file->getRepo()->getBackend();
221  return Status::wrap(
222  $be->streamFile( [ 'src' => $this->path, 'headers' => $headers ] ) );
223  }
224  // FS-file
225  $success = StreamFile::stream( $this->getLocalCopyPath(), $headers );
226  return $success ? Status::newGood() : Status::newFatal( 'backend-fail-stream', $this->path );
227  }
228 
236  public function streamFile( $headers = [] ) {
237  return $this->streamFileWithStatus( $headers )->isOK();
238  }
239 
248  protected function linkWrap( $linkAttribs, $contents ) {
249  if ( isset( $linkAttribs['href'] ) ) {
250  return Xml::tags( 'a', $linkAttribs, $contents );
251  }
252  $parserEnableLegacyMediaDOM = MediaWikiServices::getInstance()
253  ->getMainConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM );
254  if ( $parserEnableLegacyMediaDOM ) {
255  return $contents;
256  }
257  return Xml::tags( 'span', $linkAttribs ?: null, $contents );
258  }
259 
265  public function getDescLinkAttribs( $title = null, $params = [] ) {
266  if ( is_array( $params ) ) {
267  $query = $params;
268  } else {
269  $query = [];
270  }
271  if ( $this->page && $this->page !== 1 ) {
272  $query['page'] = $this->page;
273  }
274  if ( $this->lang ) {
275  $query['lang'] = $this->lang;
276  }
277 
278  if ( is_string( $params ) && $params !== '' ) {
279  $query = $params . '&' . wfArrayToCgi( $query );
280  }
281 
282  $attribs = [
283  'href' => $this->file->getTitle()->getLocalURL( $query ),
284  ];
285 
286  $parserEnableLegacyMediaDOM = MediaWikiServices::getInstance()
287  ->getMainConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM );
288  if ( $parserEnableLegacyMediaDOM ) {
289  $attribs['class'] = 'image';
290  } else {
291  $attribs['class'] = 'mw-file-description';
292  }
293 
294  if ( $title ) {
295  $attribs['title'] = $title;
296  }
297 
298  return $attribs;
299  }
300 }
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e....
$success
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.
Base class for the output of MediaHandler::doTransform() and File::transform().
streamFileWithStatus( $headers=[])
Stream the file if there were no errors.
string null false $path
Filesystem 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.
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...
string false $url
URL path to the thumb.
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....
string false $lang
Language code, false if not set.
string false $storagePath
Permanent storage path.
isError()
This will be overridden to return true in error classes.
getDescLinkAttribs( $title=null, $params=[])
toHtml( $options=[])
Fetch HTML for this transform output.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:73
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:85
static wrap( $sv)
Succinct helper method to wrap a StatusValue.
Definition: Status.php:64
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:51
static tags( $element, $attribs, $contents)
Same as Xml::element(), but does not escape contents.
Definition: Xml.php:135