From a46e9ad58b44521f51124ba3cc893a2bcab7adef Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sun, 6 Dec 2009 13:53:11 -0500 Subject: [PATCH] bring in a bunch of stuff from cp 2.8 --- .../backends/ComicPressBackendFilesystem.inc | 22 ++++++++- .../ComicPressBackendFilesystemTest.php | 47 +++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/classes/backends/ComicPressBackendFilesystem.inc b/classes/backends/ComicPressBackendFilesystem.inc index 5420e2b..213a79c 100644 --- a/classes/backends/ComicPressBackendFilesystem.inc +++ b/classes/backends/ComicPressBackendFilesystem.inc @@ -6,6 +6,7 @@ class ComicPressBackendFilesystem extends ComicPressBackend { var $search_string = ''; var $id, $files_by_type = array(); + // TODO Make this more generic (ex: date-Ymd calls _replace_date($post, $type, "Ymd")) function process_search_string($post, $type) { $this->_searches = array($this->search_string); @@ -83,12 +84,29 @@ class ComicPressBackendFilesystem extends ComicPressBackend { return false; } + function get_regex_dirname($input) { + return dirname(ComicPressBackendFilesystem::resolve_regex_path($input)); + } + + function get_regex_filename($input) { + $input = preg_replace('#\\\(?![.])#', '/', $input); + $input = preg_replace('#^.*\/([^\/]+)$#', '$1', $input); + $input = preg_replace('#(?get_regex_dirname($pattern); if (is_dir($dir)) { - $pattern = str_replace('*', '.*', basename($pattern)); + $pattern = $this->get_regex_filename($pattern); if (($dh = opendir($dir)) !== false) { while (($file = readdir($dh)) !== false) { $target = $dir . '/' . $file; diff --git a/test/backends/ComicPressBackendFilesystemTest.php b/test/backends/ComicPressBackendFilesystemTest.php index 5a7b66a..9bec1f5 100644 --- a/test/backends/ComicPressBackendFilesystemTest.php +++ b/test/backends/ComicPressBackendFilesystemTest.php @@ -204,4 +204,51 @@ class ComicPressBackendFilesystemTest extends PHPUnit_Framework_TestCase { $this->assertEquals($return, ComicPressBackendFilesystem::generate_from_id($id)); } + + function providerTestResolveRegexPath() { + return array( + array('test', 'test'), + array('te\.st', 'te.st'), + array('te\st', 'te/st'), + ); + } + + /** + * @dataProvider providerTestResolveRegexPath + */ + function testResolveRegexPath($input, $expected_output) { + $this->assertEquals($expected_output, ComicPressBackendFilesystem::resolve_regex_path($input)); + } + + function providerTestGetRegexDirname() { + return array( + array('/test/test2', '/test') + ); + } + + /** + * @dataProvider providerTestGetRegexDirname + */ + function testGetRegexDirname($input, $expected_output) { + $this->assertEquals($expected_output, ComicPressBackendFilesystem::get_regex_dirname($input)); + } + + function providerTestGetRegexFilename() { + return array( + array('/test/test2', 'test2'), + array('c:\test\test2', 'test2'), + array('/test/test2\.cat', 'test2\.cat'), + array('c:\test\test2\.cat', 'test2\.cat'), + array('C:/inetpub/a\.windows\.directory/comics/2009-11-24.*\..*', '2009-11-24.*\..*'), + array('c:\test\test2\.cat*', 'test2\.cat.*'), + array('c:\test\test2\.cat.*', 'test2\.cat.*'), + ); + } + + /** + * @dataProvider providerTestGetRegexFilename + */ + function testGetRegexFilename($input, $expected_output) { + $this->assertEquals($expected_output, ComicPressBackendFilesystem::get_regex_filename($input)); + } }