(bug 14706) Added support for the Imagick PHP extension. Based on patch by Leslie...
[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 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 protected function linkWrap( $linkAttribs, $contents ) {
83 if ( $linkAttribs ) {
84 return Xml::tags( 'a', $linkAttribs, $contents );
85 } else {
86 return $contents;
87 }
88 }
89
90 function getDescLinkAttribs( $title = null, $params = '' ) {
91 $query = $this->page ? ( 'page=' . urlencode( $this->page ) ) : '';
92 if( $params ) {
93 $query .= $query ? '&'.$params : $params;
94 }
95 $attribs = array(
96 'href' => $this->file->getTitle()->getLocalURL( $query ),
97 'class' => 'image',
98 );
99 if ( $title ) {
100 $attribs['title'] = $title;
101 }
102 return $attribs;
103 }
104 }
105
106
107 /**
108 * Media transform output for images
109 *
110 * @ingroup Media
111 */
112 class ThumbnailImage extends MediaTransformOutput {
113
114 /**
115 * @param $file File object
116 * @param $url String: URL path to the thumb
117 * @param $width Integer: file's width
118 * @param $height Integer: file's height
119 * @param $path String: filesystem path to the thumb
120 * @param $page Integer: page number, for multipage files
121 * @private
122 */
123 function __construct( $file, $url, $width, $height, $path = false, $page = false ) {
124 $this->file = $file;
125 $this->url = $url;
126 # These should be integers when they get here.
127 # If not, there's a bug somewhere. But let's at
128 # least produce valid HTML code regardless.
129 $this->width = round( $width );
130 $this->height = round( $height );
131 $this->path = $path;
132 $this->page = $page;
133 }
134
135 /**
136 * Return HTML <img ... /> tag for the thumbnail, will include
137 * width and height attributes and a blank alt text (as required).
138 *
139 * @param $options Associative array of options. Boolean options
140 * should be indicated with a value of true for true, and false or
141 * absent for false.
142 *
143 * alt HTML alt attribute
144 * title HTML title attribute
145 * desc-link Boolean, show a description link
146 * file-link Boolean, show a file download link
147 * valign vertical-align property, if the output is an inline element
148 * img-class Class applied to the \<img\> tag, if there is such a tag
149 * desc-query String, description link query params
150 * custom-url-link Custom URL to link to
151 * custom-title-link Custom Title object to link to
152 * custom target-link Value of the target attribute, for custom-target-link
153 *
154 * For images, desc-link and file-link are implemented as a click-through. For
155 * sounds and videos, they may be displayed in other ways.
156 *
157 * @return string
158 */
159 function toHtml( $options = array() ) {
160 if ( count( func_get_args() ) == 2 ) {
161 throw new MWException( __METHOD__ .' called in the old style' );
162 }
163
164 $alt = empty( $options['alt'] ) ? '' : $options['alt'];
165
166 $query = empty( $options['desc-query'] ) ? '' : $options['desc-query'];
167
168 if ( !empty( $options['custom-url-link'] ) ) {
169 $linkAttribs = array( 'href' => $options['custom-url-link'] );
170 if ( !empty( $options['title'] ) ) {
171 $linkAttribs['title'] = $options['title'];
172 }
173 if ( !empty( $options['custom-target-link'] ) ) {
174 $linkAttribs['target'] = $options['custom-target-link'];
175 }
176 } elseif ( !empty( $options['custom-title-link'] ) ) {
177 $title = $options['custom-title-link'];
178 $linkAttribs = array(
179 'href' => $title->getLinkUrl(),
180 'title' => empty( $options['title'] ) ? $title->getFullText() : $options['title']
181 );
182 } elseif ( !empty( $options['desc-link'] ) ) {
183 $linkAttribs = $this->getDescLinkAttribs( empty( $options['title'] ) ? null : $options['title'], $query );
184 } elseif ( !empty( $options['file-link'] ) ) {
185 $linkAttribs = array( 'href' => $this->file->getURL() );
186 } else {
187 $linkAttribs = false;
188 }
189
190 $attribs = array(
191 'alt' => $alt,
192 'src' => $this->url,
193 'width' => $this->width,
194 'height' => $this->height,
195 );
196 if ( !empty( $options['valign'] ) ) {
197 $attribs['style'] = "vertical-align: {$options['valign']}";
198 }
199 if ( !empty( $options['img-class'] ) ) {
200 $attribs['class'] = $options['img-class'];
201 }
202 return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
203 }
204
205 }
206
207 /**
208 * Basic media transform error class
209 *
210 * @ingroup Media
211 */
212 class MediaTransformError extends MediaTransformOutput {
213 var $htmlMsg, $textMsg, $width, $height, $url, $path;
214
215 function __construct( $msg, $width, $height /*, ... */ ) {
216 $args = array_slice( func_get_args(), 3 );
217 $htmlArgs = array_map( 'htmlspecialchars', $args );
218 $htmlArgs = array_map( 'nl2br', $htmlArgs );
219
220 $this->htmlMsg = wfMessage( $msg )->rawParams( $htmlArgs )->escaped();
221 $this->textMsg = wfMessage( $msg )->rawParams( $htmlArgs )->text();
222 $this->width = intval( $width );
223 $this->height = intval( $height );
224 $this->url = false;
225 $this->path = false;
226 }
227
228 function toHtml( $options = array() ) {
229 return "<div class=\"MediaTransformError\" style=\"" .
230 "width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" .
231 $this->htmlMsg .
232 "</div>";
233 }
234
235 function toText() {
236 return $this->textMsg;
237 }
238
239 function getHtmlMsg() {
240 return $this->htmlMsg;
241 }
242
243 function isError() {
244 return true;
245 }
246 }
247
248 /**
249 * Shortcut class for parameter validation errors
250 *
251 * @ingroup Media
252 */
253 class TransformParameterError extends MediaTransformError {
254 function __construct( $params ) {
255 parent::__construct( 'thumbnail_error',
256 max( isset( $params['width'] ) ? $params['width'] : 0, 120 ),
257 max( isset( $params['height'] ) ? $params['height'] : 0, 120 ),
258 wfMsg( 'thumbnail_invalid_params' ) );
259 }
260 }