Commit 52ff566c authored by Thomas Citharel's avatar Thomas Citharel 🌲

Merge branch 'fix-symlinks-for-wiki' into 'framasites'

Fix symlinks for wiki with code from Composer

See merge request framasoft/framasite!17
parents 3df949b7 37be61d4
Pipeline #26075 passed with stage
in 7 minutes and 4 seconds
......@@ -415,9 +415,10 @@ EOT;
*/
public function linkDomainFolder(string $source, string $link)
{
$fs = new Filesystem();
$this->logger->debug('Symlinking ' . $this->accountsPath . '/' . $source . '.frama.wiki' . ' and ' . $this->accountsPath . '/' . $link);
$fs->symlink($this->accountsPath . '/' . $source . '.frama.wiki', $this->accountsPath . '/' . $link);
$sourcePath = $this->accountsPath . '/' . $source . '.frama.wiki';
$targetPath = $this->accountsPath . '/' . $link;
$this->relativeSymlink($sourcePath, $targetPath);
}
/**
......@@ -429,4 +430,102 @@ EOT;
$this->logger->debug('Unlinking ' . $this->accountsPath . '/' . $domain);
$fs->remove($this->accountsPath . '/' . $domain);
}
/**
* Creates a relative symlink from $link to $target
*
* @param string $target The path of the binary file to be symlinked
* @param string $link The path where the symlink should be created
* @return bool
*/
private function relativeSymlink($target, $link)
{
$cwd = getcwd();
$relativePath = $this->findShortestPath($link, $target);
chdir(dirname($link));
$result = @symlink($relativePath, $link);
  • Pourquoi pas simplement @symlink($source . '.frama.wiki', $domain) ?

    Enfin, simplement $this->relativeSymlink($source . '.frama.wiki', $domain); ligne 421 et un @symlink($target, $link); dans cette fonction

    Edited by Luc Didry
  • Le souci c'est que ça fait des symlinks absolus comme wiki.fiat-tux.fr -> /home/tcit/dev/frama/framasites/framasites/wiki/users/luc.frama.wiki

  • Avec ce commit j'obtenais wiki.fiat-tux.fr -> ./luc.frama.wiki en local.

Please register or sign in to reply
chdir($cwd);
return (bool) $result;
}
/**
* Returns the shortest path from $from to $to
*
* @param string $from
* @param string $to
* @param bool $directories if true, the source/target are considered to be directories
* @throws \InvalidArgumentException
* @return string
*/
private function findShortestPath($from, $to, $directories = false)
{
if (!$this->isAbsolutePath($from) || !$this->isAbsolutePath($to)) {
throw new \InvalidArgumentException(sprintf('$from (%s) and $to (%s) must be absolute paths.', $from, $to));
}
$from = lcfirst($this->normalizePath($from));
$to = lcfirst($this->normalizePath($to));
if ($directories) {
$from = rtrim($from, '/') . '/dummy_file';
}
if (dirname($from) === dirname($to)) {
return './'.basename($to);
}
$commonPath = $to;
while (strpos($from.'/', $commonPath.'/') !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/?$}i', $commonPath)) {
$commonPath = strtr(dirname($commonPath), '\\', '/');
}
if (0 !== strpos($from, $commonPath) || '/' === $commonPath) {
return $to;
}
$commonPath = rtrim($commonPath, '/') . '/';
$sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/');
$commonPathCode = str_repeat('../', $sourcePathDepth);
return ($commonPathCode . substr($to, strlen($commonPath))) ?: './';
}
/**
* Checks if the given path is absolute
*
* @param string $path
* @return bool
*/
private function isAbsolutePath($path)
{
return substr($path, 0, 1) === '/' || substr($path, 1, 1) === ':';
}
/**
* Normalize a path. This replaces backslashes with slashes, removes ending
* slash and collapses redundant separators and up-level references.
*
* @param string $path Path to the file or directory
* @return string
*/
private function normalizePath($path)
{
$parts = array();
$path = strtr($path, '\\', '/');
$prefix = '';
$absolute = false;
// extract a prefix being a protocol://, protocol:, protocol://drive: or simply drive:
if (preg_match('{^( [0-9a-z]{2,}+: (?: // (?: [a-z]: )? )? | [a-z]: )}ix', $path, $match)) {
$prefix = $match[1];
$path = substr($path, strlen($prefix));
}
if (substr($path, 0, 1) === '/') {
$absolute = true;
$path = substr($path, 1);
}
$up = false;
foreach (explode('/', $path) as $chunk) {
if ('..' === $chunk && ($absolute || $up)) {
array_pop($parts);
$up = !(empty($parts) || '..' === end($parts));
} elseif ('.' !== $chunk && '' !== $chunk) {
$parts[] = $chunk;
$up = '..' !== $chunk;
}
}
return $prefix.($absolute ? '/' : '').implode('/', $parts);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment