Fix for r47482: put the RELEASE-NOTES entry in the "bug fixes" section and not the...
[mediawiki.git] / maintenance / importImages.inc.php
blob290f3c0794b548355ff04bb5660f2d3721de0987
1 <?php
3 /**
4 * Support functions for the importImages script
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
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
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;
28 return $files;
29 } else {
30 return false;
32 } else {
33 return false;
37 /**
38 * Split a filename into filename and extension
40 * @param $filename Filename
41 * @return array
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 );
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.
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
65 function findAuxFile( $file, $auxExtension, $maxStrip = 1 ) {
66 if ( strpos( $auxExtension, '.' ) !== 0 ) {
67 $auxExtension = '.' . $auxExtension;
70 $d = dirname( $file );
71 $n = basename( $file );
73 while ( $maxStrip >= 0 ) {
74 $f = $d . '/' . $n . $auxExtension;
76 if ( file_exists( $f ) ) {
77 return $f;
80 $idx = strrpos( $n, '.' );
81 if ( !$idx ) break;
83 $n = substr( $n, 0, $idx );
84 $maxStrip -= 1;
87 return false;