]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - maintenance/importImages.inc.php
MediaWiki 1.15.1
[autoinstallsdev/mediawiki.git] / maintenance / importImages.inc.php
1 <?php
2
3 /**
4  * Support functions for the importImages script
5  *
6  * @file
7  * @ingroup 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  * Find an auxilliary file with the given extension, matching 
53  * the give base file path. $maxStrip determines how many extensions 
54  * may be stripped from the original file name before appending the
55  * new extension. For example, with $maxStrip = 1 (the default), 
56  * file files acme.foo.bar.txt and acme.foo.txt would be auxilliary
57  * files for acme.foo.bar and the extension ".txt". With $maxStrip = 2,
58  * acme.txt would also be acceptable.
59  *
60  * @param $file base path
61  * @param $auxExtension the extension to be appended to the base path
62  * @param $maxStrip the maximum number of extensions to strip from the base path (default: 1)
63  * @return string or false
64  */
65 function findAuxFile( $file, $auxExtension, $maxStrip = 1 ) {
66         if ( strpos( $auxExtension, '.' ) !== 0 ) {
67                 $auxExtension = '.' . $auxExtension;
68         }
69
70         $d = dirname( $file );
71         $n = basename( $file );
72
73         while ( $maxStrip >= 0 ) {
74                 $f = $d . '/' . $n . $auxExtension;
75
76                 if ( file_exists( $f ) ) {
77                         return $f;
78                 }
79
80                 $idx = strrpos( $n, '.' );
81                 if ( !$idx ) break;
82
83                 $n = substr( $n, 0, $idx );
84                 $maxStrip -= 1;
85         }
86
87         return false;
88 }