diff options
-rw-r--r-- | modules/comment/helpers/comment_theme.php | 4 | ||||
-rw-r--r-- | modules/gallery/controllers/javascript.php | 64 | ||||
-rw-r--r-- | modules/gallery/helpers/gallery_theme.php | 34 | ||||
-rw-r--r-- | modules/gallery/libraries/Theme_View.php | 67 | ||||
-rw-r--r-- | modules/gallery/models/cache.php | 20 | ||||
-rw-r--r-- | modules/gallery/tests/Cache_Test.php | 178 | ||||
-rw-r--r-- | themes/admin_default/views/admin.html.php | 10 | ||||
-rw-r--r-- | themes/default/views/movie.html.php | 1 | ||||
-rw-r--r-- | themes/default/views/page.html.php | 12 | ||||
-rw-r--r-- | themes/default/views/photo.html.php | 1 |
10 files changed, 359 insertions, 32 deletions
diff --git a/modules/comment/helpers/comment_theme.php b/modules/comment/helpers/comment_theme.php index d9f1acf4..89b2f57c 100644 --- a/modules/comment/helpers/comment_theme.php +++ b/modules/comment/helpers/comment_theme.php @@ -19,8 +19,8 @@ */ class comment_theme_Core { static function head($theme) { - $url = url::file("modules/comment/js/comment.js"); - return "<script src=\"$url\" type=\"text/javascript\"></script>\n"; + $theme->script("modules/comment/js/comment.js"); + return ""; } static function photo_bottom($theme) { 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 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2009 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +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 */ + 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}"; + + if (!file_exists($file)) { + Kohana::show_404(); + } + + $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; + } + + 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\""); + } + + header("Content-Type: text/javascript; charset=UTF-8"); + + header("Expires: " . gmdate(21474383647)); + header("Last-Modified: " . gmdate($stats[9])); + + Kohana::close_buffers(false); + + $fd = fopen($file, "rb"); + fpassthru($fd); + fclose($fd); + } +} + diff --git a/modules/gallery/helpers/gallery_theme.php b/modules/gallery/helpers/gallery_theme.php index a96c8f5b..b6b24b27 100644 --- a/modules/gallery/helpers/gallery_theme.php +++ b/modules/gallery/helpers/gallery_theme.php @@ -21,6 +21,22 @@ class gallery_theme_Core { static function head($theme) { $session = Session::instance(); $buf = ""; + $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') { + $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') { + $theme->script("lib/flowplayer.js"); + } + $theme->script($theme->url("js/ui.init.js", false, true)); if ($session->get("debug")) { $buf .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . url::file("modules/gallery/css/debug.css") . "\" />"; @@ -29,7 +45,7 @@ class gallery_theme_Core { && access::can("edit", $theme->item())) { $buf .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . url::file("modules/gallery/css/quick.css") . "\" />"; - $buf .= html::script("modules/gallery/js/quick.js"); + $theme->script("modules/gallery/js/quick.js"); } if (module::is_active("rss")) { @@ -43,8 +59,8 @@ class gallery_theme_Core { if ($session->get("l10n_mode", false)) { $buf .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . url::file("modules/gallery/css/l10n_client.css") . "\" />"; - $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; @@ -79,6 +95,14 @@ class gallery_theme_Core { static function admin_head($theme) { $session = Session::instance(); $buf = ""; + $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 .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . url::file("modules/gallery/css/debug.css") . "\" />"; @@ -87,8 +111,8 @@ class gallery_theme_Core { if ($session->get("l10n_mode", false)) { $buf .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . url::file("modules/gallery/css/l10n_client.css") . "\" />"; - $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 4612a74b..167f8a8d 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. @@ -68,9 +69,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() { @@ -167,6 +168,42 @@ class Theme_View_Core extends View { return message::get(); } + public function script($file) { + $this->scripts[$file] = 1; + } + + private function _combine_script() { + $links = array(); + $key = ""; + foreach (array_keys($this->scripts) as $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 "<script type=\"text/javascript\" src=\"" . url::site("javascript/combined/$key") . + "\"></script>"; + } + /** * Handle all theme functions that insert module content. */ @@ -196,7 +233,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( @@ -204,6 +262,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( 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 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2009 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +class Cache_Model extends ORM {} diff --git a/modules/gallery/tests/Cache_Test.php b/modules/gallery/tests/Cache_Test.php new file mode 100644 index 00000000..a5e0e7a0 --- /dev/null +++ b/modules/gallery/tests/Cache_Test.php @@ -0,0 +1,178 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2009 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +class Cache_Test extends Unit_Test_Case { + private $_driver; + public function setup() { + Database::instance()->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" => "<tag1>, <tag2>", + "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" => "<tag1>, <tag2>", + "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 diff --git a/themes/admin_default/views/admin.html.php b/themes/admin_default/views/admin.html.php index b7cfaa40..dfbafe31 100644 --- a/themes/admin_default/views/admin.html.php +++ b/themes/admin_default/views/admin.html.php @@ -20,15 +20,7 @@ <link rel="stylesheet" type="text/css" href="<?= $theme->url("css/fix-ie.css") ?>" media="screen,print,projection" /> <![endif]--> - <script src="<?= url::file("lib/jquery.js") ?>" type="text/javascript"></script> - <script src="<?= url::file("lib/jquery.form.js") ?>" type="text/javascript"></script> - <script src="<?= url::file("lib/jquery-ui.js") ?>" type="text/javascript"></script> - <script src="<?= url::file("lib/gallery.common.js") ?>" type="text/javascript"></script> - <script src="<?= url::file("lib/gallery.dialog.js") ?>" type="text/javascript"></script> - <script src="<?= url::file("lib/superfish/js/superfish.js") ?>" type="text/javascript"></script> - <script src="<?= $theme->url("js/jquery.dropshadow.js") ?>" type="text/javascript"></script> - <script src="<?= $theme->url("js/ui.init.js") ?>" type="text/javascript"></script> - <?= $theme->admin_head() ?> + <?= $theme->admin_head() ?> </head> <body <?= $theme->body_attributes() ?>> 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 @@ <?php defined("SYSPATH") or die("No direct script access.") ?> -<script src="<?= url::file("lib/flowplayer.js") ?>" type="text/javascript"></script> <div id="gItem"> <?= $theme->photo_top() ?> diff --git a/themes/default/views/page.html.php b/themes/default/views/page.html.php index f6286f95..c74d4097 100644 --- a/themes/default/views/page.html.php +++ b/themes/default/views/page.html.php @@ -49,18 +49,6 @@ </style> <? endif ?> <? endif ?> - <script src="<?= url::file("lib/jquery.js") ?>" type="text/javascript"></script> - <script src="<?= url::file("lib/jquery.form.js") ?>" type="text/javascript"></script> - <script src="<?= url::file("lib/jquery-ui.js") ?>" type="text/javascript"></script> - <script src="<?= url::file("lib/gallery.common.js") ?>" type="text/javascript"></script> - <script src="<?= url::file("lib/gallery.dialog.js") ?>" type="text/javascript"></script> - <script src="<?= url::file("lib/gallery.form.js") ?>" type="text/javascript"></script> - <script src="<?= url::file("lib/superfish/js/superfish.js") ?>" type="text/javascript"></script> - <? if ($theme->page_type == 'photo'): ?> - <script src="<?= url::file("lib/jquery.scrollTo.js") ?>" type="text/javascript"></script> - <script src="<?= url::file("lib/jquery.localscroll.js") ?>" type="text/javascript"></script> - <? endif ?> - <script src="<?= $theme->url("js/ui.init.js") ?>" type="text/javascript"></script> <?= $theme->head() ?> </head> 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 @@ <? if (access::can("view_full", $theme->item())): ?> <!-- Use javascript to show the full size as an overlay on the current page --> -<script src="<?= url::file("lib/gallery.show_full_size.js") ?>" type="text/javascript"></script> <script> $(document).ready(function() { $(".gFullSizeLink").click(function() { |