Whitespace fixes for r62402
[lhc/web/wiklou.git] / includes / 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 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.
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 * @param string $path Filesystem path to the thumb
108 * @param string $url URL path to the thumb
109 * @private
110 */
111 function ThumbnailImage( $file, $url, $width, $height, $path = false, $page = false ) {
112 $this->file = $file;
113 $this->url = $url;
114 # These should be integers when they get here.
115 # If not, there's a bug somewhere. But let's at
116 # least produce valid HTML code regardless.
117 $this->width = round( $width );
118 $this->height = round( $height );
119 $this->path = $path;
120 $this->page = $page;
121 }
122
123 /**
124 * Return HTML <img ... /> tag for the thumbnail, will include
125 * width and height attributes and a blank alt text (as required).
126 *
127 * @param array $options Associative array of options. Boolean options
128 * should be indicated with a value of true for true, and false or
129 * absent for false.
130 *
131 * alt HTML alt attribute
132 * title HTML title attribute
133 * desc-link Boolean, show a description link
134 * file-link Boolean, show a file download link
135 * valign vertical-align property, if the output is an inline element
136 * img-class Class applied to the <img> tag, if there is such a tag
137 * desc-query String, description link query params
138 * custom-url-link Custom URL to link to
139 * custom-title-link Custom Title object to link to
140 *
141 * For images, desc-link and file-link are implemented as a click-through. For
142 * sounds and videos, they may be displayed in other ways.
143 *
144 * @return string
145 * @public
146 */
147 function toHtml( $options = array() ) {
148 if ( count( func_get_args() ) == 2 ) {
149 throw new MWException( __METHOD__ .' called in the old style' );
150 }
151
152 $alt = empty( $options['alt'] ) ? '' : $options['alt'];
153
154 $query = empty( $options['desc-query'] ) ? '' : $options['desc-query'];
155
156 if ( !empty( $options['custom-url-link'] ) ) {
157 $linkAttribs = array( 'href' => $options['custom-url-link'] );
158 if ( !empty( $options['title'] ) ) {
159 $linkAttribs['title'] = $options['title'];
160 }
161 } elseif ( !empty( $options['custom-title-link'] ) ) {
162 $title = $options['custom-title-link'];
163 $linkAttribs = array(
164 'href' => $title->getLinkUrl(),
165 'title' => empty( $options['title'] ) ? $title->getFullText() : $options['title']
166 );
167 } elseif ( !empty( $options['desc-link'] ) ) {
168 $linkAttribs = $this->getDescLinkAttribs( empty( $options['title'] ) ? null : $options['title'], $query );
169 } elseif ( !empty( $options['file-link'] ) ) {
170 $linkAttribs = array( 'href' => $this->file->getURL() );
171 } else {
172 $linkAttribs = false;
173 }
174
175 $attribs = array(
176 'alt' => $alt,
177 'src' => $this->url,
178 'width' => $this->width,
179 'height' => $this->height,
180 );
181 if ( !empty( $options['valign'] ) ) {
182 $attribs['style'] = "vertical-align: {$options['valign']}";
183 }
184 if ( !empty( $options['img-class'] ) ) {
185 $attribs['class'] = $options['img-class'];
186 }
187 return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
188 }
189
190 }
191
192 /**
193 * Basic media transform error class
194 *
195 * @ingroup Media
196 */
197 class MediaTransformError extends MediaTransformOutput {
198 var $htmlMsg, $textMsg, $width, $height, $url, $path;
199
200 function __construct( $msg, $width, $height /*, ... */ ) {
201 $args = array_slice( func_get_args(), 3 );
202 $htmlArgs = array_map( 'htmlspecialchars', $args );
203 $htmlArgs = array_map( 'nl2br', $htmlArgs );
204
205 $this->htmlMsg = wfMsgReplaceArgs( htmlspecialchars( wfMsgGetKey( $msg, true ) ), $htmlArgs );
206 $this->textMsg = wfMsgReal( $msg, $args );
207 $this->width = intval( $width );
208 $this->height = intval( $height );
209 $this->url = false;
210 $this->path = false;
211 }
212
213 function toHtml( $options = array() ) {
214 return "<table class=\"MediaTransformError\" style=\"" .
215 "width: {$this->width}px; height: {$this->height}px;\"><tr><td>" .
216 $this->htmlMsg .
217 "</td></tr></table>";
218 }
219
220 function toText() {
221 return $this->textMsg;
222 }
223
224 function getHtmlMsg() {
225 return $this->htmlMsg;
226 }
227
228 function isError() {
229 return true;
230 }
231 }
232
233 /**
234 * Shortcut class for parameter validation errors
235 *
236 * @ingroup Media
237 */
238 class TransformParameterError extends MediaTransformError {
239 function __construct( $params ) {
240 parent::__construct( 'thumbnail_error',
241 max( isset( $params['width'] ) ? $params['width'] : 0, 180 ),
242 max( isset( $params['height'] ) ? $params['height'] : 0, 180 ),
243 wfMsg( 'thumbnail_invalid_params' ) );
244 }
245 }