* (bug 4679) Work around buggy basename() function in PHP5, which breaks
authorBrion Vibber <brion@users.mediawiki.org>
Fri, 20 Jan 2006 09:03:16 +0000 (09:03 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Fri, 20 Jan 2006 09:03:16 +0000 (09:03 +0000)
  uploads of files starting with multibyte characters on Linux.
  wfBaseName() doesn't suffer this bug, and understands backslash on
  both Unix and Windows.

RELEASE-NOTES
includes/GlobalFunctions.php
includes/SpecialUpload.php
tests/GlobalTest.php

index 30f1cda..0269e16 100644 (file)
@@ -544,6 +544,10 @@ fully support the editing toolbar, but was found to be too confusing.
   problem under PHP 5 with APC as opcode cache. See details:
   http://mail.wikipedia.org/pipermail/wikitech-l/2006-January/033660.html
 * Small changes to tabs in Monobook skin c/o Chris Ware
+* (bug 4679) Work around buggy basename() function in PHP5, which breaks
+  uploads of files starting with multibyte characters on Linux.
+  wfBaseName() doesn't suffer this bug, and understands backslash on
+  both Unix and Windows.
 
 
 === Caveats ===
index 8a74427..5b4d8bc 100644 (file)
@@ -1791,5 +1791,23 @@ function wfRegexReplacement( $string ) {
        return $string;
 }
 
+/**
+ * Return the final portion of a pathname.
+ * Reimplemented because PHP5's basename() is buggy with multibyte text.
+ * http://bugs.php.net/bug.php?id=33898
+ *
+ * PHP's basename() only considers '\' a pathchar on Windows and Netware.
+ * We'll consider it so always, as we don't want \s in our Unix paths either.
+ * 
+ * @param string $path
+ * @return string
+ */
+function wfBaseName( $path ) {
+       if( preg_match( '#([^/\\\\]*)[/\\\\]*$#', $path, $matches ) ) {
+               return $matches[1];
+       } else {
+               return '';
+       }
+}
 
 ?>
index ff32564..8ca7657 100644 (file)
@@ -171,9 +171,9 @@ class UploadForm {
 
                # Chop off any directories in the given filename
                if ( $this->mDestFile ) {
-                       $basename = basename( $this->mDestFile );
+                       $basename = wfBaseName( $this->mDestFile );
                } else {
-                       $basename = basename( $this->mOname );
+                       $basename = wfBaseName( $this->mOname );
                }
 
                /**
index a3aed8b..1567a18 100644 (file)
@@ -178,6 +178,32 @@ class GlobalTest extends PHPUnit_TestCase {
                        wfTimestamp( TS_DB, '2001-01-15 12:34:56' ),
                        'TS_DB to TS_DB' );
        }
+       
+       function testBasename() {
+               $sets = array(
+                       '' => '',
+                       '/' => '',
+                       '\\' => '',
+                       '//' => '',
+                       '\\\\' => '',
+                       'a' => 'a',
+                       'aaaa' => 'aaaa',
+                       '/a' => 'a',
+                       '\\a' => 'a',
+                       '/aaaa' => 'aaaa',
+                       '\\aaaa' => 'aaaa',
+                       '/aaaa/' => 'aaaa',
+                       '\\aaaa\\' => 'aaaa',
+                       '\\aaaa\\' => 'aaaa',
+                       '/mnt/upload3/wikipedia/en/thumb/8/8b/Zork_Grand_Inquisitor_box_cover.jpg/93px-Zork_Grand_Inquisitor_box_cover.jpg' => '93px-Zork_Grand_Inquisitor_box_cover.jpg',
+                       'C:\\Progra~1\\Wikime~1\\Wikipe~1\\VIEWER.EXE' => 'VIEWER.EXE',
+                       'Östergötland_coat_of_arms.png' => 'Östergötland_coat_of_arms.png',
+                       );
+               foreach( $sets as $from => $to ) {
+                       $this->assertEquals( $to, wfBaseName( $from ),
+                               "wfBaseName('$from') => '$to'");
+               }
+       }
 
        /* TODO: many more! */
 }