Merge "Drop characters in Unicode Private Use Area in conversion tables."
[lhc/web/wiklou.git] / includes / media / MediaTransformOutput.php
1 <?php
2 /**
3 * Base class for the output of file transformation methods.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Media
22 */
23
24 /**
25 * Base class for the output of MediaHandler::doTransform() and File::transform().
26 *
27 * @ingroup Media
28 */
29 abstract class MediaTransformOutput {
30 /**
31 * @var File
32 */
33 var $file;
34
35 var $width, $height, $url, $page, $path;
36 protected $storagePath = false;
37
38 /**
39 * Get the width of the output box
40 */
41 public function getWidth() {
42 return $this->width;
43 }
44
45 /**
46 * Get the height of the output box
47 */
48 public function getHeight() {
49 return $this->height;
50 }
51
52 /**
53 * @return string The thumbnail URL
54 */
55 public function getUrl() {
56 return $this->url;
57 }
58
59 /**
60 * @return string|bool The permanent thumbnail storage path
61 */
62 public function getStoragePath() {
63 return $this->storagePath;
64 }
65
66 /**
67 * @param $storagePath string The permanent storage path
68 * @return void
69 */
70 public function setStoragePath( $storagePath ) {
71 $this->storagePath = $storagePath;
72 }
73
74 /**
75 * Fetch HTML for this transform output
76 *
77 * @param $options array Associative array of options. Boolean options
78 * should be indicated with a value of true for true, and false or
79 * absent for false.
80 *
81 * alt Alternate text or caption
82 * desc-link Boolean, show a description link
83 * file-link Boolean, show a file download link
84 * custom-url-link Custom URL to link to
85 * custom-title-link Custom Title object to link to
86 * valign vertical-align property, if the output is an inline element
87 * img-class Class applied to the "<img>" tag, if there is such a tag
88 *
89 * For images, desc-link and file-link are implemented as a click-through. For
90 * sounds and videos, they may be displayed in other ways.
91 *
92 * @return string
93 */
94 abstract public function toHtml( $options = array() );
95
96 /**
97 * This will be overridden to return true in error classes
98 * @return bool
99 */
100 public function isError() {
101 return false;
102 }
103
104 /**
105 * Check if an output thumbnail file actually exists.
106 * This will return false if there was an error, the
107 * thumbnail is to be handled client-side only, or if
108 * transformation was deferred via TRANSFORM_LATER.
109 *
110 * @return Bool
111 */
112 public function hasFile() {
113 // If TRANSFORM_LATER, $this->path will be false.
114 // Note: a null path means "use the source file".
115 return ( !$this->isError() && ( $this->path || $this->path === null ) );
116 }
117
118 /**
119 * Check if the output thumbnail is the same as the source.
120 * This can occur if the requested width was bigger than the source.
121 *
122 * @return Bool
123 */
124 public function fileIsSource() {
125 return ( !$this->isError() && $this->path === null );
126 }
127
128 /**
129 * Get the path of a file system copy of the thumbnail.
130 * Callers should never write to this path.
131 *
132 * @return string|bool Returns false if there isn't one
133 */
134 public function getLocalCopyPath() {
135 if ( $this->isError() ) {
136 return false;
137 } elseif ( $this->path === null ) {
138 return $this->file->getLocalRefPath();
139 } else {
140 return $this->path; // may return false
141 }
142 }
143
144 /**
145 * Stream the file if there were no errors
146 *
147 * @param $headers Array Additional HTTP headers to send on success
148 * @return Bool success
149 */
150 public function streamFile( $headers = array() ) {
151 return $this->path && StreamFile::stream( $this->getLocalCopyPath(), $headers );
152 }
153
154 /**
155 * Wrap some XHTML text in an anchor tag with the given attributes
156 *
157 * @param $linkAttribs array
158 * @param $contents string
159 *
160 * @return string
161 */
162 protected function linkWrap( $linkAttribs, $contents ) {
163 if ( $linkAttribs ) {
164 return Xml::tags( 'a', $linkAttribs, $contents );
165 } else {
166 return $contents;
167 }
168 }
169
170 /**
171 * @param $title string
172 * @param $params array
173 * @return array
174 */
175 public function getDescLinkAttribs( $title = null, $params = '' ) {
176 $query = $this->page ? ( 'page=' . urlencode( $this->page ) ) : '';
177 if( $params ) {
178 $query .= $query ? '&'.$params : $params;
179 }
180 $attribs = array(
181 'href' => $this->file->getTitle()->getLocalURL( $query ),
182 'class' => 'image',
183 );
184 if ( $title ) {
185 $attribs['title'] = $title;
186 }
187 return $attribs;
188 }
189 }
190
191 /**
192 * Media transform output for images
193 *
194 * @ingroup Media
195 */
196 class ThumbnailImage extends MediaTransformOutput {
197 /**
198 * Get a thumbnail object from a file and parameters.
199 * If $path is set to null, the output file is treated as a source copy.
200 * If $path is set to false, no output file will be created.
201 *
202 * @param $file File object
203 * @param $url String: URL path to the thumb
204 * @param $width Integer: file's width
205 * @param $height Integer: file's height
206 * @param $path String|bool|null: filesystem path to the thumb
207 * @param $page Integer: page number, for multipage files
208 * @private
209 */
210 function __construct( $file, $url, $width, $height, $path = false, $page = false ) {
211 $this->file = $file;
212 $this->url = $url;
213 # These should be integers when they get here.
214 # If not, there's a bug somewhere. But let's at
215 # least produce valid HTML code regardless.
216 $this->width = round( $width );
217 $this->height = round( $height );
218 $this->path = $path;
219 $this->page = $page;
220 }
221
222 /**
223 * Return HTML <img ... /> tag for the thumbnail, will include
224 * width and height attributes and a blank alt text (as required).
225 *
226 * @param $options array Associative array of options. Boolean options
227 * should be indicated with a value of true for true, and false or
228 * absent for false.
229 *
230 * alt HTML alt attribute
231 * title HTML title attribute
232 * desc-link Boolean, show a description link
233 * file-link Boolean, show a file download link
234 * valign vertical-align property, if the output is an inline element
235 * img-class Class applied to the \<img\> tag, if there is such a tag
236 * desc-query String, description link query params
237 * custom-url-link Custom URL to link to
238 * custom-title-link Custom Title object to link to
239 * custom target-link Value of the target attribute, for custom-target-link
240 *
241 * For images, desc-link and file-link are implemented as a click-through. For
242 * sounds and videos, they may be displayed in other ways.
243 *
244 * @return string
245 */
246 function toHtml( $options = array() ) {
247 if ( count( func_get_args() ) == 2 ) {
248 throw new MWException( __METHOD__ .' called in the old style' );
249 }
250
251 $alt = empty( $options['alt'] ) ? '' : $options['alt'];
252
253 $query = empty( $options['desc-query'] ) ? '' : $options['desc-query'];
254
255 if ( !empty( $options['custom-url-link'] ) ) {
256 $linkAttribs = array( 'href' => $options['custom-url-link'] );
257 if ( !empty( $options['title'] ) ) {
258 $linkAttribs['title'] = $options['title'];
259 }
260 if ( !empty( $options['custom-target-link'] ) ) {
261 $linkAttribs['target'] = $options['custom-target-link'];
262 }
263 } elseif ( !empty( $options['custom-title-link'] ) ) {
264 $title = $options['custom-title-link'];
265 $linkAttribs = array(
266 'href' => $title->getLinkURL(),
267 'title' => empty( $options['title'] ) ? $title->getFullText() : $options['title']
268 );
269 } elseif ( !empty( $options['desc-link'] ) ) {
270 $linkAttribs = $this->getDescLinkAttribs( empty( $options['title'] ) ? null : $options['title'], $query );
271 } elseif ( !empty( $options['file-link'] ) ) {
272 $linkAttribs = array( 'href' => $this->file->getURL() );
273 } else {
274 $linkAttribs = false;
275 }
276
277 $attribs = array(
278 'alt' => $alt,
279 'src' => $this->url,
280 'width' => $this->width,
281 'height' => $this->height,
282 );
283 if ( !empty( $options['valign'] ) ) {
284 $attribs['style'] = "vertical-align: {$options['valign']}";
285 }
286 if ( !empty( $options['img-class'] ) ) {
287 $attribs['class'] = $options['img-class'];
288 }
289 return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
290 }
291
292 }
293
294 /**
295 * Basic media transform error class
296 *
297 * @ingroup Media
298 */
299 class MediaTransformError extends MediaTransformOutput {
300 var $htmlMsg, $textMsg, $width, $height, $url, $path;
301
302 function __construct( $msg, $width, $height /*, ... */ ) {
303 $args = array_slice( func_get_args(), 3 );
304 $htmlArgs = array_map( 'htmlspecialchars', $args );
305 $htmlArgs = array_map( 'nl2br', $htmlArgs );
306
307 $this->htmlMsg = wfMessage( $msg )->rawParams( $htmlArgs )->escaped();
308 $this->textMsg = wfMessage( $msg )->rawParams( $htmlArgs )->text();
309 $this->width = intval( $width );
310 $this->height = intval( $height );
311 $this->url = false;
312 $this->path = false;
313 }
314
315 function toHtml( $options = array() ) {
316 return "<div class=\"MediaTransformError\" style=\"" .
317 "width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" .
318 $this->htmlMsg .
319 "</div>";
320 }
321
322 function toText() {
323 return $this->textMsg;
324 }
325
326 function getHtmlMsg() {
327 return $this->htmlMsg;
328 }
329
330 function isError() {
331 return true;
332 }
333 }
334
335 /**
336 * Shortcut class for parameter validation errors
337 *
338 * @ingroup Media
339 */
340 class TransformParameterError extends MediaTransformError {
341 function __construct( $params ) {
342 parent::__construct( 'thumbnail_error',
343 max( isset( $params['width'] ) ? $params['width'] : 0, 120 ),
344 max( isset( $params['height'] ) ? $params['height'] : 0, 120 ),
345 wfMsg( 'thumbnail_invalid_params' ) );
346 }
347 }