| 1 |
<?php
|
| 2 |
// $Id: boost.api.inc,v 1.2 2006/11/25 16:42:35 arto Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Implements the Boost API for static page caching.
|
| 7 |
*/
|
| 8 |
|
| 9 |
//////////////////////////////////////////////////////////////////////////////
|
| 10 |
// BOOST API
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Determines whether a given Drupal page can be cached or not.
|
| 14 |
*
|
| 15 |
* To avoid potentially troublesome situations, the user login page is never
|
| 16 |
* cached, nor are any admin pages. At present, we also refuse to cache any
|
| 17 |
* RSS feeds provided by Drupal, since they would require special handling
|
| 18 |
* in the mod_rewrite ruleset as they shouldn't be sent out using the
|
| 19 |
* text/html content type.
|
| 20 |
*/
|
| 21 |
function boost_is_cacheable($path) {
|
| 22 |
$alias = drupal_get_path_alias($path);
|
| 23 |
$path = drupal_get_normal_path($path); // normalize path
|
| 24 |
|
| 25 |
// Never cache the basic user login/registration pages or any administration pages
|
| 26 |
if ($path == 'user' || preg_match('!^user/(login|register|password)!', $path) || preg_match('!^admin!', $path))
|
| 27 |
return FALSE;
|
| 28 |
|
| 29 |
// At present, RSS feeds are not cacheable due to content type restrictions
|
| 30 |
if ($path == 'rss.xml' || preg_match('!/feed$!', $path))
|
| 31 |
return FALSE;
|
| 32 |
|
| 33 |
// Don't cache comment reply pages
|
| 34 |
if (preg_match('!^comment/reply!', $path))
|
| 35 |
return FALSE;
|
| 36 |
|
| 37 |
// Match the user's cacheability settings against the path
|
| 38 |
if (BOOST_CACHEABILITY_OPTION == 2) {
|
| 39 |
$result = drupal_eval(BOOST_CACHEABILITY_PAGES);
|
| 40 |
return !empty($result);
|
| 41 |
}
|
| 42 |
$regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote(BOOST_CACHEABILITY_PAGES, '/')) .')$/';
|
| 43 |
return !(BOOST_CACHEABILITY_OPTION xor preg_match($regexp, $alias));
|
| 44 |
}
|
| 45 |
|
| 46 |
/**
|
| 47 |
* Determines whether a given Drupal page is currently cached or not.
|
| 48 |
*/
|
| 49 |
function boost_is_cached($path) {
|
| 50 |
$path = (empty($path) ? BOOST_FRONTPAGE : $path);
|
| 51 |
$alias = drupal_get_path_alias($path);
|
| 52 |
$path = drupal_get_normal_path($path); // normalize path
|
| 53 |
|
| 54 |
// TODO: also determine if alias/symlink exists?
|
| 55 |
return file_exists(boost_file_path($path));
|
| 56 |
}
|
| 57 |
|
| 58 |
/**
|
| 59 |
* Deletes all static files currently in the cache.
|
| 60 |
*/
|
| 61 |
function boost_cache_clear_all() {
|
| 62 |
clearstatcache();
|
| 63 |
if (($cache_dir = boost_cache_directory()) && file_exists($cache_dir)) {
|
| 64 |
return _boost_rmdir_rf($cache_dir);
|
| 65 |
}
|
| 66 |
}
|
| 67 |
|
| 68 |
/**
|
| 69 |
* Deletes all expired static files currently in the cache.
|
| 70 |
*/
|
| 71 |
function boost_cache_expire_all() {
|
| 72 |
clearstatcache();
|
| 73 |
if (($cache_dir = boost_cache_directory()) && file_exists($cache_dir)) {
|
| 74 |
_boost_rmdir_rf($cache_dir, 'boost_file_is_expired');
|
| 75 |
}
|
| 76 |
return TRUE;
|
| 77 |
}
|
| 78 |
|
| 79 |
/**
|
| 80 |
* Expires the static file cache for a given page, or multiple pages
|
| 81 |
* matching a wildcard.
|
| 82 |
*/
|
| 83 |
function boost_cache_expire($path, $wildcard = FALSE) {
|
| 84 |
// TODO: handle wildcard.
|
| 85 |
|
| 86 |
$alias = drupal_get_path_alias($path);
|
| 87 |
$path = drupal_get_normal_path($path); // normalize path
|
| 88 |
|
| 89 |
$filename = boost_file_path($path);
|
| 90 |
if (file_exists($filename))
|
| 91 |
@unlink($filename);
|
| 92 |
|
| 93 |
if ($alias != $path) {
|
| 94 |
$symlink = boost_file_path($alias);
|
| 95 |
if (is_link($symlink))
|
| 96 |
@unlink($symlink);
|
| 97 |
}
|
| 98 |
|
| 99 |
return TRUE;
|
| 100 |
}
|
| 101 |
|
| 102 |
/**
|
| 103 |
* Returns the cached contents of the specified page, if available.
|
| 104 |
*/
|
| 105 |
function boost_cache_get($path) {
|
| 106 |
$path = drupal_get_normal_path($path); // normalize path
|
| 107 |
|
| 108 |
$filename = boost_file_path($path);
|
| 109 |
if (file_exists($filename) && is_readable($filename))
|
| 110 |
return file_get_contents($filename);
|
| 111 |
|
| 112 |
return NULL;
|
| 113 |
}
|
| 114 |
|
| 115 |
|
| 116 |
/**
|
| 117 |
* Replaces the cached contents of the specified page, if stale.
|
| 118 |
*/
|
| 119 |
function boost_cache_set($path, $data = '') {
|
| 120 |
// Append the Boost footer with the current timestamp
|
| 121 |
$data = rtrim($data) . "\n" . str_replace('%date', date('Y-m-d H:i:s'), BOOST_BANNER);
|
| 122 |
|
| 123 |
// Execute the pre-process function if one has been defined
|
| 124 |
if (function_exists(BOOST_PRE_PROCESS_FUNCTION))
|
| 125 |
$data = call_user_func(BOOST_PRE_PROCESS_FUNCTION, $data);
|
| 126 |
|
| 127 |
$alias = drupal_get_path_alias($path);
|
| 128 |
$path = drupal_get_normal_path($path); // normalize path
|
| 129 |
|
| 130 |
// Create or update the static file as needed
|
| 131 |
$filename = boost_file_path($path);
|
| 132 |
_boost_mkdir_p(dirname($filename));
|
| 133 |
if (!file_exists($filename) || boost_file_is_expired($filename)) {
|
| 134 |
if (file_put_contents($filename, $data) === FALSE) {
|
| 135 |
watchdog('boost', t('Unable to write file: %file', array('%file' => $filename)), WATCHDOG_WARNING);
|
| 136 |
}
|
| 137 |
}
|
| 138 |
|
| 139 |
// If a URL alias is defined, create that as a symlink to the actual file
|
| 140 |
if ($alias != $path) {
|
| 141 |
$symlink = boost_file_path($alias);
|
| 142 |
_boost_mkdir_p(dirname($symlink));
|
| 143 |
if (!is_link($symlink) || realpath(readlink($symlink)) != realpath($filename)) {
|
| 144 |
if (file_exists($symlink))
|
| 145 |
@unlink($symlink);
|
| 146 |
if (!_boost_symlink($filename, $symlink)) {
|
| 147 |
watchdog('boost', t('Unable to create symlink: %link to %target', array('%link' => $symlink, '%target' => $filename)), WATCHDOG_WARNING);
|
| 148 |
}
|
| 149 |
}
|
| 150 |
}
|
| 151 |
|
| 152 |
return TRUE;
|
| 153 |
}
|
| 154 |
|
| 155 |
/**
|
| 156 |
* Returns the full directory path to the static file cache directory.
|
| 157 |
*/
|
| 158 |
function boost_cache_directory($user_id = 0, $host = NULL) {
|
| 159 |
global $user, $base_url;
|
| 160 |
$user_id = 0; //(!is_null($user_id) ? $user_id : BOOST_USER_ID);
|
| 161 |
$parts = parse_url($base_url);
|
| 162 |
$host = (!empty($host) ? $host : $parts['host']);
|
| 163 |
|
| 164 |
// FIXME: correctly handle Drupal subdirectory installations.
|
| 165 |
return implode('/', array(getcwd(), BOOST_FILE_PATH, $host, $user_id));
|
| 166 |
}
|
| 167 |
|
| 168 |
/**
|
| 169 |
* Returns the static file path for a Drupal page.
|
| 170 |
*/
|
| 171 |
function boost_file_path($path) {
|
| 172 |
if ($path == BOOST_FRONTPAGE)
|
| 173 |
$path = 'index'; // special handling for Drupal front page
|
| 174 |
return implode('/', array(boost_cache_directory(), $path)) . BOOST_FILE_EXTENSION;
|
| 175 |
}
|
| 176 |
|
| 177 |
/**
|
| 178 |
* Returns the age of a cached file, measured in seconds since it was last
|
| 179 |
* updated.
|
| 180 |
*/
|
| 181 |
function boost_file_get_age($filename) {
|
| 182 |
return time() - filemtime($filename);
|
| 183 |
}
|
| 184 |
|
| 185 |
/**
|
| 186 |
* Determines whether a cached file has expired, i.e. whether its age exceeds
|
| 187 |
* the maximum cache lifetime as defined by Drupal's system settings.
|
| 188 |
*/
|
| 189 |
function boost_file_is_expired($filename) {
|
| 190 |
if (is_link($filename))
|
| 191 |
return FALSE;
|
| 192 |
return boost_file_get_age($filename) > variable_get('cache_lifetime', 600);
|
| 193 |
}
|
| 194 |
|
| 195 |
//////////////////////////////////////////////////////////////////////////////
|