MediaWiki master
MediaTransformOutput.php
Go to the documentation of this file.
1<?php
2
30
37abstract class MediaTransformOutput {
41 public $responsiveUrls = [];
42
44 protected $file;
45
47 protected $width;
48
50 protected $height;
51
53 protected $url;
54
56 protected $page;
57
59 protected $path;
60
62 protected $lang;
63
65 protected $storagePath = false;
66
70 public function getWidth() {
71 return $this->width;
72 }
73
77 public function getHeight() {
78 return $this->height;
79 }
80
84 public function getFile() {
85 return $this->file;
86 }
87
95 public function getExtension() {
96 return $this->path ? FileBackend::extensionFromPath( $this->path ) : false;
97 }
98
104 public function getUrl() {
105 return $this->url;
106 }
107
113 public function getStoragePath() {
114 return $this->storagePath;
115 }
116
123 public function setStoragePath( $storagePath ) {
124 $this->storagePath = $storagePath;
125 if ( $this->path === false ) {
126 $this->path = $storagePath;
127 }
128 }
129
150 abstract public function toHtml( $options = [] );
151
156 public function isError() {
157 return false;
158 }
159
171 public function hasFile() {
172 // If TRANSFORM_LATER, $this->path will be false.
173 // Note: a null path means "use the source file".
174 return ( !$this->isError() && ( $this->path || $this->path === null ) );
175 }
176
183 public function fileIsSource() {
184 return ( !$this->isError() && $this->path === null );
185 }
186
193 public function getLocalCopyPath() {
194 if ( $this->isError() ) {
195 return false;
196 }
197
198 if ( $this->path === null ) {
199 return $this->file->getLocalRefPath(); // assume thumb was not scaled
200 }
201 if ( FileBackend::isStoragePath( $this->path ) ) {
202 $be = $this->file->getRepo()->getBackend();
203 // The temp file will be process cached by FileBackend
204 $fsFile = $be->getLocalReference( [ 'src' => $this->path ] );
205
206 return $fsFile ? $fsFile->getPath() : false;
207 }
208 return $this->path; // may return false
209 }
210
218 public function streamFileWithStatus( $headers = [] ) {
219 if ( !$this->path ) {
220 return Status::newFatal( 'backend-fail-stream', '<no path>' );
221 }
222
223 $repo = $this->file->getRepo();
224
225 if ( $repo && FileBackend::isStoragePath( $this->path ) ) {
226 return Status::wrap(
227 $repo->getBackend()->streamFile(
228 [ 'src' => $this->path, 'headers' => $headers, ]
229 )
230 );
231 } else {
232 $streamer = new HTTPFileStreamer(
233 $this->getLocalCopyPath(),
234 $repo ? $repo->getBackend()->getStreamerOptions() : []
235 );
236
237 $success = $streamer->stream( $headers );
238 return $success ? Status::newGood()
239 : Status::newFatal( 'backend-fail-stream', $this->path );
240 }
241 }
242
250 public function streamFile( $headers = [] ) {
251 return $this->streamFileWithStatus( $headers )->isOK();
252 }
253
262 protected function linkWrap( $linkAttribs, $contents ) {
263 if ( isset( $linkAttribs['href'] ) ) {
264 return Xml::tags( 'a', $linkAttribs, $contents );
265 }
266 $parserEnableLegacyMediaDOM = MediaWikiServices::getInstance()
267 ->getMainConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM );
268 if ( $parserEnableLegacyMediaDOM ) {
269 return $contents;
270 }
271 return Xml::tags( 'span', $linkAttribs ?: null, $contents );
272 }
273
279 public function getDescLinkAttribs( $title = null, $params = [] ) {
280 if ( is_array( $params ) ) {
281 $query = $params;
282 } else {
283 $query = [];
284 }
285 if ( $this->page && $this->page !== 1 ) {
286 $query['page'] = $this->page;
287 }
288 if ( $this->lang ) {
289 $query['lang'] = $this->lang;
290 }
291
292 if ( is_string( $params ) && $params !== '' ) {
293 $query = $params . '&' . wfArrayToCgi( $query );
294 }
295
296 $attribs = [
297 'href' => $this->file->getTitle()->getLocalURL( $query ),
298 ];
299
300 $parserEnableLegacyMediaDOM = MediaWikiServices::getInstance()
301 ->getMainConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM );
302 if ( $parserEnableLegacyMediaDOM ) {
303 $attribs['class'] = 'image';
304 } else {
305 $attribs['class'] = 'mw-file-description';
306 }
307
308 if ( $title ) {
309 $attribs['title'] = $title;
310 }
311
312 return $attribs;
313 }
314}
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e....
array $params
The job parameters.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:74
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.
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.
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).