00001 <?php
00012 abstract class MediaTransformOutput {
00013 var $file, $width, $height, $url, $page, $path;
00014
00018 function getWidth() {
00019 return $this->width;
00020 }
00021
00025 function getHeight() {
00026 return $this->height;
00027 }
00028
00032 function getUrl() {
00033 return $this->url;
00034 }
00035
00039 function getPath() {
00040 return $this->path;
00041 }
00042
00063 abstract function toHtml( $options = array() );
00064
00068 function isError() {
00069 return false;
00070 }
00071
00075 protected function linkWrap( $linkAttribs, $contents ) {
00076 if ( $linkAttribs ) {
00077 return Xml::tags( 'a', $linkAttribs, $contents );
00078 } else {
00079 return $contents;
00080 }
00081 }
00082
00083 function getDescLinkAttribs( $title = null, $params = '' ) {
00084 $query = $this->page ? ( 'page=' . urlencode( $this->page ) ) : '';
00085 if( $params ) {
00086 $query .= $query ? '&'.$params : $params;
00087 }
00088 $attribs = array(
00089 'href' => $this->file->getTitle()->getLocalURL( $query ),
00090 'class' => 'image',
00091 );
00092 if ( $title ) {
00093 $attribs['title'] = $title;
00094 }
00095 return $attribs;
00096 }
00097 }
00098
00099
00105 class ThumbnailImage extends MediaTransformOutput {
00111 function ThumbnailImage( $file, $url, $width, $height, $path = false, $page = false ) {
00112 $this->file = $file;
00113 $this->url = $url;
00114 # These should be integers when they get here.
00115 # If not, there's a bug somewhere. But let's at
00116 # least produce valid HTML code regardless.
00117 $this->width = round( $width );
00118 $this->height = round( $height );
00119 $this->path = $path;
00120 $this->page = $page;
00121 }
00122
00147 function toHtml( $options = array() ) {
00148 if ( count( func_get_args() ) == 2 ) {
00149 throw new MWException( __METHOD__ .' called in the old style' );
00150 }
00151
00152 $alt = empty( $options['alt'] ) ? '' : $options['alt'];
00153
00154 $query = empty( $options['desc-query'] ) ? '' : $options['desc-query'];
00155
00156 if ( !empty( $options['custom-url-link'] ) ) {
00157 $linkAttribs = array( 'href' => $options['custom-url-link'] );
00158 if ( !empty( $options['title'] ) ) {
00159 $linkAttribs['title'] = $options['title'];
00160 }
00161 } elseif ( !empty( $options['custom-title-link'] ) ) {
00162 $title = $options['custom-title-link'];
00163 $linkAttribs = array(
00164 'href' => $title->getLinkUrl(),
00165 'title' => empty( $options['title'] ) ? $title->getFullText() : $options['title']
00166 );
00167 } elseif ( !empty( $options['desc-link'] ) ) {
00168 $linkAttribs = $this->getDescLinkAttribs( empty( $options['title'] ) ? null : $options['title'], $query );
00169 } elseif ( !empty( $options['file-link'] ) ) {
00170 $linkAttribs = array( 'href' => $this->file->getURL() );
00171 } else {
00172 $linkAttribs = false;
00173 }
00174
00175 $attribs = array(
00176 'alt' => $alt,
00177 'src' => $this->url,
00178 'width' => $this->width,
00179 'height' => $this->height,
00180 );
00181 if ( !empty( $options['valign'] ) ) {
00182 $attribs['style'] = "vertical-align: {$options['valign']}";
00183 }
00184 if ( !empty( $options['img-class'] ) ) {
00185 $attribs['class'] = $options['img-class'];
00186 }
00187 return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
00188 }
00189
00190 }
00191
00197 class MediaTransformError extends MediaTransformOutput {
00198 var $htmlMsg, $textMsg, $width, $height, $url, $path;
00199
00200 function __construct( $msg, $width, $height ) {
00201 $args = array_slice( func_get_args(), 3 );
00202 $htmlArgs = array_map( 'htmlspecialchars', $args );
00203 $htmlArgs = array_map( 'nl2br', $htmlArgs );
00204
00205 $this->htmlMsg = wfMsgReplaceArgs( htmlspecialchars( wfMsgGetKey( $msg, true ) ), $htmlArgs );
00206 $this->textMsg = wfMsgReal( $msg, $args );
00207 $this->width = intval( $width );
00208 $this->height = intval( $height );
00209 $this->url = false;
00210 $this->path = false;
00211 }
00212
00213 function toHtml( $options = array() ) {
00214 return "<table class=\"MediaTransformError\" style=\"" .
00215 "width: {$this->width}px; height: {$this->height}px;\"><tr><td>" .
00216 $this->htmlMsg .
00217 "</td></tr></table>";
00218 }
00219
00220 function toText() {
00221 return $this->textMsg;
00222 }
00223
00224 function getHtmlMsg() {
00225 return $this->htmlMsg;
00226 }
00227
00228 function isError() {
00229 return true;
00230 }
00231 }
00232
00238 class TransformParameterError extends MediaTransformError {
00239 function __construct( $params ) {
00240 parent::__construct( 'thumbnail_error',
00241 max( isset( $params['width'] ) ? $params['width'] : 0, 180 ),
00242 max( isset( $params['height'] ) ? $params['height'] : 0, 180 ),
00243 wfMsg( 'thumbnail_invalid_params' ) );
00244 }
00245 }