Skip to content

Commit

Permalink
Merge branch 'master' of git@github.com:gallery/gallery3
Browse files Browse the repository at this point in the history
  • Loading branch information
andyst committed Jun 30, 2009
2 parents 333c9d4 + 8af19c0 commit 06172ec
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 66 deletions.
40 changes: 31 additions & 9 deletions modules/gallery/controllers/combined.php
Expand Up @@ -18,31 +18,51 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class Combined_Controller extends Controller {
/**
* Return the combined Javascript bundle associated with the given key.
*/
public function javascript($key) {
if (preg_match('/[^0-9a-f]/', $key)) {
// The key can't contain non-hex, so just terminate early
Kohana::show_404();
}
$key = substr($key, 0, strlen($key) - 3); // strip off the trailing .js
return $this->_emit("javascript", $key);
}

// We don't need to save the session for this request
Session::abort_save();
/**
* Return the combined CSS bundle associated with the given key.
*/
public function css($key) {
$key = substr($key, 0, strlen($key) - 4); // strip off the trailing .css
return $this->_emit("css", $key);
}

/**
* Print out a cached entry.
* @param string the combined entry type (either "javascript" or "css")
* @param string the key (typically an md5 sum)
*/
private function _emit($type, $key) {
// 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;
}

if (empty($key)) {
Kohana::show_404();
}

// We don't need to save the session for this request
Session::abort_save();

$cache = Cache::instance();
if (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") !== false ) {
$content = $cache->get("{$key}_gz");
}

if (empty($content)) {
if (!$content) {
$content = $cache->get($key);
}

if (empty($content)) {
if (!$content) {
Kohana::show_404();
}

Expand All @@ -51,12 +71,14 @@ public function javascript($key) {
header("Cache-Control: public");
}

header("Content-Type: text/javascript; charset=UTF-8");
// $type is either 'javascript' or 'css'
header("Content-Type: text/$type; charset=UTF-8");
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);
print $content;
}

}

18 changes: 5 additions & 13 deletions modules/gallery/helpers/gallery_theme.php
Expand Up @@ -22,13 +22,11 @@ static function head($theme) {
$session = Session::instance();
$buf = "";
if ($session->get("debug")) {
$buf .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" .
url::file("modules/gallery/css/debug.css") . "\" />";
$theme->css("modules/gallery/css/debug.css");
}
if (($theme->page_type == "album" || $theme->page_type == "photo")
&& access::can("edit", $theme->item())) {
$buf .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" .
url::file("modules/gallery/css/quick.css") . "\" />";
$theme->css("modules/gallery/css/quick.css");
$theme->script("modules/gallery/js/quick.js");
}

Expand All @@ -41,8 +39,7 @@ static function head($theme) {
}

if ($session->get("l10n_mode", false)) {
$buf .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" .
url::file("modules/gallery/css/l10n_client.css") . "\" />";
$theme->css("modules/gallery/css/l10n_client.css");
$theme->script("lib/jquery.cookie.js");
$theme->script("modules/gallery/js/l10n_client.js");
}
Expand Down Expand Up @@ -78,20 +75,15 @@ static function thumb_bottom($theme, $child) {

static function admin_head($theme) {
$session = Session::instance();
$buf = "";
if ($session->get("debug")) {
$buf .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" .
url::file("modules/gallery/css/debug.css") . "\" />";
$theme->css("modules/gallery/css/debug.css");
}

if ($session->get("l10n_mode", false)) {
$buf .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" .
url::file("modules/gallery/css/l10n_client.css") . "\" />";
$theme->css("modules/gallery/css/l10n_client.css");
$theme->script("lib/jquery.cookie.js");
$theme->script("modules/gallery/js/l10n_client.js");
}

return $buf;
}

static function page_bottom($theme) {
Expand Down
5 changes: 3 additions & 2 deletions modules/gallery/libraries/Admin_View.php
Expand Up @@ -98,8 +98,9 @@ public function __call($function, $args) {
}
}

if ($function == "admin_head") {
array_unshift($blocks, $this->combine_script());
if ($function == "head") {
array_unshift($blocks, $this->combine_files($this->css, "css"));
array_unshift($blocks, $this->combine_files($this->scripts, "javascript"));
}

if (Session::instance()->get("debug")) {
Expand Down
96 changes: 82 additions & 14 deletions modules/gallery/libraries/Gallery_View.php
Expand Up @@ -20,6 +20,7 @@
class Gallery_View_Core extends View {
protected $theme_name = null;
protected $scripts = array();
protected $css = array();

/**
* Add a script to the combined scripts list.
Expand Down Expand Up @@ -49,41 +50,108 @@ public function theme_url($path, $absolute_url=false) {
}

/**
* Combine a series of Javascript files into a single one and cache it in the database, then
* return a single <script> element to refer to it.
* Add a css file to the combined css list.
* @param $file the relative path to a script from the gallery3 directory
*/
public function css($file, $theme_relative=false) {
$this->css[$file] = 1;
}

/**
* Add a css file to the combined css list.
* @param $file the relative path to a script from the base of the active theme
* @param
*/
protected function combine_script() {
public function theme_css($file) {
$file = "themes/{$this->theme_name}/$file";
$this->css[$file] = 1;
}

/**
* Combine a series of files into a single one and cache it in the database.
*/
protected function combine_files($files, $type) {
$links = array();
$key = "";
foreach (array_keys($this->scripts) as $file) {
$key = array();

foreach (array_keys($files) 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],";
$key[] = "$file $stats[7] $stats[9]";
} else {
Kohana::log("alert", "Javascript file missing: " . $file);
Kohana::log("error", "missing file ($type): $file");
}
}

$key = md5($key);
$key = md5(join(" ", $key));
$cache = Cache::instance();
$contents = $cache->get($key);

$contents = "";
if (empty($contents)) {
$contents = "";
foreach ($links as $link) {
$contents .= file_get_contents($link);
if ($type == "css") {
$contents .= "/* $link */\n" . $this->process_css($link) . "\n";
} else {
$contents .= "/* $link */\n" . file_get_contents($link) . "\n";
}
}
$cache->set($key, $contents, array("javascript"), 30 * 84600);

$cache->set($key, $contents, array($type), 30 * 84600);
if (function_exists("gzencode")) {
$cache->set("{$key}_gz", gzencode($contents, 9, FORCE_GZIP),
array("javascript", "gzip"), 30 * 84600);
array($type, "gzip"), 30 * 84600);
}
}

if ($type == "css") {
return "<!-- LOOKING FOR YOUR CSS? It's all been combined into the link below -->\n" .
html::stylesheet("combined/css/$key", "screen,print,projection", true);
} else {
return "<!-- LOOKING FOR YOUR JAVASCRIPT? It's all been combined into the link below -->\n" .
html::script("combined/javascript/$key", true);
}
}

/**
* Convert relative references inside a CSS file to absolute ones so that when it's served from
* a new location as part of a combined bundle the references are still correct.
* @param string the path to the css file
*/
private function process_css($css_file) {
static $PATTERN = "#url\(\s*['|\"]{0,1}(.*?)['|\"]{0,1}\s*\)#";
$docroot_length = strlen(DOCROOT);

$css = file_get_contents($css_file);
if (preg_match_all($PATTERN, $css, $matches, PREG_SET_ORDER)) {
$search = $replace = array();
foreach ($matches as $match) {
$relative = substr(realpath(dirname($css_file) . "/$match[1]"), $docroot_length);
if (!empty($relative)) {
$search[] = $match[0];
$replace[] = "url('" . url::abs_file($relative) . "')";
} else {
Kohana::log("error", "Missing URL reference '{$match[1]}' in CSS file '$css_file'");
}
}
$css = str_replace($search, $replace, $css);
}
$imports = preg_match_all("#@import\s*['|\"]{0,1}(.*?)['|\"]{0,1};#",
$css, $matches, PREG_SET_ORDER);

if ($imports) {
$search = $replace = array();
foreach ($matches as $match) {
$search[] = $match[0];
$replace[] = $this->process_css(dirname($css_file) . "/$match[1]");
}
$css = str_replace($search, $replace, $css);
}

// Handcraft the script link because html::script will add a .js extenstion
return "<script type=\"text/javascript\" src=\"" . url::site("combined/javascript/$key") .
"\"></script>";
return $css;
}
}
5 changes: 3 additions & 2 deletions modules/gallery/libraries/Theme_View.php
Expand Up @@ -201,8 +201,8 @@ public function __call($function, $args) {
array("gallery_theme", $function),
array_merge(array($this), $args));
}

}

foreach (module::active() as $module) {
if ($module->name == "gallery") {
continue;
Expand All @@ -216,7 +216,8 @@ public function __call($function, $args) {
}

if ($function == "head") {
array_unshift($blocks, $this->combine_script());
array_unshift($blocks, $this->combine_files($this->css, "css"));
array_unshift($blocks, $this->combine_files($this->scripts, "javascript"));
}

if (Session::instance()->get("debug")) {
Expand Down
3 changes: 1 addition & 2 deletions modules/organize/helpers/organize_theme.php
Expand Up @@ -22,7 +22,6 @@ static function head($theme) {
// @tdo remove the addition css and organize.js (just here to test)
$theme->script("modules/organize/js/organize_init.js");
$theme->script("modules/organize/js/organize.js");
return "<link rel=\"stylesheet\" type=\"text/css\" href=\"" .
url::file("modules/organize/css/organize.css") . "\" />";
$theme->css("modules/organize/css/organize.css");
}
}
3 changes: 1 addition & 2 deletions modules/server_add/helpers/server_add_theme.php
Expand Up @@ -27,8 +27,7 @@ static function head($theme) {
static function admin_head($theme) {
$head = array();
if (strpos(Router::$current_uri, "admin/server_add") !== false) {
$head[] = "<link media=\"screen, projection\" rel=\"stylesheet\" type=\"text/css\" href=\"" .
url::file("lib/jquery.autocomplete.css") . "\" />";
$theme->css("lib/jquery.autocomplete.css");
$base = url::site("__ARGS__");
$csrf = access::csrf_token();
$head[] = "<script> var base_url = \"$base\"; var csrf = \"$csrf\";</script>";
Expand Down
19 changes: 7 additions & 12 deletions themes/admin_default/views/admin.html.php
Expand Up @@ -6,19 +6,14 @@
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title><?= t("Admin Dashboard") ?></title>
<link rel="shortcut icon" href="<?= url::file("themes/default/images/favicon.ico") ?>" type="image/x-icon" />
<link rel="stylesheet" type="text/css" href="<?= url::file("lib/yui/reset-fonts-grids.css") ?>"
media="screen,projection" />
<link rel="stylesheet" type="text/css" type="text/css" href="<?= url::file("lib/themeroller/ui.base.css") ?>"
media="screen,projection" />
<link rel="stylesheet" type="text/css" href="<?= url::file("lib/superfish/css/superfish.css") ?>"
media="screen,projection" />
<link rel="stylesheet" type="text/css" href="<?= url::file("themes/default/css/screen.css") ?>"
media="screen,projection" />
<link rel="stylesheet" type="text/css" href="<?= $theme->theme_url("css/screen.css") ?>"
media="screen,projection" />

<?= $theme->css("lib/yui/reset-fonts-grids.css") ?>
<?= $theme->css("lib/themeroller/ui.base.css") ?>
<?= $theme->css("lib/superfish/css/superfish.css") ?>
<?= $theme->css("themes/default/css/screen.css") ?>
<?= $theme->theme_css("css/screen.css") ?>
<!--[if IE]>
<link rel="stylesheet" type="text/css" href="<?= $theme->theme_url("css/fix-ie.css") ?>"
media="screen,print,projection" />
<?= $theme->theme_css("css/fix-ie.css") ?>
<![endif]-->

<?= $theme->script("lib/jquery.js") ?>
Expand Down
Empty file modified themes/default/images/ico-error.png 100755 → 100644
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified themes/default/images/ico-help.png 100755 → 100644
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified themes/default/images/ico-info.png 100755 → 100644
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified themes/default/images/ico-success.png 100755 → 100644
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified themes/default/images/ico-warning.png 100755 → 100644
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 5 additions & 10 deletions themes/default/views/page.html.php
Expand Up @@ -24,17 +24,12 @@
<? endif ?>
</title>
<link rel="shortcut icon" href="<?= $theme->theme_url("images/favicon.ico") ?>" type="image/x-icon" />
<link rel="stylesheet" type="text/css" href="<?= url::file("lib/yui/reset-fonts-grids.css") ?>"
media="screen,print,projection" />
<link rel="stylesheet" type="text/css" href="<?= url::file("lib/superfish/css/superfish.css") ?>"
media="screen" />
<link rel="stylesheet" type="text/css" href="<?= url::file("lib/themeroller/ui.base.css") ?>"
media="screen,print,projection" />
<link rel="stylesheet" type="text/css" href="<?= $theme->theme_url("css/screen.css") ?>"
media="screen,print,projection" />
<?= $theme->css("lib/yui/reset-fonts-grids.css") ?>
<?= $theme->css("lib/superfish/css/superfish.css") ?>
<?= $theme->css("lib/themeroller/ui.base.css") ?>
<?= $theme->theme_css("css/screen.css") ?>
<!--[if lt IE 8]>
<link rel="stylesheet" type="text/css" href="<?= $theme->theme_url("css/fix-ie.css") ?>"
media="screen,print,projection" />
<?= $theme->theme_css("css/fix-ie.css") ?>
<![endif]-->
<? if ($theme->page_type == 'album'): ?>
<? if ($thumb_proportion != 1): ?>
Expand Down

0 comments on commit 06172ec

Please sign in to comment.