* Introduced media handler modules for file-type specific operations: thumbnailing...
[lhc/web/wiklou.git] / includes / MediaTransformOutput.php
1 <?php
2
3 /**
4 * Base class for the output of MediaHandler::doTransform() and Image::transform().
5 */
6 abstract class MediaTransformOutput {
7 /**
8 * Get the width of the output box
9 */
10 function getWidth() {
11 return $this->width;
12 }
13
14 /**
15 * Get the height of the output box
16 */
17 function getHeight() {
18 return $this->height;
19 }
20
21 /**
22 * @return string The thumbnail URL
23 */
24 function getUrl() {
25 return $this->url;
26 }
27
28 /**
29 * @return string Destination file path (local filesystem)
30 */
31 function getPath() {
32 return $this->path;
33 }
34
35 /**
36 * Fetch HTML for this transform output
37 * @param array $attribs Advisory associative array of HTML attributes supplied
38 * by the linker. These can be incorporated into the output in any way.
39 * @param array $linkAttribs Attributes of a suggested enclosing <a> tag.
40 * May be ignored.
41 */
42 abstract function toHtml( $attribs = array() , $linkAttribs = false );
43
44 /**
45 * This will be overridden to return true in error classes
46 */
47 function isError() {
48 return false;
49 }
50
51 /**
52 * Wrap some XHTML text in an anchor tag with the given attributes
53 */
54 protected function linkWrap( $linkAttribs, $contents ) {
55 if ( $linkAttribs ) {
56 return Xml::tags( 'a', $linkAttribs, $contents );
57 } else {
58 return $contents;
59 }
60 }
61 }
62
63
64 /**
65 * Media transform output for images
66 */
67 class ThumbnailImage extends MediaTransformOutput {
68 /**
69 * @param string $path Filesystem path to the thumb
70 * @param string $url URL path to the thumb
71 * @private
72 */
73 function ThumbnailImage( $url, $width, $height, $path = false ) {
74 $this->url = $url;
75 # These should be integers when they get here.
76 # If not, there's a bug somewhere. But let's at
77 # least produce valid HTML code regardless.
78 $this->width = round( $width );
79 $this->height = round( $height );
80 $this->path = $path;
81 }
82
83 /**
84 * Return HTML <img ... /> tag for the thumbnail, will include
85 * width and height attributes and a blank alt text (as required).
86 *
87 * You can set or override additional attributes by passing an
88 * associative array of name => data pairs. The data will be escaped
89 * for HTML output, so should be in plaintext.
90 *
91 * If $linkAttribs is given, the image will be enclosed in an <a> tag.
92 *
93 * @param array $attribs
94 * @param array $linkAttribs
95 * @return string
96 * @public
97 */
98 function toHtml( $attribs = array(), $linkAttribs = false ) {
99 $attribs['src'] = $this->url;
100 $attribs['width'] = $this->width;
101 $attribs['height'] = $this->height;
102 if( !isset( $attribs['alt'] ) ) $attribs['alt'] = '';
103 return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
104 }
105
106 }
107
108 /**
109 * Basic media transform error class
110 */
111 class MediaTransformError extends MediaTransformOutput {
112 var $htmlMsg, $textMsg, $width, $height, $url, $path;
113
114 function __construct( $msg, $width, $height /*, ... */ ) {
115 $args = array_slice( func_get_args(), 3 );
116 $htmlArgs = array_map( 'htmlspecialchars', $args );
117 $htmlArgs = array_map( 'nl2br', $htmlArgs );
118
119 $this->htmlMsg = wfMsgReplaceArgs( htmlspecialchars( wfMsgGetKey( $msg, true ) ), $htmlArgs );
120 $this->textMsg = wfMsgReal( $msg, $args );
121 $this->width = intval( $width );
122 $this->height = intval( $height );
123 $this->url = false;
124 $this->path = false;
125 }
126
127 function toHtml( $attribs = array(), $linkAttribs = false ) {
128 return "<table class=\"MediaTransformError\" style=\"" .
129 "width: {$this->width}px; height: {$this->height}px;\"><tr><td>" .
130 $this->htmlMsg .
131 "</td></tr></table>";
132 }
133
134 function toText() {
135 return $this->textMsg;
136 }
137
138 function getHtmlMsg() {
139 return $this->htmlMsg;
140 }
141
142 function isError() {
143 return true;
144 }
145 }
146
147 /**
148 * Shortcut class for parameter validation errors
149 */
150 class TransformParameterError extends MediaTransformError {
151 function __construct( $params ) {
152 parent::__construct( 'thumbnail_error',
153 max( @$params['width'], 180 ), max( @$params['height'], 180 ),
154 wfMsg( 'thumbnail_invalid_params' ) );
155 }
156 }
157
158 ?>