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