3 * Base class for the output of file transformation methods.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Base class for the output of MediaHandler::doTransform() and File::transform().
29 abstract class MediaTransformOutput
{
35 var $width, $height, $url, $page, $path;
36 protected $storagePath = false;
39 * @return integer Width of the output box
41 public function getWidth() {
46 * @return integer Height of the output box
48 public function getHeight() {
53 * Get the final extension of the thumbnail.
54 * Returns false for scripted transformations.
55 * @return string|false
57 public function getExtension() {
58 return $this->path ? FileBackend
::extensionFromPath( $this->path
) : false;
62 * @return string|false The thumbnail URL
64 public function getUrl() {
69 * @return string|bool The permanent thumbnail storage path
71 public function getStoragePath() {
72 return $this->storagePath
;
76 * @param $storagePath string The permanent storage path
79 public function setStoragePath( $storagePath ) {
80 $this->storagePath
= $storagePath;
84 * Fetch HTML for this transform output
86 * @param $options array Associative array of options. Boolean options
87 * should be indicated with a value of true for true, and false or
90 * alt Alternate text or caption
91 * desc-link Boolean, show a description link
92 * file-link Boolean, show a file download link
93 * custom-url-link Custom URL to link to
94 * custom-title-link Custom Title object to link to
95 * valign vertical-align property, if the output is an inline element
96 * img-class Class applied to the "<img>" tag, if there is such a tag
98 * For images, desc-link and file-link are implemented as a click-through. For
99 * sounds and videos, they may be displayed in other ways.
103 abstract public function toHtml( $options = array() );
106 * This will be overridden to return true in error classes
109 public function isError() {
114 * Check if an output thumbnail file actually exists.
115 * This will return false if there was an error, the
116 * thumbnail is to be handled client-side only, or if
117 * transformation was deferred via TRANSFORM_LATER.
121 public function hasFile() {
122 // If TRANSFORM_LATER, $this->path will be false.
123 // Note: a null path means "use the source file".
124 return ( !$this->isError() && ( $this->path ||
$this->path
=== null ) );
128 * Check if the output thumbnail is the same as the source.
129 * This can occur if the requested width was bigger than the source.
133 public function fileIsSource() {
134 return ( !$this->isError() && $this->path
=== null );
138 * Get the path of a file system copy of the thumbnail.
139 * Callers should never write to this path.
141 * @return string|bool Returns false if there isn't one
143 public function getLocalCopyPath() {
144 if ( $this->isError() ) {
146 } elseif ( $this->path
=== null ) {
147 return $this->file
->getLocalRefPath();
149 return $this->path
; // may return false
154 * Stream the file if there were no errors
156 * @param $headers Array Additional HTTP headers to send on success
157 * @return Bool success
159 public function streamFile( $headers = array() ) {
160 return $this->path
&& StreamFile
::stream( $this->getLocalCopyPath(), $headers );
164 * Wrap some XHTML text in an anchor tag with the given attributes
166 * @param $linkAttribs array
167 * @param $contents string
171 protected function linkWrap( $linkAttribs, $contents ) {
172 if ( $linkAttribs ) {
173 return Xml
::tags( 'a', $linkAttribs, $contents );
180 * @param $title string
181 * @param $params array
184 public function getDescLinkAttribs( $title = null, $params = '' ) {
185 $query = $this->page ?
( 'page=' . urlencode( $this->page
) ) : '';
187 $query .= $query ?
'&'.$params : $params;
190 'href' => $this->file
->getTitle()->getLocalURL( $query ),
194 $attribs['title'] = $title;
201 * Media transform output for images
205 class ThumbnailImage
extends MediaTransformOutput
{
207 * Get a thumbnail object from a file and parameters.
208 * If $path is set to null, the output file is treated as a source copy.
209 * If $path is set to false, no output file will be created.
211 * @param $file File object
212 * @param $url String: URL path to the thumb
213 * @param $width Integer: file's width
214 * @param $height Integer: file's height
215 * @param $path String|bool|null: filesystem path to the thumb
216 * @param $page Integer: page number, for multipage files
219 function __construct( $file, $url, $width, $height, $path = false, $page = false ) {
222 # These should be integers when they get here.
223 # If not, there's a bug somewhere. But let's at
224 # least produce valid HTML code regardless.
225 $this->width
= round( $width );
226 $this->height
= round( $height );
232 * Return HTML <img ... /> tag for the thumbnail, will include
233 * width and height attributes and a blank alt text (as required).
235 * @param $options array Associative array of options. Boolean options
236 * should be indicated with a value of true for true, and false or
239 * alt HTML alt attribute
240 * title HTML title attribute
241 * desc-link Boolean, show a description link
242 * file-link Boolean, show a file download link
243 * valign vertical-align property, if the output is an inline element
244 * img-class Class applied to the \<img\> tag, if there is such a tag
245 * desc-query String, description link query params
246 * custom-url-link Custom URL to link to
247 * custom-title-link Custom Title object to link to
248 * custom target-link Value of the target attribute, for custom-target-link
249 * parser-extlink-* Attributes added by parser for external links:
250 * parser-extlink-rel: add rel="nofollow"
251 * parser-extlink-target: link target, but overridden by custom-target-link
253 * For images, desc-link and file-link are implemented as a click-through. For
254 * sounds and videos, they may be displayed in other ways.
258 function toHtml( $options = array() ) {
259 if ( count( func_get_args() ) == 2 ) {
260 throw new MWException( __METHOD__
.' called in the old style' );
263 $alt = empty( $options['alt'] ) ?
'' : $options['alt'];
265 $query = empty( $options['desc-query'] ) ?
'' : $options['desc-query'];
267 if ( !empty( $options['custom-url-link'] ) ) {
268 $linkAttribs = array( 'href' => $options['custom-url-link'] );
269 if ( !empty( $options['title'] ) ) {
270 $linkAttribs['title'] = $options['title'];
272 if ( !empty( $options['custom-target-link'] ) ) {
273 $linkAttribs['target'] = $options['custom-target-link'];
274 } elseif ( !empty( $options['parser-extlink-target'] ) ) {
275 $linkAttribs['target'] = $options['parser-extlink-target'];
277 if ( !empty( $options['parser-extlink-rel'] ) ) {
278 $linkAttribs['rel'] = $options['parser-extlink-rel'];
280 } elseif ( !empty( $options['custom-title-link'] ) ) {
281 $title = $options['custom-title-link'];
282 $linkAttribs = array(
283 'href' => $title->getLinkURL(),
284 'title' => empty( $options['title'] ) ?
$title->getFullText() : $options['title']
286 } elseif ( !empty( $options['desc-link'] ) ) {
287 $linkAttribs = $this->getDescLinkAttribs( empty( $options['title'] ) ?
null : $options['title'], $query );
288 } elseif ( !empty( $options['file-link'] ) ) {
289 $linkAttribs = array( 'href' => $this->file
->getURL() );
291 $linkAttribs = false;
297 'width' => $this->width
,
298 'height' => $this->height
,
300 if ( !empty( $options['valign'] ) ) {
301 $attribs['style'] = "vertical-align: {$options['valign']}";
303 if ( !empty( $options['img-class'] ) ) {
304 $attribs['class'] = $options['img-class'];
306 return $this->linkWrap( $linkAttribs, Xml
::element( 'img', $attribs ) );
312 * Basic media transform error class
316 class MediaTransformError
extends MediaTransformOutput
{
317 var $htmlMsg, $textMsg, $width, $height, $url, $path;
319 function __construct( $msg, $width, $height /*, ... */ ) {
320 $args = array_slice( func_get_args(), 3 );
321 $htmlArgs = array_map( 'htmlspecialchars', $args );
322 $htmlArgs = array_map( 'nl2br', $htmlArgs );
324 $this->htmlMsg
= wfMessage( $msg )->rawParams( $htmlArgs )->escaped();
325 $this->textMsg
= wfMessage( $msg )->rawParams( $htmlArgs )->text();
326 $this->width
= intval( $width );
327 $this->height
= intval( $height );
332 function toHtml( $options = array() ) {
333 return "<div class=\"MediaTransformError\" style=\"" .
334 "width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" .
340 return $this->textMsg
;
343 function getHtmlMsg() {
344 return $this->htmlMsg
;
353 * Shortcut class for parameter validation errors
357 class TransformParameterError
extends MediaTransformError
{
358 function __construct( $params ) {
359 parent
::__construct( 'thumbnail_error',
360 max( isset( $params['width'] ) ?
$params['width'] : 0, 120 ),
361 max( isset( $params['height'] ) ?
$params['height'] : 0, 120 ),
362 wfMessage( 'thumbnail_invalid_params' )->text() );