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( $alt = false, $params = '' ) {
00084 $query = $this->page ? ( 'page=' . urlencode( $this->page ) ) : '';
00085 if( $params ) {
00086 $query .= $query ? '&'.$params : $params;
00087 }
00088 $title = $this->file->getTitle();
00089 if ( strval( $alt ) === '' ) {
00090 $alt = $title->getText();
00091 }
00092 return array(
00093 'href' => $this->file->getTitle()->getLocalURL( $query ),
00094 'class' => 'image',
00095 'title' => $alt
00096 );
00097 }
00098 }
00099
00100
00106 class ThumbnailImage extends MediaTransformOutput {
00112 function ThumbnailImage( $file, $url, $width, $height, $path = false, $page = false ) {
00113 $this->file = $file;
00114 $this->url = $url;
00115 # These should be integers when they get here.
00116 # If not, there's a bug somewhere. But let's at
00117 # least produce valid HTML code regardless.
00118 $this->width = round( $width );
00119 $this->height = round( $height );
00120 $this->path = $path;
00121 $this->page = $page;
00122 }
00123
00148 function toHtml( $options = array() ) {
00149 if ( count( func_get_args() ) == 2 ) {
00150 throw new MWException( __METHOD__ .' called in the old style' );
00151 }
00152
00153 $alt = empty( $options['alt'] ) ? '' : $options['alt'];
00154 # Note: if title is empty and alt is not, make the title empty, don't
00155 # use alt; only use alt if title is not set
00156 $title = !isset( $options['title'] ) ? $alt : $options['title'];
00157 $query = empty($options['desc-query']) ? '' : $options['desc-query'];
00158
00159 if ( !empty( $options['custom-url-link'] ) ) {
00160 $linkAttribs = array( 'href' => $options['custom-url-link'] );
00161 } elseif ( !empty( $options['custom-title-link'] ) ) {
00162 $title = $options['custom-title-link'];
00163 $linkAttribs = array( 'href' => $title->getLinkUrl(), 'title' => $title->getFullText() );
00164 } elseif ( !empty( $options['desc-link'] ) ) {
00165 $linkAttribs = $this->getDescLinkAttribs( $title, $query );
00166 } elseif ( !empty( $options['file-link'] ) ) {
00167 $linkAttribs = array( 'href' => $this->file->getURL() );
00168 } else {
00169 $linkAttribs = false;
00170 }
00171
00172 $attribs = array(
00173 'alt' => $alt,
00174 'src' => $this->url,
00175 'width' => $this->width,
00176 'height' => $this->height,
00177 'border' => 0,
00178 );
00179 if ( !empty( $options['valign'] ) ) {
00180 $attribs['style'] = "vertical-align: {$options['valign']}";
00181 }
00182 if ( !empty( $options['img-class'] ) ) {
00183 $attribs['class'] = $options['img-class'];
00184 }
00185 return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
00186 }
00187
00188 }
00189
00195 class MediaTransformError extends MediaTransformOutput {
00196 var $htmlMsg, $textMsg, $width, $height, $url, $path;
00197
00198 function __construct( $msg, $width, $height ) {
00199 $args = array_slice( func_get_args(), 3 );
00200 $htmlArgs = array_map( 'htmlspecialchars', $args );
00201 $htmlArgs = array_map( 'nl2br', $htmlArgs );
00202
00203 $this->htmlMsg = wfMsgReplaceArgs( htmlspecialchars( wfMsgGetKey( $msg, true ) ), $htmlArgs );
00204 $this->textMsg = wfMsgReal( $msg, $args );
00205 $this->width = intval( $width );
00206 $this->height = intval( $height );
00207 $this->url = false;
00208 $this->path = false;
00209 }
00210
00211 function toHtml( $options = array() ) {
00212 return "<table class=\"MediaTransformError\" style=\"" .
00213 "width: {$this->width}px; height: {$this->height}px;\"><tr><td>" .
00214 $this->htmlMsg .
00215 "</td></tr></table>";
00216 }
00217
00218 function toText() {
00219 return $this->textMsg;
00220 }
00221
00222 function getHtmlMsg() {
00223 return $this->htmlMsg;
00224 }
00225
00226 function isError() {
00227 return true;
00228 }
00229 }
00230
00236 class TransformParameterError extends MediaTransformError {
00237 function __construct( $params ) {
00238 parent::__construct( 'thumbnail_error',
00239 max( isset( $params['width'] ) ? $params['width'] : 0, 180 ),
00240 max( isset( $params['height'] ) ? $params['height'] : 0, 180 ),
00241 wfMsg( 'thumbnail_invalid_params' ) );
00242 }
00243 }