MediaWiki master
MediaTransformOutput.php
Go to the documentation of this file.
1<?php
2
31
38abstract class MediaTransformOutput {
42 public $responsiveUrls = [];
43
45 protected $file;
46
48 protected $width;
49
51 protected $height;
52
54 protected $url;
55
57 protected $page;
58
60 protected $path;
61
63 protected $lang;
64
66 protected $storagePath = false;
67
71 public function getWidth() {
72 return $this->width;
73 }
74
78 public function getHeight() {
79 return $this->height;
80 }
81
85 public function getFile() {
86 return $this->file;
87 }
88
96 public function getExtension() {
97 return $this->path ? FileBackend::extensionFromPath( $this->path ) : false;
98 }
99
105 public function getUrl() {
106 return $this->url;
107 }
108
114 public function getStoragePath() {
115 return $this->storagePath;
116 }
117
124 public function setStoragePath( $storagePath ) {
125 $this->storagePath = $storagePath;
126 if ( $this->path === false ) {
127 $this->path = $storagePath;
128 }
129 }
130
151 abstract public function toHtml( $options = [] );
152
157 public function isError() {
158 return false;
159 }
160
172 public function hasFile() {
173 // If TRANSFORM_LATER, $this->path will be false.
174 // Note: a null path means "use the source file".
175 return ( !$this->isError() && ( $this->path || $this->path === null ) );
176 }
177
184 public function fileIsSource() {
185 return ( !$this->isError() && $this->path === null );
186 }
187
194 public function getLocalCopyPath() {
195 if ( $this->isError() ) {
196 return false;
197 }
198
199 if ( $this->path === null ) {
200 return $this->file->getLocalRefPath(); // assume thumb was not scaled
201 }
202 if ( FileBackend::isStoragePath( $this->path ) ) {
203 $be = $this->file->getRepo()->getBackend();
204 // The temp file will be process cached by FileBackend
205 $fsFile = $be->getLocalReference( [ 'src' => $this->path ] );
206
207 return $fsFile ? $fsFile->getPath() : false;
208 }
209 return $this->path; // may return false
210 }
211
219 public function streamFileWithStatus( $headers = [] ) {
220 if ( !$this->path ) {
221 return Status::newFatal( 'backend-fail-stream', '<no path>' );
222 }
223
224 $repo = $this->file->getRepo();
225
226 if ( $repo && FileBackend::isStoragePath( $this->path ) ) {
227 return Status::wrap(
228 $repo->getBackend()->streamFile(
229 [ 'src' => $this->path, 'headers' => $headers, ]
230 )
231 );
232 } else {
233 $streamer = new HTTPFileStreamer(
234 $this->getLocalCopyPath(),
235 $repo ? $repo->getBackend()->getStreamerOptions() : []
236 );
237
238 $success = $streamer->stream( $headers );
239 return $success ? Status::newGood()
240 : Status::newFatal( 'backend-fail-stream', $this->path );
241 }
242 }
243
251 public function streamFile( $headers = [] ) {
252 return $this->streamFileWithStatus( $headers )->isOK();
253 }
254
263 protected function linkWrap( $linkAttribs, $contents ) {
264 if ( isset( $linkAttribs['href'] ) ) {
265 return Xml::tags( 'a', $linkAttribs, $contents );
266 }
267 $parserEnableLegacyMediaDOM = MediaWikiServices::getInstance()
268 ->getMainConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM );
269 if ( $parserEnableLegacyMediaDOM ) {
270 return $contents;
271 }
272 return Xml::tags( 'span', $linkAttribs ?: null, $contents );
273 }
274
280 public function getDescLinkAttribs( $title = null, $params = [] ) {
281 if ( is_array( $params ) ) {
282 $query = $params;
283 } else {
284 $query = [];
285 }
286 if ( $this->page && $this->page !== 1 ) {
287 $query['page'] = $this->page;
288 }
289 if ( $this->lang ) {
290 $query['lang'] = $this->lang;
291 }
292
293 if ( is_string( $params ) && $params !== '' ) {
294 $query = $params . '&' . wfArrayToCgi( $query );
295 }
296
297 $attribs = [
298 'href' => $this->file->getTitle()->getLocalURL( $query ),
299 ];
300
301 $parserEnableLegacyMediaDOM = MediaWikiServices::getInstance()
302 ->getMainConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM );
303 if ( $parserEnableLegacyMediaDOM ) {
304 $attribs['class'] = 'image';
305 } else {
306 $attribs['class'] = 'mw-file-description';
307 }
308
309 if ( $title ) {
310 $attribs['title'] = $title;
311 }
312
313 return $attribs;
314 }
315}
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:79
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).
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.