Coding Convention cleanup, removed extraneous references to $_SERVER
[mediawiki.git] / maintenance / importImages.inc
blob1f693fa10838ea88b4862108a4d4b88c722fcd8d
1 <?php
3 /**
4  * Support functions for the importImages script
5  *
6  * @file
7  * @ingroup Maintenance
8  * @author Rob Church <robchur@gmail.com>
9  */
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                         $files = array();
22                         while( ( $file = readdir( $dhl ) ) !== false ) {
23                                 if( is_file( $dir . '/' . $file ) ) {
24                                         list( /* $name */, $ext ) = splitFilename( $dir . '/' . $file );
25                                         if( array_search( strtolower( $ext ), $exts ) !== false )
26                                                 $files[] = $dir . '/' . $file;
27                                 }
28                         }
29                         return $files;
30                 } else {
31                         return array();
32                 }
33         } else {
34                 return array();
35         }
38 /**
39  * Split a filename into filename and extension
40  *
41  * @param $filename Filename
42  * @return array
43  */
44 function splitFilename( $filename ) {
45         $parts = explode( '.', $filename );
46         $ext = $parts[ count( $parts ) - 1 ];
47         unset( $parts[ count( $parts ) - 1 ] );
48         $fname = implode( '.', $parts );
49         return array( $fname, $ext );
52 /**
53  * Find an auxilliary file with the given extension, matching 
54  * the give base file path. $maxStrip determines how many extensions 
55  * may be stripped from the original file name before appending the
56  * new extension. For example, with $maxStrip = 1 (the default), 
57  * file files acme.foo.bar.txt and acme.foo.txt would be auxilliary
58  * files for acme.foo.bar and the extension ".txt". With $maxStrip = 2,
59  * acme.txt would also be acceptable.
60  *
61  * @param $file base path
62  * @param $auxExtension the extension to be appended to the base path
63  * @param $maxStrip the maximum number of extensions to strip from the base path (default: 1)
64  * @return string or false
65  */
66 function findAuxFile( $file, $auxExtension, $maxStrip = 1 ) {
67         if ( strpos( $auxExtension, '.' ) !== 0 ) {
68                 $auxExtension = '.' . $auxExtension;
69         }
71         $d = dirname( $file );
72         $n = basename( $file );
74         while ( $maxStrip >= 0 ) {
75                 $f = $d . '/' . $n . $auxExtension;
77                 if ( file_exists( $f ) ) {
78                         return $f;
79                 }
81                 $idx = strrpos( $n, '.' );
82                 if ( !$idx ) break;
84                 $n = substr( $n, 0, $idx );
85                 $maxStrip -= 1;
86         }
88         return false;