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