more restrictions

This commit is contained in:
John Bintz 2009-11-07 20:56:49 -05:00
parent 57ef1784d6
commit 4d9392470c
7 changed files with 341 additions and 144 deletions

View File

@ -16,15 +16,6 @@ class ComicPress {
'storyline_order' => ''
);
var $additional_stylesheets = array();
var $additional_javascripts = array();
var $comic_post_attachments_cache = array();
var $category_tree = array();
var $partial_paths = array();
var $layouts = null;
function &get_instance() {
static $instance;

View File

@ -1,8 +1,6 @@
<?php
class ComicPressDBInterface {
var $_non_comic_categories, $_all_categories;
function ComicPressDBInterface() {}
function get_instance() {
@ -14,27 +12,22 @@ class ComicPressDBInterface {
function _get_categories() { return get_categories("hide_empty=0"); }
/**
* Set the comic categories for the current run of ComicPress.
*/
function set_comic_categories($categories) {
$this->_all_categories = get_all_category_ids();
$this->_non_comic_categories = array_values(array_diff($this->_all_categories, $categories));
}
function _get_categories_to_exclude($category = null) {
return (is_null($category)) ? $this->_non_comic_categories : array_values(array_diff($this->_all_categories, array($category)));
}
/**
* Find the terminal post in a specific category.
*/
function get_terminal_post_in_category($category_id, $first = true) {
function get_terminal_post($first = true, $include_categories = null) {
$this->_prepare_wp_query();
$sort_order = $first ? "asc" : "desc";
$terminal_comic_query = new WP_Query();
$terminal_comic_query->query("showposts=1&order=${sort_order}&cat=${category_id}&status=publish");
$query_parameters = array(
'showposts' => 1,
'order' => $first ? "asc" : "desc",
'status' => 'publish'
);
if (is_array($include_categories)) {
$query_parameters['category_in'] = $include_categories;
}
$terminal_comic_query->query($query_parameters);
$post = false;
if ($terminal_comic_query->have_posts()) {
$post = reset($terminal_comic_query->posts);
@ -47,15 +40,15 @@ class ComicPressDBInterface {
/**
* Get the first comic in a category.
*/
function get_first_comic($category_id) {
return $this->get_terminal_post_in_category($category_id);
function get_first_comic($include_categories = null) {
return $this->get_terminal_post(true, $include_categories);
}
/**
* Get the last comic in a category.
*/
function get_last_comic($category_id) {
return $this->get_terminal_post_in_category($category_id, false);
function get_last_comic($include_categories = null) {
return $this->get_terminal_post(false, $include_categories);
}
/**

View File

@ -7,8 +7,9 @@ class ComicPressStoryline {
var $_structure;
var $_category_search;
function read_from_options() {
function &read_from_options() {
$this->create_structure($this->get_flattened_storyline());
return $this;
}
/**
@ -382,6 +383,15 @@ class ComicPressStoryline {
}
function _find_children($parent) {
if (!is_numeric($parent)) {
foreach (get_all_category_ids() as $id) {
$category = get_category($id);
if ($category->slug == $parent) {
$parent = $id; break;
}
}
}
if (is_numeric($parent)) {
$children = array($parent);
do {
$found_children = false;
@ -399,19 +409,29 @@ class ComicPressStoryline {
return $children;
}
return false;
}
function &include_children($parent = null) {
$this->_category_search = array_unique(array_merge($this->_category_search, $this->_find_children($parent)));
function &_include($method, $parameter) {
$this->_category_search = array_unique(array_merge($this->_category_search, $this->{$method}($parameter)));
sort($this->_category_search);
return $this;
}
function &exclude_children($parent = null) {
$this->_category_search = array_diff($this->_category_search, $this->_find_children($parent));
function &_exclude($method, $parameter) {
$this->_category_search = array_diff($this->_category_search, $this->{$method}($parameter));
sort($this->_category_search);
return $this;
}
function &include_children($parent = null) {
return $this->_include('_find_children', $parent);
}
function &exclude_children($parent = null) {
return $this->_exclude('_find_children', $parent);
}
function _find_level_or_above($level = null) {
$found = array();
foreach ($this->_structure as $category_id => $info) {
@ -421,15 +441,42 @@ class ComicPressStoryline {
}
function &include_level_or_above($level = null) {
$this->_category_search = array_unique(array_merge($this->_category_search, $this->_find_level_or_above($level)));
sort($this->_category_search);
return $this;
return $this->_include('_find_level_or_above', $level);
}
function &exclude_level_or_above($level = null) {
$this->_category_search = array_diff($this->_category_search, $this->_find_level_or_above($level));
sort($this->_category_search);
return $this;
return $this->_exclude('_find_level_or_above', $level);
}
function _find_only($id = null) {
if (isset($this->_structure[$id])) {
return array($id);
}
return array();
}
function &include_only($id = null) {
return $this->_include('_find_only', $id);
}
function &exclude_only($id = null) {
return $this->_exclude('_find_only', $id);
}
function _find_level($level = null) {
$found = array();
foreach ($this->_structure as $category_id => $info) {
if ($info['level'] == $level) { $found[] = $category_id; }
}
return $found;
}
function &include_level($id = null) {
return $this->_include('_find_level', $id);
}
function &exclude_level($id = null) {
return $this->_exclude('_find_level', $id);
}
function end_search() {
@ -437,6 +484,39 @@ class ComicPressStoryline {
$this->_category_search = array();
return $result;
}
function build_from_restrictions($restrictions = null) {
$this->read_from_options()->exclude_all();
$include_all = true;
if (is_array($restrictions)) {
if (!empty($restrictions)) {
$include_all = false;
}
}
if (!$include_all) {
foreach ($restrictions as $type => $list) {
foreach ((array)$list as $restriction) {
switch ($type) {
case 'child_of':
$this->include_children($restriction);
break;
case 'only':
$this->include_only($restriction);
break;
case 'level':
$this->include_level($restriction);
break;
}
}
}
} else {
$this->include_all();
}
return $this->end_search();
}
}
?>

View File

@ -42,12 +42,80 @@ function F($name, $path, $override_post = null) {
return ComicPress::get_instance()->find_file($name, $path, $comic_post->find_parents());
}
function FinishComicPress() {
/**
* Finish rendering this template and shove the output into application.php.
*/
function finish_comicpress() {
$content = ob_get_clean();
include(F('application.php', ''));
}
/**
* Protect global $post and $wp_query.
*/
function protect_query() {
global $post, $wp_query, $__post, $__wp_query;
$__post = $post;
$__wp_query = $wp_query;
}
/**
* Restore global $post and $wp_query.
*/
function unprotect_query() {
global $post, $wp_query, $__post, $__wp_query;
$post = $__post;
$wp_query = $__wp_query;
$__post = $__wp_query = null;
}
function retrieve_storyline_post($which, $restrictions = null, $override_post = null) {
global $post;
$storyline = new ComicPressStoryline();
$storyline->read_from_options()->exclude_all();
if (!is_null($restrictions)) {
if (is_array($restrictions)) {
foreach ($restrictions as $type => $list) {
switch ($type) {
case 'child_of':
foreach ($list as $restriction) { $storyline->include_children($restriction); }
break;
}
}
}
} else {
$storyline->include_all();
}
$categories = $storyline->end_search();
var_dump($categories);
$dbi = ComicPressDBInterface::get_instance();
$new_post = false;
switch ($which) {
case 'first':
case 'last':
case 'next':
case 'previous':
$method = "get_${which}_post";
if (method_exists($dbi, $method)) {
$new_post = $dbi->{$method}($categories);
}
break;
}
return $new_post;
}
/**
* Display the list of Storyline categories.
*/

View File

@ -1,5 +1,7 @@
<?php
echo "hello";
$current_post = retrieve_storyline_post('last', array('child_of' => array('amoc', 'dawns-dictionary-drama')));
FinishComicPress();
var_dump($current_post);
finish_comicpress();
?>

View File

@ -14,35 +14,6 @@ class ComicPressDBInterfaceTest extends PHPUnit_Framework_TestCase {
$b = ComicPressDBInterface::get_instance();
$this->assertEquals("test", $b->test);
}
function testSetComicCategories() {
$dbi = ComicPressDBInterface::get_instance();
for ($i = 1; $i <= 4; ++$i) { add_category($i, (object)array()); }
$dbi->set_comic_categories(array(2,3));
$this->assertEquals(array(1,2,3,4), $dbi->_all_categories);
$this->assertEquals(array(1,4), $dbi->_non_comic_categories);
}
function providerTestGetCategoriesToExclude() {
return array(
array(null, array(1 ,4)),
array(2, array(1, 3, 4)),
);
}
/**
* @dataProvider providerTestGetCategoriesToExclude
*/
function testGetCategoriesToExclude($category, $expected_results) {
$dbi = ComicPressDBInterface::get_instance();
$dbi->_all_categories = array(1,2,3,4);
$dbi->_non_comic_categories = array(1,4);
$this->assertEquals($expected_results, $dbi->_get_categories_to_exclude($category));
}
}
?>

View File

@ -346,7 +346,16 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(array(), $this->css->_category_search);
}
function testFindChildren() {
function providerTestFindChildren() {
return array(
array(2), array('test')
);
}
/**
* @dataProvider providerTestFindChildren
*/
function testFindChildren($search) {
$this->css->_structure = array(
'1' => array('next' => 2),
'2' => array('parent' => 1, 'previous' => 1, 'next' => 3),
@ -354,7 +363,9 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
'4' => array('parent' => 2, 'previous' => 3)
);
$this->assertEquals(array(2,3,4), $this->css->_find_children(2));
add_category(2, (object)array('slug' => 'test'));
$this->assertEquals(array(2,3,4), $this->css->_find_children($search));
}
function testIncludeChildren() {
@ -413,6 +424,87 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(array(1,2,3), $this->css->end_search());
$this->assertEquals(array(), $this->css->_category_search);
}
function providerTestFindOnly() {
return array(
array(1, array(1)),
array(5, array()),
);
}
/**
* @dataProvider providerTestFindOnly
*/
function testFindOnly($id, $expected_return) {
$this->css->_structure = array(
'1' => array('next' => 2, 'level' => 1),
'2' => array('parent' => 1, 'previous' => 1, 'next' => 3, 'level' => 2),
'3' => array('parent' => 2, 'next' => 4, 'previous' => 2, 'level' => 3),
'4' => array('parent' => 2, 'previous' => 3, 'level' => 3)
);
$this->assertEquals($expected_return, $this->css->_find_only($id));
}
function providerTestFindLevel() {
return array(
array(1, array(1)),
array(2, array(2)),
array(3, array(3,4))
);
}
/**
* @dataProvider providerTestFindLevel
*/
function testFindLevel($id, $expected_return) {
$this->css->_structure = array(
'1' => array('next' => 2, 'level' => 1),
'2' => array('parent' => 1, 'previous' => 1, 'next' => 3, 'level' => 2),
'3' => array('parent' => 2, 'next' => 4, 'previous' => 2, 'level' => 3),
'4' => array('parent' => 2, 'previous' => 3, 'level' => 3)
);
$this->assertEquals($expected_return, $this->css->_find_level($id));
}
function providerTestBuildFromRestrictions() {
return array(
array(
null,
array(1,2,3,4,5,6,7)
),
array(
array(),
array(1,2,3,4,5,6,7)
),
array(
array('child_of' => 1),
array(1,2,3)
),
array(
array('only' => 1),
array(1)
),
array(
array('child_of' => 1, 'only' => 7),
array(1,2,3,7)
),
array(
array('level' => 1),
array(1,4,7)
)
);
}
/**
* @dataProvider providerTestBuildFromRestrictions
*/
function testBuildFromRestrictions($restrictions, $expected_categories) {
$this->css->set_flattened_storyline('0/1,0/1/2,0/1/3,0/4,0/4/5,0/4/6,0/7');
$this->assertEquals($expected_categories, $this->css->build_from_restrictions($restrictions));
}
}
?>