From 08694eaf85c6d75eb64c4722883549d63f4ced68 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 14 Jan 2010 20:54:50 -0500 Subject: [PATCH] performance enhancements for category parent -> child mapping --- classes/ComicPressDBInterface.inc | 17 ++++++++++-- classes/ComicPressStoryline.inc | 44 ++++++++++++++++++++++++++----- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/classes/ComicPressDBInterface.inc b/classes/ComicPressDBInterface.inc index 71b07d0..b47b3c5 100644 --- a/classes/ComicPressDBInterface.inc +++ b/classes/ComicPressDBInterface.inc @@ -106,6 +106,19 @@ class ComicPressDBInterface { function get_next_post($categories = null, $override_post = null) { return $this->get_adjacent_post($categories, true, $override_post); } // @codeCoverageIgnoreEnd -} -?> + function get_parent_child_category_ids() { + global $wpdb; + + $parent_child_categories = array(); + + $result = $wpdb->get_results("SELECT term_id, parent FROM $wpdb->term_taxonomy", ARRAY_A); + if (!empty($result)) { + foreach ($result as $row) { + $parent_child_categories[$row['term_id']] = $row['parent']; + } + } + + return $parent_child_categories; + } +} diff --git a/classes/ComicPressStoryline.inc b/classes/ComicPressStoryline.inc index ca7c608..7a3c1b9 100644 --- a/classes/ComicPressStoryline.inc +++ b/classes/ComicPressStoryline.inc @@ -247,19 +247,36 @@ class ComicPressStoryline { return $this->_merge_simple_storyline($simple_storyline); } + // @codeCoverageIgnoreStart + function get_comicpress_dbi() { + return ComicPressDBInterface::get_instance(); + } + // @codeCoverageIgnoreEnd + /** * Get a simple structure. */ function get_category_simple_structure($parent = null) { - $structure = array(); - foreach (get_all_category_ids() as $category_id) { - $category = get_category($category_id); - if (!isset($structure[$category->parent])) { - $structure[$category->parent] = array(); - } - $structure[$category->parent][$category_id] = true; + $cache_key = $this->generate_cache_key('storyline-structure', $parent); + $result = wp_cache_get($cache_key, 'comicpress'); + if (is_array($result)) { + return $result; } + + $structure = array(); + $dbi = $this->get_comicpress_dbi(); + + $result = $dbi->get_parent_child_category_ids(); + + foreach ($result as $cat_id => $cat_parent) { + if (!isset($structure[$cat_parent])) { + $structure[$cat_parent] = array(); + } + $structure[$cat_parent][$cat_id] = true; + } + $structure = $this->_merge_simple_storyline($structure); + if (!empty($parent)) { if (isset($structure[0])) { foreach ($structure[0] as $key => $children) { @@ -267,9 +284,19 @@ class ComicPressStoryline { } } } + + wp_cache_set($cache_key, $structure, 'comicpress'); + return $structure; } + function generate_cache_key($key_name, $param) { + if (!empty($param)) { + $key_name = "${key_name}-${param}"; + } + return $key_name; + } + /** * Get a flattened category node list. */ @@ -339,6 +366,9 @@ class ComicPressStoryline { $valid_ids = get_all_category_ids(); + if (!isset($comicpress->comicpress_options['category_groupings'])) { $comicpress->comicpress_options['category_groupings'] = array(); } + if (!is_array($comicpress->comicpress_options['category_groupings'])) { $comicpress->comicpress_options['category_groupings'] = array(); } + foreach ($comicpress->comicpress_options['category_groupings'] as $group_id => $category_ids) { $comicpress->comicpress_options['category_groupings'][$group_id] = array_intersect($category_ids, $valid_ids); if (empty($comicpress->comicpress_options['category_groupings'][$group_id])) {