jenkins-bot has submitted this change and it was merged. Change subject: Add wikimedia/relpath 1.0.3 ......................................................................
Add wikimedia/relpath 1.0.3 Corresponding mediawiki/core change: If29924908. Change-Id: Ia3d5364854002fd626d50a3cc3fc1edf98f55a87 --- M composer.json M composer.lock M composer/autoload_files.php M composer/installed.json A wikimedia/relpath/LICENSE A wikimedia/relpath/README.md A wikimedia/relpath/src/RelPath.php 7 files changed, 270 insertions(+), 2 deletions(-) Approvals: Krinkle: Looks good to me, approved jenkins-bot: Verified diff --git a/composer.json b/composer.json index 6c0a4bd..0b5c891 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,7 @@ "wikimedia/cldr-plural-rule-parser": "1.0.0", "wikimedia/composer-merge-plugin": "1.2.1", "wikimedia/ip-set": "1.0.1", + "wikimedia/relpath": "1.0.3", "wikimedia/utfnormal": "1.0.3", "wikimedia/wrappedstring": "2.0.0", "zordius/lightncandy": "0.21" diff --git a/composer.lock b/composer.lock index 8dd9963..7d4f965 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "96d4aedd683905380ed0c99d2ec321f4", - "content-hash": "788a8989a97620704c1e04ca61f2582e", + "hash": "6551ecfafd6fb8f4672316012253be56", + "content-hash": "6f7d40f95a0ba1e9b234c0edb2f31bd3", "packages": [ { "name": "composer/semver", @@ -928,6 +928,48 @@ "time": "2015-06-29 20:21:27" }, { + "name": "wikimedia/relpath", + "version": "1.0.3", + "source": { + "type": "git", + "url": "https://github.com/wikimedia/RelPath.git", + "reference": "ee38e19f3b059e5cec8863b03e35a451b8f35106" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/wikimedia/RelPath/zipball/ee38e19f3b059e5cec8863b03e35a451b8f35106", + "reference": "ee38e19f3b059e5cec8863b03e35a451b8f35106", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "jakub-onderka/php-parallel-lint": "^0.9.0.0", + "mediawiki/mediawiki-codesniffer": "^0.4.0.0", + "phpunit/phpunit": "^4.8.9.0" + }, + "type": "library", + "autoload": { + "files": [ + "src/RelPath.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ori Livneh", + "email": "o...@wikimedia.org" + } + ], + "description": "Compute a relative filepath between two paths.", + "homepage": "https://www.mediawiki.org/wiki/RelPath", + "time": "2015-09-29 21:17:03" + }, + { "name": "wikimedia/utfnormal", "version": "v1.0.3", "source": { diff --git a/composer/autoload_files.php b/composer/autoload_files.php index fba07a7..52ef672 100644 --- a/composer/autoload_files.php +++ b/composer/autoload_files.php @@ -7,4 +7,5 @@ return array( $vendorDir . '/mediawiki/at-ease/src/Functions.php', + $vendorDir . '/wikimedia/relpath/src/RelPath.php', ); diff --git a/composer/installed.json b/composer/installed.json index 5f67fa3..7a92ffb 100644 --- a/composer/installed.json +++ b/composer/installed.json @@ -1095,5 +1095,49 @@ "php", "stylesheet" ] + }, + { + "name": "wikimedia/relpath", + "version": "1.0.3", + "version_normalized": "1.0.3.0", + "source": { + "type": "git", + "url": "https://github.com/wikimedia/RelPath.git", + "reference": "ee38e19f3b059e5cec8863b03e35a451b8f35106" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/wikimedia/RelPath/zipball/ee38e19f3b059e5cec8863b03e35a451b8f35106", + "reference": "ee38e19f3b059e5cec8863b03e35a451b8f35106", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "jakub-onderka/php-parallel-lint": "^0.9.0.0", + "mediawiki/mediawiki-codesniffer": "^0.4.0.0", + "phpunit/phpunit": "^4.8.9.0" + }, + "time": "2015-09-29 21:17:03", + "type": "library", + "installation-source": "dist", + "autoload": { + "files": [ + "src/RelPath.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ori Livneh", + "email": "o...@wikimedia.org" + } + ], + "description": "Compute a relative filepath between two paths.", + "homepage": "https://www.mediawiki.org/wiki/RelPath" } ] diff --git a/wikimedia/relpath/LICENSE b/wikimedia/relpath/LICENSE new file mode 100644 index 0000000..ad55c12 --- /dev/null +++ b/wikimedia/relpath/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2015 Ori Livneh <o...@wikimedia.org> + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/wikimedia/relpath/README.md b/wikimedia/relpath/README.md new file mode 100644 index 0000000..421be52 --- /dev/null +++ b/wikimedia/relpath/README.md @@ -0,0 +1,20 @@ +RelPath +======= + +RelPath is a small PHP library for computing a relative filepath to some path, +either from the current directory or from an optional start directory. + +Here is how you use it: + +<pre lang="php"> +$relPath = RelPath\getRelativePath( '/srv/mediawiki/resources/src/startup.js', '/srv/mediawiki' ); +// Result: string(24) "resources/src/startup.js" + +$fullPath = RelPath\joinPath( '/srv/mediawiki', 'resources/src/startup.js' ); +// Result: string(39) "/srv/mediawiki/resources/src/startup.js" +</pre> + +License +------- + +MIT diff --git a/wikimedia/relpath/src/RelPath.php b/wikimedia/relpath/src/RelPath.php new file mode 100644 index 0000000..055f468 --- /dev/null +++ b/wikimedia/relpath/src/RelPath.php @@ -0,0 +1,141 @@ +<?php +/** + * Copyright (c) 2015 Ori Livneh <o...@wikimedia.org> + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * @file + * @author Ori Livneh <o...@wikimedia.org> + */ + +namespace RelPath; + +/** + * Split a path into path components. + * + * @param string $path File path. + * @return array Array of path components. + */ +function splitPath( $path ) { + $fragments = array(); + + while ( true ) { + $cur = dirname( $path ); + if ( $cur === $path || ( $cur === '.' && basename( $path ) === $path ) ) { + break; + } + + $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 ); +} + +/** + * Return a relative filepath to path either from the current directory or from + * an optional start directory. Both paths must be absolute. + * + * @param string $path File path. + * @param string $start Start directory. Optional; if not specified, the current + * working directory will be used. + * @return array|bool Array of path components or false if file does not exist. + */ +function getRelativePath( $path, $start = null ) { + if ( $start === null ) { + $start = getcwd(); + } + + if ( substr( $path, 0, 1 ) !== '/' || substr( $start, 0, 1 ) !== '/' ) { + return false; + } + + $pathParts = splitPath( $path ); + $countPathParts = count( $pathParts ); + + $startParts = splitPath( $start ); + $countStartParts = count( $startParts ); + + $commonLength = min( $countPathParts, $countStartParts ); + for ( $i = 0; $i < $commonLength; $i++ ) { + if ( $startParts[$i] !== $pathParts[$i] ) { + break; + } + } + + $relList = ( $countStartParts > $i ) + ? array_fill( 0, $countStartParts - $i, '..' ) + : array(); + + $relList = array_merge( $relList, array_slice( $pathParts, $i ) ); + + return implode( '/', $relList ) ?: '.'; +} + + +/** + * Join path components. + * + * @param string $base Base path. + * @param string $path File path to join to base path. + * @return string + */ +function joinPath( $base, $path ) { + if ( substr( $path, 0, 1 ) === '/' ) { + return $path; // $path is absolute. + } + + if ( substr( $base, 0, 1 ) !== '/' ) { + return false; // $base is relative. + } + + $pathParts = splitPath( $path ); + $resultParts = splitPath( $base ); + + while ( ( $part = array_shift( $pathParts ) ) !== null ) { + switch ( $part ) { + case '.': + break; + case '..': + if ( count( $resultParts ) > 1 ) { + array_pop( $resultParts ); + } + break; + default: + $resultParts[] = $part; + break; + } + } + + return implode( '/', $resultParts ); +} -- To view, visit https://gerrit.wikimedia.org/r/242217 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ia3d5364854002fd626d50a3cc3fc1edf98f55a87 Gerrit-PatchSet: 5 Gerrit-Project: mediawiki/vendor Gerrit-Branch: master Gerrit-Owner: Ori.livneh <o...@wikimedia.org> Gerrit-Reviewer: Gilles <gdu...@wikimedia.org> Gerrit-Reviewer: Krinkle <krinklem...@gmail.com> Gerrit-Reviewer: Legoktm <legoktm.wikipe...@gmail.com> Gerrit-Reviewer: Ori.livneh <o...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits