MediaWiki  1.23.13
MediaTransformOutput.php
Go to the documentation of this file.
1 <?php
29 abstract class MediaTransformOutput {
33  public $responsiveUrls = array();
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  }
110 
131  abstract public function toHtml( $options = array() );
132 
137  public function isError() {
138  return false;
139  }
140 
149  public function hasFile() {
150  // If TRANSFORM_LATER, $this->path will be false.
151  // Note: a null path means "use the source file".
152  return ( !$this->isError() && ( $this->path || $this->path === null ) );
153  }
154 
161  public function fileIsSource() {
162  return ( !$this->isError() && $this->path === null );
163  }
164 
171  public function getLocalCopyPath() {
172  if ( $this->isError() ) {
173  return false;
174  } elseif ( $this->path === null ) {
175  return $this->file->getLocalRefPath(); // assume thumb was not scaled
176  } elseif ( FileBackend::isStoragePath( $this->path ) ) {
177  $be = $this->file->getRepo()->getBackend();
178  // The temp file will be process cached by FileBackend
179  $fsFile = $be->getLocalReference( array( 'src' => $this->path ) );
180 
181  return $fsFile ? $fsFile->getPath() : false;
182  } else {
183  return $this->path; // may return false
184  }
185  }
186 
193  public function streamFile( $headers = array() ) {
194  if ( !$this->path ) {
195  return false;
196  } elseif ( FileBackend::isStoragePath( $this->path ) ) {
197  $be = $this->file->getRepo()->getBackend();
198 
199  return $be->streamFile( array( 'src' => $this->path, 'headers' => $headers ) )->isOK();
200  } else { // FS-file
201  return StreamFile::stream( $this->getLocalCopyPath(), $headers );
202  }
203  }
204 
212  protected function linkWrap( $linkAttribs, $contents ) {
213  if ( $linkAttribs ) {
214  return Xml::tags( 'a', $linkAttribs, $contents );
215  } else {
216  return $contents;
217  }
218  }
219 
225  public function getDescLinkAttribs( $title = null, $params = array() ) {
226  if ( is_array( $params ) ) {
227  $query = $params;
228  } else {
229  $query = array();
230  }
231  if ( $this->page && $this->page !== 1 ) {
232  $query['page'] = $this->page;
233  }
234  if ( $this->lang ) {
235  $query['lang'] = $this->lang;
236  }
237 
238  if ( is_string( $params ) && $params !== '' ) {
239  $query = $params . '&' . wfArrayToCgi( $query );
240  }
241 
242  $attribs = array(
243  'href' => $this->file->getTitle()->getLocalURL( $query ),
244  'class' => 'image',
245  );
246  if ( $title ) {
247  $attribs['title'] = $title;
248  }
249 
250  return $attribs;
251  }
252 }
253 
259 class ThumbnailImage extends MediaTransformOutput {
272  function __construct( $file, $url, $path = false, $parameters = array() ) {
273  # Previous parameters:
274  # $file, $url, $width, $height, $path = false, $page = false
275 
276  $defaults = array(
277  'page' => false,
278  'lang' => false
279  );
280 
281  if ( is_array( $parameters ) ) {
282  $actualParams = $parameters + $defaults;
283  } else {
284  # Using old format, should convert. Later a warning could be added here.
285  $numArgs = func_num_args();
286  $actualParams = array(
287  'width' => $path,
288  'height' => $parameters,
289  'page' => ( $numArgs > 5 ) ? func_get_arg( 5 ) : false
290  ) + $defaults;
291  $path = ( $numArgs > 4 ) ? func_get_arg( 4 ) : false;
292  }
293 
294  $this->file = $file;
295  $this->url = $url;
296  $this->path = $path;
297 
298  # These should be integers when they get here.
299  # If not, there's a bug somewhere. But let's at
300  # least produce valid HTML code regardless.
301  $this->width = round( $actualParams['width'] );
302  $this->height = round( $actualParams['height'] );
303 
304  $this->page = $actualParams['page'];
305  $this->lang = $actualParams['lang'];
306  }
307 
340  function toHtml( $options = array() ) {
341  if ( count( func_get_args() ) == 2 ) {
342  throw new MWException( __METHOD__ . ' called in the old style' );
343  }
344 
345  $alt = empty( $options['alt'] ) ? '' : $options['alt'];
346 
347  $query = empty( $options['desc-query'] ) ? '' : $options['desc-query'];
348 
349  if ( !empty( $options['custom-url-link'] ) ) {
350  $linkAttribs = array( 'href' => $options['custom-url-link'] );
351  if ( !empty( $options['title'] ) ) {
352  $linkAttribs['title'] = $options['title'];
353  }
354  if ( !empty( $options['custom-target-link'] ) ) {
355  $linkAttribs['target'] = $options['custom-target-link'];
356  } elseif ( !empty( $options['parser-extlink-target'] ) ) {
357  $linkAttribs['target'] = $options['parser-extlink-target'];
358  }
359  if ( !empty( $options['parser-extlink-rel'] ) ) {
360  $linkAttribs['rel'] = $options['parser-extlink-rel'];
361  }
362  } elseif ( !empty( $options['custom-title-link'] ) ) {
364  $title = $options['custom-title-link'];
365  $linkAttribs = array(
366  'href' => $title->getLinkURL(),
367  'title' => empty( $options['title'] ) ? $title->getFullText() : $options['title']
368  );
369  } elseif ( !empty( $options['desc-link'] ) ) {
370  $linkAttribs = $this->getDescLinkAttribs(
371  empty( $options['title'] ) ? null : $options['title'],
372  $query
373  );
374  } elseif ( !empty( $options['file-link'] ) ) {
375  $linkAttribs = array( 'href' => $this->file->getURL() );
376  } else {
377  $linkAttribs = false;
378  }
379 
380  $attribs = array(
381  'alt' => $alt,
382  'src' => $this->url,
383  );
384 
385  if ( empty( $options['no-dimensions'] ) ) {
386  $attribs['width'] = $this->width;
387  $attribs['height'] = $this->height;
388  }
389  if ( !empty( $options['valign'] ) ) {
390  $attribs['style'] = "vertical-align: {$options['valign']}";
391  }
392  if ( !empty( $options['img-class'] ) ) {
393  $attribs['class'] = $options['img-class'];
394  }
395  if ( isset( $options['override-height'] ) ) {
396  $attribs['height'] = $options['override-height'];
397  }
398  if ( isset( $options['override-width'] ) ) {
399  $attribs['width'] = $options['override-width'];
400  }
401 
402  // Additional densities for responsive images, if specified.
403  if ( !empty( $this->responsiveUrls ) ) {
404  $attribs['srcset'] = Html::srcSet( $this->responsiveUrls );
405  }
406 
407  wfRunHooks( 'ThumbnailBeforeProduceHTML', array( $this, &$attribs, &$linkAttribs ) );
408 
409  return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
410  }
411 }
412 
420  private $htmlMsg;
421 
423  private $textMsg;
424 
425  function __construct( $msg, $width, $height /*, ... */ ) {
426  $args = array_slice( func_get_args(), 3 );
427  $htmlArgs = array_map( 'htmlspecialchars', $args );
428  $htmlArgs = array_map( 'nl2br', $htmlArgs );
429 
430  $this->htmlMsg = wfMessage( $msg )->rawParams( $htmlArgs )->escaped();
431  $this->textMsg = wfMessage( $msg )->rawParams( $htmlArgs )->text();
432  $this->width = intval( $width );
433  $this->height = intval( $height );
434  $this->url = false;
435  $this->path = false;
436  }
437 
438  function toHtml( $options = array() ) {
439  return "<div class=\"MediaTransformError\" style=\"" .
440  "width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" .
441  $this->htmlMsg .
442  "</div>";
443  }
444 
445  function toText() {
446  return $this->textMsg;
447  }
448 
449  function getHtmlMsg() {
450  return $this->htmlMsg;
451  }
452 
453  function isError() {
454  return true;
455  }
456 }
457 
464  function __construct( $params ) {
465  parent::__construct( 'thumbnail_error',
466  max( isset( $params['width'] ) ? $params['width'] : 0, 120 ),
467  max( isset( $params['height'] ) ? $params['height'] : 0, 120 ),
468  wfMessage( 'thumbnail_invalid_params' )->text() );
469  }
470 }
MediaTransformError\isError
isError()
This will be overridden to return true in error classes.
Definition: MediaTransformOutput.php:442
Html\srcSet
static srcSet( $urls)
Generate a srcset attribute value from an array mapping pixel densities to URLs.
Definition: Html.php:906
MediaTransformError
Basic media transform error class.
Definition: MediaTransformOutput.php:409
ThumbnailImage
Media transform output for images.
Definition: MediaTransformOutput.php:250
MediaTransformOutput\$responsiveUrls
array $responsiveUrls
Associative array mapping optional supplementary image files from pixel density (eg 1....
Definition: MediaTransformOutput.php:32
MediaTransformOutput\getFile
getFile()
Definition: MediaTransformOutput.php:67
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
MediaTransformOutput\isError
isError()
This will be overridden to return true in error classes.
Definition: MediaTransformOutput.php:128
Xml\tags
static tags( $element, $attribs=null, $contents)
Same as Xml::element(), but does not escape contents.
Definition: Xml.php:131
ThumbnailImage\toHtml
toHtml( $options=array())
Return HTML.
Definition: MediaTransformOutput.php:331
text
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition: design.txt:12
MediaTransformOutput\getStoragePath
getStoragePath()
Definition: MediaTransformOutput.php:90
MediaTransformOutput\toHtml
toHtml( $options=array())
Fetch HTML for this transform output.
MediaTransformOutput\$file
File $file
object *
Definition: MediaTransformOutput.php:34
FileBackend\extensionFromPath
static extensionFromPath( $path, $case='lowercase')
Get the final extension from a storage or FS path.
Definition: FileBackend.php:1401
$params
$params
Definition: styleTest.css.php:40
MediaTransformError\__construct
__construct( $msg, $width, $height)
Definition: MediaTransformOutput.php:414
MediaTransformOutput\getWidth
getWidth()
Definition: MediaTransformOutput.php:53
MediaTransformOutput\$url
string $url
URL path to the thumb *.
Definition: MediaTransformOutput.php:40
MediaTransformOutput\hasFile
hasFile()
Check if an output thumbnail file actually exists.
Definition: MediaTransformOutput.php:140
MediaTransformOutput\getUrl
getUrl()
Definition: MediaTransformOutput.php:83
MediaTransformOutput\$lang
bool string $lang
Language code, false if not set *.
Definition: MediaTransformOutput.php:46
MediaTransformOutput\linkWrap
linkWrap( $linkAttribs, $contents)
Wrap some XHTML text in an anchor tag with the given attributes.
Definition: MediaTransformOutput.php:203
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:93
StreamFile\stream
static stream( $fname, $headers=array(), $sendErrors=true)
Stream a file to the browser, adding all the headings and fun stuff.
Definition: StreamFile.php:41
MediaTransformOutput\getDescLinkAttribs
getDescLinkAttribs( $title=null, $params=array())
Definition: MediaTransformOutput.php:216
MediaTransformOutput\getExtension
getExtension()
Get the final extension of the thumbnail.
Definition: MediaTransformOutput.php:76
MediaTransformOutput\fileIsSource
fileIsSource()
Check if the output thumbnail is the same as the source.
Definition: MediaTransformOutput.php:152
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:50
MediaTransformError\getHtmlMsg
getHtmlMsg()
Definition: MediaTransformOutput.php:438
MWException
MediaWiki exception.
Definition: MWException.php:26
MediaTransformOutput\setStoragePath
setStoragePath( $storagePath)
Definition: MediaTransformOutput.php:98
FileBackend\isStoragePath
static isStoragePath( $path)
Check if a given path is a "mwstore://" path.
Definition: FileBackend.php:1330
wfMessage
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 after in associative array form externallinks including delete and has completed for all link tables 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
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
wfRunHooks
wfRunHooks( $event, array $args=array(), $deprecatedVersion=null)
Call hook functions defined in $wgHooks.
Definition: GlobalFunctions.php:4058
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
TransformParameterError\__construct
__construct( $params)
Definition: MediaTransformOutput.php:453
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:188
$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:1530
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
MediaTransformOutput\$height
int $height
Image height *.
Definition: MediaTransformOutput.php:38
MediaTransformOutput\$storagePath
bool string $storagePath
Permanent storage path *.
Definition: MediaTransformOutput.php:48
TransformParameterError
Shortcut class for parameter validation errors.
Definition: MediaTransformOutput.php:452
ThumbnailImage\__construct
__construct( $file, $url, $path=false, $parameters=array())
Get a thumbnail object from a file and parameters.
Definition: MediaTransformOutput.php:263
MediaTransformError\toHtml
toHtml( $options=array())
Fetch HTML for this transform output.
Definition: MediaTransformOutput.php:427
MediaTransformError\$htmlMsg
string $htmlMsg
HTML formatted version of the error *.
Definition: MediaTransformOutput.php:410
MediaTransformOutput\getLocalCopyPath
getLocalCopyPath()
Get the path of a file system copy of the thumbnail.
Definition: MediaTransformOutput.php:162
MediaTransformOutput
Base class for the output of MediaHandler::doTransform() and File::transform().
Definition: MediaTransformOutput.php:29
$args
if( $line===false) $args
Definition: cdb.php:62
MediaTransformError\toText
toText()
Definition: MediaTransformOutput.php:434
MediaTransformOutput\$path
bool string $path
Filesystem path to the thumb *.
Definition: MediaTransformOutput.php:44
MediaTransformOutput\streamFile
streamFile( $headers=array())
Stream the file if there were no errors.
Definition: MediaTransformOutput.php:184
MediaTransformError\$textMsg
string $textMsg
Plain text formatted version of the error *.
Definition: MediaTransformOutput.php:412
MediaTransformOutput\getHeight
getHeight()
Definition: MediaTransformOutput.php:60
$query
return true to allow those checks to and false if checking is done use this to change the tables headers temp or archived zone change it to an object instance and return false override the list derivative used the name of the old file when set the default code will be skipped add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition: hooks.txt:1105
$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:1530
MediaTransformOutput\$width
int $width
Image width *.
Definition: MediaTransformOutput.php:36
MediaTransformOutput\$page
bool string $page
Definition: MediaTransformOutput.php:42
page
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values my talk page
Definition: hooks.txt:1961
wfArrayToCgi
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes two arrays as input, and returns a CGI-style string, e.g.
Definition: GlobalFunctions.php:414