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