summaryrefslogtreecommitdiff
path: root/maintenance/importImages.inc.php
diff options
context:
space:
mode:
Diffstat (limited to 'maintenance/importImages.inc.php')
-rw-r--r--maintenance/importImages.inc.php39
1 files changed, 39 insertions, 0 deletions
diff --git a/maintenance/importImages.inc.php b/maintenance/importImages.inc.php
index 53895778..290f3c07 100644
--- a/maintenance/importImages.inc.php
+++ b/maintenance/importImages.inc.php
@@ -46,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