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