Merge "Fix zhtable's Makefile.py"
[lhc/web/wiklou.git] / includes / media / MediaTransformOutput.php
1 <?php
2 /**
3 * Base class for the output of file transformation methods.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Media
22 */
23
24 /**
25 * Base class for the output of MediaHandler::doTransform() and File::transform().
26 *
27 * @ingroup Media
28 */
29 abstract class MediaTransformOutput {
30 /**
31 * @var File
32 */
33 var $file;
34
35 var $width, $height, $url, $page, $path;
36 protected $storagePath = false;
37
38 /**
39 * @return integer Width of the output box
40 */
41 public function getWidth() {
42 return $this->width;
43 }
44
45 /**
46 * @return integer Height of the output box
47 */
48 public function getHeight() {
49 return $this->height;
50 }
51
52 /**
53 * Get the final extension of the thumbnail.
54 * Returns false for scripted transformations.
55 * @return string|false
56 */
57 public function getExtension() {
58 return $this->path ? FileBackend::extensionFromPath( $this->path ) : false;
59 }
60
61 /**
62 * @return string|false The thumbnail URL
63 */
64 public function getUrl() {
65 return $this->url;
66 }
67
68 /**
69 * @return string|bool The permanent thumbnail storage path
70 */
71 public function getStoragePath() {
72 return $this->storagePath;
73 }
74
75 /**
76 * @param $storagePath string The permanent storage path
77 * @return void
78 */
79 public function setStoragePath( $storagePath ) {
80 $this->storagePath = $storagePath;
81 }
82
83 /**
84 * Fetch HTML for this transform output
85 *
86 * @param $options array Associative array of options. Boolean options
87 * should be indicated with a value of true for true, and false or
88 * absent for false.
89 *
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
97 *
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.
100 *
101 * @return string
102 */
103 abstract public function toHtml( $options = array() );
104
105 /**
106 * This will be overridden to return true in error classes
107 * @return bool
108 */
109 public function isError() {
110 return false;
111 }
112
113 /**
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.
118 *
119 * @return Bool
120 */
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 ) );
125 }
126
127 /**
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.
130 *
131 * @return Bool
132 */
133 public function fileIsSource() {
134 return ( !$this->isError() && $this->path === null );
135 }
136
137 /**
138 * Get the path of a file system copy of the thumbnail.
139 * Callers should never write to this path.
140 *
141 * @return string|bool Returns false if there isn't one
142 */
143 public function getLocalCopyPath() {
144 if ( $this->isError() ) {
145 return false;
146 } elseif ( $this->path === null ) {
147 return $this->file->getLocalRefPath();
148 } else {
149 return $this->path; // may return false
150 }
151 }
152
153 /**
154 * Stream the file if there were no errors
155 *
156 * @param $headers Array Additional HTTP headers to send on success
157 * @return Bool success
158 */
159 public function streamFile( $headers = array() ) {
160 if ( !$this->path ) {
161 return false;
162 } elseif ( FileBackend::isStoragePath( $this->path ) ) {
163 $be = $this->file->getRepo()->getBackend();
164 return $be->streamFile( array( 'src' => $this->path, 'headers' => $headers ) )->isOK();
165 } else { // FS-file
166 return StreamFile::stream( $this->getLocalCopyPath(), $headers );
167 }
168 }
169
170 /**
171 * Wrap some XHTML text in an anchor tag with the given attributes
172 *
173 * @param $linkAttribs array
174 * @param $contents string
175 *
176 * @return string
177 */
178 protected function linkWrap( $linkAttribs, $contents ) {
179 if ( $linkAttribs ) {
180 return Xml::tags( 'a', $linkAttribs, $contents );
181 } else {
182 return $contents;
183 }
184 }
185
186 /**
187 * @param $title string
188 * @param $params array
189 * @return array
190 */
191 public function getDescLinkAttribs( $title = null, $params = '' ) {
192 $query = $this->page ? ( 'page=' . urlencode( $this->page ) ) : '';
193 if( $params ) {
194 $query .= $query ? '&'.$params : $params;
195 }
196 $attribs = array(
197 'href' => $this->file->getTitle()->getLocalURL( $query ),
198 'class' => 'image',
199 );
200 if ( $title ) {
201 $attribs['title'] = $title;
202 }
203 return $attribs;
204 }
205 }
206
207 /**
208 * Media transform output for images
209 *
210 * @ingroup Media
211 */
212 class ThumbnailImage extends MediaTransformOutput {
213 /**
214 * Get a thumbnail object from a file and parameters.
215 * If $path is set to null, the output file is treated as a source copy.
216 * If $path is set to false, no output file will be created.
217 * $parameters should include, as a minimum, (file) 'width' and 'height'.
218 * It may also include a 'page' parameter for multipage files.
219 *
220 * @param $file File object
221 * @param $url String: URL path to the thumb
222 * @param $path String|bool|null: filesystem path to the thumb
223 * @param $parameters Array: Associative array of parameters
224 * @private
225 */
226 function __construct( $file, $url, $path = false, $parameters = array() ) {
227 # Previous parameters:
228 # $file, $url, $width, $height, $path = false, $page = false
229
230 if( is_array( $parameters ) ){
231 $defaults = array(
232 'page' => false
233 );
234 $actualParams = $parameters + $defaults;
235 } else {
236 # Using old format, should convert. Later a warning could be added here.
237 $numArgs = func_num_args();
238 $actualParams = array(
239 'width' => $path,
240 'height' => $parameters,
241 'page' => ( $numArgs > 5 ) ? func_get_arg( 5 ) : false
242 );
243 $path = ( $numArgs > 4 ) ? func_get_arg( 4 ) : false;
244 }
245
246 $this->file = $file;
247 $this->url = $url;
248 $this->path = $path;
249
250 # These should be integers when they get here.
251 # If not, there's a bug somewhere. But let's at
252 # least produce valid HTML code regardless.
253 $this->width = round( $actualParams['width'] );
254 $this->height = round( $actualParams['height'] );
255
256 $this->page = $actualParams['page'];
257 }
258
259 /**
260 * Return HTML <img ... /> tag for the thumbnail, will include
261 * width and height attributes and a blank alt text (as required).
262 *
263 * @param $options array Associative array of options. Boolean options
264 * should be indicated with a value of true for true, and false or
265 * absent for false.
266 *
267 * alt HTML alt attribute
268 * title HTML title attribute
269 * desc-link Boolean, show a description link
270 * file-link Boolean, show a file download link
271 * valign vertical-align property, if the output is an inline element
272 * img-class Class applied to the \<img\> tag, if there is such a tag
273 * desc-query String, description link query params
274 * custom-url-link Custom URL to link to
275 * custom-title-link Custom Title object to link to
276 * custom target-link Value of the target attribute, for custom-target-link
277 * parser-extlink-* Attributes added by parser for external links:
278 * parser-extlink-rel: add rel="nofollow"
279 * parser-extlink-target: link target, but overridden by custom-target-link
280 *
281 * For images, desc-link and file-link are implemented as a click-through. For
282 * sounds and videos, they may be displayed in other ways.
283 *
284 * @return string
285 */
286 function toHtml( $options = array() ) {
287 if ( count( func_get_args() ) == 2 ) {
288 throw new MWException( __METHOD__ .' called in the old style' );
289 }
290
291 $alt = empty( $options['alt'] ) ? '' : $options['alt'];
292
293 $query = empty( $options['desc-query'] ) ? '' : $options['desc-query'];
294
295 if ( !empty( $options['custom-url-link'] ) ) {
296 $linkAttribs = array( 'href' => $options['custom-url-link'] );
297 if ( !empty( $options['title'] ) ) {
298 $linkAttribs['title'] = $options['title'];
299 }
300 if ( !empty( $options['custom-target-link'] ) ) {
301 $linkAttribs['target'] = $options['custom-target-link'];
302 } elseif ( !empty( $options['parser-extlink-target'] ) ) {
303 $linkAttribs['target'] = $options['parser-extlink-target'];
304 }
305 if ( !empty( $options['parser-extlink-rel'] ) ) {
306 $linkAttribs['rel'] = $options['parser-extlink-rel'];
307 }
308 } elseif ( !empty( $options['custom-title-link'] ) ) {
309 $title = $options['custom-title-link'];
310 $linkAttribs = array(
311 'href' => $title->getLinkURL(),
312 'title' => empty( $options['title'] ) ? $title->getFullText() : $options['title']
313 );
314 } elseif ( !empty( $options['desc-link'] ) ) {
315 $linkAttribs = $this->getDescLinkAttribs( empty( $options['title'] ) ? null : $options['title'], $query );
316 } elseif ( !empty( $options['file-link'] ) ) {
317 $linkAttribs = array( 'href' => $this->file->getURL() );
318 } else {
319 $linkAttribs = false;
320 }
321
322 $attribs = array(
323 'alt' => $alt,
324 'src' => $this->url,
325 'width' => $this->width,
326 'height' => $this->height,
327 );
328 if ( !empty( $options['valign'] ) ) {
329 $attribs['style'] = "vertical-align: {$options['valign']}";
330 }
331 if ( !empty( $options['img-class'] ) ) {
332 $attribs['class'] = $options['img-class'];
333 }
334 return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
335 }
336
337 }
338
339 /**
340 * Basic media transform error class
341 *
342 * @ingroup Media
343 */
344 class MediaTransformError extends MediaTransformOutput {
345 var $htmlMsg, $textMsg, $width, $height, $url, $path;
346
347 function __construct( $msg, $width, $height /*, ... */ ) {
348 $args = array_slice( func_get_args(), 3 );
349 $htmlArgs = array_map( 'htmlspecialchars', $args );
350 $htmlArgs = array_map( 'nl2br', $htmlArgs );
351
352 $this->htmlMsg = wfMessage( $msg )->rawParams( $htmlArgs )->escaped();
353 $this->textMsg = wfMessage( $msg )->rawParams( $htmlArgs )->text();
354 $this->width = intval( $width );
355 $this->height = intval( $height );
356 $this->url = false;
357 $this->path = false;
358 }
359
360 function toHtml( $options = array() ) {
361 return "<div class=\"MediaTransformError\" style=\"" .
362 "width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" .
363 $this->htmlMsg .
364 "</div>";
365 }
366
367 function toText() {
368 return $this->textMsg;
369 }
370
371 function getHtmlMsg() {
372 return $this->htmlMsg;
373 }
374
375 function isError() {
376 return true;
377 }
378 }
379
380 /**
381 * Shortcut class for parameter validation errors
382 *
383 * @ingroup Media
384 */
385 class TransformParameterError extends MediaTransformError {
386 function __construct( $params ) {
387 parent::__construct( 'thumbnail_error',
388 max( isset( $params['width'] ) ? $params['width'] : 0, 120 ),
389 max( isset( $params['height'] ) ? $params['height'] : 0, 120 ),
390 wfMessage( 'thumbnail_invalid_params' )->text() );
391 }
392 }