From 42a5bd20a550a9edb5ec84625037b9d85889ff63 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Fri, 26 Jun 2009 07:51:29 -0700 Subject: 1) Move the generation of script tags to gallery_theme::head and gallery_theme::admin_head. This allows us to potentially manage the scripts like we do in g2 (single file and compressed) 2) Change Theme_View::_call to always call the gallery_theme::$function first. --- modules/gallery/helpers/gallery_theme.php | 20 ++++++++++++++++++++ modules/gallery/libraries/Theme_View.php | 25 +++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) (limited to 'modules') diff --git a/modules/gallery/helpers/gallery_theme.php b/modules/gallery/helpers/gallery_theme.php index a96c8f5b..24f4129b 100644 --- a/modules/gallery/helpers/gallery_theme.php +++ b/modules/gallery/helpers/gallery_theme.php @@ -21,6 +21,18 @@ class gallery_theme_Core { static function head($theme) { $session = Session::instance(); $buf = ""; + $buf .= html::script("lib/jquery.js"); + $buf .= html::script("lib/jquery.form.js"); + $buf .= html::script("lib/jquery-ui.js"); + $buf .= html::script("lib/gallery.common.js"); + $buf .= html::script("lib/gallery.dialog.js"); + $buf .= html::script("lib/gallery.form.js"); + $buf .= html::script("lib/superfish/js/superfish.js"); + if ($theme->page_type == 'photo') { + $buf .= html::script("lib/jquery.scrollTo.js"); + $buf .= html::script("lib/jquery.localscroll.js"); + } + $buf .= html::script($theme->url("js/ui.init.js", false, true)); if ($session->get("debug")) { $buf .= ""; @@ -79,6 +91,14 @@ class gallery_theme_Core { static function admin_head($theme) { $session = Session::instance(); $buf = ""; + $buf .= html::script("lib/jquery.js"); + $buf .= html::script("lib/jquery.form.js"); + $buf .= html::script("lib/jquery-ui.js"); + $buf .= html::script("lib/gallery.common.js"); + $buf .= html::script("lib/gallery.dialog.js"); + $buf .= html::script("lib/superfish/js/superfish.js"); + $buf .= html::script($theme->url("js/jquery.dropshadow.js", false, true)); + $buf .= html::script($theme->url("js/ui.init.js", false, true)); if ($session->get("debug")) { $buf .= ""; diff --git a/modules/gallery/libraries/Theme_View.php b/modules/gallery/libraries/Theme_View.php index 7b2ca840..5235fabb 100644 --- a/modules/gallery/libraries/Theme_View.php +++ b/modules/gallery/libraries/Theme_View.php @@ -68,9 +68,9 @@ class Theme_View_Core extends View { return module::get_var("gallery", "thumb_size", 200) / 200; } - public function url($path, $absolute_url=false) { + public function url($path, $absolute_url=false, $no_root=false) { $arg = "themes/{$this->theme_name}/$path"; - return $absolute_url ? url::abs_file($arg) : url::file($arg); + return $absolute_url ? url::abs_file($arg) : $no_root ? $arg : url::file($arg); } public function item() { @@ -192,7 +192,28 @@ class Theme_View_Core extends View { case "thumb_info": case "thumb_top": $blocks = array(); + if (method_exists("gallery_theme", $function)) { + switch (count($args)) { + case 0: + $blocks[] = gallery_theme::$function($this); + break; + case 1: + $blocks[] = gallery_theme::$function($this, $args[0]); + break; + case 2: + $blocks[] = gallery_theme::$function($this, $args[0], $args[1]); + break; + default: + $blocks[] = call_user_func_array( + array("gallery_theme", $function), + array_merge(array($this), $args)); + } + + } foreach (module::active() as $module) { + if ($module->name == "gallery") { + continue; + } $helper_class = "{$module->name}_theme"; if (method_exists($helper_class, $function)) { $blocks[] = call_user_func_array( -- cgit v1.2.3 From f4778699fc52c045f60d72e20bebc000d9f6bda8 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Fri, 26 Jun 2009 08:19:06 -0700 Subject: Change the movie controller to set the page type to "movie". Ticket #467 --- modules/gallery/controllers/movies.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'modules') diff --git a/modules/gallery/controllers/movies.php b/modules/gallery/controllers/movies.php index 86b0f177..d8cca825 100644 --- a/modules/gallery/controllers/movies.php +++ b/modules/gallery/controllers/movies.php @@ -44,7 +44,7 @@ class Movies_Controller extends Items_Controller { ->where("id <=", $photo->id) ->count_all(); - $template = new Theme_View("page.html", "photo"); + $template = new Theme_View("page.html", "movie"); $template->set_global("item", $photo); $template->set_global("children", array()); $template->set_global("children_count", $photo->children_count()); -- cgit v1.2.3 From 84e98e830c4967c277dfcb5bb6d18c0ec3313355 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Fri, 26 Jun 2009 08:25:24 -0700 Subject: Move the inclusion of photo and movie specific javascript into gallery_theme --- modules/gallery/helpers/gallery_theme.php | 4 ++++ themes/default/views/movie.html.php | 1 - themes/default/views/photo.html.php | 1 - 3 files changed, 4 insertions(+), 2 deletions(-) (limited to 'modules') diff --git a/modules/gallery/helpers/gallery_theme.php b/modules/gallery/helpers/gallery_theme.php index 24f4129b..0679087e 100644 --- a/modules/gallery/helpers/gallery_theme.php +++ b/modules/gallery/helpers/gallery_theme.php @@ -31,6 +31,10 @@ class gallery_theme_Core { if ($theme->page_type == 'photo') { $buf .= html::script("lib/jquery.scrollTo.js"); $buf .= html::script("lib/jquery.localscroll.js"); + $buf .= html::script("lib/gallery.show_full_size.js"); + } + if ($theme->page_type == 'movie') { + $buf .= html::script("lib/flowplayer.js"); } $buf .= html::script($theme->url("js/ui.init.js", false, true)); if ($session->get("debug")) { diff --git a/themes/default/views/movie.html.php b/themes/default/views/movie.html.php index e8559697..134e3571 100644 --- a/themes/default/views/movie.html.php +++ b/themes/default/views/movie.html.php @@ -1,5 +1,4 @@ -
photo_top() ?> diff --git a/themes/default/views/photo.html.php b/themes/default/views/photo.html.php index 1c3b81a7..4765a4e3 100644 --- a/themes/default/views/photo.html.php +++ b/themes/default/views/photo.html.php @@ -2,7 +2,6 @@ item())): ?> - \n"; + $theme->script("modules/comment/js/comment.js"); + return ""; } static function photo_bottom($theme) { diff --git a/modules/gallery/helpers/gallery_theme.php b/modules/gallery/helpers/gallery_theme.php index 0679087e..b6b24b27 100644 --- a/modules/gallery/helpers/gallery_theme.php +++ b/modules/gallery/helpers/gallery_theme.php @@ -21,22 +21,22 @@ class gallery_theme_Core { static function head($theme) { $session = Session::instance(); $buf = ""; - $buf .= html::script("lib/jquery.js"); - $buf .= html::script("lib/jquery.form.js"); - $buf .= html::script("lib/jquery-ui.js"); - $buf .= html::script("lib/gallery.common.js"); - $buf .= html::script("lib/gallery.dialog.js"); - $buf .= html::script("lib/gallery.form.js"); - $buf .= html::script("lib/superfish/js/superfish.js"); + $theme->script("lib/jquery.js"); + $theme->script("lib/jquery.form.js"); + $theme->script("lib/jquery-ui.js"); + $theme->script("lib/gallery.common.js"); + $theme->script("lib/gallery.dialog.js"); + $theme->script("lib/gallery.form.js"); + $theme->script("lib/superfish/js/superfish.js"); if ($theme->page_type == 'photo') { - $buf .= html::script("lib/jquery.scrollTo.js"); - $buf .= html::script("lib/jquery.localscroll.js"); - $buf .= html::script("lib/gallery.show_full_size.js"); + $theme->script("lib/jquery.scrollTo.js"); + $theme->script("lib/jquery.localscroll.js"); + $theme->script("lib/gallery.show_full_size.js"); } if ($theme->page_type == 'movie') { - $buf .= html::script("lib/flowplayer.js"); + $theme->script("lib/flowplayer.js"); } - $buf .= html::script($theme->url("js/ui.init.js", false, true)); + $theme->script($theme->url("js/ui.init.js", false, true)); if ($session->get("debug")) { $buf .= ""; @@ -45,7 +45,7 @@ class gallery_theme_Core { && access::can("edit", $theme->item())) { $buf .= ""; - $buf .= html::script("modules/gallery/js/quick.js"); + $theme->script("modules/gallery/js/quick.js"); } if (module::is_active("rss")) { @@ -59,8 +59,8 @@ class gallery_theme_Core { if ($session->get("l10n_mode", false)) { $buf .= ""; - $buf .= html::script("lib/jquery.cookie.js"); - $buf .= html::script("modules/gallery/js/l10n_client.js"); + $theme->script("lib/jquery.cookie.js"); + $theme->script("modules/gallery/js/l10n_client.js"); } return $buf; @@ -95,14 +95,14 @@ class gallery_theme_Core { static function admin_head($theme) { $session = Session::instance(); $buf = ""; - $buf .= html::script("lib/jquery.js"); - $buf .= html::script("lib/jquery.form.js"); - $buf .= html::script("lib/jquery-ui.js"); - $buf .= html::script("lib/gallery.common.js"); - $buf .= html::script("lib/gallery.dialog.js"); - $buf .= html::script("lib/superfish/js/superfish.js"); - $buf .= html::script($theme->url("js/jquery.dropshadow.js", false, true)); - $buf .= html::script($theme->url("js/ui.init.js", false, true)); + $theme->script("lib/jquery.js"); + $theme->script("lib/jquery.form.js"); + $theme->script("lib/jquery-ui.js"); + $theme->script("lib/gallery.common.js"); + $theme->script("lib/gallery.dialog.js"); + $theme->script("lib/superfish/js/superfish.js"); + $theme->script($theme->url("js/jquery.dropshadow.js", false, true)); + $theme->script($theme->url("js/ui.init.js", false, true)); if ($session->get("debug")) { $buf .= ""; @@ -111,8 +111,8 @@ class gallery_theme_Core { if ($session->get("l10n_mode", false)) { $buf .= ""; - $buf .= html::script("lib/jquery.cookie.js"); - $buf .= html::script("modules/gallery/js/l10n_client.js"); + $theme->script("lib/jquery.cookie.js"); + $theme->script("modules/gallery/js/l10n_client.js"); } return $buf; diff --git a/modules/gallery/libraries/Theme_View.php b/modules/gallery/libraries/Theme_View.php index 5235fabb..7696f3b0 100644 --- a/modules/gallery/libraries/Theme_View.php +++ b/modules/gallery/libraries/Theme_View.php @@ -19,6 +19,7 @@ */ class Theme_View_Core extends View { private $theme_name = null; + private $scripts = array(); /** * Attempts to load a view and pre-load view data. @@ -163,6 +164,19 @@ class Theme_View_Core extends View { return message::get(); } + public function script($file) { + $this->scripts[$file] = 1; + } + + private function _combine_script() { + $links = array(); + Kohana::log("error", Kohana::debug($this->scripts)); + foreach (array_keys($this->scripts) as $file) { + $links[] = html::script($file); + } + return empty($links) ? "" : implode("\n", $links); + } + /** * Handle all theme functions that insert module content. */ @@ -221,6 +235,11 @@ class Theme_View_Core extends View { array_merge(array($this), $args)); } } + + if ($function == "head" || $function == "admin_head") { + array_unshift($blocks, $this->_combine_script()); + } + if (Session::instance()->get("debug")) { if ($function != "head") { array_unshift( -- cgit v1.2.3 From 11f08ee4399c62cc1d2c36457a214c6db693db06 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Fri, 26 Jun 2009 21:42:02 -0700 Subject: Implement the combined javascript controller. --- modules/gallery/controllers/javascript.php | 64 ++++++++++++++++++++++++++++++ modules/gallery/libraries/Theme_View.php | 29 ++++++++++++-- 2 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 modules/gallery/controllers/javascript.php (limited to 'modules') diff --git a/modules/gallery/controllers/javascript.php b/modules/gallery/controllers/javascript.php new file mode 100644 index 00000000..d3c0ded5 --- /dev/null +++ b/modules/gallery/controllers/javascript.php @@ -0,0 +1,64 @@ +scripts)); + $key = ""; foreach (array_keys($this->scripts) as $file) { - $links[] = html::script($file); + $path = DOCROOT . $file; + if (file_exists($path)) { + $stats = stat($path); + $links[] = $path; + // 7 == size, 9 == mtime, see http://php.net/stat + $key = "{$key}$file $stats[7] $stats[9],"; + } else { + Kohana::log("warn", "Javascript file missing: " . $file); + } + } + + $key = md5($key); + $file = "tmp/CombinedJavascript_$key"; + if (!file_exists(VARPATH . $file)) { + $contents = ''; + foreach ($links as $link) { + $contents .= file_get_contents($link); + } + file_put_contents(VARPATH . $file, $contents); + if (function_exists("gzencode")) { + file_put_contents(VARPATH . "{$file}_gzip", gzencode($contents, 9, FORCE_GZIP)); + } } - return empty($links) ? "" : implode("\n", $links); + + return ""; } /** -- cgit v1.2.3 From 52ecdcdff2ffd37760c0d0edbe7cd2fcc62a47fc Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Sat, 27 Jun 2009 23:24:23 -0700 Subject: Implemented a Database driver for the Kohana Cache library. Rather then writing our own caching algorithm, we can leverage the Kohana library. This has the added advantage of allowing the administrator to replace the default caching with a 3rd party caching algorithm. --- modules/gallery/models/cache.php | 20 ++++ modules/gallery/tests/Cache_Test.php | 178 +++++++++++++++++++++++++++++++++++ 2 files changed, 198 insertions(+) create mode 100644 modules/gallery/models/cache.php create mode 100644 modules/gallery/tests/Cache_Test.php (limited to 'modules') diff --git a/modules/gallery/models/cache.php b/modules/gallery/models/cache.php new file mode 100644 index 00000000..99e19a6e --- /dev/null +++ b/modules/gallery/models/cache.php @@ -0,0 +1,20 @@ +from("caches")->where(1)->delete(); + $this->_driver = new Cache_Database_Driver(); + } + + public function cache_exists_test() { + $db = Database::instance(); + + $this->assert_false($this->_driver->exists("test_key"), "test_key should not be defined"); + + $id = md5(rand()); + $db->insert("caches", array("id" => $id, "tags" => ", ", + "expiration" => 84600 + time(), + "cache" => serialize("some test data"))); + + $this->assert_true($this->_driver->exists($id), "test_key should be defined"); + } + + public function cache_get_test() { + $db = Database::instance(); + + $id = md5(rand()); + $db->insert("caches", array("id" => $id, "tags" => ", ", + "expiration" => 84600 + time(), + "cache" => serialize("some test data"))); + + $data = $this->_driver->get($id); + $this->assert_equal("some test data", $data, "cached data should match"); + + $data = $this->_driver->get(""); + $this->assert_equal(null, $data, "cached data should not be found"); + } + + public function cache_set_test() { + $db = Database::instance(); + + $id = md5(rand()); + $original_data = array("field1" => "value1", "field2" => "value2"); + $this->_driver->set($id, $original_data, array("tag1", "tag2"), 84600); + + $data = $this->_driver->get($id); + $this->assert_equal($original_data, $data, "cached data should match"); + } + + public function cache_find_test() { + $db = Database::instance(); + + $id1 = md5(rand()); + $value1 = array("field1" => "value1", "field2" => "value2"); + $this->_driver->set($id1, $value1, array("tag1", "tag2"), 84600); + + $id2 = md5(rand()); + $value2 = array("field3" => "value3", "field4" => "value4"); + $this->_driver->set($id2, $value2, array("tag2", "tag3"), 84600); + + $id3 = md5(rand()); + $value3 = array("field5" => "value5", "field6" => "value6"); + $this->_driver->set($id3, $value3, array("tag3", "tag4"), 84600); + + $data = $this->_driver->find("tag2"); + + $expected = array($id1 => $value1, $id2 => $value2); + ksort($expected); + $this->assert_equal($expected, $data, "Expected id1 & id2"); + + $data = $this->_driver->find("tag4"); + $this->assert_equal(array($id3 => $value3), $data, "Expected id3"); + } + + public function cache_delete_expired_test() { + $db = Database::instance(); + + $id1 = md5(rand()); + $value1 = array("field1" => "value1", "field2" => "value2"); + $this->_driver->set($id1, $value1, array("tag1", "tag2"), -84600); + + $id2 = md5(rand()); + $value2 = array("field3" => "value3", "field4" => "value4"); + $this->_driver->set($id2, $value2, array("tag2", "tag3"), -846000); + + $id3 = md5(rand()); + $value3 = array("field5" => "value5", "field6" => "value6"); + $this->_driver->set($id3, $value3, array("tag3", "tag4"), -84600); + + $data = $this->_driver->delete_expired(); + + $this->assert_false($this->_driver->exists($id1), "$id1 should have been deleted"); + $this->assert_false($this->_driver->exists($id2), "$id2 should have been deleted"); + $this->assert_false($this->_driver->exists($id3), "$id3 should have been deleted"); + } + + public function cache_delete_id_test() { + $db = Database::instance(); + + $id1 = md5(rand()); + $value1 = array("field1" => "value1", "field2" => "value2"); + $this->_driver->set($id1, $value1, array("tag1", "tag2"), 84600); + + $id2 = md5(rand()); + $value2 = array("field3" => "value3", "field4" => "value4"); + $this->_driver->set($id2, $value2, array("tag2", "tag3"), 846000); + + $id3 = md5(rand()); + $value3 = array("field5" => "value5", "field6" => "value6"); + $this->_driver->set($id3, $value3, array("tag3", "tag4"), 84600); + + $this->_driver->delete($id1); + + $this->assert_false($this->_driver->exists($id1), "$id1 should have been deleted"); + $this->assert_true($this->_driver->exists($id2), "$id2 should not have been deleted"); + $this->assert_true($this->_driver->exists($id3), "$id3 should not have been deleted"); + } + + public function cache_delete_tag_test() { + $db = Database::instance(); + + $id1 = md5(rand()); + $value1 = array("field1" => "value1", "field2" => "value2"); + $this->_driver->set($id1, $value1, array("tag1", "tag2"), 84600); + + $id2 = md5(rand()); + $value2 = array("field3" => "value3", "field4" => "value4"); + $this->_driver->set($id2, $value2, array("tag2", "tag3"), 846000); + + $id3 = md5(rand()); + $value3 = array("field5" => "value5", "field6" => "value6"); + $this->_driver->set($id3, $value3, array("tag3", "tag4"), 84600); + + $data = $this->_driver->delete("tag3", true); + + $this->assert_true($this->_driver->exists($id1), "$id1 should not have been deleted"); + $this->assert_false($this->_driver->exists($id2), "$id2 should have been deleted"); + $this->assert_false($this->_driver->exists($id3), "$id3 should have been deleted"); + } + + public function cache_delete_all_test() { + $db = Database::instance(); + + $id1 = md5(rand()); + $value1 = array("field1" => "value1", "field2" => "value2"); + $this->_driver->set($id1, $value1, array("tag1", "tag2"), 84600); + + $id2 = md5(rand()); + $value2 = array("field3" => "value3", "field4" => "value4"); + $this->_driver->set($id2, $value2, array("tag2", "tag3"), 846000); + + $id3 = md5(rand()); + $value3 = array("field5" => "value5", "field6" => "value6"); + $this->_driver->set($id3, $value3, array("tag3", "tag4"), 84600); + + $data = $this->_driver->delete(true); + + $this->assert_false($this->_driver->exists($id1), "$id1 should have been deleted"); + $this->assert_false($this->_driver->exists($id2), "$id2 should have been deleted"); + $this->assert_false($this->_driver->exists($id3), "$id3 should have been deleted"); + } +} \ No newline at end of file -- cgit v1.2.3 From 3000c789986d93b3911a3217d7153b0de041c887 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Sat, 27 Jun 2009 23:30:02 -0700 Subject: Update the version number and upgrade method for gallery to reflect the addition of the cache table. --- modules/gallery/helpers/gallery_installer.php | 25 ++++++++++++++++++++++++- modules/gallery/module.info | 2 +- 2 files changed, 25 insertions(+), 2 deletions(-) (limited to 'modules') diff --git a/modules/gallery/helpers/gallery_installer.php b/modules/gallery/helpers/gallery_installer.php index df555d52..92fc662d 100644 --- a/modules/gallery/helpers/gallery_installer.php +++ b/modules/gallery/helpers/gallery_installer.php @@ -181,6 +181,15 @@ class gallery_installer { UNIQUE KEY(`module_name`, `name`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;"); + $db->query("CREATE TABLE {caches} ( + `id` varchar(255) NOT NULL, + `tags` varchar(255), + `expiration` int(9) NOT NULL, + `cache` text, + PRIMARY KEY (`id`), + KEY (`tags`)) + ENGINE=InnoDB DEFAULT CHARSET=utf8;"); + foreach (array("albums", "logs", "modules", "resizes", "thumbs", "tmp", "uploads") as $dir) { @mkdir(VARPATH . $dir); } @@ -249,10 +258,11 @@ class gallery_installer { module::set_var("gallery", "show_credits", 1); // @todo this string needs to be picked up by l10n_scanner module::set_var("gallery", "credits", "Powered by Gallery %version"); - module::set_version("gallery", 3); + module::set_version("gallery", 4); } static function upgrade($version) { + $db = Database::instance(); if ($version == 1) { module::set_var("gallery", "date_format", "Y-M-d"); module::set_var("gallery", "date_time_format", "Y-M-d H:i:s"); @@ -265,6 +275,18 @@ class gallery_installer { module::set_var("gallery", "show_credits", 1); module::set_version("gallery", $version = 3); } + + if ($version == 3) { + $db->query("CREATE TABLE {caches} ( + `id` varchar(255) NOT NULL, + `tags` varchar(255), + `expiration` int(9) NOT NULL, + `cache` text, + PRIMARY KEY (`id`), + KEY (`tags`)) + ENGINE=InnoDB DEFAULT CHARSET=utf8;"); + module::set_version("gallery", $version = 4); + } } static function uninstall() { @@ -282,6 +304,7 @@ class gallery_installer { $db->query("DROP TABLE IF EXISTS {tasks}"); $db->query("DROP TABLE IF EXISTS {themes}"); $db->query("DROP TABLE IF EXISTS {vars}"); + $db->query("DROP TABLE IF EXISTS {caches}"); foreach (array("albums", "resizes", "thumbs", "uploads", "modules", "logs", "database.php") as $entry) { system("/bin/rm -rf " . VARPATH . $entry); diff --git a/modules/gallery/module.info b/modules/gallery/module.info index e5b1f809..1a44ce51 100644 --- a/modules/gallery/module.info +++ b/modules/gallery/module.info @@ -1,3 +1,3 @@ name = Gallery 3 description = Gallery core application -version = 3 +version = 4 -- cgit v1.2.3 From 2d38370ec4c7f7d68ed60e78a3a118ae20fcf540 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Sat, 27 Jun 2009 23:34:07 -0700 Subject: The rest of the caching driver implementation that i somehow forgot. --- modules/gallery/config/cache.php | 32 ++++ .../gallery/libraries/drivers/Cache/Database.php | 181 +++++++++++++++++++++ 2 files changed, 213 insertions(+) create mode 100644 modules/gallery/config/cache.php create mode 100644 modules/gallery/libraries/drivers/Cache/Database.php (limited to 'modules') diff --git a/modules/gallery/config/cache.php b/modules/gallery/config/cache.php new file mode 100644 index 00000000..5f2cd6de --- /dev/null +++ b/modules/gallery/config/cache.php @@ -0,0 +1,32 @@ + File cache is fast and reliable, but requires many filesystem lookups. + * > Database cache can be used to cache items remotely, but is slower. + * > Memcache is very high performance, but prevents cache tags from being used. + * + * params - Driver parameters, specific to each driver. + * + * lifetime - Default lifetime of caches in seconds. By default caches are stored for + * thirty minutes. Specific lifetime can also be set when creating a new cache. + * Setting this to 0 will never automatically delete caches. + * + * requests - Average number of cache requests that will processed before all expired + * caches are deleted. This is commonly referred to as "garbage collection". + * Setting this to 0 or a negative number will disable automatic garbage collection. + */ +$config['default'] = array +( + 'driver' => 'database', + 'params' => null, + 'lifetime' => 84600, + 'requests' => 1000 +); diff --git a/modules/gallery/libraries/drivers/Cache/Database.php b/modules/gallery/libraries/drivers/Cache/Database.php new file mode 100644 index 00000000..e008f473 --- /dev/null +++ b/modules/gallery/libraries/drivers/Cache/Database.php @@ -0,0 +1,181 @@ +db = Database::instance(); + + if (!$this->db->table_exists("caches")) { + throw new Kohana_Exception('cache.driver_error', "Cache table is not defined"); + } + Kohana::log('debug', 'Cache Database Driver Initialized'); + } + + /** + * Checks if a cache id is already set. + * + * @param string cache id + * @return boolean + */ + public function exists($id) { + $count = $this->db->count_records("caches", array('id' => $id, "expiration >=" => time())); + return $count > 0; + } + + /** + * Sets a cache item to the given data, tags, and lifetime. + * + * @param string cache id to set + * @param string data in the cache + * @param array cache tags + * @param integer lifetime + * @return bool + */ + public function set($id, $data, array $tags = NULL, $lifetime) { + if (!empty($tags)) { + // Escape the tags, adding brackets so the tag can be explicitly matched + $tags = '<' . implode('>,<', $tags) . '>'; + } + + // Cache Database driver expects unix timestamp + if ($lifetime !== 0) { + $lifetime += time(); + } + + $data = serialize($data); + if ($this->exists($id)) { + $status = $this->db->update("caches", + array("tags" => $tags, "expiration" => $lifetime, "cache" => $data), array("id" => $id)); + } else { + $status = $this->db->insert("caches", + array("id" => $id, "tags" => $tags, "expiration" => $lifetime, "cache" => $data)); + } + + return count($status) > 0; + } + + /** + * Finds an array of ids for a given tag. + * + * @param string tag name + * @return array of ids that match the tag + */ + public function find($tag) { + $db_result = $this->db->from("caches") + ->like("tags", "<$tag>") + ->get() + ->result(true); + + // An array will always be returned + $result = array(); + + if ($db_result->count() > 0) { + // Disable notices for unserializing + $ER = error_reporting(~E_NOTICE); + + foreach ($db_result as $row) { + // Add each cache to the array + $result[$row->id] = unserialize($row->cache); + } + + // Turn notices back on + error_reporting($ER); + } + + return $result; + } + + /** + * Fetches a cache item. This will delete the item if it is expired or if + * the hash does not match the stored hash. + * + * @param string cache id + * @return mixed|NULL + */ + public function get($id) { + $data = null; + $result = $this->db->getwhere("caches", array('id' => $id)); + + if (count($result) > 0) { + $cache = $result->current(); + // Make sure the expiration is valid and that the hash matches + if ($cache->expiration != 0 && $cache->expiration <= time()) { + // Cache is not valid, delete it now + $this->delete($cache->id); + } else { + // Disable notices for unserializing + $ER = error_reporting(~E_NOTICE); + + // Return the valid cache data + $data = unserialize($cache->cache); + + // Turn notices back on + error_reporting($ER); + } + } + + return $data; + } + + /** + * Deletes a cache item by id or tag + * + * @param string cache id or tag, or true for "all items" + * @param bool delete a tag + * @return bool + */ + public function delete($id, $tag = false) { + $this->db->from("caches"); + if ($id === true) { + $this->db->where(1); + // Delete all caches + } else if ($tag === true) { + $this->db->like("tags", "<$id>"); + } else { + $this->db->where("id", $id); + } + + $status = $this->db->delete(); + + return count($status) > 0; + } + + /** + * Deletes all cache files that are older than the current time. + */ + public function delete_expired() { + // Delete all expired caches + $status = $this->db->from("caches") + ->where(array("expiration !=" => 0, "expiration <=" => time())) + ->delete(); + + return count($status) > 0; + } + +} // End Cache Database Driver \ No newline at end of file -- cgit v1.2.3 From a0c07d4b549f10dcd954777ae7d846a9b81246d8 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Sun, 28 Jun 2009 07:49:35 -0700 Subject: Clean up code (i.e. preamble, tabs) from the caching implementation so the unit tests pass --- modules/gallery/config/cache.php | 31 +++++++++++++++++----- .../gallery/libraries/drivers/Cache/Database.php | 12 ++++----- modules/gallery/tests/Gallery_Installer_Test.php | 3 --- 3 files changed, 30 insertions(+), 16 deletions(-) (limited to 'modules') diff --git a/modules/gallery/config/cache.php b/modules/gallery/config/cache.php index 5f2cd6de..cc3ac87d 100644 --- a/modules/gallery/config/cache.php +++ b/modules/gallery/config/cache.php @@ -1,5 +1,23 @@ - 'database', - 'params' => null, - 'lifetime' => 84600, - 'requests' => 1000 +$config["default"] = array ( + "driver" => "database", + "params" => null, + "lifetime" => 84600, + "requests" => 1000 ); diff --git a/modules/gallery/libraries/drivers/Cache/Database.php b/modules/gallery/libraries/drivers/Cache/Database.php index e008f473..70235e05 100644 --- a/modules/gallery/libraries/drivers/Cache/Database.php +++ b/modules/gallery/libraries/drivers/Cache/Database.php @@ -1,4 +1,4 @@ -db = Database::instance(); if (!$this->db->table_exists("caches")) { - throw new Kohana_Exception('cache.driver_error', "Cache table is not defined"); + throw new Kohana_Exception("cache.driver_error", "Cache table is not defined"); } - Kohana::log('debug', 'Cache Database Driver Initialized'); + Kohana::log("debug", "Cache Database Driver Initialized"); } /** @@ -44,7 +44,7 @@ class Cache_Database_Driver implements Cache_Driver { * @return boolean */ public function exists($id) { - $count = $this->db->count_records("caches", array('id' => $id, "expiration >=" => time())); + $count = $this->db->count_records("caches", array("id" => $id, "expiration >=" => time())); return $count > 0; } @@ -60,7 +60,7 @@ class Cache_Database_Driver implements Cache_Driver { public function set($id, $data, array $tags = NULL, $lifetime) { if (!empty($tags)) { // Escape the tags, adding brackets so the tag can be explicitly matched - $tags = '<' . implode('>,<', $tags) . '>'; + $tags = "<" . implode(">,<", $tags) . ">"; } // Cache Database driver expects unix timestamp @@ -120,7 +120,7 @@ class Cache_Database_Driver implements Cache_Driver { */ public function get($id) { $data = null; - $result = $this->db->getwhere("caches", array('id' => $id)); + $result = $this->db->getwhere("caches", array("id" => $id)); if (count($result) > 0) { $cache = $result->current(); diff --git a/modules/gallery/tests/Gallery_Installer_Test.php b/modules/gallery/tests/Gallery_Installer_Test.php index 001b7d26..27157d6e 100644 --- a/modules/gallery/tests/Gallery_Installer_Test.php +++ b/modules/gallery/tests/Gallery_Installer_Test.php @@ -31,9 +31,6 @@ class Gallery_Installer_Test extends Unit_Test_Case { public function install_registers_gallery_module_test() { $gallery = ORM::factory("module")->where("name", "gallery")->find(); $this->assert_equal("gallery", $gallery->name); - - // This is probably too volatile to keep for long - $this->assert_equal(2, $gallery->version); } public function install_creates_root_item_test() { -- cgit v1.2.3 From 7a3310e91b50f37b09a1c9d10173409244015653 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Sun, 28 Jun 2009 13:14:47 -0700 Subject: Change the cache column of the caches table to a large blob. This fixes ticket #485 and gives us the extra adavantage of not having to serialize the data (as the database driver handles that for us) --- modules/gallery/helpers/gallery_installer.php | 9 +++++++-- modules/gallery/libraries/drivers/Cache/Database.php | 5 ++--- modules/gallery/module.info | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) (limited to 'modules') diff --git a/modules/gallery/helpers/gallery_installer.php b/modules/gallery/helpers/gallery_installer.php index 92fc662d..8ccbd51b 100644 --- a/modules/gallery/helpers/gallery_installer.php +++ b/modules/gallery/helpers/gallery_installer.php @@ -185,7 +185,7 @@ class gallery_installer { `id` varchar(255) NOT NULL, `tags` varchar(255), `expiration` int(9) NOT NULL, - `cache` text, + `cache` longblob, PRIMARY KEY (`id`), KEY (`tags`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;"); @@ -258,7 +258,7 @@ class gallery_installer { module::set_var("gallery", "show_credits", 1); // @todo this string needs to be picked up by l10n_scanner module::set_var("gallery", "credits", "Powered by Gallery %version"); - module::set_version("gallery", 4); + module::set_version("gallery", 5); } static function upgrade($version) { @@ -287,6 +287,11 @@ class gallery_installer { ENGINE=InnoDB DEFAULT CHARSET=utf8;"); module::set_version("gallery", $version = 4); } + if ($version == 4) { + Cache::instance()->delete_all(); + $db->query("ALTER TABLE {caches} modify column cache LONGBLOB"); + module::set_version("gallery", $version = 5); + } } static function uninstall() { diff --git a/modules/gallery/libraries/drivers/Cache/Database.php b/modules/gallery/libraries/drivers/Cache/Database.php index 70235e05..158f7b3a 100644 --- a/modules/gallery/libraries/drivers/Cache/Database.php +++ b/modules/gallery/libraries/drivers/Cache/Database.php @@ -68,7 +68,6 @@ class Cache_Database_Driver implements Cache_Driver { $lifetime += time(); } - $data = serialize($data); if ($this->exists($id)) { $status = $this->db->update("caches", array("tags" => $tags, "expiration" => $lifetime, "cache" => $data), array("id" => $id)); @@ -101,7 +100,7 @@ class Cache_Database_Driver implements Cache_Driver { foreach ($db_result as $row) { // Add each cache to the array - $result[$row->id] = unserialize($row->cache); + $result[$row->id] = $row->cache; } // Turn notices back on @@ -133,7 +132,7 @@ class Cache_Database_Driver implements Cache_Driver { $ER = error_reporting(~E_NOTICE); // Return the valid cache data - $data = unserialize($cache->cache); + $data = $cache->cache; // Turn notices back on error_reporting($ER); diff --git a/modules/gallery/module.info b/modules/gallery/module.info index 1a44ce51..817fdddd 100644 --- a/modules/gallery/module.info +++ b/modules/gallery/module.info @@ -1,3 +1,3 @@ name = Gallery 3 description = Gallery core application -version = 4 +version = 5 -- cgit v1.2.3 From bf26ca727e615e61a8f61a2bbf1a83572cfa5c20 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Sun, 28 Jun 2009 15:30:13 -0700 Subject: Change the combined javascript to use the new caching functionality and respect the HTTP_IF_MODIFIED_SINCE header request. --- modules/gallery/controllers/javascript.php | 37 +++++++++++++++++------------- modules/gallery/libraries/Theme_View.php | 11 +++++---- 2 files changed, 28 insertions(+), 20 deletions(-) (limited to 'modules') diff --git a/modules/gallery/controllers/javascript.php b/modules/gallery/controllers/javascript.php index d3c0ded5..bc231e0a 100644 --- a/modules/gallery/controllers/javascript.php +++ b/modules/gallery/controllers/javascript.php @@ -20,30 +20,35 @@ class Javascript_Controller extends Controller { public function combined($key) { if (preg_match('/[^0-9a-f]/', $key)) { - /* The key can't contain non-hex, so just terminate early */ + // The key can't contain non-hex, so just terminate early Kohana::show_404(); } // We don't need to save the session for this request Session::abort_save(); - Kohana::log("error", Kohana::debug($_SERVER)); // Dump out the javascript file - $ext = strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") !== false ? "_gzip" : ""; - $file = VARPATH . "tmp/CombinedJavascript_$key{$ext}"; + $cache = Cache::instance(); - if (!file_exists($file)) { - Kohana::show_404(); + $modified = $cache->get("{$key}_modified"); + if (!empty($_SERVER["HTTP_IF_MODIFIED_SINCE"]) && !empty($modified)) { + header('HTTP/1.0 304 Not Modified'); + return; } - $stats = stat($file); - if (!empty($_SERVER["HTTP_IF_MODIFIED_SINCE"]) && - $stats[9] <= $_SERVER["HTTP_IF_MODIFIED_SINCE"]) { - header("HTTP/1.0 304 Not Modified"); - return; + $content = ""; + if (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") !== false ) { + $content = $cache->get("{$key}_gz"); + } + + if (empty($content)) { + $content = $cache->get($key); + } + + if (empty($content)) { + Kohana::show_404(); } - Kohana::log("error", Kohana::debug($_SERVER)); if (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") !== false) { header("Content-Encoding: gzip"); header("Cache-Control: private, x-gzip-ok=\"public\""); @@ -52,13 +57,13 @@ class Javascript_Controller extends Controller { header("Content-Type: text/javascript; charset=UTF-8"); header("Expires: " . gmdate(21474383647)); - header("Last-Modified: " . gmdate($stats[9])); + header("Last-Modified: " . $modified); Kohana::close_buffers(false); - $fd = fopen($file, "rb"); - fpassthru($fd); - fclose($fd); + $handle = fopen("php://output", "wb"); + fwrite($handle, $content); + fclose($handle); } } diff --git a/modules/gallery/libraries/Theme_View.php b/modules/gallery/libraries/Theme_View.php index 167f8a8d..f55cb55a 100644 --- a/modules/gallery/libraries/Theme_View.php +++ b/modules/gallery/libraries/Theme_View.php @@ -188,18 +188,21 @@ class Theme_View_Core extends View { } $key = md5($key); - $file = "tmp/CombinedJavascript_$key"; - if (!file_exists(VARPATH . $file)) { + $contents = Cache::instance()->get($key); + if (empty($contents)) { $contents = ''; foreach ($links as $link) { $contents .= file_get_contents($link); } - file_put_contents(VARPATH . $file, $contents); + Cache::instance()->set($key, $contents, array("javascript"), 84600); if (function_exists("gzencode")) { - file_put_contents(VARPATH . "{$file}_gzip", gzencode($contents, 9, FORCE_GZIP)); + Cache::instance()->set("{$key}_gz", gzencode($contents, 9, FORCE_GZIP), + array("javascript", "gzip"), 84600); } + Cache::instance()->set("{$key}_modified", time(), array("javascript", "modified"), 84600); } + // Handcraft the script link because html::script will add a .js extenstion return ""; } -- cgit v1.2.3 From aa31e1f0090522c3cfb3a44b947ad8c33a275979 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 28 Jun 2009 16:24:51 -0700 Subject: Tweak the cache implementation 1) Drop the *_modified key, we don't really need it. The modification date is not relevant to our browser caching strategy. 2) Fix multiple issues with the Expires header and just hardcode it to the biggest possibly value for code clarity. 3) print the $content out directly instead of using fwrite 4) Minor cleanups in the installer. --- modules/gallery/controllers/javascript.php | 21 ++++++------------ modules/gallery/helpers/gallery_installer.php | 32 +++++++++++++-------------- modules/gallery/libraries/Theme_View.php | 16 +++++++++----- 3 files changed, 33 insertions(+), 36 deletions(-) (limited to 'modules') diff --git a/modules/gallery/controllers/javascript.php b/modules/gallery/controllers/javascript.php index bc231e0a..ba5cbf4b 100644 --- a/modules/gallery/controllers/javascript.php +++ b/modules/gallery/controllers/javascript.php @@ -27,16 +27,13 @@ class Javascript_Controller extends Controller { // We don't need to save the session for this request Session::abort_save(); - // Dump out the javascript file - $cache = Cache::instance(); - - $modified = $cache->get("{$key}_modified"); - if (!empty($_SERVER["HTTP_IF_MODIFIED_SINCE"]) && !empty($modified)) { + // Our data is immutable, so if they already have a copy then it needs no updating. + if (!empty($_SERVER["HTTP_IF_MODIFIED_SINCE"])) { header('HTTP/1.0 304 Not Modified'); return; } - $content = ""; + $cache = Cache::instance(); if (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") !== false ) { $content = $cache->get("{$key}_gz"); } @@ -51,19 +48,15 @@ class Javascript_Controller extends Controller { if (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") !== false) { header("Content-Encoding: gzip"); - header("Cache-Control: private, x-gzip-ok=\"public\""); + header("Cache-Control: public"); } header("Content-Type: text/javascript; charset=UTF-8"); - - header("Expires: " . gmdate(21474383647)); - header("Last-Modified: " . $modified); + header("Expires: Tue, 19 Jan 2038 00:00:00 GMT"); + header("Last-Modified: " . gmdate("D, d M Y H:i:s T", time())); Kohana::close_buffers(false); - - $handle = fopen("php://output", "wb"); - fwrite($handle, $content); - fclose($handle); + print $content; } } diff --git a/modules/gallery/helpers/gallery_installer.php b/modules/gallery/helpers/gallery_installer.php index 8ccbd51b..d0bfa629 100644 --- a/modules/gallery/helpers/gallery_installer.php +++ b/modules/gallery/helpers/gallery_installer.php @@ -32,6 +32,15 @@ class gallery_installer { PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;"); + $db->query("CREATE TABLE {caches} ( + `id` varchar(255) NOT NULL, + `tags` varchar(255), + `expiration` int(9) NOT NULL, + `cache` longblob, + PRIMARY KEY (`id`), + KEY (`tags`)) + ENGINE=InnoDB DEFAULT CHARSET=utf8;"); + $db->query("CREATE TABLE {graphics_rules} ( `id` int(9) NOT NULL auto_increment, `active` BOOLEAN default 0, @@ -181,15 +190,6 @@ class gallery_installer { UNIQUE KEY(`module_name`, `name`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;"); - $db->query("CREATE TABLE {caches} ( - `id` varchar(255) NOT NULL, - `tags` varchar(255), - `expiration` int(9) NOT NULL, - `cache` longblob, - PRIMARY KEY (`id`), - KEY (`tags`)) - ENGINE=InnoDB DEFAULT CHARSET=utf8;"); - foreach (array("albums", "logs", "modules", "resizes", "thumbs", "tmp", "uploads") as $dir) { @mkdir(VARPATH . $dir); } @@ -278,13 +278,13 @@ class gallery_installer { if ($version == 3) { $db->query("CREATE TABLE {caches} ( - `id` varchar(255) NOT NULL, - `tags` varchar(255), - `expiration` int(9) NOT NULL, - `cache` text, - PRIMARY KEY (`id`), - KEY (`tags`)) - ENGINE=InnoDB DEFAULT CHARSET=utf8;"); + `id` varchar(255) NOT NULL, + `tags` varchar(255), + `expiration` int(9) NOT NULL, + `cache` text, + PRIMARY KEY (`id`), + KEY (`tags`)) + ENGINE=InnoDB DEFAULT CHARSET=utf8;"); module::set_version("gallery", $version = 4); } if ($version == 4) { diff --git a/modules/gallery/libraries/Theme_View.php b/modules/gallery/libraries/Theme_View.php index f55cb55a..c3acfeb3 100644 --- a/modules/gallery/libraries/Theme_View.php +++ b/modules/gallery/libraries/Theme_View.php @@ -172,6 +172,10 @@ class Theme_View_Core extends View { $this->scripts[$file] = 1; } + /** + * Combine a series of Javascript files into a single one and cache it in the database, then + * return a single "; - $head[] = html::script("lib/jquery.autocomplete.js"); - $head[] = html::script("modules/server_add/js/admin.js"); + $theme->script("lib/jquery.autocomplete.js"); + $theme->script("modules/server_add/js/admin.js"); } return implode("\n", $head); diff --git a/modules/tag/helpers/tag_theme.php b/modules/tag/helpers/tag_theme.php index a32d71b6..b48ad178 100644 --- a/modules/tag/helpers/tag_theme.php +++ b/modules/tag/helpers/tag_theme.php @@ -19,8 +19,11 @@ */ class tag_theme_Core { static function head($theme) { - $url = url::file("modules/tag/js/tag.js"); - return ""; + $theme->script("modules/tag/js/tag.js"); + } + + static function admin_head($theme) { + $theme->script("modules/tag/js/tag.js"); } static function sidebar_blocks($theme) { diff --git a/modules/tag/views/admin_tags.html.php b/modules/tag/views/admin_tags.html.php index 21661c48..7d201da7 100644 --- a/modules/tag/views/admin_tags.html.php +++ b/modules/tag/views/admin_tags.html.php @@ -1,5 +1,4 @@ -