MediaWiki master
MediaTransformOutput.php
Go to the documentation of this file.
1<?php
2
28
35abstract class MediaTransformOutput {
39 public $responsiveUrls = [];
40
42 protected $file;
43
45 protected $width;
46
48 protected $height;
49
51 protected $url;
52
54 protected $page;
55
57 protected $path;
58
60 protected $lang;
61
63 protected $storagePath = false;
64
68 public function getWidth() {
69 return $this->width;
70 }
71
75 public function getHeight() {
76 return $this->height;
77 }
78
82 public function getFile() {
83 return $this->file;
84 }
85
93 public function getExtension() {
94 return $this->path ? FileBackend::extensionFromPath( $this->path ) : false;
95 }
96
102 public function getUrl() {
103 return $this->url;
104 }
105
111 public function getStoragePath() {
112 return $this->storagePath;
113 }
114
121 public function setStoragePath( $storagePath ) {
122 $this->storagePath = $storagePath;
123 if ( $this->path === false ) {
124 $this->path = $storagePath;
125 }
126 }
127
148 abstract public function toHtml( $options = [] );
149
154 public function isError() {
155 return false;
156 }
157
169 public function hasFile() {
170 // If TRANSFORM_LATER, $this->path will be false.
171 // Note: a null path means "use the source file".
172 return ( !$this->isError() && ( $this->path || $this->path === null ) );
173 }
174
181 public function fileIsSource() {
182 return ( !$this->isError() && $this->path === null );
183 }
184
191 public function getLocalCopyPath() {
192 if ( $this->isError() ) {
193 return false;
194 }
195
196 if ( $this->path === null ) {
197 return $this->file->getLocalRefPath(); // assume thumb was not scaled
198 }
199 if ( FileBackend::isStoragePath( $this->path ) ) {
200 $be = $this->file->getRepo()->getBackend();
201 // The temp file will be process cached by FileBackend
202 $fsFile = $be->getLocalReference( [ 'src' => $this->path ] );
203
204 return $fsFile ? $fsFile->getPath() : false;
205 }
206 return $this->path; // may return false
207 }
208
216 public function streamFileWithStatus( $headers = [] ) {
217 if ( !$this->path ) {
218 return Status::newFatal( 'backend-fail-stream', '<no path>' );
219 }
220
221 $repo = $this->file->getRepo();
222
223 if ( $repo && FileBackend::isStoragePath( $this->path ) ) {
224 return Status::wrap(
225 $repo->getBackend()->streamFile(
226 [ 'src' => $this->path, 'headers' => $headers, ]
227 )
228 );
229 } else {
230 $streamer = new HTTPFileStreamer(
231 $this->getLocalCopyPath(),
232 $repo ? $repo->getBackend()->getStreamerOptions() : []
233 );
234
235 $success = $streamer->stream( $headers );
236 return $success ? Status::newGood()
237 : Status::newFatal( 'backend-fail-stream', $this->path );
238 }
239 }
240
248 public function streamFile( $headers = [] ) {
249 return $this->streamFileWithStatus( $headers )->isOK();
250 }
251
260 protected function linkWrap( $linkAttribs, $contents ) {
261 if ( isset( $linkAttribs['href'] ) ) {
262 return Xml::tags( 'a', $linkAttribs, $contents );
263 }
264 $parserEnableLegacyMediaDOM = MediaWikiServices::getInstance()
265 ->getMainConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM );
266 if ( $parserEnableLegacyMediaDOM ) {
267 return $contents;
268 }
269 return Xml::tags( 'span', $linkAttribs ?: null, $contents );
270 }
271
277 public function getDescLinkAttribs( $title = null, $params = [] ) {
278 if ( is_array( $params ) ) {
279 $query = $params;
280 } else {
281 $query = [];
282 }
283 if ( $this->page && $this->page !== 1 ) {
284 $query['page'] = $this->page;
285 }
286 if ( $this->lang ) {
287 $query['lang'] = $this->lang;
288 }
289
290 if ( is_string( $params ) && $params !== '' ) {
291 $query = $params . '&' . wfArrayToCgi( $query );
292 }
293
294 $attribs = [
295 'href' => $this->file->getTitle()->getLocalURL( $query ),
296 ];
297
298 $parserEnableLegacyMediaDOM = MediaWikiServices::getInstance()
299 ->getMainConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM );
300 if ( $parserEnableLegacyMediaDOM ) {
301 $attribs['class'] = 'image';
302 } else {
303 $attribs['class'] = 'mw-file-description';
304 }
305
306 if ( $title ) {
307 $attribs['title'] = $title;
308 }
309
310 return $attribs;
311 }
312}
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.
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.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:73
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