r32045 committed from wrong working branch. Revert and commit the one I wanted.
[mediawiki.git] / includes / MediaTransformOutput.php
blobc6cf9ac2f05681668a3d63596b2a95b43171233e
1 <?php
3 /**
4 * Base class for the output of MediaHandler::doTransform() and File::transform().
6 * @addtogroup Media
7 */
8 abstract class MediaTransformOutput {
9 var $file, $width, $height, $url, $page, $path;
11 /**
12 * Get the width of the output box
14 function getWidth() {
15 return $this->width;
18 /**
19 * Get the height of the output box
21 function getHeight() {
22 return $this->height;
25 /**
26 * @return string The thumbnail URL
28 function getUrl() {
29 return $this->url;
32 /**
33 * @return string Destination file path (local filesystem)
35 function getPath() {
36 return $this->path;
39 /**
40 * Fetch HTML for this transform output
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.
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
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.
55 * @return string
57 abstract function toHtml( $options = array() );
59 /**
60 * This will be overridden to return true in error classes
62 function isError() {
63 return false;
66 /**
67 * Wrap some XHTML text in an anchor tag with the given attributes
69 protected function linkWrap( $linkAttribs, $contents ) {
70 if ( $linkAttribs ) {
71 return Xml::tags( 'a', $linkAttribs, $contents );
72 } else {
73 return $contents;
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();
83 return array(
84 'href' => $this->file->getTitle()->getLocalURL( $query ),
85 'class' => 'image',
86 'title' => $alt
92 /**
93 * Media transform output for images
95 * @addtogroup Media
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
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;
116 * Return HTML <img ... /> tag for the thumbnail, will include
117 * width and height attributes and a blank alt text (as required).
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.
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
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.
132 * @return string
133 * @public
135 function toHtml( $options = array() ) {
136 if ( count( func_get_args() ) == 2 ) {
137 throw new MWException( __METHOD__ .' called in the old style' );
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;
149 $attribs = array(
150 'alt' => $alt,
151 'src' => $this->url,
152 'width' => $this->width,
153 'height' => $this->height,
154 'border' => 0,
156 if ( !empty( $options['valign'] ) ) {
157 $attribs['style'] = "vertical-align: {$options['valign']}";
159 if ( !empty( $options['img-class'] ) ) {
160 $attribs['class'] = $options['img-class'];
162 return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
168 * Basic media transform error class
170 * @addtogroup Media
172 class MediaTransformError extends MediaTransformOutput {
173 var $htmlMsg, $textMsg, $width, $height, $url, $path;
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 );
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;
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>";
195 function toText() {
196 return $this->textMsg;
199 function getHtmlMsg() {
200 return $this->htmlMsg;
203 function isError() {
204 return true;
209 * Shortcut class for parameter validation errors
211 * @addtogroup Media
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' ) );