Ori.livneh has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/242351

Change subject: Collapse redundant path segments; add extra torture tests
......................................................................

Collapse redundant path segments; add extra torture tests

* Normalize '/foo/bar/../baz' to '/foo/baz'.
* Normalize '/foo/bar/./baz' to '/foo/bar/baz'.
* Add test cases for these and a few other corner cases.

Change-Id: I34b83445e5bb9fd4b0941b0c6294002d6ce2e421
---
M src/RelPath.php
M tests/RelPathTest.php
2 files changed, 22 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/RelPath refs/changes/51/242351/1

diff --git a/src/RelPath.php b/src/RelPath.php
index d54b502..055f468 100644
--- a/src/RelPath.php
+++ b/src/RelPath.php
@@ -35,17 +35,30 @@
  */
 function splitPath( $path ) {
        $fragments = array();
+
        while ( true ) {
                $cur = dirname( $path );
                if ( $cur === $path || ( $cur === '.' && basename( $path ) === 
$path ) ) {
                        break;
                }
-               $fragments[] = trim( substr( $path, strlen( $cur ) ), '/' );
+
+               $fragment = trim( substr( $path, strlen( $cur ) ), '/' );
+
+               if ( !$fragments ) {
+                       $fragments[] = $fragment;
+               } elseif ( $fragment === '..' && basename( $cur ) !== '..' ) {
+                       $cur = dirname( $cur );
+               } elseif ( $fragment !== '.' ) {
+                       $fragments[] = $fragment;
+               }
+
                $path = $cur;
        }
+
        if ( $path !== '' ) {
                $fragments[] = trim( $path, '/' );
        }
+
        return array_reverse( $fragments );
 }
 
@@ -103,7 +116,7 @@
        }
 
        if ( substr( $base, 0, 1 ) !== '/' ) {
-               return false;
+               return false;  // $base is relative.
        }
 
        $pathParts = splitPath( $path );
@@ -123,5 +136,6 @@
                        break;
                }
        }
+
        return implode( '/', $resultParts );
 }
diff --git a/tests/RelPathTest.php b/tests/RelPathTest.php
index 8b83c12..2deeb37 100644
--- a/tests/RelPathTest.php
+++ b/tests/RelPathTest.php
@@ -62,12 +62,18 @@
                return array(
                        array( '/foo/bar', './baz', '/foo/bar/baz' ),
                        array( '/foo/bar', '/tmp/file/', '/tmp/file/' ),
+                       array( '/foo/0/bar', '/tmp/0/file/', '/tmp/0/file/' ),
+                       array( '/foo/./bar', '/tmp/0/.file/', '/tmp/0/.file/' ),
+                       array( '/foo/./bar', 'tmp/0/.file/', 
'/foo/bar/tmp/0/.file' ),
                        array( '/foo/bar', 'file', '/foo/bar/file' ),
                        array( '/foo/bar', '.file', '/foo/bar/.file' ),
                        array( '/foo//bar', '../baz', '/foo/baz' ),
+                       array( '/foo/bar', '../././baz/.', '/foo/baz' ),
                        array( '/foo//bar', '../../baz', '/baz' ),
                        array( '/foo//bar', '../../../baz', '/baz' ),
                        array( '/', '../../../baz', '/baz' ),
+                       array( '/foo/bar/../baz', 'quux', '/foo/baz/quux' ),
+                       array( 'foo/bar', 'quux', false ),
                );
        }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/242351
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I34b83445e5bb9fd4b0941b0c6294002d6ce2e421
Gerrit-PatchSet: 1
Gerrit-Project: RelPath
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to