bring in a bunch of stuff from cp 2.8

This commit is contained in:
John Bintz 2009-12-06 13:53:11 -05:00
parent 19c0b51477
commit a46e9ad58b
2 changed files with 67 additions and 2 deletions

View File

@ -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('#(?<![.])\*#', '.*', $input);
return $input;
}
function resolve_regex_path($input) {
$input = str_replace('\.', '.', $input);
$input = str_replace('\\', '/', $input);
return $input;
}
function find_matching_files($patterns) {
$matches = array();
foreach ($patterns as $pattern) {
$dir = dirname($pattern);
$dir = $this->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;

View File

@ -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));
}
}