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)
committerKunal Mehta <legoktm@member.fsf.org>
Tue, 16 Oct 2018 00:31:16 +0000 (17:31 -0700)
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
(cherry picked from commit d88e924b6e5a7d529c471980e14f72430a94e546)

RELEASE-NOTES-1.31
includes/media/FormatMetadata.php
includes/upload/UploadStash.php

index 81854ab..9308a92 100644 (file)
@@ -13,6 +13,7 @@ THIS IS NOT A RELEASE YET
   of "break".
 * (T206979) Fix PHP 7.3 warnings of using "compact()" when some variables may
   not be set.
+* Fix PHP 7.3 warnings "preg_replace(): [...] invalid range in character class"
 
 == MediaWiki 1.31.1 ==
 
index 3ea71a9..6cb33ac 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;
 
        /**