MediaWiki  1.30.0
MediaTransformOutput.php
Go to the documentation of this file.
1 <?php
29 abstract class MediaTransformOutput {
33  public $responsiveUrls = [];
34 
36  protected $file;
37 
39  protected $width;
40 
42  protected $height;
43 
45  protected $url;
46 
48  protected $page;
49 
51  protected $path;
52 
54  protected $lang;
55 
57  protected $storagePath = false;
58 
62  public function getWidth() {
63  return $this->width;
64  }
65 
69  public function getHeight() {
70  return $this->height;
71  }
72 
76  public function getFile() {
77  return $this->file;
78  }
79 
85  public function getExtension() {
86  return $this->path ? FileBackend::extensionFromPath( $this->path ) : false;
87  }
88 
92  public function getUrl() {
93  return $this->url;
94  }
95 
99  public function getStoragePath() {
100  return $this->storagePath;
101  }
102 
107  public function setStoragePath( $storagePath ) {
108  $this->storagePath = $storagePath;
109  if ( $this->path === false ) {
110  $this->path = $storagePath;
111  }
112  }
113 
134  abstract public function toHtml( $options = [] );
135 
140  public function isError() {
141  return false;
142  }
143 
155  public function hasFile() {
156  // If TRANSFORM_LATER, $this->path will be false.
157  // Note: a null path means "use the source file".
158  return ( !$this->isError() && ( $this->path || $this->path === null ) );
159  }
160 
167  public function fileIsSource() {
168  return ( !$this->isError() && $this->path === null );
169  }
170 
177  public function getLocalCopyPath() {
178  if ( $this->isError() ) {
179  return false;
180  } elseif ( $this->path === null ) {
181  return $this->file->getLocalRefPath(); // assume thumb was not scaled
182  } elseif ( FileBackend::isStoragePath( $this->path ) ) {
183  $be = $this->file->getRepo()->getBackend();
184  // The temp file will be process cached by FileBackend
185  $fsFile = $be->getLocalReference( [ 'src' => $this->path ] );
186 
187  return $fsFile ? $fsFile->getPath() : false;
188  } else {
189  return $this->path; // may return false
190  }
191  }
192 
200  public function streamFileWithStatus( $headers = [] ) {
201  if ( !$this->path ) {
202  return Status::newFatal( 'backend-fail-stream', '<no path>' );
203  } elseif ( FileBackend::isStoragePath( $this->path ) ) {
204  $be = $this->file->getRepo()->getBackend();
205  return $be->streamFile( [ 'src' => $this->path, 'headers' => $headers ] );
206  } else { // FS-file
207  $success = StreamFile::stream( $this->getLocalCopyPath(), $headers );
208  return $success ? Status::newGood() : Status::newFatal( 'backend-fail-stream', $this->path );
209  }
210  }
211 
219  public function streamFile( $headers = [] ) {
220  $this->streamFileWithStatus( $headers )->isOK();
221  }
222 
230  protected function linkWrap( $linkAttribs, $contents ) {
231  if ( $linkAttribs ) {
232  return Xml::tags( 'a', $linkAttribs, $contents );
233  } else {
234  return $contents;
235  }
236  }
237 
243  public function getDescLinkAttribs( $title = null, $params = [] ) {
244  if ( is_array( $params ) ) {
245  $query = $params;
246  } else {
247  $query = [];
248  }
249  if ( $this->page && $this->page !== 1 ) {
250  $query['page'] = $this->page;
251  }
252  if ( $this->lang ) {
253  $query['lang'] = $this->lang;
254  }
255 
256  if ( is_string( $params ) && $params !== '' ) {
257  $query = $params . '&' . wfArrayToCgi( $query );
258  }
259 
260  $attribs = [
261  'href' => $this->file->getTitle()->getLocalURL( $query ),
262  'class' => 'image',
263  ];
264  if ( $title ) {
265  $attribs['title'] = $title;
266  }
267 
268  return $attribs;
269  }
270 }
271 
290  function __construct( $file, $url, $path = false, $parameters = [] ) {
291  # Previous parameters:
292  # $file, $url, $width, $height, $path = false, $page = false
293 
294  $defaults = [
295  'page' => false,
296  'lang' => false
297  ];
298 
299  if ( is_array( $parameters ) ) {
300  $actualParams = $parameters + $defaults;
301  } else {
302  # Using old format, should convert. Later a warning could be added here.
303  $numArgs = func_num_args();
304  $actualParams = [
305  'width' => $path,
306  'height' => $parameters,
307  'page' => ( $numArgs > 5 ) ? func_get_arg( 5 ) : false
308  ] + $defaults;
309  $path = ( $numArgs > 4 ) ? func_get_arg( 4 ) : false;
310  }
311 
312  $this->file = $file;
313  $this->url = $url;
314  $this->path = $path;
315 
316  # These should be integers when they get here.
317  # If not, there's a bug somewhere. But let's at
318  # least produce valid HTML code regardless.
319  $this->width = round( $actualParams['width'] );
320  $this->height = round( $actualParams['height'] );
321 
322  $this->page = $actualParams['page'];
323  $this->lang = $actualParams['lang'];
324  }
325 
358  function toHtml( $options = [] ) {
359  if ( count( func_get_args() ) == 2 ) {
360  throw new MWException( __METHOD__ . ' called in the old style' );
361  }
362 
363  $alt = isset( $options['alt'] ) ? $options['alt'] : '';
364 
365  $query = isset( $options['desc-query'] ) ? $options['desc-query'] : '';
366 
367  $attribs = [
368  'alt' => $alt,
369  'src' => $this->url,
370  ];
371 
372  if ( !empty( $options['custom-url-link'] ) ) {
373  $linkAttribs = [ 'href' => $options['custom-url-link'] ];
374  if ( !empty( $options['title'] ) ) {
375  $linkAttribs['title'] = $options['title'];
376  }
377  if ( !empty( $options['custom-target-link'] ) ) {
378  $linkAttribs['target'] = $options['custom-target-link'];
379  } elseif ( !empty( $options['parser-extlink-target'] ) ) {
380  $linkAttribs['target'] = $options['parser-extlink-target'];
381  }
382  if ( !empty( $options['parser-extlink-rel'] ) ) {
383  $linkAttribs['rel'] = $options['parser-extlink-rel'];
384  }
385  } elseif ( !empty( $options['custom-title-link'] ) ) {
387  $title = $options['custom-title-link'];
388  $linkAttribs = [
389  'href' => $title->getLinkURL(),
390  'title' => empty( $options['title'] ) ? $title->getFullText() : $options['title']
391  ];
392  } elseif ( !empty( $options['desc-link'] ) ) {
393  $linkAttribs = $this->getDescLinkAttribs(
394  empty( $options['title'] ) ? null : $options['title'],
395  $query
396  );
397  } elseif ( !empty( $options['file-link'] ) ) {
398  $linkAttribs = [ 'href' => $this->file->getUrl() ];
399  } else {
400  $linkAttribs = false;
401  if ( !empty( $options['title'] ) ) {
402  $attribs['title'] = $options['title'];
403  }
404  }
405 
406  if ( empty( $options['no-dimensions'] ) ) {
407  $attribs['width'] = $this->width;
408  $attribs['height'] = $this->height;
409  }
410  if ( !empty( $options['valign'] ) ) {
411  $attribs['style'] = "vertical-align: {$options['valign']}";
412  }
413  if ( !empty( $options['img-class'] ) ) {
414  $attribs['class'] = $options['img-class'];
415  }
416  if ( isset( $options['override-height'] ) ) {
417  $attribs['height'] = $options['override-height'];
418  }
419  if ( isset( $options['override-width'] ) ) {
420  $attribs['width'] = $options['override-width'];
421  }
422 
423  // Additional densities for responsive images, if specified.
424  // If any of these urls is the same as src url, it'll be excluded.
425  $responsiveUrls = array_diff( $this->responsiveUrls, [ $this->url ] );
426  if ( !empty( $responsiveUrls ) ) {
427  $attribs['srcset'] = Html::srcSet( $responsiveUrls );
428  }
429 
430  Hooks::run( 'ThumbnailBeforeProduceHTML', [ $this, &$attribs, &$linkAttribs ] );
431 
432  return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
433  }
434 }
435 
443  private $msg;
444 
445  function __construct( $msg, $width, $height /*, ... */ ) {
446  $args = array_slice( func_get_args(), 3 );
447  $this->msg = wfMessage( $msg )->params( $args );
448  $this->width = intval( $width );
449  $this->height = intval( $height );
450  $this->url = false;
451  $this->path = false;
452  }
453 
454  function toHtml( $options = [] ) {
455  return "<div class=\"MediaTransformError\" style=\"" .
456  "width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" .
457  $this->getHtmlMsg() .
458  "</div>";
459  }
460 
461  function toText() {
462  return $this->msg->text();
463  }
464 
465  function getHtmlMsg() {
466  return $this->msg->escaped();
467  }
468 
469  function getMsg() {
470  return $this->msg;
471  }
472 
473  function isError() {
474  return true;
475  }
476 
477  function getHttpStatusCode() {
478  return 500;
479  }
480 }
481 
488  function __construct( $params ) {
489  parent::__construct( 'thumbnail_error',
490  max( isset( $params['width'] ) ? $params['width'] : 0, 120 ),
491  max( isset( $params['height'] ) ? $params['height'] : 0, 120 ),
492  wfMessage( 'thumbnail_invalid_params' )
493  );
494  }
495 
496  function getHttpStatusCode() {
497  return 400;
498  }
499 }
500 
508  function __construct( $params, $maxImageArea ) {
509  $msg = wfMessage( 'thumbnail_toobigimagearea' );
510  $msg->rawParams(
511  $msg->getLanguage()->formatComputingNumbers( $maxImageArea, 1000, "size-$1pixel" )
512  );
513 
514  parent::__construct( 'thumbnail_error',
515  max( isset( $params['width'] ) ? $params['width'] : 0, 120 ),
516  max( isset( $params['height'] ) ? $params['height'] : 0, 120 ),
517  $msg
518  );
519  }
520 
521  function getHttpStatusCode() {
522  return 400;
523  }
524 }
MediaTransformError\isError
isError()
This will be overridden to return true in error classes.
Definition: MediaTransformOutput.php:473
MediaTransformError
Basic media transform error class.
Definition: MediaTransformOutput.php:441
ThumbnailImage
Media transform output for images.
Definition: MediaTransformOutput.php:277
file
We ve cleaned up the code here by removing clumps of infrequently used code and moving them off somewhere else It s much easier for someone working with this code to see what s _really_ going and make changes or fix bugs In we can take all the code that deals with the little used title reversing we can concentrate it all in an extension file
Definition: hooks.txt:91
MediaTransformOutput\$responsiveUrls
array $responsiveUrls
Associative array mapping optional supplementary image files from pixel density (eg 1....
Definition: MediaTransformOutput.php:33
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
MediaTransformOutput\getFile
getFile()
Definition: MediaTransformOutput.php:76
MediaTransformError\$msg
Message $msg
Definition: MediaTransformOutput.php:443
TransformParameterError\getHttpStatusCode
getHttpStatusCode()
Definition: MediaTransformOutput.php:496
MediaTransformOutput\isError
isError()
This will be overridden to return true in error classes.
Definition: MediaTransformOutput.php:140
Xml\tags
static tags( $element, $attribs=null, $contents)
Same as Xml::element(), but does not escape contents.
Definition: Xml.php:131
captcha-old.count
count
Definition: captcha-old.py:249
MediaTransformOutput\getStoragePath
getStoragePath()
Definition: MediaTransformOutput.php:99
TransformTooBigImageAreaError\__construct
__construct( $params, $maxImageArea)
Definition: MediaTransformOutput.php:508
MediaTransformOutput\$file
File $file
Definition: MediaTransformOutput.php:36
MediaTransformOutput\toHtml
toHtml( $options=[])
Fetch HTML for this transform output.
FileBackend\extensionFromPath
static extensionFromPath( $path, $case='lowercase')
Get the final extension from a storage or FS path.
Definition: FileBackend.php:1506
StatusValue\newFatal
static newFatal( $message)
Factory function for fatal errors.
Definition: StatusValue.php:68
$params
$params
Definition: styleTest.css.php:40
MediaTransformError\__construct
__construct( $msg, $width, $height)
Definition: MediaTransformOutput.php:445
page
target page
Definition: All_system_messages.txt:1267
$success
$success
Definition: NoLocalSettings.php:44
ThumbnailImage\__construct
__construct( $file, $url, $path=false, $parameters=[])
Get a thumbnail object from a file and parameters.
Definition: MediaTransformOutput.php:290
MediaTransformOutput\getWidth
getWidth()
Definition: MediaTransformOutput.php:62
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MediaTransformOutput\$url
string $url
URL path to the thumb.
Definition: MediaTransformOutput.php:45
StreamFile\stream
static stream( $fname, $headers=[], $sendErrors=true, $optHeaders=[], $flags=0)
Stream a file to the browser, adding all the headings and fun stuff.
Definition: StreamFile.php:45
MediaTransformOutput\hasFile
hasFile()
Check if an output thumbnail file actually exists.
Definition: MediaTransformOutput.php:155
MediaTransformOutput\getUrl
getUrl()
Definition: MediaTransformOutput.php:92
MediaTransformOutput\streamFile
streamFile( $headers=[])
Stream the file if there were no errors.
Definition: MediaTransformOutput.php:219
MediaTransformOutput\$lang
bool string $lang
Language code, false if not set.
Definition: MediaTransformOutput.php:54
MediaTransformOutput\linkWrap
linkWrap( $linkAttribs, $contents)
Wrap some XHTML text in an anchor tag with the given attributes.
Definition: MediaTransformOutput.php:230
TransformTooBigImageAreaError\getHttpStatusCode
getHttpStatusCode()
Definition: MediaTransformOutput.php:521
$query
null for the wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition: hooks.txt:1581
MediaTransformOutput\getExtension
getExtension()
Get the final extension of the thumbnail.
Definition: MediaTransformOutput.php:85
MediaTransformOutput\fileIsSource
fileIsSource()
Check if the output thumbnail is the same as the source.
Definition: MediaTransformOutput.php:167
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:51
MediaTransformError\getHtmlMsg
getHtmlMsg()
Definition: MediaTransformOutput.php:465
Html\srcSet
static srcSet(array $urls)
Generate a srcset attribute value.
Definition: Html.php:990
ThumbnailImage\toHtml
toHtml( $options=[])
Return HTML.
Definition: MediaTransformOutput.php:358
MWException
MediaWiki exception.
Definition: MWException.php:26
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:932
MediaTransformOutput\setStoragePath
setStoragePath( $storagePath)
Definition: MediaTransformOutput.php:107
FileBackend\isStoragePath
static isStoragePath( $path)
Check if a given path is a "mwstore://" path.
Definition: FileBackend.php:1435
$attribs
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing & $attribs
Definition: hooks.txt:1965
Xml\element
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:39
MediaTransformOutput\streamFileWithStatus
streamFileWithStatus( $headers=[])
Stream the file if there were no errors.
Definition: MediaTransformOutput.php:200
MediaTransformError\toHtml
toHtml( $options=[])
Fetch HTML for this transform output.
Definition: MediaTransformOutput.php:454
TransformTooBigImageAreaError
Shortcut class for parameter file size errors.
Definition: MediaTransformOutput.php:507
TransformParameterError\__construct
__construct( $params)
Definition: MediaTransformOutput.php:488
MediaTransformOutput\getDescLinkAttribs
getDescLinkAttribs( $title=null, $params=[])
Definition: MediaTransformOutput.php:243
MediaTransformOutput\$height
int $height
Image height.
Definition: MediaTransformOutput.php:42
MediaTransformOutput\$storagePath
bool string $storagePath
Permanent storage path
Definition: MediaTransformOutput.php:57
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:81
MediaTransformError\getMsg
getMsg()
Definition: MediaTransformOutput.php:469
TransformParameterError
Shortcut class for parameter validation errors.
Definition: MediaTransformOutput.php:487
MediaTransformOutput\getLocalCopyPath
getLocalCopyPath()
Get the path of a file system copy of the thumbnail.
Definition: MediaTransformOutput.php:177
MediaTransformOutput
Base class for the output of MediaHandler::doTransform() and File::transform().
Definition: MediaTransformOutput.php:29
$args
if( $line===false) $args
Definition: cdb.php:63
MediaTransformError\toText
toText()
Definition: MediaTransformOutput.php:461
MediaTransformOutput\$path
bool string $path
Filesystem path to the thumb
Definition: MediaTransformOutput.php:51
$options
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1965
MediaTransformOutput\getHeight
getHeight()
Definition: MediaTransformOutput.php:69
width
width
Definition: parserTests.txt:163
wfMessage
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
MediaTransformError\getHttpStatusCode
getHttpStatusCode()
Definition: MediaTransformOutput.php:477
MediaTransformOutput\$width
int $width
Image width.
Definition: MediaTransformOutput.php:39
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:203
MediaTransformOutput\$page
bool string $page
Definition: MediaTransformOutput.php:48
array
the array() calling protocol came about after MediaWiki 1.4rc1.
wfArrayToCgi
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e....
Definition: GlobalFunctions.php:442