MediaTransformOutput.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * @file
  4. * @ingroup Media
  5. */
  6. /**
  7. * Base class for the output of MediaHandler::doTransform() and File::transform().
  8. *
  9. * @ingroup Media
  10. */
  11. abstract class MediaTransformOutput {
  12. var $file, $width, $height, $url, $page, $path;
  13. /**
  14. * Get the width of the output box
  15. */
  16. function getWidth() {
  17. return $this->width;
  18. }
  19. /**
  20. * Get the height of the output box
  21. */
  22. function getHeight() {
  23. return $this->height;
  24. }
  25. /**
  26. * @return string The thumbnail URL
  27. */
  28. function getUrl() {
  29. return $this->url;
  30. }
  31. /**
  32. * @return string Destination file path (local filesystem)
  33. */
  34. function getPath() {
  35. return $this->path;
  36. }
  37. /**
  38. * Fetch HTML for this transform output
  39. *
  40. * @param array $options Associative array of options. Boolean options
  41. * should be indicated with a value of true for true, and false or
  42. * absent for false.
  43. *
  44. * alt Alternate text or caption
  45. * desc-link Boolean, show a description link
  46. * file-link Boolean, show a file download link
  47. * custom-url-link Custom URL to link to
  48. * custom-title-link Custom Title object to link to
  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. * This will be overridden to return true in error classes
  60. */
  61. function isError() {
  62. return false;
  63. }
  64. /**
  65. * Wrap some XHTML text in an anchor tag with the given attributes
  66. */
  67. protected function linkWrap( $linkAttribs, $contents ) {
  68. if ( $linkAttribs ) {
  69. return Xml::tags( 'a', $linkAttribs, $contents );
  70. } else {
  71. return $contents;
  72. }
  73. }
  74. function getDescLinkAttribs( $alt = false, $params = '' ) {
  75. $query = $this->page ? ( 'page=' . urlencode( $this->page ) ) : '';
  76. if( $params ) {
  77. $query .= $query ? '&'.$params : $params;
  78. }
  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. * Media transform output for images
  92. *
  93. * @ingroup Media
  94. */
  95. class ThumbnailImage extends MediaTransformOutput {
  96. /**
  97. * @param string $path Filesystem path to the thumb
  98. * @param string $url URL path to the thumb
  99. * @private
  100. */
  101. function ThumbnailImage( $file, $url, $width, $height, $path = false, $page = false ) {
  102. $this->file = $file;
  103. $this->url = $url;
  104. # These should be integers when they get here.
  105. # If not, there's a bug somewhere. But let's at
  106. # least produce valid HTML code regardless.
  107. $this->width = round( $width );
  108. $this->height = round( $height );
  109. $this->path = $path;
  110. $this->page = $page;
  111. }
  112. /**
  113. * Return HTML <img ... /> tag for the thumbnail, will include
  114. * width and height attributes and a blank alt text (as required).
  115. *
  116. * @param array $options Associative array of options. Boolean options
  117. * should be indicated with a value of true for true, and false or
  118. * absent for false.
  119. *
  120. * alt HTML alt attribute
  121. * title HTML title attribute
  122. * desc-link Boolean, show a description link
  123. * file-link Boolean, show a file download link
  124. * valign vertical-align property, if the output is an inline element
  125. * img-class Class applied to the <img> tag, if there is such a tag
  126. * desc-query String, description link query params
  127. * custom-url-link Custom URL to link to
  128. * custom-title-link Custom Title object to link to
  129. *
  130. * For images, desc-link and file-link are implemented as a click-through. For
  131. * sounds and videos, they may be displayed in other ways.
  132. *
  133. * @return string
  134. * @public
  135. */
  136. function toHtml( $options = array() ) {
  137. if ( count( func_get_args() ) == 2 ) {
  138. throw new MWException( __METHOD__ .' called in the old style' );
  139. }
  140. $alt = empty( $options['alt'] ) ? '' : $options['alt'];
  141. # Note: if title is empty and alt is not, make the title empty, don't
  142. # use alt; only use alt if title is not set
  143. $title = !isset( $options['title'] ) ? $alt : $options['title'];
  144. $query = empty($options['desc-query']) ? '' : $options['desc-query'];
  145. if ( !empty( $options['custom-url-link'] ) ) {
  146. $linkAttribs = array( 'href' => $options['custom-url-link'] );
  147. } elseif ( !empty( $options['custom-title-link'] ) ) {
  148. $title = $options['custom-title-link'];
  149. $linkAttribs = array( 'href' => $title->getLinkUrl(), 'title' => $title->getFullText() );
  150. } elseif ( !empty( $options['desc-link'] ) ) {
  151. $linkAttribs = $this->getDescLinkAttribs( $title, $query );
  152. } elseif ( !empty( $options['file-link'] ) ) {
  153. $linkAttribs = array( 'href' => $this->file->getURL() );
  154. } else {
  155. $linkAttribs = false;
  156. }
  157. $attribs = array(
  158. 'alt' => $alt,
  159. 'src' => $this->url,
  160. 'width' => $this->width,
  161. 'height' => $this->height,
  162. 'border' => 0,
  163. );
  164. if ( !empty( $options['valign'] ) ) {
  165. $attribs['style'] = "vertical-align: {$options['valign']}";
  166. }
  167. if ( !empty( $options['img-class'] ) ) {
  168. $attribs['class'] = $options['img-class'];
  169. }
  170. return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
  171. }
  172. }
  173. /**
  174. * Basic media transform error class
  175. *
  176. * @ingroup Media
  177. */
  178. class MediaTransformError extends MediaTransformOutput {
  179. var $htmlMsg, $textMsg, $width, $height, $url, $path;
  180. function __construct( $msg, $width, $height /*, ... */ ) {
  181. $args = array_slice( func_get_args(), 3 );
  182. $htmlArgs = array_map( 'htmlspecialchars', $args );
  183. $htmlArgs = array_map( 'nl2br', $htmlArgs );
  184. $this->htmlMsg = wfMsgReplaceArgs( htmlspecialchars( wfMsgGetKey( $msg, true ) ), $htmlArgs );
  185. $this->textMsg = wfMsgReal( $msg, $args );
  186. $this->width = intval( $width );
  187. $this->height = intval( $height );
  188. $this->url = false;
  189. $this->path = false;
  190. }
  191. function toHtml( $options = array() ) {
  192. return "<table class=\"MediaTransformError\" style=\"" .
  193. "width: {$this->width}px; height: {$this->height}px;\"><tr><td>" .
  194. $this->htmlMsg .
  195. "</td></tr></table>";
  196. }
  197. function toText() {
  198. return $this->textMsg;
  199. }
  200. function getHtmlMsg() {
  201. return $this->htmlMsg;
  202. }
  203. function isError() {
  204. return true;
  205. }
  206. }
  207. /**
  208. * Shortcut class for parameter validation errors
  209. *
  210. * @ingroup Media
  211. */
  212. class TransformParameterError extends MediaTransformError {
  213. function __construct( $params ) {
  214. parent::__construct( 'thumbnail_error',
  215. max( isset( $params['width'] ) ? $params['width'] : 0, 180 ),
  216. max( isset( $params['height'] ) ? $params['height'] : 0, 180 ),
  217. wfMsg( 'thumbnail_invalid_params' ) );
  218. }
  219. }