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