bd53a162276a1601cc099f0c6ba4bf18beda51d2
4 * Base class for the output of MediaHandler::doTransform() and File::transform().
8 abstract class MediaTransformOutput
{
9 var $file, $width, $height, $url, $page, $path;
12 * Get the width of the output box
19 * Get the height of the output box
21 function getHeight() {
26 * @return string The thumbnail URL
33 * @return string Destination file path (local filesystem)
40 * Fetch HTML for this transform output
42 * @param array $options Associative array of options. Boolean options
43 * should be indicated with a value of true for true, and false or
46 * alt Alternate text or caption
47 * desc-link Boolean, show a description link
48 * file-link Boolean, show a file download link
49 * valign vertical-align property, if the output is an inline element
50 * img-class Class applied to the <img> tag, if there is such a tag
52 * For images, desc-link and file-link are implemented as a click-through. For
53 * sounds and videos, they may be displayed in other ways.
57 abstract function toHtml( $options = array() );
60 * This will be overridden to return true in error classes
67 * Wrap some XHTML text in an anchor tag with the given attributes
69 protected function linkWrap( $linkAttribs, $contents ) {
71 return Xml
::tags( 'a', $linkAttribs, $contents );
77 function getDescLinkAttribs( $alt = false, $params = '' ) {
78 $query = $this->page ?
( 'page=' . urlencode( $this->page
) ) : '';
80 $query .= $query ?
'&'.$params : $params;
82 $title = $this->file
->getTitle();
83 if ( strval( $alt ) === '' ) {
84 $alt = $title->getText();
87 'href' => $this->file
->getTitle()->getLocalURL( $query ),
96 * Media transform output for images
100 class ThumbnailImage
extends MediaTransformOutput
{
102 * @param string $path Filesystem path to the thumb
103 * @param string $url URL path to the thumb
106 function ThumbnailImage( $file, $url, $width, $height, $path = false, $page = false ) {
109 # These should be integers when they get here.
110 # If not, there's a bug somewhere. But let's at
111 # least produce valid HTML code regardless.
112 $this->width
= round( $width );
113 $this->height
= round( $height );
119 * Return HTML <img ... /> tag for the thumbnail, will include
120 * width and height attributes and a blank alt text (as required).
122 * @param array $options Associative array of options. Boolean options
123 * should be indicated with a value of true for true, and false or
126 * alt Alternate text or caption
127 * desc-link Boolean, show a description link
128 * file-link Boolean, show a file download link
129 * valign vertical-align property, if the output is an inline element
130 * img-class Class applied to the <img> tag, if there is such a tag
131 * desc-query String, description link query params
133 * For images, desc-link and file-link are implemented as a click-through. For
134 * sounds and videos, they may be displayed in other ways.
139 function toHtml( $options = array() ) {
140 if ( count( func_get_args() ) == 2 ) {
141 throw new MWException( __METHOD__
.' called in the old style' );
144 $alt = empty( $options['alt'] ) ?
'' : $options['alt'];
145 $query = empty($options['desc-query']) ?
'' : $options['desc-query'];
146 if ( !empty( $options['desc-link'] ) ) {
147 $linkAttribs = $this->getDescLinkAttribs( $alt, $query );
148 } elseif ( !empty( $options['file-link'] ) ) {
149 $linkAttribs = array( 'href' => $this->file
->getURL() );
151 $linkAttribs = false;
157 'width' => $this->width
,
158 'height' => $this->height
,
161 if ( !empty( $options['valign'] ) ) {
162 $attribs['style'] = "vertical-align: {$options['valign']}";
164 if ( !empty( $options['img-class'] ) ) {
165 $attribs['class'] = $options['img-class'];
167 return $this->linkWrap( $linkAttribs, Xml
::element( 'img', $attribs ) );
173 * Basic media transform error class
177 class MediaTransformError
extends MediaTransformOutput
{
178 var $htmlMsg, $textMsg, $width, $height, $url, $path;
180 function __construct( $msg, $width, $height /*, ... */ ) {
181 $args = array_slice( func_get_args(), 3 );
182 $htmlArgs = array_map( 'htmlspecialchars', $args );
183 $htmlArgs = array_map( 'nl2br', $htmlArgs );
185 $this->htmlMsg
= wfMsgReplaceArgs( htmlspecialchars( wfMsgGetKey( $msg, true ) ), $htmlArgs );
186 $this->textMsg
= wfMsgReal( $msg, $args );
187 $this->width
= intval( $width );
188 $this->height
= intval( $height );
193 function toHtml( $options = array() ) {
194 return "<table class=\"MediaTransformError\" style=\"" .
195 "width: {$this->width}px; height: {$this->height}px;\"><tr><td>" .
197 "</td></tr></table>";
201 return $this->textMsg
;
204 function getHtmlMsg() {
205 return $this->htmlMsg
;
214 * Shortcut class for parameter validation errors
218 class TransformParameterError
extends MediaTransformError
{
219 function __construct( $params ) {
220 parent
::__construct( 'thumbnail_error',
221 max( isset( $params['width'] ) ?
$params['width'] : 0, 180 ),
222 max( isset( $params['height'] ) ?
$params['height'] : 0, 180 ),
223 wfMsg( 'thumbnail_invalid_params' ) );