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 return $this->url;
94 }
95
101 public function getStoragePath() {
102 return $this->storagePath;
103 }
104
111 public function setStoragePath( $storagePath ) {
112 $this->storagePath = $storagePath;
113 if ( $this->path === false ) {
114 $this->path = $storagePath;
115 }
116 }
117
138 abstract public function toHtml( $options = [] );
139
144 public function isError() {
145 return false;
146 }
147
159 public function hasFile() {
160 // If TRANSFORM_LATER, $this->path will be false.
161 // Note: a null path means "use the source file".
162 return ( !$this->isError() && ( $this->path || $this->path === null ) );
163 }
164
171 public function fileIsSource() {
172 return ( !$this->isError() && $this->path === null );
173 }
174
181 public function getLocalCopyPath() {
182 if ( $this->isError() ) {
183 return false;
184 }
185
186 if ( $this->path === null ) {
187 // assume thumb was not scaled
188 return $this->file->getLocalRefPath();
189 }
190 if ( FileBackend::isStoragePath( $this->path ) ) {
191 $be = $this->file->getRepo()->getBackend();
192 // The temp file is process-cached by FileBackend
193 $fsFile = $be->getLocalReference( [ 'src' => $this->path ] );
194
195 return $fsFile ? $fsFile->getPath() : false;
196 }
197 // may return false
198 return $this->path;
199 }
200
208 public function streamFileWithStatus( $headers = [] ) {
209 if ( !$this->path ) {
210 return Status::newFatal( 'backend-fail-stream', '<no path>' );
211 }
212
213 $repo = $this->file->getRepo();
214
215 if ( $repo && FileBackend::isStoragePath( $this->path ) ) {
216 return Status::wrap(
217 $repo->getBackend()->streamFile(
218 [ 'src' => $this->path, 'headers' => $headers, ]
219 )
220 );
221 }
222
223 $streamer = new HTTPFileStreamer(
224 $this->getLocalCopyPath(),
225 $repo ? $repo->getBackend()->getStreamerOptions() : []
226 );
227
228 $success = $streamer->stream( $headers );
229
230 return $success ? Status::newGood()
231 : Status::newFatal( 'backend-fail-stream', $this->path );
232 }
233
241 public function streamFile( $headers = [] ) {
242 return $this->streamFileWithStatus( $headers )->isOK();
243 }
244
253 protected function linkWrap( $linkAttribs, $contents ) {
254 if ( isset( $linkAttribs['href'] ) ) {
255 return Html::rawElement( 'a', $linkAttribs, $contents );
256 }
257 return Html::rawElement( 'span', $linkAttribs ?: [], $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 'class' => 'mw-file-description',
285 ];
286
287 if ( $title ) {
288 $attribs['title'] = $title;
289 }
290
291 return $attribs;
292 }
293}
294
296class_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:79
This class is a collection of static functions that serve two purposes:
Definition Html.php:43
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.