Fix for r47482: put the RELEASE-NOTES entry in the "bug fixes" section and not the...
[mediawiki.git] / includes / MediaTransformOutput.php
blob0367494fd6eaaa5ffd880f873028049121751840
1 <?php
2 /**
3 * @file
4 * @ingroup Media
5 */
7 /**
8 * Base class for the output of MediaHandler::doTransform() and File::transform().
10 * @ingroup Media
12 abstract class MediaTransformOutput {
13 var $file, $width, $height, $url, $page, $path;
15 /**
16 * Get the width of the output box
18 function getWidth() {
19 return $this->width;
22 /**
23 * Get the height of the output box
25 function getHeight() {
26 return $this->height;
29 /**
30 * @return string The thumbnail URL
32 function getUrl() {
33 return $this->url;
36 /**
37 * @return string Destination file path (local filesystem)
39 function getPath() {
40 return $this->path;
43 /**
44 * Fetch HTML for this transform output
46 * @param array $options Associative array of options. Boolean options
47 * should be indicated with a value of true for true, and false or
48 * absent for false.
50 * alt Alternate text or caption
51 * desc-link Boolean, show a description link
52 * file-link Boolean, show a file download link
53 * custom-url-link Custom URL to link to
54 * custom-title-link Custom Title object to link to
55 * valign vertical-align property, if the output is an inline element
56 * img-class Class applied to the <img> tag, if there is such a tag
58 * For images, desc-link and file-link are implemented as a click-through. For
59 * sounds and videos, they may be displayed in other ways.
61 * @return string
63 abstract function toHtml( $options = array() );
65 /**
66 * This will be overridden to return true in error classes
68 function isError() {
69 return false;
72 /**
73 * Wrap some XHTML text in an anchor tag with the given attributes
75 protected function linkWrap( $linkAttribs, $contents ) {
76 if ( $linkAttribs ) {
77 return Xml::tags( 'a', $linkAttribs, $contents );
78 } else {
79 return $contents;
83 function getDescLinkAttribs( $alt = false, $params = '' ) {
84 $query = $this->page ? ( 'page=' . urlencode( $this->page ) ) : '';
85 if( $params ) {
86 $query .= $query ? '&'.$params : $params;
88 $title = $this->file->getTitle();
89 if ( strval( $alt ) === '' ) {
90 $alt = $title->getText();
92 return array(
93 'href' => $this->file->getTitle()->getLocalURL( $query ),
94 'class' => 'image',
95 'title' => $alt
102 * Media transform output for images
104 * @ingroup Media
106 class ThumbnailImage extends MediaTransformOutput {
108 * @param string $path Filesystem path to the thumb
109 * @param string $url URL path to the thumb
110 * @private
112 function ThumbnailImage( $file, $url, $width, $height, $path = false, $page = false ) {
113 $this->file = $file;
114 $this->url = $url;
115 # These should be integers when they get here.
116 # If not, there's a bug somewhere. But let's at
117 # least produce valid HTML code regardless.
118 $this->width = round( $width );
119 $this->height = round( $height );
120 $this->path = $path;
121 $this->page = $page;
125 * Return HTML <img ... /> tag for the thumbnail, will include
126 * width and height attributes and a blank alt text (as required).
128 * @param array $options Associative array of options. Boolean options
129 * should be indicated with a value of true for true, and false or
130 * absent for false.
132 * alt HTML alt attribute
133 * title HTML title attribute
134 * desc-link Boolean, show a description link
135 * file-link Boolean, show a file download link
136 * valign vertical-align property, if the output is an inline element
137 * img-class Class applied to the <img> tag, if there is such a tag
138 * desc-query String, description link query params
139 * custom-url-link Custom URL to link to
140 * custom-title-link Custom Title object to link to
142 * For images, desc-link and file-link are implemented as a click-through. For
143 * sounds and videos, they may be displayed in other ways.
145 * @return string
146 * @public
148 function toHtml( $options = array() ) {
149 if ( count( func_get_args() ) == 2 ) {
150 throw new MWException( __METHOD__ .' called in the old style' );
153 $alt = empty( $options['alt'] ) ? '' : $options['alt'];
154 # Note: if title is empty and alt is not, make the title empty, don't
155 # use alt; only use alt if title is not set
156 $title = !isset( $options['title'] ) ? $alt : $options['title'];
157 $query = empty($options['desc-query']) ? '' : $options['desc-query'];
159 if ( !empty( $options['custom-url-link'] ) ) {
160 $linkAttribs = array( 'href' => $options['custom-url-link'] );
161 } elseif ( !empty( $options['custom-title-link'] ) ) {
162 $title = $options['custom-title-link'];
163 $linkAttribs = array( 'href' => $title->getLinkUrl(), 'title' => $title->getFullText() );
164 } elseif ( !empty( $options['desc-link'] ) ) {
165 $linkAttribs = $this->getDescLinkAttribs( $title, $query );
166 } elseif ( !empty( $options['file-link'] ) ) {
167 $linkAttribs = array( 'href' => $this->file->getURL() );
168 } else {
169 $linkAttribs = false;
172 $attribs = array(
173 'alt' => $alt,
174 'src' => $this->url,
175 'width' => $this->width,
176 'height' => $this->height,
177 'border' => 0,
179 if ( !empty( $options['valign'] ) ) {
180 $attribs['style'] = "vertical-align: {$options['valign']}";
182 if ( !empty( $options['img-class'] ) ) {
183 $attribs['class'] = $options['img-class'];
185 return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
191 * Basic media transform error class
193 * @ingroup Media
195 class MediaTransformError extends MediaTransformOutput {
196 var $htmlMsg, $textMsg, $width, $height, $url, $path;
198 function __construct( $msg, $width, $height /*, ... */ ) {
199 $args = array_slice( func_get_args(), 3 );
200 $htmlArgs = array_map( 'htmlspecialchars', $args );
201 $htmlArgs = array_map( 'nl2br', $htmlArgs );
203 $this->htmlMsg = wfMsgReplaceArgs( htmlspecialchars( wfMsgGetKey( $msg, true ) ), $htmlArgs );
204 $this->textMsg = wfMsgReal( $msg, $args );
205 $this->width = intval( $width );
206 $this->height = intval( $height );
207 $this->url = false;
208 $this->path = false;
211 function toHtml( $options = array() ) {
212 return "<table class=\"MediaTransformError\" style=\"" .
213 "width: {$this->width}px; height: {$this->height}px;\"><tr><td>" .
214 $this->htmlMsg .
215 "</td></tr></table>";
218 function toText() {
219 return $this->textMsg;
222 function getHtmlMsg() {
223 return $this->htmlMsg;
226 function isError() {
227 return true;
232 * Shortcut class for parameter validation errors
234 * @ingroup Media
236 class TransformParameterError extends MediaTransformError {
237 function __construct( $params ) {
238 parent::__construct( 'thumbnail_error',
239 max( isset( $params['width'] ) ? $params['width'] : 0, 180 ),
240 max( isset( $params['height'] ) ? $params['height'] : 0, 180 ),
241 wfMsg( 'thumbnail_invalid_params' ) );