has common filename pattern and better generated from posts

This commit is contained in:
John Bintz 2009-11-25 17:27:55 -05:00
parent 4831a731d1
commit 88e26c8f7d
2 changed files with 54 additions and 17 deletions

View File

@ -84,6 +84,7 @@ class ComicPressBackendFilesystem extends ComicPressBackend {
}
function find_matching_files($patterns) {
$matches = array();
foreach ($patterns as $pattern) {
$dir = dirname($pattern);
if (is_dir($dir)) {
@ -93,7 +94,7 @@ class ComicPressBackendFilesystem extends ComicPressBackend {
$target = $dir . '/' . $file;
if (is_file($target) || is_link($target)) {
if (preg_match('#' . $pattern. '#', $file) > 0) {
return $target;
$matches[] = $target;
}
}
}
@ -101,7 +102,7 @@ class ComicPressBackendFilesystem extends ComicPressBackend {
}
}
}
return false;
return $matches;
}
function generate_from_post($post) {
@ -118,6 +119,7 @@ class ComicPressBackendFilesystem extends ComicPressBackend {
if (isset($comicpress->comicpress_options['image_types'])) {
$files = array();
$all_patterns = array();
foreach (array_keys($comicpress->comicpress_options['image_types']) as $type) {
$patterns = $this->process_search_string($post, $type);
if (!empty($patterns)) {
@ -126,18 +128,28 @@ class ComicPressBackendFilesystem extends ComicPressBackend {
$files[$type] = $result;
}
}
$all_patterns = array_merge($all_patterns, $patterns);
}
if (!empty($files)) {
$fs = new ComicPressBackendFilesystem();
$fs->id = 'filesystem-' . $post->ID;
$fs->files_by_type = $files;
$return[] = $fs;
if (($filename_pattern = $this->has_common_filename_pattern($all_patterns)) !== false) {
if (!empty($files)) {
foreach ($this->group_by_root($filename_pattern, $files) as $root => $files_for_root) {
$fs = new ComicPressBackendFilesystem();
$fs->id = 'filesystem-' . $post->ID . '-' . $root;
$fs->files_by_type = $files_for_root;
$return[] = $fs;
}
}
}
}
return $return;
}
function has_common_filename_pattern($patterns) {
$filename_patterns = array_unique(array_map('basename', $patterns));
return (count($filename_patterns) == 1) ? reset($filename_patterns) : false;
}
function group_by_root($filename_pattern, $all_files) {
$roots = array();
$filename_pattern = str_replace('*', '(.*)', basename($filename_pattern));

View File

@ -66,12 +66,27 @@ class ComicPressBackendFilesystemTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($expected_searches, $fs->process_search_string($posts[$post_id_to_use], 'comic'));
}
function providerTestHasCommonFilenamePattern() {
return array(
array(array('/test/*.jpg', '/test2/*.jpg'), '*.jpg'),
array(array('/test/*.jpg', '/test2/*.gif'), false)
);
}
/**
* @dataProvider providerTestHasCommonFilenamePattern
*/
function testHasCommonFilenamePattern($patterns, $expected_result) {
$this->assertTrue($expected_result === $this->fs->has_common_filename_pattern($patterns));
}
function providerTestFindMatchingFiles() {
return array(
array(array('/blah'), false),
array(array('/comic/2008-01-01.jpg'), false),
array(array('/comic/2009-01-01.jpg'), vfsStream::url('root/comic/2009-01-01.jpg')),
array(array('/comic/2009-01-01-test.jpg'), vfsStream::url('root/comic/2009-01-01-test.jpg')),
array(array('/blah'), array()),
array(array('/comic/2008-01-01.jpg'), array()),
array(array('/comic/2009-01-01.jpg'), array(vfsStream::url('root/comic/2009-01-01.jpg'))),
array(array('/comic/2009-01-01-test.jpg'), array(vfsStream::url('root/comic/2009-01-01-test.jpg'))),
array(array('/comic/2009-01-01.jpg', '/comic/2009-01-02.jpg'), array(vfsStream::url('root/comic/2009-01-01.jpg'))),
);
}
@ -101,20 +116,30 @@ class ComicPressBackendFilesystemTest extends PHPUnit_Framework_TestCase {
$comicpress->comicpress_options['backend_options']['filesystem']['search_pattern'] = 'test';
$fs = $this->getMock('ComicPressBackendFilesystem', array('process_search_string', 'find_matching_files'));
$fs = $this->getMock('ComicPressBackendFilesystem', array('process_search_string', 'find_matching_files', 'group_by_root', 'has_common_filename_pattern'));
$fs->expects($this->at(0))->method('process_search_string')->with($post, 'comic')->will($this->returnValue(array('comic')));
$fs->expects($this->at(1))->method('find_matching_files')->with(array('comic'))->will($this->returnValue('comic'));
$fs->expects($this->at(1))->method('find_matching_files')->with(array('comic'))->will($this->returnValue(array('comic')));
$fs->expects($this->at(2))->method('process_search_string')->with($post, 'rss')->will($this->returnValue(array('rss')));
$fs->expects($this->at(3))->method('find_matching_files')->with(array('rss'))->will($this->returnValue('rss'));
$fs->expects($this->at(3))->method('find_matching_files')->with(array('rss'))->will($this->returnValue(array('rss')));
$fs->expects($this->at(4))->method('has_common_filename_pattern')->with(array('comic', 'rss'))->will($this->returnValue('test'));
$fs->expects($this->at(5))->method('group_by_root')->with('test', array(
'comic' => array('comic'),
'rss' => array('rss')
))->will($this->returnValue(array(
'root' => array(
'comic' => array('comic'),
'rss' => array('rss'),
)
)));
$return = $fs->generate_from_post($post);
$this->assertEquals(1, count($return));
$this->assertEquals('filesystem-1', $return[0]->id);
$this->assertEquals('filesystem-1-root', $return[0]->id);
$this->assertEquals(array(
'comic' => 'comic',
'rss' => 'rss'
'comic' => array('comic'),
'rss' => array('rss')
), $return[0]->files_by_type);
}