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  } elseif ( $this->path === null ) {
194  return $this->file->getLocalRefPath(); // assume thumb was not scaled
195  } elseif ( FileBackend::isStoragePath( $this->path ) ) {
196  $be = $this->file->getRepo()->getBackend();
197  // The temp file will be process cached by FileBackend
198  $fsFile = $be->getLocalReference( [ 'src' => $this->path ] );
199 
200  return $fsFile ? $fsFile->getPath() : false;
201  } else {
202  return $this->path; // may return false
203  }
204  }
205 
213  public function streamFileWithStatus( $headers = [] ) {
214  if ( !$this->path ) {
215  return Status::newFatal( 'backend-fail-stream', '<no path>' );
216  } elseif ( FileBackend::isStoragePath( $this->path ) ) {
217  $be = $this->file->getRepo()->getBackend();
218  return Status::wrap(
219  $be->streamFile( [ 'src' => $this->path, 'headers' => $headers ] ) );
220  } else { // FS-file
221  $success = StreamFile::stream( $this->getLocalCopyPath(), $headers );
222  return $success ? Status::newGood() : Status::newFatal( 'backend-fail-stream', $this->path );
223  }
224  }
225 
233  public function streamFile( $headers = [] ) {
234  return $this->streamFileWithStatus( $headers )->isOK();
235  }
236 
245  protected function linkWrap( $linkAttribs, $contents ) {
246  if ( isset( $linkAttribs['href'] ) ) {
247  return Xml::tags( 'a', $linkAttribs, $contents );
248  } else {
249  $parserEnableLegacyMediaDOM = MediaWikiServices::getInstance()
250  ->getMainConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM );
251  if ( $parserEnableLegacyMediaDOM ) {
252  return $contents;
253  } else {
254  return Xml::tags( 'span', $linkAttribs ?: null, $contents );
255  }
256  }
257  }
258 
264  public function getDescLinkAttribs( $title = null, $params = [] ) {
265  if ( is_array( $params ) ) {
266  $query = $params;
267  } else {
268  $query = [];
269  }
270  if ( $this->page && $this->page !== 1 ) {
271  $query['page'] = $this->page;
272  }
273  if ( $this->lang ) {
274  $query['lang'] = $this->lang;
275  }
276 
277  if ( is_string( $params ) && $params !== '' ) {
278  $query = $params . '&' . wfArrayToCgi( $query );
279  }
280 
281  $attribs = [
282  'href' => $this->file->getTitle()->getLocalURL( $query ),
283  ];
284 
285  $parserEnableLegacyMediaDOM = MediaWikiServices::getInstance()
286  ->getMainConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM );
287  if ( $parserEnableLegacyMediaDOM ) {
288  $attribs['class'] = 'image';
289  } else {
290  $attribs['class'] = 'mw-file-description';
291  }
292 
293  if ( $title ) {
294  $attribs['title'] = $title;
295  }
296 
297  return $attribs;
298  }
299 }
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:63
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:134