Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add logic for DIRECTORY_SEPARATOR to account for "/" or "\" in reposi… #432

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/writers/Oaipmh.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ public function writeMetadataFile($metadata, $path, $overwrite = true)
public function normalizeFilename($string)
{
$string = urldecode($string);
$pattern = '#'.DIRECTORY_SEPARATOR.'#';
Copy link
Collaborator

@mjordan mjordan Jul 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We run MIK on Windows, and since DIRECTORY_SEPARATOR on Windows is \, we've been burned using DIRECTORY_SEPARATOR unless the pattern is run through PHP's preg_quote(), since \ is also an escape character in regexes. So I recommend, and have used, something like this:

$pattern = preg_quote('#' . DIRECTORY_SEPARATOR . '#');

I can test with that code if you want, report back, and if it works on Windows and Linux, I'll merge.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds great.

Copy link
Collaborator

@mjordan mjordan Jul 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit more end-of-the-day hacking around has produced:

    /**
     * Convert %3A (:) and other characters in filenames into underscores (_).
     */
    public function normalizeFilename($string)
    {
        $string = urldecode($string);
        $bad_chars = [
            ':' => '_',
            DIRECTORY_SEPARATOR => '_',
        ];
        $string = str_replace(array_keys($bad_chars), $bad_chars, $string);
        return $string;
    }

This replaces : in my local tests. Could you test to see if it catches the directory separator? The benefit of str_replace() is that you don't need to worry about nasty cross-platform issues (I hope). Also adding new bad characters is pretty easy.

$string = preg_replace($pattern, '-', $string);
$string = preg_replace('/:/', '_', $string);
return $string;
}
Expand Down