e3ff69ade45da3a6da256779bb6bb9417aa96452
[lhc/web/wiklou.git] / maintenance / importImages.inc.php
1 <?php
2
3 /**
4 * Support functions for the importImages script
5 *
6 * @package MediaWiki
7 * @subpackage Maintenance
8 * @author Rob Church <robchur@gmail.com>
9 */
10
11 /**
12 * Search a directory for files with one of a set of extensions
13 *
14 * @param $dir Path to directory to search
15 * @param $exts Array of extensions to search for
16 * @return mixed Array of filenames on success, or false on failure
17 */
18 function findFiles( $dir, $exts ) {
19 if( is_dir( $dir ) ) {
20 if( $dhl = opendir( $dir ) ) {
21 while( ( $file = readdir( $dhl ) ) !== false ) {
22 if( is_file( $dir . '/' . $file ) ) {
23 list( /* $name */, $ext ) = splitFilename( $dir . '/' . $file );
24 if( array_search( strtolower( $ext ), $exts ) !== false )
25 $files[] = $dir . '/' . $file;
26 }
27 }
28 return $files;
29 } else {
30 return false;
31 }
32 } else {
33 return false;
34 }
35 }
36
37 /**
38 * Split a filename into filename and extension
39 *
40 * @param $filename Filename
41 * @return array
42 */
43 function splitFilename( $filename ) {
44 $parts = explode( '.', $filename );
45 $ext = $parts[ count( $parts ) - 1 ];
46 unset( $parts[ count( $parts ) - 1 ] );
47 $fname = implode( '.', $parts );
48 return array( $fname, $ext );
49 }
50
51 /**
52 * Given an image hash, check that the structure exists to save the image file
53 * and create it if it doesn't
54 *
55 * @param $hash Part of an image hash, e.g. /f/fd/
56 */
57 function makeHashPath( $hash ) {
58 global $wgUploadDirectory;
59 $parts = explode( '/', substr( $hash, 1, strlen( $hash ) - 2 ) );
60 if( !is_dir( $wgUploadDirectory . '/' . $parts[0] ) )
61 mkdir( $wgUploadDirectory . '/' . $parts[0] );
62 if( !is_dir( $wgUploadDirectory . '/' . $hash ) )
63 mkdir( $wgUploadDirectory . '/' . $hash );
64 }
65
66
67 ?>