PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/phase3/includes/media/MediaTransformOutput.php

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