]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - maintenance/importImages.inc.php
MediaWiki 1.14.0
[autoinstallsdev/mediawiki.git] / maintenance / importImages.inc.php
index 9a68bac028d5161a38ace50786fcca097cf6c286..290f3c0794b548355ff04bb5660f2d3721de0987 100644 (file)
@@ -3,7 +3,8 @@
 /**
  * Support functions for the importImages script
  *
- * @addtogroup Maintenance
+ * @file
+ * @ingroup Maintenance
  * @author Rob Church <robchur@gmail.com>
  */
 
@@ -45,4 +46,43 @@ function splitFilename( $filename ) {
        unset( $parts[ count( $parts ) - 1 ] );
        $fname = implode( '.', $parts );
        return array( $fname, $ext );
+}
+
+/**
+ * Find an auxilliary file with the given extension, matching 
+ * the give base file path. $maxStrip determines how many extensions 
+ * may be stripped from the original file name before appending the
+ * new extension. For example, with $maxStrip = 1 (the default), 
+ * file files acme.foo.bar.txt and acme.foo.txt would be auxilliary
+ * files for acme.foo.bar and the extension ".txt". With $maxStrip = 2,
+ * acme.txt would also be acceptable.
+ *
+ * @param $file base path
+ * @param $auxExtension the extension to be appended to the base path
+ * @param $maxStrip the maximum number of extensions to strip from the base path (default: 1)
+ * @return string or false
+ */
+function findAuxFile( $file, $auxExtension, $maxStrip = 1 ) {
+       if ( strpos( $auxExtension, '.' ) !== 0 ) {
+               $auxExtension = '.' . $auxExtension;
+       }
+
+       $d = dirname( $file );
+       $n = basename( $file );
+
+       while ( $maxStrip >= 0 ) {
+               $f = $d . '/' . $n . $auxExtension;
+
+               if ( file_exists( $f ) ) {
+                       return $f;
+               }
+
+               $idx = strrpos( $n, '.' );
+               if ( !$idx ) break;
+
+               $n = substr( $n, 0, $idx );
+               $maxStrip -= 1;
+       }
+
+       return false;
 }
\ No newline at end of file