oh wow, filesystem finding is really working

This commit is contained in:
John Bintz 2010-02-01 22:34:28 -05:00
parent 739753ee8e
commit 1dd4c8a97d
8 changed files with 243 additions and 66 deletions

View File

@ -7,7 +7,14 @@ require_once(dirname(__FILE__) . '/ComicPressComicPost.inc');
*/
class ComicPress {
var $comicpress_options = array(
'image_types' => array(
'image_types' => array(),
'helpers' => array(),
'storyline_order' => '',
'enabled_backends' => null,
'category_groupings' => array()
);
var $default_image_types = array(
'comic' => array(
'default' => true,
'name' => 'Comic',
@ -27,12 +34,7 @@ class ComicPress {
'default' => false,
'name' => 'Minithumb',
'dimensions' => '100x'
),
),
'helpers' => array(),
'storyline_order' => '',
'enabled_backends' => null,
'category_groupings' => array()
)
);
var $backends = array();
@ -56,8 +58,10 @@ class ComicPress {
*/
function load() {
$result = get_option('comicpress-options');
if (is_array($result)) {
if (!empty($result) && is_array($result)) {
$this->comicpress_options = $this->_array_merge_replace_recursive($this->comicpress_options, $result);
} else {
$this->comicpress_options['image_types'] = $this->default_image_types;
}
}
@ -297,4 +301,3 @@ class ComicPress {
}
class ComicPressException extends Exception {}

View File

@ -1,6 +1,8 @@
<?php
class ComicPressBackend {
function __construct() {}
function _embed_image($size) {
$size = $this->ensure_type($size);
$extras = array();

View File

@ -17,13 +17,9 @@ class ComicPressBackendAttachment extends ComicPressBackend {
$comicpress = ComicPress::get_instance();
$dims = array();
if (isset($comicpress->comicpress_options['image_types'])) {
if (isset($comicpress->comicpress_options['image_types'][$size])) {
if (isset($comicpress->comicpress_options['image_types'][$size]['dimensions'])) {
$dims = array_combine(array('width', 'height'), explode("x", $comicpress->comicpress_options['image_types'][$size]['dimensions']));
}
}
}
return $dims;
}

View File

@ -4,7 +4,31 @@ require_once(dirname(__file__) . '/../ComicPressBackend.inc');
class ComicPressBackendFilesystem extends ComicPressBackend {
var $search_string = '';
var $id, $files_by_type = array();
var $id, $files_by_type = array(), $file_urls_by_type = array();
var $source_name;
function __construct() {
parent::__construct();
$this->source_name = __('Filesystem', 'comicpress');
}
function dims($size = null) {
$dims = array();
if ($result = getimagesize($this->files_by_type[$this->ensure_type($size)])) {
$dims = array_combine(array('width', 'height'), array_slice($result, 0, 2));
}
return $dims;
}
function url($size = null) {
return $this->file_urls_by_type[$this->ensure_type($size)];
}
function file($size = null) { return $this->files_by_type[$this->ensure_type($size)]; }
function alt() {}
function title() {}
}
class ComicPressBackendFilesystemFactory {
@ -17,24 +41,38 @@ class ComicPressBackendFilesystemFactory {
list($all, $post_id, $root) = $matches;
if (($result = get_post_meta($post_id, 'backend_filesystem_files_by_type', true)) !== false) {
if (isset($result[$root])) {
if (is_array($result)) {
if (isset($result[0][$root])) {
$return = new ComicPressBackendFilesystem();
$return->id = $id;
$return->files_by_type = $result[$root];
foreach (array('files_by_type', 'file_urls_by_type') as $index => $name) {
$return->{$name} = $result[$index][$root];
}
return $return;
}
}
}
}
return false;
}
function _get_search_pattern() {
return $this->_get_pattern('search_pattern');
}
function _get_url_pattern() {
return $this->_get_pattern('url_pattern');
}
function _get_pattern($which) {
$comicpress = ComicPress::get_instance();
if (isset(
$comicpress->comicpress_options['backend_options']['filesystem']['search_pattern']
$comicpress->comicpress_options['backend_options']['filesystem'][$which]
)) {
return (string)$comicpress->comicpress_options['backend_options']['filesystem']['search_pattern'];
return (string)$comicpress->comicpress_options['backend_options']['filesystem'][$which];
}
return '';
@ -44,7 +82,7 @@ class ComicPressBackendFilesystemFactory {
$return = array();
$comicpress = ComicPress::get_instance();
$this->search_pattern = $this->_get_search_pattern();
$this->search_string = $this->_get_search_pattern();
if (isset($comicpress->comicpress_options['image_types'])) {
$files = array();
@ -62,11 +100,15 @@ class ComicPressBackendFilesystemFactory {
if (($filename_pattern = $this->has_common_filename_pattern($all_patterns)) !== false) {
if (!empty($files)) {
$grouped_by_root = $this->group_by_root($filename_pattern, $files);
update_post_meta($post->ID, 'backend_filesystem_files_by_type', $grouped_by_root);
$urls_by_root = $this->get_urls_for_post_roots($grouped_by_root, $post);
update_post_meta($post->ID, 'backend_filesystem_files_by_type', array($grouped_by_root, $urls_by_root));
foreach ($grouped_by_root as $root => $files_for_root) {
$fs = new ComicPressBackendFilesystem();
$fs->id = 'filesystem-' . $post->ID . '-' . $root;
$fs->files_by_type = $files_for_root;
$fs->file_urls_by_type = $urls_by_root[$root];
$return[] = $fs;
}
}
@ -76,16 +118,15 @@ class ComicPressBackendFilesystemFactory {
return $return;
}
function process_search_string($post, $type) {
function process_search_string($post, $type, $filename = null) {
$this->_searches = array($this->search_string);
$this->_filename = $filename;
do {
$any_found = false;
for ($i = 0; $i < count($this->_searches); ++$i) {
$search = $this->_searches[$i];
if (preg_match('#%([a-z0-9\-]+)%#i', $search, $matches) > 0) {
$any_found = true;
if (preg_match('#%([a-z0-9-]+?)%#i', $search, $matches) > 0) {
$found = false;
$parts = explode('-', $matches[1]);
foreach (array(
@ -93,6 +134,7 @@ class ComicPressBackendFilesystemFactory {
'_replace_' . strtolower($parts[0]) => implode('-', array_slice($parts, 1))
) as $method => $additional) {
if (method_exists($this, $method)) {
$any_found = true;
$found = true;
$result = $this->{$method}($post, $type, $additional);
if ($result !== false) {
@ -119,9 +161,13 @@ class ComicPressBackendFilesystemFactory {
}
// @codeCoverageIgnoreStart
function _replace_wordpress($post, $type) { return ABSPATH; }
function _replace_wordpress($post, $type) { return untrailingslashit(ABSPATH); }
// @codeCoverageIgnoreEnd
function _replace_wordpress_url($post, $type) { return untrailingslashit(get_option('home')); }
function _replace_filename($post, $type) { return $this->_filename; }
function _replace_type($post, $type) { return $type; }
function _replace_upload_path($post, $type) { return get_option('upload_path'); }
@ -132,7 +178,7 @@ class ComicPressBackendFilesystemFactory {
if (isset($comicpress->comicpress_options['backend_options']['filesystem']['folders'][$type])) {
return $comicpress->comicpress_options['backend_options']['filesystem']['folders'][$type];
}
return false;
return '';
}
function _replace_date($post, $type, $additional) {
@ -239,12 +285,29 @@ class ComicPressBackendFilesystemFactory {
return $roots;
}
function get_urls_for_post_roots($roots, $post) {
$urls = array();
$this->search_string = $this->_get_url_pattern();
foreach ($roots as $root => $files) {
$urls[$root] = array();
foreach ($files as $type => $file) {
$urls[$root][$type] = reset($this->process_search_string($post, $type, basename($file)));
}
}
return $urls;
}
}
// @codeCoverageIgnoreStart
class ComicPressBackendFilesystemAdmin {
function options_admin() {
$pattern = ComicPressBackendFilesystemFactory::_get_search_pattern();
$factory = new ComicPressBackendFilesystemFactory();
$filesystem_pattern = $factory->_get_search_pattern();
$url_pattern = $factory->_get_url_pattern();
include('partials/backend-filesystem/options-admin.inc');
}
@ -274,7 +337,7 @@ class ComicPressBackendFilesystemAdmin {
$comicpress->comicpress_options['backend_options']['filesystem'] = array();
}
foreach (array('folders', 'search_pattern') as $valid_field) {
foreach (array('folders', 'search_pattern', 'url_pattern') as $valid_field) {
if (is_array($info[$valid_field])) {
$comicpress->comicpress_options['backend_options']['filesystem'][$valid_field] = array();
foreach ($info[$valid_field] as $field => $value) {

View File

@ -3,5 +3,23 @@
<td>
<input type="text" name="cp[backend_options][filesystem][folders][<?php echo esc_attr($type) ?>]" value="<?php echo esc_attr($path) ?>" />
<br /><em><?php _e('(the folder path to fill in when using %type-folder%)', 'comicpress') ?></em>
<?php
$backend = new ComicPressBackendFilesystemFactory();
$backend->search_string = $backend->_get_search_pattern();
if (!empty($backend->search_string)) {
$search_paths = $backend->process_search_string((object)array(
'post_date' => date('Y-m-d H:i:s')
), $type); ?>
<p>
<?php _e('With the current saved settings, files that match this pattern will be attached to the posts of that same date:', 'comicpress') ?>
<ul>
<?php foreach ($search_paths as $path) { ?>
<li><code><?php echo $path ?></code></li>
<?php } ?>
</ul>
</p>
<?php }
?>
</td>
</tr>

View File

@ -6,20 +6,30 @@
<?php _e('Filesystem Pattern:', 'comicpress') ?>
</th>
<td>
<input style="font-size: 1.5em; width: 100%" type="text" name="cp[backend_options][filesystem][search_pattern]" value="<?php echo $pattern ?>" />
<input style="font-size: 1.5em; width: 100%" type="text" name="cp[backend_options][filesystem][search_pattern]" value="<?php echo esc_attr($filesystem_pattern) ?>" />
</td>
</tr>
<tr>
<th scope="row">
<?php _e('URL Pattern:', 'comicpress') ?>
</th>
<td>
<input style="font-size: 1.5em; width: 100%" type="text" name="cp[backend_options][filesystem][url_pattern]" value="<?php echo esc_attr($url_pattern) ?>" />
</td>
</tr>
</table>
<p>
<?php _e('<strong>URL Pattern</strong> is similar to how WordPress permalinks are constructed:', 'comicpress') ?>
<?php _e('<strong>Filesystem and URL Pattern</strong> are defined similarly to how WordPress permalinks are constructed:', 'comicpress') ?>
</p>
<ul>
<li><?php _e('<strong>%wordpress%</strong>: the local path to the WordPress installation', 'comicpress') ?></li>
<li><?php _e('<strong>%wordpress-url%</strong>: the URL to the WordPress installation', 'comicpress') ?></li>
<li><?php _e('<strong>%upload-path%</strong>: the value of the upload_path option, used by WMPU', 'comicpress') ?></li>
<li><?php _e('<strong>%type%</strong>: the image type short name', 'comicpress') ?></li>
<li><?php _e('<strong>%type-folder%</strong>: the image type folder', 'comicpress') ?></li>
<li><?php _e('<strong>%date-(pattern)%</strong>: the date of the post as run through the date() function. Ex: <em>%date-Y-m-d%</em>', 'comicpress') ?></li>
<li><?php _e('<strong>%filename%</strong>: the filename of a file found for the post', 'comicpress') ?></li>
</ul>
<input class="button-primary" type="submit" value="<?php _e('Submit Updated ComicPress Options', 'comicpress') ?>" />
</div>

View File

@ -134,14 +134,7 @@ class ComicPressTest extends PHPUnit_Framework_TestCase {
'dimensions' => '1000x'
)
)
), '1000x'),
array(array(
'image_types' => array(
'comic' => array(
'test' => 'hello'
)
)
), 'default')
), '1000x')
);
}
@ -150,7 +143,9 @@ class ComicPressTest extends PHPUnit_Framework_TestCase {
*/
function testLoad($options_array, $expected_dimensions) {
update_option('comicpress-options', $options_array);
if ($expected_dimensions == 'default') { $expected_dimensions = $this->cp->comicpress_options['image_types']['comic']['dimensions']; }
if ($expected_dimensions == 'default') {
$expected_dimensions = $this->cp->default_image_types['comic']['dimensions'];
}
$this->cp->load();
$this->assertEquals($expected_dimensions, $this->cp->comicpress_options['image_types']['comic']['dimensions']);
}

View File

@ -19,6 +19,7 @@ class ComicPressBackendFilesystemFactoryTest extends PHPUnit_Framework_TestCase
$valid_backend = new ComicPressBackendFilesystem();
$valid_backend->id = 'filesystem-1--test';
$valid_backend->files_by_type = array('comic' => 'comic-file');
$valid_backend->file_urls_by_type = array('comic' => 'comic-url');
return array(
array('blah', false),
@ -35,7 +36,14 @@ class ComicPressBackendFilesystemFactoryTest extends PHPUnit_Framework_TestCase
function testGenerateFromID($id, $is_successful) {
wp_insert_post((object)array('ID' => 1));
update_post_meta(1, 'backend_filesystem_files_by_type', array('-test' => array('comic' => 'comic-file')));
update_post_meta(
1,
'backend_filesystem_files_by_type',
array(
array('-test' => array('comic' => 'comic-file')),
array('-test' => array('comic' => 'comic-url')),
)
);
if ($is_successful) {
$return = $is_successful;
@ -58,7 +66,13 @@ class ComicPressBackendFilesystemFactoryTest extends PHPUnit_Framework_TestCase
$comicpress->comicpress_options['backend_options']['filesystem']['search_pattern'] = 'test';
$fs = $this->getMock('ComicPressBackendFilesystemFactory', array('process_search_string', 'find_matching_files', 'group_by_root', 'has_common_filename_pattern'));
$fs = $this->getMock('ComicPressBackendFilesystemFactory', array(
'process_search_string',
'find_matching_files',
'group_by_root',
'has_common_filename_pattern',
'get_urls_for_post_roots'
));
$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(array('comic')));
@ -74,6 +88,19 @@ class ComicPressBackendFilesystemFactoryTest extends PHPUnit_Framework_TestCase
'rss' => 'rss',
)
)));
$fs->expects($this->at(6))->method('get_urls_for_post_roots')->with(
array(
'root' => array(
'comic' => 'comic',
'rss' => 'rss',
)
), $post
)->will($this->returnValue(array(
'root' => array(
'comic' => 'comic-url',
'rss' => 'rss-url',
)
)));
$return = $fs->generate_from_post($post);
@ -85,10 +112,23 @@ class ComicPressBackendFilesystemFactoryTest extends PHPUnit_Framework_TestCase
), $return[0]->files_by_type);
$this->assertEquals(array(
'comic' => 'comic-url',
'rss' => 'rss-url'
), $return[0]->file_urls_by_type);
$this->assertEquals(array(
array(
'root' => array(
'comic' => 'comic',
'rss' => 'rss',
)
),
array(
'root' => array(
'comic' => 'comic-url',
'rss' => 'rss-url',
)
)
), get_post_meta(1, 'backend_filesystem_files_by_type', true));
}
@ -117,7 +157,24 @@ class ComicPressBackendFilesystemFactoryTest extends PHPUnit_Framework_TestCase
),
2
),
array('%wordpress%/%upload-path%/comic/%date-Y%/%date-Y-m-d%*.jpg', array('/wordpress/upload/comic/2009/2009-01-01*.jpg')),
array(
'%wordpress%/%upload-path%/comic/%date-Y%/%date-Y-m-d%*.jpg',
array(
'/wordpress/upload/comic/2009/2009-01-01*.jpg'
)
),
array(
'%wordpress-url%/%type%/%filename%',
array(
'http://wordpress/comic/filename.jpg'
)
),
array(
'http://cdn.domain.name/%type%/%filename%',
array(
'http://cdn.domain.name/comic/filename.jpg'
)
),
);
}
@ -142,6 +199,7 @@ class ComicPressBackendFilesystemFactoryTest extends PHPUnit_Framework_TestCase
wp_set_post_categories(2, array(4));
update_option('upload_path', 'upload');
update_option('home', 'http://wordpress/');
$fs->search_string = $string;
@ -150,7 +208,7 @@ class ComicPressBackendFilesystemFactoryTest extends PHPUnit_Framework_TestCase
'backend_options' => array('filesystem' => array('folders' => array('comic' => 'comic-folder')))
);
$this->assertEquals($expected_searches, $fs->process_search_string($posts[$post_id_to_use], 'comic'));
$this->assertEquals($expected_searches, $fs->process_search_string($posts[$post_id_to_use], 'comic', 'filename.jpg'));
}
@ -303,4 +361,36 @@ class ComicPressBackendFilesystemFactoryTest extends PHPUnit_Framework_TestCase
);
$this->assertEquals($expected_result, $this->fa->_replace_type_folder(null, $type));
}
function testGetURLPattern() {
$comicpress = ComicPress::get_instance(true);
$comicpress->comicpress_options = array(
'backend_options' => array('filesystem' => array('url_pattern' => 'pattern'))
);
$this->assertEquals('pattern', $this->fa->_get_url_pattern());
}
function testGetURLsForPostRoots() {
$roots = array(
'one' => array(
'comic' => '/this/file1.jpg'
),
'two' => array(
'rss' => '/this/file2.jpg'
)
);
$fa = $this->getMock('ComicPressBackendFilesystemFactory', array('_get_url_pattern'));
$fa->expects($this->any())->method('_get_url_pattern')->will($this->returnValue('test/%type%/%date-Y%/%filename%'));
$this->assertEquals(array(
'one' => array(
'comic' => 'test/comic/2010/file1.jpg'
),
'two' => array(
'rss' => 'test/rss/2010/file2.jpg'
)
), $fa->get_urls_for_post_roots($roots, (object)array('post_date' => '2010-01-01')));
}
}