95e856817ebbd857ca7928939675c9549a69fc77
[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->getPath() );
156 return array(
157 'width' => $gis[0],
158 'height' => $gis[1]
159 );
160 }
161
162 /**
163 * Get an array structure that looks like this:
164 *
165 * array(
166 * 'visible' => array(
167 * 'Human-readable name' => 'Human readable value',
168 * ...
169 * ),
170 * 'collapsed' => array(
171 * 'Human-readable name' => 'Human readable value',
172 * ...
173 * )
174 * )
175 * The UI will format this into a table where the visible fields are always
176 * visible, and the collapsed fields are optionally visible.
177 *
178 * The function should return false if there is no metadata to display.
179 */
180 function formatMetadata( $image, $metadata ) {
181 return false;
182 }
183
184 protected static function addMeta( &$array, $visibility, $type, $id, $value, $param = false ) {
185 $array[$visibility][] = array(
186 'id' => "$type-$id",
187 'name' => wfMsg( "$type-$id", $param ),
188 'value' => wfEscapeWikiText( $value )
189 );
190 }
191
192 }
193
194 /**
195 * Media handler abstract base class for images
196 *
197 * @addtogroup Media
198 */
199 abstract class ImageHandler extends MediaHandler {
200 function validateParam( $name, $value ) {
201 if ( in_array( $name, array( 'width', 'height' ) ) ) {
202 if ( $value <= 0 ) {
203 return false;
204 } else {
205 return true;
206 }
207 } else {
208 return false;
209 }
210 }
211
212 function makeParamString( $params ) {
213 if ( isset( $params['physicalWidth'] ) ) {
214 $width = $params['physicalWidth'];
215 } elseif ( isset( $params['width'] ) ) {
216 $width = $params['width'];
217 } else {
218 throw new MWException( 'No width specified to '.__METHOD__ );
219 }
220 # Removed for ProofreadPage
221 #$width = intval( $width );
222 return "{$width}px";
223 }
224
225 function parseParamString( $str ) {
226 $m = false;
227 if ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
228 return array( 'width' => $m[1] );
229 } else {
230 return false;
231 }
232 }
233
234 function getScriptParams( $params ) {
235 return array( 'width' => $params['width'] );
236 }
237
238 function normaliseParams( $image, &$params ) {
239 $mimeType = $image->getMimeType();
240
241 if ( !isset( $params['width'] ) ) {
242 return false;
243 }
244 if ( !isset( $params['page'] ) ) {
245 $params['page'] = 1;
246 }
247 $srcWidth = $image->getWidth( $params['page'] );
248 $srcHeight = $image->getHeight( $params['page'] );
249 if ( isset( $params['height'] ) && $params['height'] != -1 ) {
250 if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
251 $params['width'] = wfFitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
252 }
253 }
254 $params['height'] = File::scaleHeight( $srcWidth, $srcHeight, $params['width'] );
255 if ( !$this->validateThumbParams( $params['width'], $params['height'], $srcWidth, $srcHeight, $mimeType ) ) {
256 return false;
257 }
258 return true;
259 }
260
261 /**
262 * Get a transform output object without actually doing the transform
263 */
264 function getTransform( $image, $dstPath, $dstUrl, $params ) {
265 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self::TRANSFORM_LATER );
266 }
267
268 /**
269 * Validate thumbnail parameters and fill in the correct height
270 *
271 * @param integer &$width Specified width (input/output)
272 * @param integer &$height Height (output only)
273 * @return false to indicate that an error should be returned to the user.
274 */
275 function validateThumbParams( &$width, &$height, $srcWidth, $srcHeight, $mimeType ) {
276 $width = intval( $width );
277
278 # Sanity check $width
279 if( $width <= 0) {
280 wfDebug( __METHOD__.": Invalid destination width: $width\n" );
281 return false;
282 }
283 if ( $srcWidth <= 0 ) {
284 wfDebug( __METHOD__.": Invalid source width: $srcWidth\n" );
285 return false;
286 }
287
288 $height = File::scaleHeight( $srcWidth, $srcHeight, $width );
289 return true;
290 }
291
292 function getScriptedTransform( $image, $script, $params ) {
293 if ( !$this->normaliseParams( $image, $params ) ) {
294 return false;
295 }
296 $url = $script . '&' . wfArrayToCGI( $this->getScriptParams( $params ) );
297 return new ThumbnailImage( $url, $params['width'], $params['height'] );
298 }
299
300 /**
301 * Check for zero-sized thumbnails. These can be generated when
302 * no disk space is available or some other error occurs
303 *
304 * @param $dstPath The location of the suspect file
305 * @param $retval Return value of some shell process, file will be deleted if this is non-zero
306 * @return true if removed, false otherwise
307 */
308 function removeBadFile( $dstPath, $retval = 0 ) {
309 if( file_exists( $dstPath ) ) {
310 $thumbstat = stat( $dstPath );
311 if( $thumbstat['size'] == 0 || $retval != 0 ) {
312 wfDebugLog( 'thumbnail',
313 sprintf( 'Removing bad %d-byte thumbnail "%s"',
314 $thumbstat['size'], $dstPath ) );
315 unlink( $dstPath );
316 return true;
317 }
318 }
319 return false;
320 }
321
322 function getImageSize( $image, $path ) {
323 wfSuppressWarnings();
324 $gis = getimagesize( $path );
325 wfRestoreWarnings();
326 return $gis;
327 }
328 }
329
330