Fix Bug #30322 “SVG metadata is read incorrectly” by applying supplied patch
[lhc/web/wiklou.git] / includes / media / MediaTransformOutput.php
1 <?php
2 /**
3 * Base class for the output of file transformation methods.
4 *
5 * @file
6 * @ingroup Media
7 */
8
9 /**
10 * Base class for the output of MediaHandler::doTransform() and File::transform().
11 *
12 * @ingroup Media
13 */
14 abstract class MediaTransformOutput {
15 /**
16 * @var File
17 */
18 var $file;
19
20 var $width, $height, $url, $page, $path;
21
22 /**
23 * Get the width of the output box
24 */
25 function getWidth() {
26 return $this->width;
27 }
28
29 /**
30 * Get the height of the output box
31 */
32 function getHeight() {
33 return $this->height;
34 }
35
36 /**
37 * @return string The thumbnail URL
38 */
39 function getUrl() {
40 return $this->url;
41 }
42
43 /**
44 * @return String: destination file path (local filesystem)
45 */
46 function getPath() {
47 return $this->path;
48 }
49
50 /**
51 * Fetch HTML for this transform output
52 *
53 * @param $options array Associative array of options. Boolean options
54 * should be indicated with a value of true for true, and false or
55 * absent for false.
56 *
57 * alt Alternate text or caption
58 * desc-link Boolean, show a description link
59 * file-link Boolean, show a file download link
60 * custom-url-link Custom URL to link to
61 * custom-title-link Custom Title object to link to
62 * valign vertical-align property, if the output is an inline element
63 * img-class Class applied to the <img> tag, if there is such a tag
64 *
65 * For images, desc-link and file-link are implemented as a click-through. For
66 * sounds and videos, they may be displayed in other ways.
67 *
68 * @return string
69 */
70 abstract function toHtml( $options = array() );
71
72 /**
73 * This will be overridden to return true in error classes
74 */
75 function isError() {
76 return false;
77 }
78
79 /**
80 * Wrap some XHTML text in an anchor tag with the given attributes
81 *
82 * @param $linkAttribs array
83 * @param $contents string
84 *
85 * @return string
86 */
87 protected function linkWrap( $linkAttribs, $contents ) {
88 if ( $linkAttribs ) {
89 return Xml::tags( 'a', $linkAttribs, $contents );
90 } else {
91 return $contents;
92 }
93 }
94
95 /**
96 * @param $title string
97 * @param $params array
98 * @return array
99 */
100 function getDescLinkAttribs( $title = null, $params = '' ) {
101 $query = $this->page ? ( 'page=' . urlencode( $this->page ) ) : '';
102 if( $params ) {
103 $query .= $query ? '&'.$params : $params;
104 }
105 $attribs = array(
106 'href' => $this->file->getTitle()->getLocalURL( $query ),
107 'class' => 'image',
108 );
109 if ( $title ) {
110 $attribs['title'] = $title;
111 }
112 return $attribs;
113 }
114 }
115
116 /**
117 * Media transform output for images
118 *
119 * @ingroup Media
120 */
121 class ThumbnailImage extends MediaTransformOutput {
122
123 /**
124 * @param $file File object
125 * @param $url String: URL path to the thumb
126 * @param $width Integer: file's width
127 * @param $height Integer: file's height
128 * @param $path String: filesystem path to the thumb
129 * @param $page Integer: page number, for multipage files
130 * @private
131 */
132 function __construct( $file, $url, $width, $height, $path = false, $page = false ) {
133 $this->file = $file;
134 $this->url = $url;
135 # These should be integers when they get here.
136 # If not, there's a bug somewhere. But let's at
137 # least produce valid HTML code regardless.
138 $this->width = round( $width );
139 $this->height = round( $height );
140 $this->path = $path;
141 $this->page = $page;
142 }
143
144 /**
145 * Return HTML <img ... /> tag for the thumbnail, will include
146 * width and height attributes and a blank alt text (as required).
147 *
148 * @param $options array Associative array of options. Boolean options
149 * should be indicated with a value of true for true, and false or
150 * absent for false.
151 *
152 * alt HTML alt attribute
153 * title HTML title attribute
154 * desc-link Boolean, show a description link
155 * file-link Boolean, show a file download link
156 * valign vertical-align property, if the output is an inline element
157 * img-class Class applied to the \<img\> tag, if there is such a tag
158 * desc-query String, description link query params
159 * custom-url-link Custom URL to link to
160 * custom-title-link Custom Title object to link to
161 * custom target-link Value of the target attribute, for custom-target-link
162 *
163 * For images, desc-link and file-link are implemented as a click-through. For
164 * sounds and videos, they may be displayed in other ways.
165 *
166 * @return string
167 */
168 function toHtml( $options = array() ) {
169 if ( count( func_get_args() ) == 2 ) {
170 throw new MWException( __METHOD__ .' called in the old style' );
171 }
172
173 $alt = empty( $options['alt'] ) ? '' : $options['alt'];
174
175 $query = empty( $options['desc-query'] ) ? '' : $options['desc-query'];
176
177 if ( !empty( $options['custom-url-link'] ) ) {
178 $linkAttribs = array( 'href' => $options['custom-url-link'] );
179 if ( !empty( $options['title'] ) ) {
180 $linkAttribs['title'] = $options['title'];
181 }
182 if ( !empty( $options['custom-target-link'] ) ) {
183 $linkAttribs['target'] = $options['custom-target-link'];
184 }
185 } elseif ( !empty( $options['custom-title-link'] ) ) {
186 $title = $options['custom-title-link'];
187 $linkAttribs = array(
188 'href' => $title->getLinkUrl(),
189 'title' => empty( $options['title'] ) ? $title->getFullText() : $options['title']
190 );
191 } elseif ( !empty( $options['desc-link'] ) ) {
192 $linkAttribs = $this->getDescLinkAttribs( empty( $options['title'] ) ? null : $options['title'], $query );
193 } elseif ( !empty( $options['file-link'] ) ) {
194 $linkAttribs = array( 'href' => $this->file->getURL() );
195 } else {
196 $linkAttribs = false;
197 }
198
199 $attribs = array(
200 'alt' => $alt,
201 'src' => $this->url,
202 'width' => $this->width,
203 'height' => $this->height,
204 );
205 if ( !empty( $options['valign'] ) ) {
206 $attribs['style'] = "vertical-align: {$options['valign']}";
207 }
208 if ( !empty( $options['img-class'] ) ) {
209 $attribs['class'] = $options['img-class'];
210 }
211 return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
212 }
213
214 }
215
216 /**
217 * Basic media transform error class
218 *
219 * @ingroup Media
220 */
221 class MediaTransformError extends MediaTransformOutput {
222 var $htmlMsg, $textMsg, $width, $height, $url, $path;
223
224 function __construct( $msg, $width, $height /*, ... */ ) {
225 $args = array_slice( func_get_args(), 3 );
226 $htmlArgs = array_map( 'htmlspecialchars', $args );
227 $htmlArgs = array_map( 'nl2br', $htmlArgs );
228
229 $this->htmlMsg = wfMessage( $msg )->rawParams( $htmlArgs )->escaped();
230 $this->textMsg = wfMessage( $msg )->rawParams( $htmlArgs )->text();
231 $this->width = intval( $width );
232 $this->height = intval( $height );
233 $this->url = false;
234 $this->path = false;
235 }
236
237 function toHtml( $options = array() ) {
238 return "<div class=\"MediaTransformError\" style=\"" .
239 "width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" .
240 $this->htmlMsg .
241 "</div>";
242 }
243
244 function toText() {
245 return $this->textMsg;
246 }
247
248 function getHtmlMsg() {
249 return $this->htmlMsg;
250 }
251
252 function isError() {
253 return true;
254 }
255 }
256
257 /**
258 * Shortcut class for parameter validation errors
259 *
260 * @ingroup Media
261 */
262 class TransformParameterError extends MediaTransformError {
263 function __construct( $params ) {
264 parent::__construct( 'thumbnail_error',
265 max( isset( $params['width'] ) ? $params['width'] : 0, 120 ),
266 max( isset( $params['height'] ) ? $params['height'] : 0, 120 ),
267 wfMsg( 'thumbnail_invalid_params' ) );
268 }
269 }