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