nicer format for static groups record
[lhc/web/wiklou.git] / includes / Image.php
1 <?php
2 /**
3 * @package MediaWiki
4 */
5
6 # NOTE FOR WINDOWS USERS:
7 # To enable EXIF functions, add the folloing lines to the
8 # "Windows extensions" section of php.ini:
9 #
10 # extension=extensions/php_mbstring.dll
11 # extension=extensions/php_exif.dll
12
13 if ($wgShowEXIF)
14 require_once('Exif.php');
15
16
17 /**
18 * Class to represent an image
19 *
20 * Provides methods to retrieve paths (physical, logical, URL),
21 * to generate thumbnails or for uploading.
22 * @package MediaWiki
23 */
24 class Image
25 {
26 /**#@+
27 * @access private
28 */
29 var $name, # name of the image (constructor)
30 $imagePath, # Path of the image (loadFromXxx)
31 $url, # Image URL (accessor)
32 $title, # Title object for this image (constructor)
33 $fileExists, # does the image file exist on disk? (loadFromXxx)
34 $fromSharedDirectory, # load this image from $wgSharedUploadDirectory (loadFromXxx)
35 $historyLine, # Number of line to return by nextHistoryLine() (constructor)
36 $historyRes, # result of the query for the image's history (nextHistoryLine)
37 $width, # \
38 $height, # |
39 $bits, # --- returned by getimagesize (loadFromXxx)
40 $type, # |
41 $attr, # /
42 $size, # Size in bytes (loadFromXxx)
43 $metadata, # Metadata
44 $exif, # The Exif class
45 $dataLoaded; # Whether or not all this has been loaded from the database (loadFromXxx)
46
47
48 /**#@-*/
49
50 /**
51 * Create an Image object from an image name
52 *
53 * @param string $name name of the image, used to create a title object using Title::makeTitleSafe
54 * @access public
55 */
56 function newFromName( $name ) {
57 $title = Title::makeTitleSafe( NS_IMAGE, $name );
58 return new Image( $title );
59 }
60
61 /**
62 * Obsolete factory function, use constructor
63 */
64 function newFromTitle( $title ) {
65 return new Image( $title );
66 }
67
68 function Image( $title ) {
69 global $wgShowEXIF;
70
71 $this->title =& $title;
72 $this->name = $title->getDBkey();
73 $this->metadata = serialize ( array() ) ;
74
75 $n = strrpos( $this->name, '.' );
76 $this->extension = strtolower( $n ? substr( $this->name, $n + 1 ) : '' );
77 $this->historyLine = 0;
78
79 $this->dataLoaded = false;
80
81 if ($wgShowEXIF)
82 $this->exif = new Exif;
83 }
84
85 /**
86 * Get the memcached keys
87 * Returns an array, first element is the local cache key, second is the shared cache key, if there is one
88 */
89 function getCacheKeys( $shared = false ) {
90 global $wgDBname, $wgUseSharedUploads, $wgSharedUploadDBname, $wgCacheSharedUploads;
91
92 $foundCached = false;
93 $hashedName = md5($this->name);
94 $keys = array( "$wgDBname:Image:$hashedName" );
95 if ( $wgUseSharedUploads && $wgSharedUploadDBname && $wgCacheSharedUploads ) {
96 $keys[] = "$wgSharedUploadDBname:Image:$hashedName";
97 }
98 return $keys;
99 }
100
101 /**
102 * Try to load image metadata from memcached. Returns true on success.
103 */
104 function loadFromCache() {
105 global $wgUseSharedUploads, $wgMemc;
106 $fname = 'Image::loadFromMemcached';
107 wfProfileIn( $fname );
108 $this->dataLoaded = false;
109 $keys = $this->getCacheKeys();
110 $cachedValues = $wgMemc->get( $keys[0] );
111
112 // Check if the key existed and belongs to this version of MediaWiki
113 if (!empty($cachedValues) && is_array($cachedValues) && isset($cachedValues['width']) && $cachedValues['fileExists']) {
114 if ( $wgUseSharedUploads && $cachedValues['fromShared']) {
115 # if this is shared file, we need to check if image
116 # in shared repository has not changed
117 if ( isset( $keys[1] ) ) {
118 $commonsCachedValues = $wgMemc->get( $keys[1] );
119 if (!empty($commonsCachedValues) && is_array($commonsCachedValues) && isset($commonsCachedValues['width'])) {
120 $this->name = $commonsCachedValues['name'];
121 $this->imagePath = $commonsCachedValues['imagePath'];
122 $this->fileExists = $commonsCachedValues['fileExists'];
123 $this->width = $commonsCachedValues['width'];
124 $this->height = $commonsCachedValues['height'];
125 $this->bits = $commonsCachedValues['bits'];
126 $this->type = $commonsCachedValues['type'];
127 $this->metadata = $commonsCachedValues['metadata'];
128 $this->size = $commonsCachedValues['size'];
129 $this->fromSharedDirectory = true;
130 $this->dataLoaded = true;
131 $this->imagePath = $this->getFullPath(true);
132 }
133 }
134 }
135 else {
136 $this->name = $cachedValues['name'];
137 $this->imagePath = $cachedValues['imagePath'];
138 $this->fileExists = $cachedValues['fileExists'];
139 $this->width = $cachedValues['width'];
140 $this->height = $cachedValues['height'];
141 $this->bits = $cachedValues['bits'];
142 $this->type = $cachedValues['type'];
143 $this->metadata = $cachedValues['metadata'];
144 $this->size = $cachedValues['size'];
145 $this->fromSharedDirectory = false;
146 $this->dataLoaded = true;
147 $this->imagePath = $this->getFullPath();
148 }
149 }
150
151 wfProfileOut( $fname );
152 return $this->dataLoaded;
153 }
154
155 /**
156 * Save the image metadata to memcached
157 */
158 function saveToCache() {
159 global $wgMemc;
160 $this->load();
161 // We can't cache metadata for non-existent files, because if the file later appears
162 // in commons, the local keys won't be purged.
163 if ( $this->fileExists ) {
164 $keys = $this->getCacheKeys();
165
166 $cachedValues = array('name' => $this->name,
167 'imagePath' => $this->imagePath,
168 'fileExists' => $this->fileExists,
169 'fromShared' => $this->fromSharedDirectory,
170 'width' => $this->width,
171 'height' => $this->height,
172 'bits' => $this->bits,
173 'type' => $this->type,
174 'metadata' => $this->metadata,
175 'size' => $this->size);
176
177 $wgMemc->set( $keys[0], $cachedValues );
178 }
179 }
180
181 /**
182 * Load metadata from the file itself
183 */
184 function loadFromFile() {
185 global $wgUseSharedUploads, $wgSharedUploadDirectory, $wgLang;
186 $fname = 'Image::loadFromFile';
187 wfProfileIn( $fname );
188 $this->imagePath = $this->getFullPath();
189 $this->fileExists = file_exists( $this->imagePath );
190 $this->fromSharedDirectory = false;
191 $gis = false;
192
193 # If the file is not found, and a shared upload directory is used, look for it there.
194 if (!$this->fileExists && $wgUseSharedUploads && $wgSharedUploadDirectory) {
195 # In case we're on a wgCapitalLinks=false wiki, we
196 # capitalize the first letter of the filename before
197 # looking it up in the shared repository.
198 $sharedImage = Image::newFromName( $wgLang->ucfirst($this->name) );
199 $this->fileExists = file_exists( $sharedImage->getFullPath(true) );
200 if ( $this->fileExists ) {
201 $this->name = $sharedImage->name;
202 $this->imagePath = $this->getFullPath(true);
203 $this->fromSharedDirectory = true;
204 }
205 }
206
207 if ( $this->fileExists ) {
208 # Get size in bytes
209 $this->size = filesize( $this->imagePath );
210
211 # Height and width
212 # Don't try to get the width and height of sound and video files, that's bad for performance
213 if ( !Image::isKnownImageExtension( $this->extension ) ) {
214 $gis = false;
215 } elseif( $this->extension == 'svg' ) {
216 wfSuppressWarnings();
217 $gis = wfGetSVGsize( $this->imagePath );
218 wfRestoreWarnings();
219 } else {
220 wfSuppressWarnings();
221 $gis = getimagesize( $this->imagePath );
222 wfRestoreWarnings();
223 }
224 }
225 if( $gis === false ) {
226 $this->width = 0;
227 $this->height = 0;
228 $this->bits = 0;
229 $this->type = 0;
230 $this->metadata = serialize ( array() ) ;
231 } else {
232 $this->width = $gis[0];
233 $this->height = $gis[1];
234 $this->type = $gis[2];
235 $this->metadata = serialize ( $this->retrieveExifData() ) ;
236 if ( isset( $gis['bits'] ) ) {
237 $this->bits = $gis['bits'];
238 } else {
239 $this->bits = 0;
240 }
241 }
242 $this->dataLoaded = true;
243 wfProfileOut( $fname );
244 }
245
246 /**
247 * Load image metadata from the DB
248 */
249 function loadFromDB() {
250 global $wgUseSharedUploads, $wgSharedUploadDBname, $wgLang;
251 $fname = 'Image::loadFromDB';
252 wfProfileIn( $fname );
253
254 $dbr =& wfGetDB( DB_SLAVE );
255 $row = $dbr->selectRow( 'image',
256 array( 'img_size', 'img_width', 'img_height', 'img_bits', 'img_type' , 'img_metadata' ),
257 array( 'img_name' => $this->name ), $fname );
258 if ( $row ) {
259 $this->fromSharedDirectory = false;
260 $this->fileExists = true;
261 $this->loadFromRow( $row );
262 $this->imagePath = $this->getFullPath();
263 // Check for rows from a previous schema, quietly upgrade them
264 if ( $this->type == -1 ) {
265 $this->upgradeRow();
266 }
267 } elseif ( $wgUseSharedUploads && $wgSharedUploadDBname ) {
268 # In case we're on a wgCapitalLinks=false wiki, we
269 # capitalize the first letter of the filename before
270 # looking it up in the shared repository.
271 $name = $wgLang->ucfirst($this->name);
272
273 $row = $dbr->selectRow( "`$wgSharedUploadDBname`.image",
274 array( 'img_size', 'img_width', 'img_height', 'img_bits', 'img_type' ),
275 array( 'img_name' => $name ), $fname );
276 if ( $row ) {
277 $this->fromSharedDirectory = true;
278 $this->fileExists = true;
279 $this->imagePath = $this->getFullPath(true);
280 $this->name = $name;
281 $this->loadFromRow( $row );
282
283 // Check for rows from a previous schema, quietly upgrade them
284 if ( $this->type == -1 ) {
285 $this->upgradeRow();
286 }
287 }
288 }
289
290 if ( !$row ) {
291 $this->size = 0;
292 $this->width = 0;
293 $this->height = 0;
294 $this->bits = 0;
295 $this->type = 0;
296 $this->fileExists = false;
297 $this->fromSharedDirectory = false;
298 $this->metadata = serialize ( array() ) ;
299 }
300
301 # Unconditionally set loaded=true, we don't want the accessors constantly rechecking
302 $this->dataLoaded = true;
303 }
304
305 /*
306 * Load image metadata from a DB result row
307 */
308 function loadFromRow( &$row ) {
309 $this->size = $row->img_size;
310 $this->width = $row->img_width;
311 $this->height = $row->img_height;
312 $this->bits = $row->img_bits;
313 $this->type = $row->img_type;
314 $this->metadata = $row->img_metadata;
315 if ( $this->metadata == "" ) $this->metadata = serialize ( array() ) ;
316 $this->dataLoaded = true;
317 }
318
319 /**
320 * Load image metadata from cache or DB, unless already loaded
321 */
322 function load() {
323 global $wgSharedUploadDBname, $wgUseSharedUploads;
324 if ( !$this->dataLoaded ) {
325 if ( !$this->loadFromCache() ) {
326 $this->loadFromDB();
327 if ( !$wgSharedUploadDBname && $wgUseSharedUploads ) {
328 $this->loadFromFile();
329 } elseif ( $this->fileExists ) {
330 $this->saveToCache();
331 }
332 }
333 $this->dataLoaded = true;
334 }
335 }
336
337 /**
338 * Metadata was loaded from the database, but the row had a marker indicating it needs to be
339 * upgraded from the 1.4 schema, which had no width, height, bits or type. Upgrade the row.
340 */
341 function upgradeRow() {
342 global $wgDBname, $wgSharedUploadDBname;
343 $fname = 'Image::upgradeRow';
344 $this->loadFromFile();
345 $dbw =& wfGetDB( DB_MASTER );
346
347 if ( $this->fromSharedDirectory ) {
348 if ( !$wgSharedUploadDBname ) {
349 return;
350 }
351
352 // Write to the other DB using selectDB, not database selectors
353 // This avoids breaking replication in MySQL
354 $dbw->selectDB( $wgSharedUploadDBname );
355 }
356 $dbw->update( 'image',
357 array(
358 'img_width' => $this->width,
359 'img_height' => $this->height,
360 'img_bits' => $this->bits,
361 'img_type' => $this->type,
362 'img_metadata' => $this->metadata,
363 ), array( 'img_name' => $this->name ), $fname
364 );
365 if ( $this->fromSharedDirectory ) {
366 $dbw->selectDB( $wgDBname );
367 }
368 }
369
370 /**
371 * Return the name of this image
372 * @access public
373 */
374 function getName() {
375 return $this->name;
376 }
377
378 /**
379 * Return the associated title object
380 * @access public
381 */
382 function getTitle() {
383 return $this->title;
384 }
385
386 /**
387 * Return the URL of the image file
388 * @access public
389 */
390 function getURL() {
391 if ( !$this->url ) {
392 $this->load();
393 if($this->fileExists) {
394 $this->url = Image::imageUrl( $this->name, $this->fromSharedDirectory );
395 } else {
396 $this->url = '';
397 }
398 }
399 return $this->url;
400 }
401
402 function getViewURL() {
403 if( $this->mustRender() ) {
404 return $this->createThumb( $this->getWidth() );
405 } else {
406 return $this->getURL();
407 }
408 }
409
410 /**
411 * Return the image path of the image in the
412 * local file system as an absolute path
413 * @access public
414 */
415 function getImagePath() {
416 $this->load();
417 return $this->imagePath;
418 }
419
420 /**
421 * Return the width of the image
422 *
423 * Returns -1 if the file specified is not a known image type
424 * @access public
425 */
426 function getWidth() {
427 $this->load();
428 return $this->width;
429 }
430
431 /**
432 * Return the height of the image
433 *
434 * Returns -1 if the file specified is not a known image type
435 * @access public
436 */
437 function getHeight() {
438 $this->load();
439 return $this->height;
440 }
441
442 /**
443 * Return the size of the image file, in bytes
444 * @access public
445 */
446 function getSize() {
447 $this->load();
448 return $this->size;
449 }
450
451 /**
452 * Return the type of the image
453 *
454 * - 1 GIF
455 * - 2 JPG
456 * - 3 PNG
457 * - 15 WBMP
458 * - 16 XBM
459 */
460 function getType() {
461 $this->load();
462 return $this->type;
463 }
464
465 /**
466 * Return the escapeLocalURL of this image
467 * @access public
468 */
469 function getEscapeLocalURL() {
470 $this->getTitle();
471 return $this->title->escapeLocalURL();
472 }
473
474 /**
475 * Return the escapeFullURL of this image
476 * @access public
477 */
478 function getEscapeFullURL() {
479 $this->getTitle();
480 return $this->title->escapeFullURL();
481 }
482
483 /**
484 * Return the URL of an image, provided its name.
485 *
486 * @param string $name Name of the image, without the leading "Image:"
487 * @param boolean $fromSharedDirectory Should this be in $wgSharedUploadPath?
488 * @access public
489 * @static
490 */
491 function imageUrl( $name, $fromSharedDirectory = false ) {
492 global $wgUploadPath,$wgUploadBaseUrl,$wgSharedUploadPath;
493 if($fromSharedDirectory) {
494 $base = '';
495 $path = $wgSharedUploadPath;
496 } else {
497 $base = $wgUploadBaseUrl;
498 $path = $wgUploadPath;
499 }
500 $url = "{$base}{$path}" . wfGetHashPath($name, $fromSharedDirectory) . "{$name}";
501 return wfUrlencode( $url );
502 }
503
504 /**
505 * Returns true if the image file exists on disk.
506 *
507 * @access public
508 */
509 function exists() {
510 $this->load();
511 return $this->fileExists;
512 }
513
514 /**
515 *
516 * @access private
517 */
518 function thumbUrl( $width, $subdir='thumb') {
519 global $wgUploadPath, $wgUploadBaseUrl,
520 $wgSharedUploadPath,$wgSharedUploadDirectory,
521 $wgSharedThumbnailScriptPath, $wgThumbnailScriptPath;
522
523 // Generate thumb.php URL if possible
524 $script = false;
525 $url = false;
526
527 if ( $this->fromSharedDirectory ) {
528 if ( $wgSharedThumbnailScriptPath ) {
529 $script = $wgSharedThumbnailScriptPath;
530 }
531 } else {
532 if ( $wgThumbnailScriptPath ) {
533 $script = $wgThumbnailScriptPath;
534 }
535 }
536 if ( $script ) {
537 $url = $script . '?f=' . urlencode( $this->name ) . '&w=' . urlencode( $width );
538 } else {
539 $name = $this->thumbName( $width );
540 if($this->fromSharedDirectory) {
541 $base = '';
542 $path = $wgSharedUploadPath;
543 } else {
544 $base = $wgUploadBaseUrl;
545 $path = $wgUploadPath;
546 }
547 if ( Image::isHashed( $this->fromSharedDirectory ) ) {
548 $url = "{$base}{$path}/{$subdir}" .
549 wfGetHashPath($this->name, $this->fromSharedDirectory)
550 . $this->name.'/'.$name;
551 $url = wfUrlencode( $url );
552 } else {
553 $url = "{$base}{$path}/{$subdir}/{$name}";
554 }
555 }
556 return array( $script !== false, $url );
557 }
558
559 /**
560 * Return the file name of a thumbnail of the specified width
561 *
562 * @param integer $width Width of the thumbnail image
563 * @param boolean $shared Does the thumbnail come from the shared repository?
564 * @access private
565 */
566 function thumbName( $width ) {
567 $thumb = $width."px-".$this->name;
568 if( $this->extension == 'svg' ) {
569 # Rasterize SVG vector images to PNG
570 $thumb .= '.png';
571 }
572 return $thumb;
573 }
574
575 /**
576 * Create a thumbnail of the image having the specified width/height.
577 * The thumbnail will not be created if the width is larger than the
578 * image's width. Let the browser do the scaling in this case.
579 * The thumbnail is stored on disk and is only computed if the thumbnail
580 * file does not exist OR if it is older than the image.
581 * Returns the URL.
582 *
583 * Keeps aspect ratio of original image. If both width and height are
584 * specified, the generated image will be no bigger than width x height,
585 * and will also have correct aspect ratio.
586 *
587 * @param integer $width maximum width of the generated thumbnail
588 * @param integer $height maximum height of the image (optional)
589 * @access public
590 */
591 function createThumb( $width, $height=-1 ) {
592 $thumb = $this->getThumbnail( $width, $height );
593 if( is_null( $thumb ) ) return '';
594 return $thumb->getUrl();
595 }
596
597 /**
598 * As createThumb, but returns a ThumbnailImage object. This can
599 * provide access to the actual file, the real size of the thumb,
600 * and can produce a convenient <img> tag for you.
601 *
602 * @param integer $width maximum width of the generated thumbnail
603 * @param integer $height maximum height of the image (optional)
604 * @return ThumbnailImage
605 * @access public
606 */
607 function &getThumbnail( $width, $height=-1 ) {
608 if ( $height == -1 ) {
609 return $this->renderThumb( $width );
610 }
611 $this->load();
612 if ( $width < $this->width ) {
613 $thumbheight = $this->height * $width / $this->width;
614 $thumbwidth = $width;
615 } else {
616 $thumbheight = $this->height;
617 $thumbwidth = $this->width;
618 }
619 if ( $thumbheight > $height ) {
620 $thumbwidth = $thumbwidth * $height / $thumbheight;
621 $thumbheight = $height;
622 }
623 $thumb = $this->renderThumb( $thumbwidth );
624 if( is_null( $thumb ) ) {
625 $thumb = $this->iconThumb();
626 }
627 return $thumb;
628 }
629
630 /**
631 * @return ThumbnailImage
632 */
633 function iconThumb() {
634 global $wgStylePath, $wgStyleDirectory;
635
636 $try = array( 'fileicon-' . $this->extension . '.png', 'fileicon.png' );
637 foreach( $try as $icon ) {
638 $path = '/common/images/' . $icon;
639 $filepath = $wgStyleDirectory . $path;
640 if( file_exists( $filepath ) ) {
641 return new ThumbnailImage( $wgStylePath . $path, 120, 120 );
642 }
643 }
644 return null;
645 }
646
647 /**
648 * Create a thumbnail of the image having the specified width.
649 * The thumbnail will not be created if the width is larger than the
650 * image's width. Let the browser do the scaling in this case.
651 * The thumbnail is stored on disk and is only computed if the thumbnail
652 * file does not exist OR if it is older than the image.
653 * Returns an object which can return the pathname, URL, and physical
654 * pixel size of the thumbnail -- or null on failure.
655 *
656 * @return ThumbnailImage
657 * @access private
658 */
659 function renderThumb( $width, $useScript = true ) {
660 global $wgUseSquid, $wgInternalServer;
661 global $wgThumbnailScriptPath, $wgSharedThumbnailScriptPath;
662
663 $width = IntVal( $width );
664
665 $this->load();
666 if ( ! $this->exists() )
667 {
668 # If there is no image, there will be no thumbnail
669 return null;
670 }
671
672 # Sanity check $width
673 if( $width <= 0 ) {
674 # BZZZT
675 return null;
676 }
677
678 if( $width > $this->width && !$this->mustRender() ) {
679 # Don't make an image bigger than the source
680 return new ThumbnailImage( $this->getViewURL(), $this->getWidth(), $this->getHeight() );
681 }
682
683 $height = floor( $this->height * ( $width/$this->width ) );
684
685 list( $isScriptUrl, $url ) = $this->thumbUrl( $width );
686 if ( $isScriptUrl && $useScript ) {
687 // Use thumb.php to render the image
688 return new ThumbnailImage( $url, $width, $height );
689 }
690
691 $thumbName = $this->thumbName( $width, $this->fromSharedDirectory );
692 $thumbPath = wfImageThumbDir( $this->name, $this->fromSharedDirectory ).'/'.$thumbName;
693
694 if ( !file_exists( $thumbPath ) ) {
695 $oldThumbPath = wfDeprecatedThumbDir( $thumbName, 'thumb', $this->fromSharedDirectory ).
696 '/'.$thumbName;
697 $done = false;
698 if ( file_exists( $oldThumbPath ) ) {
699 if ( filemtime($oldThumbPath) >= filemtime($this->imagePath) ) {
700 rename( $oldThumbPath, $thumbPath );
701 $done = true;
702 } else {
703 unlink( $oldThumbPath );
704 }
705 }
706 if ( !$done ) {
707 $this->reallyRenderThumb( $thumbPath, $width, $height );
708
709 # Purge squid
710 # This has to be done after the image is updated and present for all machines on NFS,
711 # or else the old version might be stored into the squid again
712 if ( $wgUseSquid ) {
713 if ( substr( $url, 0, 4 ) == 'http' ) {
714 $urlArr = array( $url );
715 } else {
716 $urlArr = array( $wgInternalServer.$url );
717 }
718 wfPurgeSquidServers($urlArr);
719 }
720 }
721 }
722 return new ThumbnailImage( $url, $width, $height, $thumbPath );
723 } // END OF function renderThumb
724
725 /**
726 * Really render a thumbnail
727 *
728 * @access private
729 */
730 function reallyRenderThumb( $thumbPath, $width, $height ) {
731 global $wgSVGConverters, $wgSVGConverter,
732 $wgUseImageMagick, $wgImageMagickConvertCommand;
733
734 $this->load();
735
736 if( $this->extension == 'svg' ) {
737 global $wgSVGConverters, $wgSVGConverter;
738 if( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
739 global $wgSVGConverterPath;
740 $cmd = str_replace(
741 array( '$path/', '$width', '$input', '$output' ),
742 array( $wgSVGConverterPath,
743 $width,
744 escapeshellarg( $this->imagePath ),
745 escapeshellarg( $thumbPath ) ),
746 $wgSVGConverters[$wgSVGConverter] );
747 $conv = shell_exec( $cmd );
748 } else {
749 $conv = false;
750 }
751 } elseif ( $wgUseImageMagick ) {
752 # use ImageMagick
753 # Specify white background color, will be used for transparent images
754 # in Internet Explorer/Windows instead of default black.
755 $cmd = $wgImageMagickConvertCommand .
756 " -quality 85 -background white -geometry {$width} ".
757 escapeshellarg($this->imagePath) . " " .
758 escapeshellarg($thumbPath);
759 $conv = shell_exec( $cmd );
760 } else {
761 # Use PHP's builtin GD library functions.
762 #
763 # First find out what kind of file this is, and select the correct
764 # input routine for this.
765
766 $truecolor = false;
767
768 switch( $this->type ) {
769 case 1: # GIF
770 $src_image = imagecreatefromgif( $this->imagePath );
771 break;
772 case 2: # JPG
773 $src_image = imagecreatefromjpeg( $this->imagePath );
774 $truecolor = true;
775 break;
776 case 3: # PNG
777 $src_image = imagecreatefrompng( $this->imagePath );
778 $truecolor = ( $this->bits > 8 );
779 break;
780 case 15: # WBMP for WML
781 $src_image = imagecreatefromwbmp( $this->imagePath );
782 break;
783 case 16: # XBM
784 $src_image = imagecreatefromxbm( $this->imagePath );
785 break;
786 default:
787 return 'Image type not supported';
788 break;
789 }
790 if ( $truecolor ) {
791 $dst_image = imagecreatetruecolor( $width, $height );
792 } else {
793 $dst_image = imagecreate( $width, $height );
794 }
795 imagecopyresampled( $dst_image, $src_image,
796 0,0,0,0,
797 $width, $height, $this->width, $this->height );
798 switch( $this->type ) {
799 case 1: # GIF
800 case 3: # PNG
801 case 15: # WBMP
802 case 16: # XBM
803 imagepng( $dst_image, $thumbPath );
804 break;
805 case 2: # JPEG
806 imageinterlace( $dst_image );
807 imagejpeg( $dst_image, $thumbPath, 95 );
808 break;
809 default:
810 break;
811 }
812 imagedestroy( $dst_image );
813 imagedestroy( $src_image );
814 }
815 #
816 # Check for zero-sized thumbnails. Those can be generated when
817 # no disk space is available or some other error occurs
818 #
819 if( file_exists( $thumbPath ) ) {
820 $thumbstat = stat( $thumbPath );
821 if( $thumbstat['size'] == 0 ) {
822 unlink( $thumbPath );
823 }
824 }
825 }
826
827 /**
828 * Get all thumbnail names previously generated for this image
829 */
830 function getThumbnails( $shared = false ) {
831 if ( Image::isHashed( $shared ) ) {
832 $this->load();
833 $files = array();
834 $dir = wfImageThumbDir( $this->name, $shared );
835
836 // This generates an error on failure, hence the @
837 $handle = @opendir( $dir );
838
839 if ( $handle ) {
840 while ( false !== ( $file = readdir($handle) ) ) {
841 if ( $file{0} != '.' ) {
842 $files[] = $file;
843 }
844 }
845 closedir( $handle );
846 }
847 } else {
848 $files = array();
849 }
850
851 return $files;
852 }
853
854 /**
855 * Delete all previously generated thumbnails, refresh metadata in memcached and purge the squid
856 */
857 function purgeCache( $archiveFiles = array(), $shared = false ) {
858 global $wgInternalServer, $wgUseSquid;
859
860 // Refresh metadata cache
861 clearstatcache();
862 $this->loadFromFile();
863 $this->saveToCache();
864
865 // Delete thumbnails
866 $files = $this->getThumbnails( $shared );
867 $dir = wfImageThumbDir( $this->name, $shared );
868 $urls = array();
869 foreach ( $files as $file ) {
870 if ( preg_match( '/^(\d+)px/', $file, $m ) ) {
871 $urls[] = $wgInternalServer . $this->thumbUrl( $m[1], $this->fromSharedDirectory );
872 @unlink( "$dir/$file" );
873 }
874 }
875
876 // Purge the squid
877 if ( $wgUseSquid ) {
878 $urls[] = $wgInternalServer . $this->getViewURL();
879 foreach ( $archiveFiles as $file ) {
880 $urls[] = $wgInternalServer . wfImageArchiveUrl( $file );
881 }
882 wfPurgeSquidServers( $urls );
883 }
884 }
885
886 /**
887 * Return the image history of this image, line by line.
888 * starts with current version, then old versions.
889 * uses $this->historyLine to check which line to return:
890 * 0 return line for current version
891 * 1 query for old versions, return first one
892 * 2, ... return next old version from above query
893 *
894 * @access public
895 */
896 function nextHistoryLine() {
897 $fname = 'Image::nextHistoryLine()';
898 $dbr =& wfGetDB( DB_SLAVE );
899 if ( $this->historyLine == 0 ) {// called for the first time, return line from cur
900 $this->historyRes = $dbr->select( 'image',
901 array( 'img_size','img_description','img_user','img_user_text','img_timestamp', "'' AS oi_archive_name" ),
902 array( 'img_name' => $this->title->getDBkey() ),
903 $fname
904 );
905 if ( 0 == wfNumRows( $this->historyRes ) ) {
906 return FALSE;
907 }
908 } else if ( $this->historyLine == 1 ) {
909 $this->historyRes = $dbr->select( 'oldimage',
910 array( 'oi_size AS img_size', 'oi_description AS img_description', 'oi_user AS img_user',
911 'oi_user_text AS img_user_text', 'oi_timestamp AS img_timestamp', 'oi_archive_name'
912 ), array( 'oi_name' => $this->title->getDBkey() ), $fname, array( 'ORDER BY' => 'oi_timestamp DESC' )
913 );
914 }
915 $this->historyLine ++;
916
917 return $dbr->fetchObject( $this->historyRes );
918 }
919
920 /**
921 * Reset the history pointer to the first element of the history
922 * @access public
923 */
924 function resetHistory() {
925 $this->historyLine = 0;
926 }
927
928 /**
929 * Return true if the file is of a type that can't be directly
930 * rendered by typical browsers and needs to be re-rasterized.
931 * @return bool
932 */
933 function mustRender() {
934 $this->load();
935 return ( $this->extension == 'svg' );
936 }
937
938 /**
939 * Return the full filesystem path to the file. Note that this does
940 * not mean that a file actually exists under that location.
941 *
942 * This path depends on whether directory hashing is active or not,
943 * i.e. whether the images are all found in the same directory,
944 * or in hashed paths like /images/3/3c.
945 *
946 * @access public
947 * @param boolean $fromSharedDirectory Return the path to the file
948 * in a shared repository (see $wgUseSharedRepository and related
949 * options in DefaultSettings.php) instead of a local one.
950 *
951 */
952 function getFullPath( $fromSharedRepository = false ) {
953 global $wgUploadDirectory, $wgSharedUploadDirectory;
954 global $wgHashedUploadDirectory, $wgHashedSharedUploadDirectory;
955
956 $dir = $fromSharedRepository ? $wgSharedUploadDirectory :
957 $wgUploadDirectory;
958
959 // $wgSharedUploadDirectory may be false, if thumb.php is used
960 if ( $dir ) {
961 $fullpath = $dir . wfGetHashPath($this->name, $fromSharedRepository) . $this->name;
962 } else {
963 $fullpath = false;
964 }
965
966 return $fullpath;
967 }
968
969 /**
970 * @return bool
971 * @static
972 */
973 function isHashed( $shared ) {
974 global $wgHashedUploadDirectory, $wgHashedSharedUploadDirectory;
975 return $shared ? $wgHashedSharedUploadDirectory : $wgHashedUploadDirectory;
976 }
977
978 /**
979 * @return bool
980 * @static
981 */
982 function isKnownImageExtension( $ext ) {
983 static $extensions = array( 'svg', 'png', 'jpg', 'jpeg', 'gif', 'bmp', 'xbm' );
984 return in_array( $ext, $extensions );
985 }
986
987 /**
988 * Record an image upload in the upload log and the image table
989 */
990 function recordUpload( $oldver, $desc, $copyStatus = '', $source = '' ) {
991 global $wgUser, $wgLang, $wgTitle, $wgOut, $wgDeferredUpdateList;
992 global $wgUseCopyrightUpload, $wgUseSquid, $wgPostCommitUpdateList;
993
994 $fname = 'Image::recordUpload';
995 $dbw =& wfGetDB( DB_MASTER );
996
997 # img_name must be unique
998 if ( !$dbw->indexUnique( 'image', 'img_name' ) && !$dbw->indexExists('image','PRIMARY') ) {
999 wfDebugDieBacktrace( 'Database schema not up to date, please run maintenance/archives/patch-image_name_unique.sql' );
1000 }
1001
1002 // Delete thumbnails and refresh the metadata cache
1003 $this->purgeCache();
1004
1005 // Fail now if the image isn't there
1006 if ( !$this->fileExists || $this->fromSharedDirectory ) {
1007 return false;
1008 }
1009
1010 if ( $wgUseCopyrightUpload ) {
1011 $textdesc = '== ' . wfMsg ( 'filedesc' ) . " ==\n" . $desc . "\n" .
1012 '== ' . wfMsg ( 'filestatus' ) . " ==\n" . $copyStatus . "\n" .
1013 '== ' . wfMsg ( 'filesource' ) . " ==\n" . $source ;
1014 } else {
1015 $textdesc = $desc;
1016 }
1017
1018 $now = $dbw->timestamp();
1019
1020 # Test to see if the row exists using INSERT IGNORE
1021 # This avoids race conditions by locking the row until the commit, and also
1022 # doesn't deadlock. SELECT FOR UPDATE causes a deadlock for every race condition.
1023 $dbw->insert( 'image',
1024 array(
1025 'img_name' => $this->name,
1026 'img_size'=> $this->size,
1027 'img_width' => $this->width,
1028 'img_height' => $this->height,
1029 'img_bits' => $this->bits,
1030 'img_type' => $this->type,
1031 'img_timestamp' => $now,
1032 'img_description' => $desc,
1033 'img_user' => $wgUser->getID(),
1034 'img_user_text' => $wgUser->getName(),
1035 'img_metadata' => $this->metadata,
1036 ), $fname, 'IGNORE'
1037 );
1038 $descTitle = $this->getTitle();
1039 $purgeURLs = array();
1040
1041 if ( $dbw->affectedRows() ) {
1042 # Successfully inserted, this is a new image
1043 $id = $descTitle->getArticleID();
1044
1045 if ( $id == 0 ) {
1046 $article = new Article( $descTitle );
1047 $article->insertNewArticle( $textdesc, $desc, false, false, true );
1048 }
1049 } else {
1050 # Collision, this is an update of an image
1051 # Insert previous contents into oldimage
1052 $dbw->insertSelect( 'oldimage', 'image',
1053 array(
1054 'oi_name' => 'img_name',
1055 'oi_archive_name' => $dbw->addQuotes( $oldver ),
1056 'oi_size' => 'img_size',
1057 'oi_width' => 'img_width',
1058 'oi_height' => 'img_height',
1059 'oi_bits' => 'img_bits',
1060 'oi_type' => 'img_type',
1061 'oi_timestamp' => 'img_timestamp',
1062 'oi_description' => 'img_description',
1063 'oi_user' => 'img_user',
1064 'oi_user_text' => 'img_user_text',
1065 ), array( 'img_name' => $this->name ), $fname
1066 );
1067
1068 # Update the current image row
1069 $dbw->update( 'image',
1070 array( /* SET */
1071 'img_size' => $this->size,
1072 'img_width' => $this->width,
1073 'img_height' => $this->height,
1074 'img_bits' => $this->bits,
1075 'img_type' => $this->type,
1076 'img_timestamp' => $now,
1077 'img_user' => $wgUser->getID(),
1078 'img_user_text' => $wgUser->getName(),
1079 'img_description' => $desc,
1080 'img_metadata' => $this->metadata,
1081 ), array( /* WHERE */
1082 'img_name' => $this->name
1083 ), $fname
1084 );
1085
1086 # Invalidate the cache for the description page
1087 $descTitle->invalidateCache();
1088 $purgeURLs[] = $descTitle->getInternalURL();
1089 }
1090
1091 # Invalidate cache for all pages using this image
1092 $linksTo = $this->getLinksTo();
1093
1094 if ( $wgUseSquid ) {
1095 $u = SquidUpdate::newFromTitles( $linksTo, $purgeURLs );
1096 array_push( $wgPostCommitUpdateList, $u );
1097 }
1098 Title::touchArray( $linksTo );
1099
1100 $log = new LogPage( 'upload' );
1101 $log->addEntry( 'upload', $descTitle, $desc );
1102
1103 return true;
1104 }
1105
1106 /**
1107 * Get an array of Title objects which are articles which use this image
1108 * Also adds their IDs to the link cache
1109 *
1110 * This is mostly copied from Title::getLinksTo()
1111 */
1112 function getLinksTo( $options = '' ) {
1113 global $wgLinkCache;
1114 $fname = 'Image::getLinksTo';
1115 wfProfileIn( $fname );
1116
1117 if ( $options ) {
1118 $db =& wfGetDB( DB_MASTER );
1119 } else {
1120 $db =& wfGetDB( DB_SLAVE );
1121 }
1122
1123 extract( $db->tableNames( 'page', 'imagelinks' ) );
1124 $encName = $db->addQuotes( $this->name );
1125 $sql = "SELECT page_namespace,page_title,page_id FROM $page,$imagelinks WHERE page_id=il_from AND il_to=$encName $options";
1126 $res = $db->query( $sql, $fname );
1127
1128 $retVal = array();
1129 if ( $db->numRows( $res ) ) {
1130 while ( $row = $db->fetchObject( $res ) ) {
1131 if ( $titleObj = Title::makeTitle( $row->page_namespace, $row->page_title ) ) {
1132 $wgLinkCache->addGoodLink( $row->page_id, $titleObj->getPrefixedDBkey() );
1133 $retVal[] = $titleObj;
1134 }
1135 }
1136 }
1137 $db->freeResult( $res );
1138 return $retVal;
1139 }
1140 /**
1141 * Retrive Exif data from the database
1142 *
1143 * Retrive Exif data from the database and prune unrecognized tags
1144 * and/or tags with invalid contents
1145 *
1146 * @return array
1147 */
1148 function retrieveExifData () {
1149 if ( $this->type !== '2' ) return array ();
1150
1151 $exif = exif_read_data( $this->imagePath );
1152 foreach($exif as $k => $v) {
1153 if ( !in_array($k, array_keys($this->exif->mFlatExif)) ) {
1154 wfDebug( "Image::retrieveExifData: '$k' is not a valid Exif tag (type: '" . gettype($v) . "'; data: '$v')\n");
1155 unset($exif[$k]);
1156 }
1157 }
1158
1159 foreach($exif as $k => $v) {
1160 if ( !$this->exif->validate($k, $v) ) {
1161 wfDebug( "Image::retrieveExifData: '$k' contained invalid data (type: '" . gettype($v) . "'; data: '$v')\n");
1162 unset($exif[$k]);
1163 }
1164 }
1165 return $exif;
1166 }
1167
1168 function getExifData () {
1169 global $wgRequest;
1170 if ( $this->metadata === '0' )
1171 return array();
1172
1173 $purge = $wgRequest->getVal( 'action' ) == 'purge';
1174 $ret = unserialize ( $this->metadata );
1175
1176 $oldver = isset( $ret['MEDIAWIKI_EXIF_VERSION'] ) ? $ret['MEDIAWIKI_EXIF_VERSION'] : 0;
1177 $newver = $this->exif->version();
1178
1179 if ( !count( $ret ) || $purge || $oldver != $newver ) {
1180 $this->updateExifData( $newver );
1181 }
1182 if ( isset( $ret['MEDIAWIKI_EXIF_VERSION'] ) )
1183 unset( $ret['MEDIAWIKI_EXIF_VERSION'] );
1184
1185 foreach($ret as $k => $v) {
1186 $ret[$k] = $this->exif->format($k, $v);
1187 }
1188
1189 return $ret;
1190 }
1191
1192 function updateExifData( $version ) {
1193 $fname = 'Image:updateExifData';
1194
1195 if ( $this->getImagePath() === false ) # Not a local image
1196 return;
1197
1198 # Get EXIF data from image
1199 $exif = $this->retrieveExifData();
1200 if ( count( $exif ) ) {
1201 $exif['MEDIAWIKI_EXIF_VERSION'] = $version;
1202 $this->metadata = serialize( $exif );
1203 } else {
1204 $this->metadata = '0';
1205 }
1206
1207 # Update EXIF data in database
1208 $dbw =& wfGetDB( DB_MASTER );
1209 $dbw->update( 'image',
1210 array( 'img_metadata' => $this->metadata ),
1211 array( 'img_name' => $this->name ),
1212 $fname
1213 );
1214 }
1215
1216 } //class
1217
1218
1219 /**
1220 * Returns the image directory of an image
1221 * If the directory does not exist, it is created.
1222 * The result is an absolute path.
1223 *
1224 * This function is called from thumb.php before Setup.php is included
1225 *
1226 * @param string $fname file name of the image file
1227 * @access public
1228 */
1229 function wfImageDir( $fname ) {
1230 global $wgUploadDirectory, $wgHashedUploadDirectory;
1231
1232 if (!$wgHashedUploadDirectory) { return $wgUploadDirectory; }
1233
1234 $hash = md5( $fname );
1235 $oldumask = umask(0);
1236 $dest = $wgUploadDirectory . '/' . $hash{0};
1237 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
1238 $dest .= '/' . substr( $hash, 0, 2 );
1239 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
1240
1241 umask( $oldumask );
1242 return $dest;
1243 }
1244
1245 /**
1246 * Returns the image directory of an image's thubnail
1247 * If the directory does not exist, it is created.
1248 * The result is an absolute path.
1249 *
1250 * This function is called from thumb.php before Setup.php is included
1251 *
1252 * @param string $fname file name of the original image file
1253 * @param string $subdir (optional) subdirectory of the image upload directory that should be used for storing the thumbnail. Default is 'thumb'
1254 * @param boolean $shared (optional) use the shared upload directory
1255 * @access public
1256 */
1257 function wfImageThumbDir( $fname, $shared = false ) {
1258 $base = wfImageArchiveDir( $fname, 'thumb', $shared );
1259 if ( Image::isHashed( $shared ) ) {
1260 $dir = "$base/$fname";
1261
1262 if ( !is_dir( $base ) ) {
1263 $oldumask = umask(0);
1264 @mkdir( $base, 0777 );
1265 umask( $oldumask );
1266 }
1267
1268 if ( ! is_dir( $dir ) ) {
1269 $oldumask = umask(0);
1270 @mkdir( $dir, 0777 );
1271 umask( $oldumask );
1272 }
1273 } else {
1274 $dir = $base;
1275 }
1276
1277 return $dir;
1278 }
1279
1280 /**
1281 * Old thumbnail directory, kept for conversion
1282 */
1283 function wfDeprecatedThumbDir( $thumbName , $subdir='thumb', $shared=false) {
1284 return wfImageArchiveDir( $thumbName, $subdir, $shared );
1285 }
1286
1287 /**
1288 * Returns the image directory of an image's old version
1289 * If the directory does not exist, it is created.
1290 * The result is an absolute path.
1291 *
1292 * This function is called from thumb.php before Setup.php is included
1293 *
1294 * @param string $fname file name of the thumbnail file, including file size prefix
1295 * @param string $subdir (optional) subdirectory of the image upload directory that should be used for storing the old version. Default is 'archive'
1296 * @param boolean $shared (optional) use the shared upload directory (only relevant for other functions which call this one)
1297 * @access public
1298 */
1299 function wfImageArchiveDir( $fname , $subdir='archive', $shared=false ) {
1300 global $wgUploadDirectory, $wgHashedUploadDirectory,
1301 $wgSharedUploadDirectory, $wgHashedSharedUploadDirectory;
1302 $dir = $shared ? $wgSharedUploadDirectory : $wgUploadDirectory;
1303 $hashdir = $shared ? $wgHashedSharedUploadDirectory : $wgHashedUploadDirectory;
1304 if (!$hashdir) { return $dir.'/'.$subdir; }
1305 $hash = md5( $fname );
1306 $oldumask = umask(0);
1307
1308 # Suppress warning messages here; if the file itself can't
1309 # be written we'll worry about it then.
1310 wfSuppressWarnings();
1311
1312 $archive = $dir.'/'.$subdir;
1313 if ( ! is_dir( $archive ) ) { mkdir( $archive, 0777 ); }
1314 $archive .= '/' . $hash{0};
1315 if ( ! is_dir( $archive ) ) { mkdir( $archive, 0777 ); }
1316 $archive .= '/' . substr( $hash, 0, 2 );
1317 if ( ! is_dir( $archive ) ) { mkdir( $archive, 0777 ); }
1318
1319 wfRestoreWarnings();
1320 umask( $oldumask );
1321 return $archive;
1322 }
1323
1324
1325 /*
1326 * Return the hash path component of an image path (URL or filesystem),
1327 * e.g. "/3/3c/", or just "/" if hashing is not used.
1328 *
1329 * @param $dbkey The filesystem / database name of the file
1330 * @param $fromSharedDirectory Use the shared file repository? It may
1331 * use different hash settings from the local one.
1332 */
1333 function wfGetHashPath ( $dbkey, $fromSharedDirectory = false ) {
1334 global $wgHashedSharedUploadDirectory, $wgSharedUploadDirectory;
1335 global $wgHashedUploadDirectory;
1336
1337 if( Image::isHashed( $fromSharedDirectory ) ) {
1338 $hash = md5($dbkey);
1339 return '/' . $hash{0} . '/' . substr( $hash, 0, 2 ) . '/';
1340 } else {
1341 return '/';
1342 }
1343 }
1344
1345 /**
1346 * Returns the image URL of an image's old version
1347 *
1348 * @param string $fname file name of the image file
1349 * @param string $subdir (optional) subdirectory of the image upload directory that is used by the old version. Default is 'archive'
1350 * @access public
1351 */
1352 function wfImageArchiveUrl( $name, $subdir='archive' ) {
1353 global $wgUploadPath, $wgHashedUploadDirectory;
1354
1355 if ($wgHashedUploadDirectory) {
1356 $hash = md5( substr( $name, 15) );
1357 $url = $wgUploadPath.'/'.$subdir.'/' . $hash{0} . '/' .
1358 substr( $hash, 0, 2 ) . '/'.$name;
1359 } else {
1360 $url = $wgUploadPath.'/'.$subdir.'/'.$name;
1361 }
1362 return wfUrlencode($url);
1363 }
1364
1365 /**
1366 * Return a rounded pixel equivalent for a labeled CSS/SVG length.
1367 * http://www.w3.org/TR/SVG11/coords.html#UnitIdentifiers
1368 *
1369 * @param string $length
1370 * @return int Length in pixels
1371 */
1372 function wfScaleSVGUnit( $length ) {
1373 static $unitLength = array(
1374 'px' => 1.0,
1375 'pt' => 1.25,
1376 'pc' => 15.0,
1377 'mm' => 3.543307,
1378 'cm' => 35.43307,
1379 'in' => 90.0,
1380 '' => 1.0, // "User units" pixels by default
1381 '%' => 2.0, // Fake it!
1382 );
1383 if( preg_match( '/^(\d+)(em|ex|px|pt|pc|cm|mm|in|%|)$/', $length, $matches ) ) {
1384 $length = FloatVal( $matches[1] );
1385 $unit = $matches[2];
1386 return round( $length * $unitLength[$unit] );
1387 } else {
1388 // Assume pixels
1389 return round( FloatVal( $length ) );
1390 }
1391 }
1392
1393 /**
1394 * Compatible with PHP getimagesize()
1395 * @todo support gzipped SVGZ
1396 * @todo check XML more carefully
1397 * @todo sensible defaults
1398 *
1399 * @param string $filename
1400 * @return array
1401 */
1402 function wfGetSVGsize( $filename ) {
1403 $width = 256;
1404 $height = 256;
1405
1406 // Read a chunk of the file
1407 $f = fopen( $filename, "rt" );
1408 if( !$f ) return false;
1409 $chunk = fread( $f, 4096 );
1410 fclose( $f );
1411
1412 // Uber-crappy hack! Run through a real XML parser.
1413 if( !preg_match( '/<svg\s*([^>]*)\s*>/s', $chunk, $matches ) ) {
1414 return false;
1415 }
1416 $tag = $matches[1];
1417 if( preg_match( '/\bwidth\s*=\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches ) ) {
1418 $width = wfScaleSVGUnit( trim( substr( $matches[1], 1, -1 ) ) );
1419 }
1420 if( preg_match( '/\bheight\s*=\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches ) ) {
1421 $height = wfScaleSVGUnit( trim( substr( $matches[1], 1, -1 ) ) );
1422 }
1423
1424 return array( $width, $height, 'SVG',
1425 "width=\"$width\" height=\"$height\"" );
1426 }
1427
1428 /**
1429 * Determine if an image exists on the 'bad image list'
1430 *
1431 * @param string $name The image to check
1432 * @return bool
1433 */
1434 function wfIsBadImage( $name ) {
1435 global $wgContLang;
1436 static $titleList = false;
1437 if ( $titleList === false ) {
1438 $titleList = array();
1439
1440 $lines = explode("\n", wfMsgForContent( 'bad_image_list' ));
1441 foreach ( $lines as $line ) {
1442 if ( preg_match( '/^\*\s*\[{2}:(' . $wgContLang->getNsText( NS_IMAGE ) . ':.*?)\]{2}/', $line, $m ) ) {
1443 $t = Title::newFromText( $m[1] );
1444 $titleList[$t->getDBkey()] = 1;
1445 }
1446 }
1447 }
1448
1449 return array_key_exists( $name, $titleList );
1450 }
1451
1452
1453
1454 /**
1455 * Wrapper class for thumbnail images
1456 * @package MediaWiki
1457 */
1458 class ThumbnailImage {
1459 /**
1460 * @param string $path Filesystem path to the thumb
1461 * @param string $url URL path to the thumb
1462 * @access private
1463 */
1464 function ThumbnailImage( $url, $width, $height, $path = false ) {
1465 $this->url = $url;
1466 $this->width = $width;
1467 $this->height = $height;
1468 $this->path = $path;
1469 }
1470
1471 /**
1472 * @return string The thumbnail URL
1473 */
1474 function getUrl() {
1475 return $this->url;
1476 }
1477
1478 /**
1479 * Return HTML <img ... /> tag for the thumbnail, will include
1480 * width and height attributes and a blank alt text (as required).
1481 *
1482 * You can set or override additional attributes by passing an
1483 * associative array of name => data pairs. The data will be escaped
1484 * for HTML output, so should be in plaintext.
1485 *
1486 * @param array $attribs
1487 * @return string
1488 * @access public
1489 */
1490 function toHtml( $attribs = array() ) {
1491 $attribs['src'] = $this->url;
1492 $attribs['width'] = $this->width;
1493 $attribs['height'] = $this->height;
1494 if( !isset( $attribs['alt'] ) ) $attribs['alt'] = '';
1495
1496 $html = '<img ';
1497 foreach( $attribs as $name => $data ) {
1498 $html .= $name . '="' . htmlspecialchars( $data ) . '" ';
1499 }
1500 $html .= '/>';
1501 return $html;
1502 }
1503
1504 }
1505 ?>