MediaWiki master
MediaTransformOutput.php
Go to the documentation of this file.
1<?php
2
29
36abstract class MediaTransformOutput {
40 public $responsiveUrls = [];
41
43 protected $file;
44
46 protected $width;
47
49 protected $height;
50
52 protected $url;
53
55 protected $page;
56
58 protected $path;
59
61 protected $lang;
62
64 protected $storagePath = false;
65
69 public function getWidth() {
70 return $this->width;
71 }
72
76 public function getHeight() {
77 return $this->height;
78 }
79
83 public function getFile() {
84 return $this->file;
85 }
86
94 public function getExtension() {
95 return $this->path ? FileBackend::extensionFromPath( $this->path ) : false;
96 }
97
103 public function getUrl() {
104 return $this->url;
105 }
106
112 public function getStoragePath() {
113 return $this->storagePath;
114 }
115
122 public function setStoragePath( $storagePath ) {
123 $this->storagePath = $storagePath;
124 if ( $this->path === false ) {
125 $this->path = $storagePath;
126 }
127 }
128
149 abstract public function toHtml( $options = [] );
150
155 public function isError() {
156 return false;
157 }
158
170 public function hasFile() {
171 // If TRANSFORM_LATER, $this->path will be false.
172 // Note: a null path means "use the source file".
173 return ( !$this->isError() && ( $this->path || $this->path === null ) );
174 }
175
182 public function fileIsSource() {
183 return ( !$this->isError() && $this->path === null );
184 }
185
192 public function getLocalCopyPath() {
193 if ( $this->isError() ) {
194 return false;
195 }
196
197 if ( $this->path === null ) {
198 return $this->file->getLocalRefPath(); // assume thumb was not scaled
199 }
200 if ( FileBackend::isStoragePath( $this->path ) ) {
201 $be = $this->file->getRepo()->getBackend();
202 // The temp file will be process cached by FileBackend
203 $fsFile = $be->getLocalReference( [ 'src' => $this->path ] );
204
205 return $fsFile ? $fsFile->getPath() : false;
206 }
207 return $this->path; // may return false
208 }
209
217 public function streamFileWithStatus( $headers = [] ) {
218 if ( !$this->path ) {
219 return Status::newFatal( 'backend-fail-stream', '<no path>' );
220 }
221 if ( FileBackend::isStoragePath( $this->path ) ) {
222 $be = $this->file->getRepo()->getBackend();
223 return Status::wrap(
224 $be->streamFile( [ 'src' => $this->path, 'headers' => $headers ] ) );
225 }
226 // FS-file
227 $success = StreamFile::stream( $this->getLocalCopyPath(), $headers );
228 return $success ? Status::newGood() : Status::newFatal( 'backend-fail-stream', $this->path );
229 }
230
238 public function streamFile( $headers = [] ) {
239 return $this->streamFileWithStatus( $headers )->isOK();
240 }
241
250 protected function linkWrap( $linkAttribs, $contents ) {
251 if ( isset( $linkAttribs['href'] ) ) {
252 return Xml::tags( 'a', $linkAttribs, $contents );
253 }
254 $parserEnableLegacyMediaDOM = MediaWikiServices::getInstance()
255 ->getMainConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM );
256 if ( $parserEnableLegacyMediaDOM ) {
257 return $contents;
258 }
259 return Xml::tags( 'span', $linkAttribs ?: null, $contents );
260 }
261
267 public function getDescLinkAttribs( $title = null, $params = [] ) {
268 if ( is_array( $params ) ) {
269 $query = $params;
270 } else {
271 $query = [];
272 }
273 if ( $this->page && $this->page !== 1 ) {
274 $query['page'] = $this->page;
275 }
276 if ( $this->lang ) {
277 $query['lang'] = $this->lang;
278 }
279
280 if ( is_string( $params ) && $params !== '' ) {
281 $query = $params . '&' . wfArrayToCgi( $query );
282 }
283
284 $attribs = [
285 'href' => $this->file->getTitle()->getLocalURL( $query ),
286 ];
287
288 $parserEnableLegacyMediaDOM = MediaWikiServices::getInstance()
289 ->getMainConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM );
290 if ( $parserEnableLegacyMediaDOM ) {
291 $attribs['class'] = 'image';
292 } else {
293 $attribs['class'] = 'mw-file-description';
294 }
295
296 if ( $title ) {
297 $attribs['title'] = $title;
298 }
299
300 return $attribs;
301 }
302}
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
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.
Functions related to the output of file content.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54