MediaWiki master
MediaTransformOutput.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Media;
4
18
25abstract class MediaTransformOutput {
29 public $responsiveUrls = [];
30
32 protected $file;
33
35 protected $width;
36
38 protected $height;
39
41 protected $url;
42
44 protected $page;
45
47 protected $path;
48
50 protected $lang;
51
53 protected $storagePath = false;
54
58 public function getWidth() {
59 return $this->width;
60 }
61
65 public function getHeight() {
66 return $this->height;
67 }
68
72 public function getFile() {
73 return $this->file;
74 }
75
83 public function getExtension() {
84 return $this->path ? FileBackend::extensionFromPath( $this->path ) : false;
85 }
86
92 public function getUrl() {
93 if ( $this->url === false ) {
94 return false;
95 }
96
97 return $this->getFile()->appendRequestProvenance( $this->url, [
98 'format' => 'thumbnail',
99 ] );
100 }
101
107 public function getStoragePath() {
108 return $this->storagePath;
109 }
110
117 public function setStoragePath( $storagePath ) {
118 $this->storagePath = $storagePath;
119 if ( $this->path === false ) {
120 $this->path = $storagePath;
121 }
122 }
123
144 abstract public function toHtml( $options = [] );
145
150 public function isError() {
151 return false;
152 }
153
165 public function hasFile() {
166 // If TRANSFORM_LATER, $this->path will be false.
167 // Note: a null path means "use the source file".
168 return ( !$this->isError() && ( $this->path || $this->path === null ) );
169 }
170
177 public function fileIsSource() {
178 return ( !$this->isError() && $this->path === null );
179 }
180
187 public function getLocalCopyPath() {
188 if ( $this->isError() ) {
189 return false;
190 }
191
192 if ( $this->path === null ) {
193 // assume thumb was not scaled
194 return $this->file->getLocalRefPath();
195 }
196 if ( FileBackend::isStoragePath( $this->path ) ) {
197 $be = $this->file->getRepo()->getBackend();
198 // The temp file is process-cached by FileBackend
199 $fsFile = $be->getLocalReference( [ 'src' => $this->path ] );
200
201 return $fsFile ? $fsFile->getPath() : false;
202 }
203 // may return false
204 return $this->path;
205 }
206
214 public function streamFileWithStatus( $headers = [] ) {
215 if ( !$this->path ) {
216 return Status::newFatal( 'backend-fail-stream', '<no path>' );
217 }
218
219 $repo = $this->file->getRepo();
220
221 if ( $repo && FileBackend::isStoragePath( $this->path ) ) {
222 return Status::wrap(
223 $repo->getBackend()->streamFile(
224 [ 'src' => $this->path, 'headers' => $headers, ]
225 )
226 );
227 }
228
229 $streamer = new HTTPFileStreamer(
230 $this->getLocalCopyPath(),
231 $repo ? $repo->getBackend()->getStreamerOptions() : []
232 );
233
234 $success = $streamer->stream( $headers );
235
236 return $success ? Status::newGood()
237 : Status::newFatal( 'backend-fail-stream', $this->path );
238 }
239
247 public function streamFile( $headers = [] ) {
248 return $this->streamFileWithStatus( $headers )->isOK();
249 }
250
259 protected function linkWrap( $linkAttribs, $contents ) {
260 if ( isset( $linkAttribs['href'] ) ) {
261 return Html::rawElement( 'a', $linkAttribs, $contents );
262 }
263 return Html::rawElement( 'span', $linkAttribs ?: [], $contents );
264 }
265
271 public function getDescLinkAttribs( $title = null, $params = [] ) {
272 if ( is_array( $params ) ) {
273 $query = $params;
274 } else {
275 $query = [];
276 }
277 if ( $this->page && $this->page !== 1 ) {
278 $query['page'] = $this->page;
279 }
280 if ( $this->lang ) {
281 $query['lang'] = $this->lang;
282 }
283
284 if ( is_string( $params ) && $params !== '' ) {
285 $query = $params . '&' . wfArrayToCgi( $query );
286 }
287
288 $attribs = [
289 'href' => $this->file->getTitle()->getLocalURL( $query ),
290 'class' => 'mw-file-description',
291 ];
292
293 if ( $title ) {
294 $attribs['title'] = $title;
295 }
296
297 return $attribs;
298 }
299}
300
302class_alias( MediaTransformOutput::class, 'MediaTransformOutput' );
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e....
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:80
This class is a collection of static functions that serve two purposes:
Definition Html.php:44
Base class for the output of MediaHandler::doTransform() and File::transform().
linkWrap( $linkAttribs, $contents)
Wrap some XHTML text in an anchor tag with the given attributes or, fallback to a span in the absence...
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.
isError()
This will be overridden to return true in error classes.
fileIsSource()
Check if the output thumbnail is the same as the source.
string null false $path
Filesystem path to the thumb.
getExtension()
Get the final extension of the thumbnail.
streamFileWithStatus( $headers=[])
Stream the file if there were no errors.
string false $url
URL path to the thumb.
toHtml( $options=[])
Fetch HTML for this transform output.
hasFile()
Check if an output thumbnail file actually exists.
string false $storagePath
Permanent storage path.
getLocalCopyPath()
Get the path of a file system copy of the thumbnail.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
Base class for all file backend classes (including multi-write backends).
static extensionFromPath( $path, $case='lowercase')
Get the final extension from a storage or FS path.
static isStoragePath( $path)
Check if a given path is a "mwstore://" path.
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.