(bug 35565) Special:Log/patrol doesn't indicate whether patrolling was automatic
[mediawiki.git] / includes / media / MediaTransformOutput.php
blob380731c46bb688b5f8145a13529f6295a2f45bd2
1 <?php
2 /**
3 * Base class for the output of file transformation methods.
5 * @file
6 * @ingroup Media
7 */
9 /**
10 * Base class for the output of MediaHandler::doTransform() and File::transform().
12 * @ingroup Media
14 abstract class MediaTransformOutput {
15 /**
16 * @var File
18 var $file;
20 var $width, $height, $url, $page, $path;
21 protected $storagePath = false;
23 /**
24 * Get the width of the output box
26 public function getWidth() {
27 return $this->width;
30 /**
31 * Get the height of the output box
33 public function getHeight() {
34 return $this->height;
37 /**
38 * @return string The thumbnail URL
40 public function getUrl() {
41 return $this->url;
44 /**
45 * @return string|bool The permanent thumbnail storage path
47 public function getStoragePath() {
48 return $this->storagePath;
51 /**
52 * @param $storagePath string The permanent storage path
53 * @return void
55 public function setStoragePath( $storagePath ) {
56 $this->storagePath = $storagePath;
59 /**
60 * Fetch HTML for this transform output
62 * @param $options array Associative array of options. Boolean options
63 * should be indicated with a value of true for true, and false or
64 * absent for false.
66 * alt Alternate text or caption
67 * desc-link Boolean, show a description link
68 * file-link Boolean, show a file download link
69 * custom-url-link Custom URL to link to
70 * custom-title-link Custom Title object to link to
71 * valign vertical-align property, if the output is an inline element
72 * img-class Class applied to the <img> tag, if there is such a tag
74 * For images, desc-link and file-link are implemented as a click-through. For
75 * sounds and videos, they may be displayed in other ways.
77 * @return string
79 abstract public function toHtml( $options = array() );
81 /**
82 * This will be overridden to return true in error classes
83 * @return bool
85 public function isError() {
86 return false;
89 /**
90 * Check if an output thumbnail file actually exists.
91 * This will return false if there was an error, the
92 * thumbnail is to be handled client-side only, or if
93 * transformation was deferred via TRANSFORM_LATER.
95 * @return Bool
97 public function hasFile() {
98 // If TRANSFORM_LATER, $this->path will be false.
99 // Note: a null path means "use the source file".
100 return ( !$this->isError() && ( $this->path || $this->path === null ) );
104 * Check if the output thumbnail is the same as the source.
105 * This can occur if the requested width was bigger than the source.
107 * @return Bool
109 public function fileIsSource() {
110 return ( !$this->isError() && $this->path === null );
114 * Get the path of a file system copy of the thumbnail.
115 * Callers should never write to this path.
117 * @return string|bool Returns false if there isn't one
119 public function getLocalCopyPath() {
120 if ( $this->isError() ) {
121 return false;
122 } elseif ( $this->path === null ) {
123 return $this->file->getLocalRefPath();
124 } else {
125 return $this->path; // may return false
130 * Stream the file if there were no errors
132 * @param $headers Array Additional HTTP headers to send on success
133 * @return Bool success
135 public function streamFile( $headers = array() ) {
136 return $this->path && StreamFile::stream( $this->getLocalCopyPath(), $headers );
140 * Wrap some XHTML text in an anchor tag with the given attributes
142 * @param $linkAttribs array
143 * @param $contents string
145 * @return string
147 protected function linkWrap( $linkAttribs, $contents ) {
148 if ( $linkAttribs ) {
149 return Xml::tags( 'a', $linkAttribs, $contents );
150 } else {
151 return $contents;
156 * @param $title string
157 * @param $params array
158 * @return array
160 public function getDescLinkAttribs( $title = null, $params = '' ) {
161 $query = $this->page ? ( 'page=' . urlencode( $this->page ) ) : '';
162 if( $params ) {
163 $query .= $query ? '&'.$params : $params;
165 $attribs = array(
166 'href' => $this->file->getTitle()->getLocalURL( $query ),
167 'class' => 'image',
169 if ( $title ) {
170 $attribs['title'] = $title;
172 return $attribs;
177 * Media transform output for images
179 * @ingroup Media
181 class ThumbnailImage extends MediaTransformOutput {
183 * Get a thumbnail object from a file and parameters.
184 * If $path is set to null, the output file is treated as a source copy.
185 * If $path is set to false, no output file will be created.
187 * @param $file File object
188 * @param $url String: URL path to the thumb
189 * @param $width Integer: file's width
190 * @param $height Integer: file's height
191 * @param $path String|bool|null: filesystem path to the thumb
192 * @param $page Integer: page number, for multipage files
193 * @private
195 function __construct( $file, $url, $width, $height, $path = false, $page = false ) {
196 $this->file = $file;
197 $this->url = $url;
198 # These should be integers when they get here.
199 # If not, there's a bug somewhere. But let's at
200 # least produce valid HTML code regardless.
201 $this->width = round( $width );
202 $this->height = round( $height );
203 $this->path = $path;
204 $this->page = $page;
208 * Return HTML <img ... /> tag for the thumbnail, will include
209 * width and height attributes and a blank alt text (as required).
211 * @param $options array Associative array of options. Boolean options
212 * should be indicated with a value of true for true, and false or
213 * absent for false.
215 * alt HTML alt attribute
216 * title HTML title attribute
217 * desc-link Boolean, show a description link
218 * file-link Boolean, show a file download link
219 * valign vertical-align property, if the output is an inline element
220 * img-class Class applied to the \<img\> tag, if there is such a tag
221 * desc-query String, description link query params
222 * custom-url-link Custom URL to link to
223 * custom-title-link Custom Title object to link to
224 * custom target-link Value of the target attribute, for custom-target-link
226 * For images, desc-link and file-link are implemented as a click-through. For
227 * sounds and videos, they may be displayed in other ways.
229 * @return string
231 function toHtml( $options = array() ) {
232 if ( count( func_get_args() ) == 2 ) {
233 throw new MWException( __METHOD__ .' called in the old style' );
236 $alt = empty( $options['alt'] ) ? '' : $options['alt'];
238 $query = empty( $options['desc-query'] ) ? '' : $options['desc-query'];
240 if ( !empty( $options['custom-url-link'] ) ) {
241 $linkAttribs = array( 'href' => $options['custom-url-link'] );
242 if ( !empty( $options['title'] ) ) {
243 $linkAttribs['title'] = $options['title'];
245 if ( !empty( $options['custom-target-link'] ) ) {
246 $linkAttribs['target'] = $options['custom-target-link'];
248 } elseif ( !empty( $options['custom-title-link'] ) ) {
249 $title = $options['custom-title-link'];
250 $linkAttribs = array(
251 'href' => $title->getLinkURL(),
252 'title' => empty( $options['title'] ) ? $title->getFullText() : $options['title']
254 } elseif ( !empty( $options['desc-link'] ) ) {
255 $linkAttribs = $this->getDescLinkAttribs( empty( $options['title'] ) ? null : $options['title'], $query );
256 } elseif ( !empty( $options['file-link'] ) ) {
257 $linkAttribs = array( 'href' => $this->file->getURL() );
258 } else {
259 $linkAttribs = false;
262 $attribs = array(
263 'alt' => $alt,
264 'src' => $this->url,
265 'width' => $this->width,
266 'height' => $this->height,
268 if ( !empty( $options['valign'] ) ) {
269 $attribs['style'] = "vertical-align: {$options['valign']}";
271 if ( !empty( $options['img-class'] ) ) {
272 $attribs['class'] = $options['img-class'];
274 return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
280 * Basic media transform error class
282 * @ingroup Media
284 class MediaTransformError extends MediaTransformOutput {
285 var $htmlMsg, $textMsg, $width, $height, $url, $path;
287 function __construct( $msg, $width, $height /*, ... */ ) {
288 $args = array_slice( func_get_args(), 3 );
289 $htmlArgs = array_map( 'htmlspecialchars', $args );
290 $htmlArgs = array_map( 'nl2br', $htmlArgs );
292 $this->htmlMsg = wfMessage( $msg )->rawParams( $htmlArgs )->escaped();
293 $this->textMsg = wfMessage( $msg )->rawParams( $htmlArgs )->text();
294 $this->width = intval( $width );
295 $this->height = intval( $height );
296 $this->url = false;
297 $this->path = false;
300 function toHtml( $options = array() ) {
301 return "<div class=\"MediaTransformError\" style=\"" .
302 "width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" .
303 $this->htmlMsg .
304 "</div>";
307 function toText() {
308 return $this->textMsg;
311 function getHtmlMsg() {
312 return $this->htmlMsg;
315 function isError() {
316 return true;
321 * Shortcut class for parameter validation errors
323 * @ingroup Media
325 class TransformParameterError extends MediaTransformError {
326 function __construct( $params ) {
327 parent::__construct( 'thumbnail_error',
328 max( isset( $params['width'] ) ? $params['width'] : 0, 120 ),
329 max( isset( $params['height'] ) ? $params['height'] : 0, 120 ),
330 wfMsg( 'thumbnail_invalid_params' ) );