MediaWiki 1.44.0
MediaTransformOutput.php
Go to the documentation of this file.
1<?php
2
32
39abstract class MediaTransformOutput {
43 public $responsiveUrls = [];
44
46 protected $file;
47
49 protected $width;
50
52 protected $height;
53
55 protected $url;
56
58 protected $page;
59
61 protected $path;
62
64 protected $lang;
65
67 protected $storagePath = false;
68
72 public function getWidth() {
73 return $this->width;
74 }
75
79 public function getHeight() {
80 return $this->height;
81 }
82
86 public function getFile() {
87 return $this->file;
88 }
89
97 public function getExtension() {
98 return $this->path ? FileBackend::extensionFromPath( $this->path ) : false;
99 }
100
106 public function getUrl() {
107 return $this->url;
108 }
109
115 public function getStoragePath() {
116 return $this->storagePath;
117 }
118
125 public function setStoragePath( $storagePath ) {
126 $this->storagePath = $storagePath;
127 if ( $this->path === false ) {
128 $this->path = $storagePath;
129 }
130 }
131
152 abstract public function toHtml( $options = [] );
153
158 public function isError() {
159 return false;
160 }
161
173 public function hasFile() {
174 // If TRANSFORM_LATER, $this->path will be false.
175 // Note: a null path means "use the source file".
176 return ( !$this->isError() && ( $this->path || $this->path === null ) );
177 }
178
185 public function fileIsSource() {
186 return ( !$this->isError() && $this->path === null );
187 }
188
195 public function getLocalCopyPath() {
196 if ( $this->isError() ) {
197 return false;
198 }
199
200 if ( $this->path === null ) {
201 // assume thumb was not scaled
202 return $this->file->getLocalRefPath();
203 }
204 if ( FileBackend::isStoragePath( $this->path ) ) {
205 $be = $this->file->getRepo()->getBackend();
206 // The temp file is process-cached by FileBackend
207 $fsFile = $be->getLocalReference( [ 'src' => $this->path ] );
208
209 return $fsFile ? $fsFile->getPath() : false;
210 }
211 // may return false
212 return $this->path;
213 }
214
222 public function streamFileWithStatus( $headers = [] ) {
223 if ( !$this->path ) {
224 return Status::newFatal( 'backend-fail-stream', '<no path>' );
225 }
226
227 $repo = $this->file->getRepo();
228
229 if ( $repo && FileBackend::isStoragePath( $this->path ) ) {
230 return Status::wrap(
231 $repo->getBackend()->streamFile(
232 [ 'src' => $this->path, 'headers' => $headers, ]
233 )
234 );
235 }
236
237 $streamer = new HTTPFileStreamer(
238 $this->getLocalCopyPath(),
239 $repo ? $repo->getBackend()->getStreamerOptions() : []
240 );
241
242 $success = $streamer->stream( $headers );
243
244 return $success ? Status::newGood()
245 : Status::newFatal( 'backend-fail-stream', $this->path );
246 }
247
255 public function streamFile( $headers = [] ) {
256 return $this->streamFileWithStatus( $headers )->isOK();
257 }
258
267 protected function linkWrap( $linkAttribs, $contents ) {
268 if ( isset( $linkAttribs['href'] ) ) {
269 return Html::rawElement( 'a', $linkAttribs, $contents );
270 }
271 $parserEnableLegacyMediaDOM = MediaWikiServices::getInstance()
272 ->getMainConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM );
273 if ( $parserEnableLegacyMediaDOM ) {
274 return $contents;
275 }
276 return Html::rawElement( 'span', $linkAttribs ?: null, $contents );
277 }
278
284 public function getDescLinkAttribs( $title = null, $params = [] ) {
285 if ( is_array( $params ) ) {
286 $query = $params;
287 } else {
288 $query = [];
289 }
290 if ( $this->page && $this->page !== 1 ) {
291 $query['page'] = $this->page;
292 }
293 if ( $this->lang ) {
294 $query['lang'] = $this->lang;
295 }
296
297 if ( is_string( $params ) && $params !== '' ) {
298 $query = $params . '&' . wfArrayToCgi( $query );
299 }
300
301 $attribs = [
302 'href' => $this->file->getTitle()->getLocalURL( $query ),
303 ];
304
305 $parserEnableLegacyMediaDOM = MediaWikiServices::getInstance()
306 ->getMainConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM );
307 if ( $parserEnableLegacyMediaDOM ) {
308 $attribs['class'] = 'image';
309 } else {
310 $attribs['class'] = 'mw-file-description';
311 }
312
313 if ( $title ) {
314 $attribs['title'] = $title;
315 }
316
317 return $attribs;
318 }
319}
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e....
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.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:93
This class is a collection of static functions that serve two purposes:
Definition Html.php:57
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
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.