Adding messages for new global group 'sysadmin'. Brion, Tim, Kate and JeLuF are all...
[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 * valign vertical-align property, if the output is an inline element
54 * img-class Class applied to the <img> tag, if there is such a tag
55 *
56 * For images, desc-link and file-link are implemented as a click-through. For
57 * sounds and videos, they may be displayed in other ways.
58 *
59 * @return string
60 */
61 abstract function toHtml( $options = array() );
62
63 /**
64 * This will be overridden to return true in error classes
65 */
66 function isError() {
67 return false;
68 }
69
70 /**
71 * Wrap some XHTML text in an anchor tag with the given attributes
72 */
73 protected function linkWrap( $linkAttribs, $contents ) {
74 if ( $linkAttribs ) {
75 return Xml::tags( 'a', $linkAttribs, $contents );
76 } else {
77 return $contents;
78 }
79 }
80
81 function getDescLinkAttribs( $alt = false, $params = '' ) {
82 $query = $this->page ? ( 'page=' . urlencode( $this->page ) ) : '';
83 if( $params ) {
84 $query .= $query ? '&'.$params : $params;
85 }
86 $title = $this->file->getTitle();
87 if ( strval( $alt ) === '' ) {
88 $alt = $title->getText();
89 }
90 return array(
91 'href' => $this->file->getTitle()->getLocalURL( $query ),
92 'class' => 'image',
93 'title' => $alt
94 );
95 }
96 }
97
98
99 /**
100 * Media transform output for images
101 *
102 * @ingroup Media
103 */
104 class ThumbnailImage extends MediaTransformOutput {
105 /**
106 * @param string $path Filesystem path to the thumb
107 * @param string $url URL path to the thumb
108 * @private
109 */
110 function ThumbnailImage( $file, $url, $width, $height, $path = false, $page = false ) {
111 $this->file = $file;
112 $this->url = $url;
113 # These should be integers when they get here.
114 # If not, there's a bug somewhere. But let's at
115 # least produce valid HTML code regardless.
116 $this->width = round( $width );
117 $this->height = round( $height );
118 $this->path = $path;
119 $this->page = $page;
120 }
121
122 /**
123 * Return HTML <img ... /> tag for the thumbnail, will include
124 * width and height attributes and a blank alt text (as required).
125 *
126 * @param array $options Associative array of options. Boolean options
127 * should be indicated with a value of true for true, and false or
128 * absent for false.
129 *
130 * alt Alternate text or caption
131 * desc-link Boolean, show a description link
132 * file-link Boolean, show a file download link
133 * valign vertical-align property, if the output is an inline element
134 * img-class Class applied to the <img> tag, if there is such a tag
135 * desc-query String, description link query params
136 *
137 * For images, desc-link and file-link are implemented as a click-through. For
138 * sounds and videos, they may be displayed in other ways.
139 *
140 * @return string
141 * @public
142 */
143 function toHtml( $options = array() ) {
144 if ( count( func_get_args() ) == 2 ) {
145 throw new MWException( __METHOD__ .' called in the old style' );
146 }
147
148 $alt = empty( $options['alt'] ) ? '' : $options['alt'];
149 $query = empty($options['desc-query']) ? '' : $options['desc-query'];
150 if ( !empty( $options['desc-link'] ) ) {
151 $linkAttribs = $this->getDescLinkAttribs( $alt, $query );
152 } elseif ( !empty( $options['file-link'] ) ) {
153 $linkAttribs = array( 'href' => $this->file->getURL() );
154 } else {
155 $linkAttribs = false;
156 }
157
158 $attribs = array(
159 'alt' => $alt,
160 'src' => $this->url,
161 'width' => $this->width,
162 'height' => $this->height,
163 'border' => 0,
164 );
165 if ( !empty( $options['valign'] ) ) {
166 $attribs['style'] = "vertical-align: {$options['valign']}";
167 }
168 if ( !empty( $options['img-class'] ) ) {
169 $attribs['class'] = $options['img-class'];
170 }
171 return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
172 }
173
174 }
175
176 /**
177 * Basic media transform error class
178 *
179 * @ingroup Media
180 */
181 class MediaTransformError extends MediaTransformOutput {
182 var $htmlMsg, $textMsg, $width, $height, $url, $path;
183
184 function __construct( $msg, $width, $height /*, ... */ ) {
185 $args = array_slice( func_get_args(), 3 );
186 $htmlArgs = array_map( 'htmlspecialchars', $args );
187 $htmlArgs = array_map( 'nl2br', $htmlArgs );
188
189 $this->htmlMsg = wfMsgReplaceArgs( htmlspecialchars( wfMsgGetKey( $msg, true ) ), $htmlArgs );
190 $this->textMsg = wfMsgReal( $msg, $args );
191 $this->width = intval( $width );
192 $this->height = intval( $height );
193 $this->url = false;
194 $this->path = false;
195 }
196
197 function toHtml( $options = array() ) {
198 return "<table class=\"MediaTransformError\" style=\"" .
199 "width: {$this->width}px; height: {$this->height}px;\"><tr><td>" .
200 $this->htmlMsg .
201 "</td></tr></table>";
202 }
203
204 function toText() {
205 return $this->textMsg;
206 }
207
208 function getHtmlMsg() {
209 return $this->htmlMsg;
210 }
211
212 function isError() {
213 return true;
214 }
215 }
216
217 /**
218 * Shortcut class for parameter validation errors
219 *
220 * @ingroup Media
221 */
222 class TransformParameterError extends MediaTransformError {
223 function __construct( $params ) {
224 parent::__construct( 'thumbnail_error',
225 max( isset( $params['width'] ) ? $params['width'] : 0, 180 ),
226 max( isset( $params['height'] ) ? $params['height'] : 0, 180 ),
227 wfMsg( 'thumbnail_invalid_params' ) );
228 }
229 }