MediaWiki master
MediaTransformOutput.php
Go to the documentation of this file.
1<?php
2
32
39abstract class MediaTransformOutput {
43 public $responsiveUrls = [];
44
46 protected $file;
47
49 protected $width;
50
52 protected $height;
53
55 protected $url;
56
58 protected $page;
59
61 protected $path;
62
64 protected $lang;
65
67 protected $storagePath = false;
68
72 public function getWidth() {
73 return $this->width;
74 }
75
79 public function getHeight() {
80 return $this->height;
81 }
82
86 public function getFile() {
87 return $this->file;
88 }
89
97 public function getExtension() {
98 return $this->path ? FileBackend::extensionFromPath( $this->path ) : false;
99 }
100
106 public function getUrl() {
107 return $this->url;
108 }
109
115 public function getStoragePath() {
116 return $this->storagePath;
117 }
118
125 public function setStoragePath( $storagePath ) {
126 $this->storagePath = $storagePath;
127 if ( $this->path === false ) {
128 $this->path = $storagePath;
129 }
130 }
131
152 abstract public function toHtml( $options = [] );
153
158 public function isError() {
159 return false;
160 }
161
173 public function hasFile() {
174 // If TRANSFORM_LATER, $this->path will be false.
175 // Note: a null path means "use the source file".
176 return ( !$this->isError() && ( $this->path || $this->path === null ) );
177 }
178
185 public function fileIsSource() {
186 return ( !$this->isError() && $this->path === null );
187 }
188
195 public function getLocalCopyPath() {
196 if ( $this->isError() ) {
197 return false;
198 }
199
200 if ( $this->path === null ) {
201 return $this->file->getLocalRefPath(); // assume thumb was not scaled
202 }
203 if ( FileBackend::isStoragePath( $this->path ) ) {
204 $be = $this->file->getRepo()->getBackend();
205 // The temp file will be process cached by FileBackend
206 $fsFile = $be->getLocalReference( [ 'src' => $this->path ] );
207
208 return $fsFile ? $fsFile->getPath() : false;
209 }
210 return $this->path; // may return false
211 }
212
220 public function streamFileWithStatus( $headers = [] ) {
221 if ( !$this->path ) {
222 return Status::newFatal( 'backend-fail-stream', '<no path>' );
223 }
224
225 $repo = $this->file->getRepo();
226
227 if ( $repo && FileBackend::isStoragePath( $this->path ) ) {
228 return Status::wrap(
229 $repo->getBackend()->streamFile(
230 [ 'src' => $this->path, 'headers' => $headers, ]
231 )
232 );
233 } else {
234 $streamer = new HTTPFileStreamer(
235 $this->getLocalCopyPath(),
236 $repo ? $repo->getBackend()->getStreamerOptions() : []
237 );
238
239 $success = $streamer->stream( $headers );
240 return $success ? Status::newGood()
241 : Status::newFatal( 'backend-fail-stream', $this->path );
242 }
243 }
244
252 public function streamFile( $headers = [] ) {
253 return $this->streamFileWithStatus( $headers )->isOK();
254 }
255
264 protected function linkWrap( $linkAttribs, $contents ) {
265 if ( isset( $linkAttribs['href'] ) ) {
266 return Xml::tags( 'a', $linkAttribs, $contents );
267 }
268 $parserEnableLegacyMediaDOM = MediaWikiServices::getInstance()
269 ->getMainConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM );
270 if ( $parserEnableLegacyMediaDOM ) {
271 return $contents;
272 }
273 return Xml::tags( 'span', $linkAttribs ?: null, $contents );
274 }
275
281 public function getDescLinkAttribs( $title = null, $params = [] ) {
282 if ( is_array( $params ) ) {
283 $query = $params;
284 } else {
285 $query = [];
286 }
287 if ( $this->page && $this->page !== 1 ) {
288 $query['page'] = $this->page;
289 }
290 if ( $this->lang ) {
291 $query['lang'] = $this->lang;
292 }
293
294 if ( is_string( $params ) && $params !== '' ) {
295 $query = $params . '&' . wfArrayToCgi( $query );
296 }
297
298 $attribs = [
299 'href' => $this->file->getTitle()->getLocalURL( $query ),
300 ];
301
302 $parserEnableLegacyMediaDOM = MediaWikiServices::getInstance()
303 ->getMainConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM );
304 if ( $parserEnableLegacyMediaDOM ) {
305 $attribs['class'] = 'image';
306 } else {
307 $attribs['class'] = 'mw-file-description';
308 }
309
310 if ( $title ) {
311 $attribs['title'] = $title;
312 }
313
314 return $attribs;
315 }
316}
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e....
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.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:93
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Module of static functions for generating XML.
Definition Xml.php:37
Base class for all file backend classes (including multi-write backends).
Functions related to the output of file content.
stream( $headers=[], $sendErrors=true, $optHeaders=[], $flags=0)
Stream a file to the browser, adding all the headings and fun stuff.