Skip to content
Snippets Groups Projects
Commit f427f87f authored by Earl Miles's avatar Earl Miles
Browse files

Improvements to the css cache so Panels can do more.

parent 09811e62
No related branches found
No related tags found
No related merge requests found
...@@ -38,11 +38,43 @@ function ctools_schema_2() { ...@@ -38,11 +38,43 @@ function ctools_schema_2() {
// update the 'name' field to be 128 bytes long: // update the 'name' field to be 128 bytes long:
$schema['ctools_object_cache']['fields']['name']['length'] = 128; $schema['ctools_object_cache']['fields']['name']['length'] = 128;
// DO NOT MODIFY THIS TABLE -- this definition is used to create the table.
// Changes to this table must be made in schema_3 or higher.
$schema['ctools_css_cache'] = array(
'description' => t('A special cache used to store CSS that must be non-volatile.'),
'fields' => array(
'cid' => array(
'type' => 'varchar',
'length' => '128',
'description' => t('The CSS ID this cache object belongs to.'),
),
'filename' => array(
'type' => 'varchar',
'length' => '255',
'description' => t('The filename this CSS is stored in.'),
),
'css' => array(
'type' => 'text',
'size' => 'big',
'description' => t('CSS being stored.'),
'serialize' => TRUE,
),
'filter' => array(
'type' => 'int',
'size' => 'tiny',
'description' => t('Whether or not this CSS needs to be filtered.'),
),
),
'primary key' => array('cid'),
);
return $schema; return $schema;
} }
/** /**
* ctools' initial schema; separated for the purposes of updates. * CTools' initial schema; separated for the purposes of updates.
*
* DO NOT MAKE CHANGES HERE. This schema version is locked.
*/ */
function ctools_schema_1() { function ctools_schema_1() {
$schema['ctools_object_cache'] = array( $schema['ctools_object_cache'] = array(
...@@ -71,7 +103,7 @@ function ctools_schema_1() { ...@@ -71,7 +103,7 @@ function ctools_schema_1() {
'description' => t('The time this cache was created or updated.'), 'description' => t('The time this cache was created or updated.'),
), ),
'data' => array( 'data' => array(
'type' => 'blob', 'type' => 'text',
'size' => 'big', 'size' => 'big',
'description' => t('Serialized data being stored.'), 'description' => t('Serialized data being stored.'),
'serialize' => TRUE, 'serialize' => TRUE,
...@@ -99,3 +131,16 @@ function ctools_update_6001() { ...@@ -99,3 +131,16 @@ function ctools_update_6001() {
return $ret; return $ret;
} }
/**
* Add the new css cache table.
*/
function ctools_update_6002() {
$ret = array();
// Schema 2 is locked and should not be changed.
$schema = ctools_schema_2();
db_create_table($ret, 'ctools_css_cache', $schema['ctools_css_cache']);
return $ret;
}
...@@ -31,8 +31,85 @@ ...@@ -31,8 +31,85 @@
* a backup method of re-generating the CSS cache in case it is removed, so * a backup method of re-generating the CSS cache in case it is removed, so
* that it is easy to force a re-cache by simply deleting the contents of the * that it is easy to force a re-cache by simply deleting the contents of the
* directory. * directory.
*
* Finally, if for some reason your application cannot store the filename
* (which is true of Panels where the style can't force the display to
* resave unconditionally) you can use the ctools storage mechanism. You
* simply have to come up with a unique Id:
*
* @code
* $filename = ctools_css_store($id, $css, TRUE);
* @endcode
*
* Then later on:
* @code
* $filename = ctools_css_retrieve($id);
* drupal_add_css($filename);
* @endcode
*
* The CSS that was generated will be stored in the database, so even if the
* file was removed the cached CSS will be used. It is your module's
* responsibility to know when this CSS is outdated and needs to be
* changed. This storage is non-volatile and will never be removed unless
* you remove it:
*
* @code
* ctools_css_clear($id);
* @endcode
*/ */
/**
* Store CSS with a given id and return the filename to use.
*
* This function associates a piece of CSS with an id, and stores the
* cached filename and the actual CSS for later use with
* ctools_css_retrieve.
*/
function ctools_css_store($id, $css, $filter = TRUE) {
$filename = db_result(db_query("SELECT filename FROM {ctools_css_cache} WHERE cid = '%s'", $id));
if ($filename) {
file_delete($filename);
}
// Remove any previous records.
db_query("DELETE FROM {ctools_css_cache} WHERE cid = '%s'", $id);
$filename = ctools_css_cache($css, $filter);
db_query("INSERT INTO {ctools_css_cache} (cid, filename, css, filter) VALUES ('%s', '%s', '%s', %d)", $id, $filename, $css, $filter);
return $filename;
}
/**
* Retrieve a filename associated with an id of previously cached CSS.
*
* This will ensure the file still exists and, if not, create it.
*/
function ctools_css_retrieve($id) {
$cache = db_fetch_object(db_query("SELECT * FROM {ctools_css_cache} WHERE cid = '%s'", $id));
if (!$cache) {
return;
}
if (!file_exists($cache->filename)) {
$filename = ctools_css_cache($cache->css, $cache->filter);
if ($filename != $cache->filename) {
db_query("UPDATE {ctools_css_cache} SET filename = '%s' WHERE cid = '%s'", $filename, $id);
$cache->filename = $filename;
}
}
return $cache->filename;
}
/**
* Remove stored CSS and any associated file.
*/
function ctools_css_clear($id) {
db_query("DELETE FROM {ctools_css_cache} WHERE cid = '%s'", $id);
}
/** /**
* Write a chunk of CSS to a temporary cache file and return the file name. * Write a chunk of CSS to a temporary cache file and return the file name.
* *
...@@ -50,6 +127,9 @@ ...@@ -50,6 +127,9 @@
* @param $filter * @param $filter
* If TRUE the css will be filtered. If FALSE the text will be cached * If TRUE the css will be filtered. If FALSE the text will be cached
* as-is. * as-is.
*
* @return $filename
* The filename the CSS will be cached in.
*/ */
function ctools_css_cache($css, $filter = TRUE) { function ctools_css_cache($css, $filter = TRUE) {
if ($filter) { if ($filter) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment