Fix PHP warnings "preg_replace(): [...] invalid range in character class"
authorEdward Chernenko <edwardspec@gmail.com>
Mon, 18 Jun 2018 22:53:52 +0000 (01:53 +0300)
committerReedy <reedy@wikimedia.org>
Tue, 19 Jun 2018 00:11:33 +0000 (00:11 +0000)
This was spotted when running tests on Travis (PHP 7.3 nighly, trusty).

Two expressions inside preg_replace() contained non-escaped "-" inside [],
where this "-" meant an actual "-" character.
The warning is because "-" has special meaning inside [] ("a-z" for range),
and things like [\w-.] are considered "invalid range".

Solution is to escape "-" like this: [\w\-.]

Change-Id: I41cc217081f00f54d957b6d8052ee209412f5ff6

includes/media/FormatMetadata.php
includes/upload/UploadStash.php

index 52d7373..9ebc63f 100644 (file)
@@ -1859,9 +1859,9 @@ class FormatMetadata extends ContextSource {
                // drop all characters which are not valid in an XML tag name
                // a bunch of non-ASCII letters would be valid but probably won't
                // be used so we take the easy way
-               $key = preg_replace( '/[^a-zA-z0-9_:.-]/', '', $key );
+               $key = preg_replace( '/[^a-zA-z0-9_:.\-]/', '', $key );
                // drop characters which are invalid at the first position
-               $key = preg_replace( '/^[\d-.]+/', '', $key );
+               $key = preg_replace( '/^[\d\-.]+/', '', $key );
 
                if ( $key == '' ) {
                        $key = '_';
index e25b11e..e55ab1f 100644 (file)
@@ -52,7 +52,7 @@
  */
 class UploadStash {
        // Format of the key for files -- has to be suitable as a filename itself (e.g. ab12cd34ef.jpg)
-       const KEY_FORMAT_REGEX = '/^[\w-\.]+\.\w*$/';
+       const KEY_FORMAT_REGEX = '/^[\w\-\.]+\.\w*$/';
        const MAX_US_PROPS_SIZE = 65535;
 
        /**