Improve how slashes are stripped from filenames
authorBartosz Dziewoński <matma.rex@gmail.com>
Sun, 24 Jul 2016 19:44:35 +0000 (21:44 +0200)
committerBartosz Dziewoński <matma.rex@gmail.com>
Sun, 24 Jul 2016 22:24:36 +0000 (00:24 +0200)
* Add slash and backslash ('/' and '\') to $wgIllegalFileChars.
* Replace illegal chars before removing paths in wfStripIllegalFilenameChars().

This way users trying to upload a file with slashes in the name will
get a better filename suggestion (e.g. for 'Foo part 1/3.jpg', you
previously got '3.jpg', now you'll get 'Foo part 1-3.jpg'). Uploading
tools that don't special-case slashes will also behave better.

Change-Id: Ib78f48a5f8c92e8ab2dc773ea6789b96b3662177

includes/DefaultSettings.php
includes/GlobalFunctions.php
tests/phpunit/includes/upload/UploadBaseTest.php

index 1e60302..5926328 100644 (file)
@@ -398,9 +398,13 @@ $wgAllowImageMoving = true;
 $wgEnableAsyncUploads = false;
 
 /**
 $wgEnableAsyncUploads = false;
 
 /**
- * These are additional characters that should be replaced with '-' in filenames
+ * Additional characters that are not allowed in filenames. They are replaced with '-' when
+ * uploading. Like $wgLegalTitleChars, this is a regexp character class.
+ *
+ * Slashes and backslashes are disallowed regardless of this setting, but included here for
+ * completeness.
  */
  */
-$wgIllegalFileChars = ":";
+$wgIllegalFileChars = ":\\/\\\\";
 
 /**
  * What directory to place deleted uploads in.
 
 /**
  * What directory to place deleted uploads in.
index 66e2440..e262c81 100644 (file)
@@ -3350,9 +3350,9 @@ function wfCountDown( $seconds ) {
 }
 
 /**
 }
 
 /**
- * Replace all invalid characters with -
- * Additional characters can be defined in $wgIllegalFileChars (see bug 20489)
- * By default, $wgIllegalFileChars = ':'
+ * Replace all invalid characters with '-'.
+ * Additional characters can be defined in $wgIllegalFileChars (see T22489).
+ * By default, $wgIllegalFileChars includes ':', '/', '\'.
  *
  * @param string $name Filename to process
  * @return string
  *
  * @param string $name Filename to process
  * @return string
@@ -3360,12 +3360,13 @@ function wfCountDown( $seconds ) {
 function wfStripIllegalFilenameChars( $name ) {
        global $wgIllegalFileChars;
        $illegalFileChars = $wgIllegalFileChars ? "|[" . $wgIllegalFileChars . "]" : '';
 function wfStripIllegalFilenameChars( $name ) {
        global $wgIllegalFileChars;
        $illegalFileChars = $wgIllegalFileChars ? "|[" . $wgIllegalFileChars . "]" : '';
-       $name = wfBaseName( $name );
        $name = preg_replace(
                "/[^" . Title::legalChars() . "]" . $illegalFileChars . "/",
                '-',
                $name
        );
        $name = preg_replace(
                "/[^" . Title::legalChars() . "]" . $illegalFileChars . "/",
                '-',
                $name
        );
+       // $wgIllegalFileChars may not include '/' and '\', so we still need to do this
+       $name = wfBaseName( $name );
        return $name;
 }
 
        return $name;
 }
 
index 287af29..de6c412 100644 (file)
@@ -52,7 +52,7 @@ class UploadBaseTest extends MediaWikiTestCase {
                        [ 'ValidTitle.jpg', 'ValidTitle.jpg', UploadBase::OK,
                                'upload valid title' ],
                        /* A title with a slash */
                        [ 'ValidTitle.jpg', 'ValidTitle.jpg', UploadBase::OK,
                                'upload valid title' ],
                        /* A title with a slash */
-                       [ 'A/B.jpg', 'B.jpg', UploadBase::OK,
+                       [ 'A/B.jpg', 'A-B.jpg', UploadBase::OK,
                                'upload title with slash' ],
                        /* A title with illegal char */
                        [ 'A:B.jpg', 'A-B.jpg', UploadBase::OK,
                                'upload title with slash' ],
                        /* A title with illegal char */
                        [ 'A:B.jpg', 'A-B.jpg', UploadBase::OK,