Minor tweaks for E_STRICT error_reporting mode warnings:
[lhc/web/wiklou.git] / includes / media / Generic.php
1 <?php
2
3 /**
4 * Media-handling base classes and generic functionality
5 */
6
7 /**
8 * Base media handler class
9 *
10 * @addtogroup Media
11 */
12 abstract class MediaHandler {
13 const TRANSFORM_LATER = 1;
14
15 /**
16 * Instance cache
17 */
18 static $handlers = array();
19
20 /**
21 * Get a MediaHandler for a given MIME type from the instance cache
22 */
23 static function getHandler( $type ) {
24 global $wgMediaHandlers;
25 if ( !isset( $wgMediaHandlers[$type] ) ) {
26 wfDebug( __METHOD__ . ": no handler found for $type.\n");
27 return false;
28 }
29 $class = $wgMediaHandlers[$type];
30 if ( !isset( self::$handlers[$class] ) ) {
31 self::$handlers[$class] = new $class;
32 if ( !self::$handlers[$class]->isEnabled() ) {
33 self::$handlers[$class] = false;
34 }
35 }
36 return self::$handlers[$class];
37 }
38
39 /*
40 * Validate a thumbnail parameter at parse time.
41 * Return true to accept the parameter, and false to reject it.
42 * If you return false, the parser will do something quiet and forgiving.
43 */
44 abstract function validateParam( $name, $value );
45
46 /**
47 * Merge a parameter array into a string appropriate for inclusion in filenames
48 */
49 abstract function makeParamString( $params );
50
51 /**
52 * Parse a param string made with makeParamString back into an array
53 */
54 abstract function parseParamString( $str );
55
56 /**
57 * Changes the parameter array as necessary, ready for transformation.
58 * Should be idempotent.
59 * Returns false if the parameters are unacceptable and the transform should fail
60 */
61 abstract function normaliseParams( $image, &$params );
62
63 /**
64 * Get an image size array like that returned by getimagesize(), or false if it
65 * can't be determined.
66 *
67 * @param Image $image The image object, or false if there isn't one
68 * @param string $fileName The filename
69 * @return array
70 */
71 abstract function getImageSize( $image, $path );
72
73 /**
74 * Get handler-specific metadata which will be saved in the img_metadata field.
75 *
76 * @param Image $image The image object, or false if there isn't one
77 * @param string $fileName The filename
78 * @return string
79 */
80 function getMetadata( $image, $path ) { return ''; }
81
82 /**
83 * Get a string describing the type of metadata, for display purposes.
84 */
85 function getMetadataType( $image ) { return false; }
86
87 /**
88 * Check if the metadata string is valid for this handler.
89 * If it returns false, Image will reload the metadata from the file and update the database
90 */
91 function isMetadataValid( $image, $metadata ) { return true; }
92
93 /**
94 * Get a MediaTransformOutput object representing the transformed output. Does not
95 * actually do the transform.
96 *
97 * @param Image $image The image object
98 * @param string $dstPath Filesystem destination path
99 * @param string $dstUrl Destination URL to use in output HTML
100 * @param array $params Arbitrary set of parameters validated by $this->validateParam()
101 */
102 function getTransform( $image, $dstPath, $dstUrl, $params ) {
103 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self::TRANSFORM_LATER );
104 }
105
106 /**
107 * Get a MediaTransformOutput object representing the transformed output. Does the
108 * transform unless $flags contains self::TRANSFORM_LATER.
109 *
110 * @param Image $image The image object
111 * @param string $dstPath Filesystem destination path
112 * @param string $dstUrl Destination URL to use in output HTML
113 * @param array $params Arbitrary set of parameters validated by $this->validateParam()
114 * @param integer $flags A bitfield, may contain self::TRANSFORM_LATER
115 */
116 abstract function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 );
117
118 /**
119 * Get the thumbnail extension and MIME type for a given source MIME type
120 * @return array thumbnail extension and MIME type
121 */
122 function getThumbType( $ext, $mime ) {
123 return array( $ext, $mime );
124 }
125
126 /**
127 * True if the handled types can be transformed
128 */
129 function canRender() { return true; }
130 /**
131 * True if handled types cannot be displayed directly in a browser
132 * but can be rendered
133 */
134 function mustRender() { return false; }
135 /**
136 * True if the type has multi-page capabilities
137 */
138 function isMultiPage() { return false; }
139 /**
140 * Page count for a multi-page document, false if unsupported or unknown
141 */
142 function pageCount() { return false; }
143 /**
144 * False if the handler is disabled for all files
145 */
146 function isEnabled() { return true; }
147
148 /**
149 * Get an associative array of page dimensions
150 * Currently "width" and "height" are understood, but this might be
151 * expanded in the future.
152 * Returns false if unknown or if the document is not multi-page.
153 */
154 function getPageDimensions( $image, $page ) {
155 $gis = $this->getImageSize( $image, $image->getImagePath() );
156 return array(
157 'width' => $gis[0],
158 'height' => $gis[1]
159 );
160 }
161 }
162
163 /**
164 * Media handler abstract base class for images
165 *
166 * @addtogroup Media
167 */
168 abstract class ImageHandler extends MediaHandler {
169 function validateParam( $name, $value ) {
170 if ( in_array( $name, array( 'width', 'height' ) ) ) {
171 if ( $value <= 0 ) {
172 return false;
173 } else {
174 return true;
175 }
176 } else {
177 return false;
178 }
179 }
180
181 function makeParamString( $params ) {
182 if ( isset( $params['physicalWidth'] ) ) {
183 $width = $params['physicalWidth'];
184 } else if ( isset( $params['width'] ) ) {
185 $width = $params['width'];
186 } else {
187 $width = 180;
188 }
189 # Removed for ProofreadPage
190 #$width = intval( $width );
191 return "{$width}px";
192 }
193
194 function parseParamString( $str ) {
195 $m = false;
196 if ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
197 return array( 'width' => $m[1] );
198 } else {
199 return false;
200 }
201 }
202
203 function getScriptParams( $params ) {
204 return array( 'width' => $params['width'] );
205 }
206
207 function normaliseParams( $image, &$params ) {
208 $mimeType = $image->getMimeType();
209
210 if ( !isset( $params['width'] ) ) {
211 return false;
212 }
213 if ( !isset( $params['page'] ) ) {
214 $params['page'] = 1;
215 }
216 $srcWidth = $image->getWidth( $params['page'] );
217 $srcHeight = $image->getHeight( $params['page'] );
218 if ( isset( $params['height'] ) && $params['height'] != -1 ) {
219 if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
220 $params['width'] = wfFitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
221 }
222 }
223 $params['height'] = Image::scaleHeight( $srcWidth, $srcHeight, $params['width'] );
224 if ( !$this->validateThumbParams( $params['width'], $params['height'], $srcWidth, $srcHeight, $mimeType ) ) {
225 return false;
226 }
227 return true;
228 }
229
230 /**
231 * Get a transform output object without actually doing the transform
232 */
233 function getTransform( $image, $dstPath, $dstUrl, $params ) {
234 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self::TRANSFORM_LATER );
235 }
236
237 /**
238 * Validate thumbnail parameters and fill in the correct height
239 *
240 * @param integer &$width Specified width (input/output)
241 * @param integer &$height Height (output only)
242 * @return false to indicate that an error should be returned to the user.
243 */
244 function validateThumbParams( &$width, &$height, $srcWidth, $srcHeight, $mimeType ) {
245 $width = intval( $width );
246
247 # Sanity check $width
248 if( $width <= 0) {
249 wfDebug( __METHOD__.": Invalid destination width: $width\n" );
250 return false;
251 }
252 if ( $srcWidth <= 0 ) {
253 wfDebug( __METHOD__.": Invalid source width: $srcWidth\n" );
254 return false;
255 }
256
257 $height = Image::scaleHeight( $srcWidth, $srcHeight, $width );
258 return true;
259 }
260
261 function getScriptedTransform( $image, $script, $params ) {
262 if ( !$this->normaliseParams( $image, $params ) ) {
263 return false;
264 }
265 $url = $script . '&' . wfArrayToCGI( $this->getScriptParams( $params ) );
266 return new ThumbnailImage( $url, $params['width'], $params['height'] );
267 }
268
269 /**
270 * Check for zero-sized thumbnails. These can be generated when
271 * no disk space is available or some other error occurs
272 *
273 * @param $dstPath The location of the suspect file
274 * @param $retval Return value of some shell process, file will be deleted if this is non-zero
275 * @return true if removed, false otherwise
276 */
277 function removeBadFile( $dstPath, $retval = 0 ) {
278 $removed = false;
279 if( file_exists( $dstPath ) ) {
280 $thumbstat = stat( $dstPath );
281 if( $thumbstat['size'] == 0 || $retval != 0 ) {
282 wfDebugLog( 'thumbnail',
283 sprintf( 'Removing bad %d-byte thumbnail "%s"',
284 $thumbstat['size'], $dstPath ) );
285 unlink( $dstPath );
286 return true;
287 }
288 }
289 return false;
290 }
291
292 function getImageSize( $image, $path ) {
293 wfSuppressWarnings();
294 $gis = getimagesize( $path );
295 wfRestoreWarnings();
296 return $gis;
297 }
298 }
299
300 ?>