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,34 +7,36 @@ require_once(dirname(__FILE__) . '/ComicPressComicPost.inc');
*/ */
class ComicPress { class ComicPress {
var $comicpress_options = array( var $comicpress_options = array(
'image_types' => array( 'image_types' => array(),
'comic' => array(
'default' => true,
'name' => 'Comic',
'dimensions' => '760x'
),
'rss' => array(
'default' => false,
'name' => 'RSS',
'dimensions' => '350x'
),
'archive' => array(
'default' => false,
'name' => 'Archive',
'dimensions' => '125x'
),
'mini' => array(
'default' => false,
'name' => 'Minithumb',
'dimensions' => '100x'
),
),
'helpers' => array(), 'helpers' => array(),
'storyline_order' => '', 'storyline_order' => '',
'enabled_backends' => null, 'enabled_backends' => null,
'category_groupings' => array() 'category_groupings' => array()
); );
var $default_image_types = array(
'comic' => array(
'default' => true,
'name' => 'Comic',
'dimensions' => '760x'
),
'rss' => array(
'default' => false,
'name' => 'RSS',
'dimensions' => '350x'
),
'archive' => array(
'default' => false,
'name' => 'Archive',
'dimensions' => '125x'
),
'mini' => array(
'default' => false,
'name' => 'Minithumb',
'dimensions' => '100x'
)
);
var $backends = array(); var $backends = array();
function &get_instance($force = false) { function &get_instance($force = false) {
@ -56,8 +58,10 @@ class ComicPress {
*/ */
function load() { function load() {
$result = get_option('comicpress-options'); $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); $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 {} class ComicPressException extends Exception {}

View File

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

View File

@ -17,12 +17,8 @@ class ComicPressBackendAttachment extends ComicPressBackend {
$comicpress = ComicPress::get_instance(); $comicpress = ComicPress::get_instance();
$dims = array(); $dims = array();
if (isset($comicpress->comicpress_options['image_types'])) { if (isset($comicpress->comicpress_options['image_types'][$size]['dimensions'])) {
if (isset($comicpress->comicpress_options['image_types'][$size])) { $dims = array_combine(array('width', 'height'), explode("x", $comicpress->comicpress_options['image_types'][$size]['dimensions']));
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; return $dims;

View File

@ -4,7 +4,31 @@ require_once(dirname(__file__) . '/../ComicPressBackend.inc');
class ComicPressBackendFilesystem extends ComicPressBackend { class ComicPressBackendFilesystem extends ComicPressBackend {
var $search_string = ''; 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 { class ComicPressBackendFilesystemFactory {
@ -17,11 +41,17 @@ class ComicPressBackendFilesystemFactory {
list($all, $post_id, $root) = $matches; list($all, $post_id, $root) = $matches;
if (($result = get_post_meta($post_id, 'backend_filesystem_files_by_type', true)) !== false) { if (($result = get_post_meta($post_id, 'backend_filesystem_files_by_type', true)) !== false) {
if (isset($result[$root])) { if (is_array($result)) {
$return = new ComicPressBackendFilesystem(); if (isset($result[0][$root])) {
$return->id = $id; $return = new ComicPressBackendFilesystem();
$return->files_by_type = $result[$root]; $return->id = $id;
return $return;
foreach (array('files_by_type', 'file_urls_by_type') as $index => $name) {
$return->{$name} = $result[$index][$root];
}
return $return;
}
} }
} }
} }
@ -29,12 +59,20 @@ class ComicPressBackendFilesystemFactory {
} }
function _get_search_pattern() { 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(); $comicpress = ComicPress::get_instance();
if (isset( 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 ''; return '';
@ -44,7 +82,7 @@ class ComicPressBackendFilesystemFactory {
$return = array(); $return = array();
$comicpress = ComicPress::get_instance(); $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'])) { if (isset($comicpress->comicpress_options['image_types'])) {
$files = array(); $files = array();
@ -62,11 +100,15 @@ class ComicPressBackendFilesystemFactory {
if (($filename_pattern = $this->has_common_filename_pattern($all_patterns)) !== false) { if (($filename_pattern = $this->has_common_filename_pattern($all_patterns)) !== false) {
if (!empty($files)) { if (!empty($files)) {
$grouped_by_root = $this->group_by_root($filename_pattern, $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) { foreach ($grouped_by_root as $root => $files_for_root) {
$fs = new ComicPressBackendFilesystem(); $fs = new ComicPressBackendFilesystem();
$fs->id = 'filesystem-' . $post->ID . '-' . $root; $fs->id = 'filesystem-' . $post->ID . '-' . $root;
$fs->files_by_type = $files_for_root; $fs->files_by_type = $files_for_root;
$fs->file_urls_by_type = $urls_by_root[$root];
$return[] = $fs; $return[] = $fs;
} }
} }
@ -76,16 +118,15 @@ class ComicPressBackendFilesystemFactory {
return $return; return $return;
} }
function process_search_string($post, $type) { function process_search_string($post, $type, $filename = null) {
$this->_searches = array($this->search_string); $this->_searches = array($this->search_string);
$this->_filename = $filename;
do { do {
$any_found = false; $any_found = false;
for ($i = 0; $i < count($this->_searches); ++$i) { for ($i = 0; $i < count($this->_searches); ++$i) {
$search = $this->_searches[$i]; $search = $this->_searches[$i];
if (preg_match('#%([a-z0-9\-]+)%#i', $search, $matches) > 0) { if (preg_match('#%([a-z0-9-]+?)%#i', $search, $matches) > 0) {
$any_found = true;
$found = false; $found = false;
$parts = explode('-', $matches[1]); $parts = explode('-', $matches[1]);
foreach (array( foreach (array(
@ -93,6 +134,7 @@ class ComicPressBackendFilesystemFactory {
'_replace_' . strtolower($parts[0]) => implode('-', array_slice($parts, 1)) '_replace_' . strtolower($parts[0]) => implode('-', array_slice($parts, 1))
) as $method => $additional) { ) as $method => $additional) {
if (method_exists($this, $method)) { if (method_exists($this, $method)) {
$any_found = true;
$found = true; $found = true;
$result = $this->{$method}($post, $type, $additional); $result = $this->{$method}($post, $type, $additional);
if ($result !== false) { if ($result !== false) {
@ -119,9 +161,13 @@ class ComicPressBackendFilesystemFactory {
} }
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
function _replace_wordpress($post, $type) { return ABSPATH; } function _replace_wordpress($post, $type) { return untrailingslashit(ABSPATH); }
// @codeCoverageIgnoreEnd // @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_type($post, $type) { return $type; }
function _replace_upload_path($post, $type) { return get_option('upload_path'); } 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])) { if (isset($comicpress->comicpress_options['backend_options']['filesystem']['folders'][$type])) {
return $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) { function _replace_date($post, $type, $additional) {
@ -239,12 +285,29 @@ class ComicPressBackendFilesystemFactory {
return $roots; 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 // @codeCoverageIgnoreStart
class ComicPressBackendFilesystemAdmin { class ComicPressBackendFilesystemAdmin {
function options_admin() { 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'); include('partials/backend-filesystem/options-admin.inc');
} }
@ -274,7 +337,7 @@ class ComicPressBackendFilesystemAdmin {
$comicpress->comicpress_options['backend_options']['filesystem'] = array(); $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])) { if (is_array($info[$valid_field])) {
$comicpress->comicpress_options['backend_options']['filesystem'][$valid_field] = array(); $comicpress->comicpress_options['backend_options']['filesystem'][$valid_field] = array();
foreach ($info[$valid_field] as $field => $value) { foreach ($info[$valid_field] as $field => $value) {

View File

@ -3,5 +3,23 @@
<td> <td>
<input type="text" name="cp[backend_options][filesystem][folders][<?php echo esc_attr($type) ?>]" value="<?php echo esc_attr($path) ?>" /> <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> <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> </td>
</tr> </tr>

View File

@ -6,20 +6,30 @@
<?php _e('Filesystem Pattern:', 'comicpress') ?> <?php _e('Filesystem Pattern:', 'comicpress') ?>
</th> </th>
<td> <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> </td>
</tr> </tr>
</table> </table>
<p> <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> </p>
<ul> <ul>
<li><?php _e('<strong>%wordpress%</strong>: the local path to the WordPress installation', 'comicpress') ?></li> <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>%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%</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>%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>%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> </ul>
<input class="button-primary" type="submit" value="<?php _e('Submit Updated ComicPress Options', 'comicpress') ?>" /> <input class="button-primary" type="submit" value="<?php _e('Submit Updated ComicPress Options', 'comicpress') ?>" />
</div> </div>

View File

@ -134,14 +134,7 @@ class ComicPressTest extends PHPUnit_Framework_TestCase {
'dimensions' => '1000x' 'dimensions' => '1000x'
) )
) )
), '1000x'), ), '1000x')
array(array(
'image_types' => array(
'comic' => array(
'test' => 'hello'
)
)
), 'default')
); );
} }
@ -150,7 +143,9 @@ class ComicPressTest extends PHPUnit_Framework_TestCase {
*/ */
function testLoad($options_array, $expected_dimensions) { function testLoad($options_array, $expected_dimensions) {
update_option('comicpress-options', $options_array); 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->cp->load();
$this->assertEquals($expected_dimensions, $this->cp->comicpress_options['image_types']['comic']['dimensions']); $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 = new ComicPressBackendFilesystem();
$valid_backend->id = 'filesystem-1--test'; $valid_backend->id = 'filesystem-1--test';
$valid_backend->files_by_type = array('comic' => 'comic-file'); $valid_backend->files_by_type = array('comic' => 'comic-file');
$valid_backend->file_urls_by_type = array('comic' => 'comic-url');
return array( return array(
array('blah', false), array('blah', false),
@ -35,7 +36,14 @@ class ComicPressBackendFilesystemFactoryTest extends PHPUnit_Framework_TestCase
function testGenerateFromID($id, $is_successful) { function testGenerateFromID($id, $is_successful) {
wp_insert_post((object)array('ID' => 1)); 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) { if ($is_successful) {
$return = $is_successful; $return = $is_successful;
@ -58,7 +66,13 @@ class ComicPressBackendFilesystemFactoryTest extends PHPUnit_Framework_TestCase
$comicpress->comicpress_options['backend_options']['filesystem']['search_pattern'] = 'test'; $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(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'))); $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', '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); $return = $fs->generate_from_post($post);
@ -85,9 +112,22 @@ class ComicPressBackendFilesystemFactoryTest extends PHPUnit_Framework_TestCase
), $return[0]->files_by_type); ), $return[0]->files_by_type);
$this->assertEquals(array( $this->assertEquals(array(
'root' => array( 'comic' => 'comic-url',
'comic' => 'comic', 'rss' => 'rss-url'
'rss' => 'rss', ), $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)); ), get_post_meta(1, 'backend_filesystem_files_by_type', true));
} }
@ -117,7 +157,24 @@ class ComicPressBackendFilesystemFactoryTest extends PHPUnit_Framework_TestCase
), ),
2 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)); wp_set_post_categories(2, array(4));
update_option('upload_path', 'upload'); update_option('upload_path', 'upload');
update_option('home', 'http://wordpress/');
$fs->search_string = $string; $fs->search_string = $string;
@ -150,7 +208,7 @@ class ComicPressBackendFilesystemFactoryTest extends PHPUnit_Framework_TestCase
'backend_options' => array('filesystem' => array('folders' => array('comic' => 'comic-folder'))) '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)); $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')));
}
} }