cache_actions.rules.inc in Cache Actions 7
Same filename and directory in other branches
This file provides the rules integration for this module.
File
cache_actions.rules.incView source
<?php
/**
* @file
* This file provides the rules integration for this module.
*/
/**
* Implementation of hook_rules_action_info().
*/
function cache_actions_rules_action_info() {
// Actions that works for everyone
$actions = array(
'cache_actions_action_clear_cache' => array(
'label' => t('Clear cache bins'),
'parameter' => array(
'tables' => array(
'type' => 'list<text>',
'label' => t('Cache Bins'),
'save' => TRUE,
'options list' => '_cache_actions_get_cache_bins',
'restriction' => 'input',
),
),
'group' => t('Cache Actions'),
'base' => 'cache_actions_action_clear_cache',
),
'cache_actions_action_clear_cache_cid' => array(
'label' => t('Clear a specific cache cid'),
'parameter' => array(
'bin' => array(
'type' => 'text',
'label' => t('Cache bin'),
'description' => t('The cache table where the cid is'),
'options list' => '_cache_actions_get_cache_bins',
'restriction' => 'input',
'save' => TRUE,
),
'cid' => array(
'type' => 'text',
'label' => t('Cache key'),
'description' => t('The key to clear'),
'save' => TRUE,
),
'wildcard' => array(
'type' => 'boolean',
'label' => t('Use wildcard'),
'description' => t('Use wildcards. This means you will be clearing all cache keys that partially matches.'),
'restriction' => 'input',
'save' => TRUE,
),
),
'group' => t('Cache Actions'),
'base' => 'cache_actions_action_clear_cache_cid',
),
'cache_actions_action_clear_css_js_cache' => array(
'label' => t('Clear the css and js cache'),
'group' => t('Cache Actions'),
'base' => 'cache_actions_action_clear_css_js_cache',
),
);
// If the Views module is available, then we can clear the cache of individual views.
if (module_exists('views')) {
$actions['cache_actions_action_clear_views_cache'] = array(
'label' => t('Clear the cache for specific views'),
'parameter' => array(
'view' => array(
'type' => 'list<text>',
'label' => t('Views'),
'description' => t('The cache of the selected views will be cleared.'),
'options list' => '_cache_actions_get_views_list',
'save' => TRUE,
'restriction' => 'input',
),
),
'group' => t('Cache Actions'),
'base' => 'cache_actions_action_clear_views_cache',
);
$actions['cache_actions_action_clear_views_display_cache'] = array(
'label' => t('Clear the cache for views displays'),
'parameter' => array(
'displays' => array(
'type' => 'list<text>',
'label' => t('Displays'),
'description' => t('The cache of the selected displays will be cleared'),
'options list' => '_cache_actions_get_views_displays',
'restriction' => 'input',
'save' => TRUE,
),
),
'group' => t('Cache Actions'),
'base' => 'cache_actions_action_clear_views_display_cache',
);
}
// If the page manager module is available, then we can clear panels stuff.
if (module_exists('page_manager')) {
$actions['cache_actions_action_clear_panels_page_cache'] = array(
'label' => t('Clear the cache for panel page variants'),
'parameter' => array(
'panel' => array(
'type' => 'list<text>',
'label' => t('Panel Variants'),
'description' => t('The panel variant to invalidate.'),
),
'options' => '_cache_actions_get_panels_handlers',
'restriction' => 'input',
'save' => TRUE,
),
'group' => t('Cache Actions'),
'base' => 'cache_actions_action_clear_panels_page_cache',
);
$actions['cache_actions_action_clear_panels_pane_cache'] = array(
'label' => t('Clear the cache for panel page panes'),
'parameter' => array(
'panel' => array(
'type' => 'text',
'label' => t('Panel Page Variant'),
'description' => t('Specify the panel page you want to clear.'),
'options list' => '_cache_actions_get_page_variants',
'restriction' => 'input',
'save' => TRUE,
),
'panes' => array(
'type' => 'list<text>',
'label' => t('Panes'),
'description' => t('Specify the panes to invalidate here.'),
'options list' => '_cache_actions_get_panes',
'restriction' => 'input',
'save' => TRUE,
),
),
'group' => t('Cache Actions'),
'base' => 'cache_actions_action_clear_panels_pane_cache',
);
}
if (module_exists('panels_mini')) {
$actions['cache_actions_action_clear_panels_mini_cache'] = array(
'label' => t('Clear the cache for specific panes'),
'parameter' => array(
'panel' => array(
'type' => 'list<text>',
'label' => t('Mini Panel'),
'description' => t('Specify the mini panel variants you want to invalidate.'),
'options list' => '_cache_actions_get_mini_panels',
'save' => TRUE,
'restriction' => 'input',
),
),
'group' => t('Cache Actions'),
'base' => 'cache_actions_action_clear_panels_mini_cache',
);
$actions['cache_actions_action_clear_panels_mini_pane_cache'] = array(
'label' => t('Clear the cache for mini panel panes'),
'parameter' => array(
'panel' => array(
'type' => 'text',
'label' => t('Panel page'),
'description' => t('Specify the mini panel you want to clear.'),
'options list' => '_cache_actions_get_mini_panels',
'save' => TRUE,
'restriction' => 'input',
),
'panes' => array(
'type' => 'list<text>',
'label' => t('Panel panes'),
'options list' => '_cache_actions_get_mini_panel_panes',
'description' => t('Specify the mini panel panes you want to invalidate.'),
'save' => TRUE,
'restriction' => 'input',
),
),
'group' => t('Cache Actions'),
'base' => 'cache_actions_action_clear_panels_mini_pane_cache',
);
}
/*
// If varnish module is available, then we can clear specific varnish pages.
if (module_exists('varnish')) {
$actions['cache_actions_action_clear_varnish_page'] = array(
'label' => t('Clear paths cached in varnish'),
'arguments' => array(
'paths' => array('type' => 'string', 'label' => t('Varnish paths'), 'description' => t('The cached paths')),
),
'module' => 'Cache Actions',
);
$actions['cache_actions_action_clear_varnish_cache'] = array(
'label' => t('Clear all paths cached in varnish for this site'),
'module' => 'Cache Actions',
);
}
*/
return $actions;
}
/**
* Get all existing handlers from page manager. This is the ugliest function
* in the whole module =)
* @param bool $panes set this to true if you want the function to return
* an array consisting of the panels and the panes.
* @return the handlers.
*/
function _cache_actions_get_panels_handlers($panes = FALSE) {
$available_handlers = array();
$available_panes = array();
$js_structure = array();
// First, get all tasks. This corresponds to all types of page manager pages
// that can be used, for for instance pages, node view, node edit...
$tasks = page_manager_get_tasks();
foreach ($tasks as $task) {
// Subtasks are tasks that are created under the primary tasks, for instance
// a custom page the user has created.
$subtasks = page_manager_get_task_subtasks($task);
// If we have subtasks, then that's what we're after.
if (count($subtasks)) {
foreach ($subtasks as $subtask) {
// Subtasks have handlers. These can for instance correspond to a panel variant.
$handlers = page_manager_load_task_handlers($task, $subtask['name']);
foreach ($handlers as $handler) {
// handlers have plugins, in this case we need to get the plugin for
// this handler.
$plugin = page_manager_get_task_handler($handler->handler);
$title = page_manager_get_handler_title($plugin, $handler, $task, $subtask['name']);
$available_handlers[$subtask['admin title']][$handler->name . ':' . $handler->task . ':' . $handler->subtask] = $title;
// If we want to fetch the panes as well, then go ahead.
if ($panes) {
list($handler_panes, $pane_js) = _cache_actions_load_panes($handler, $title, TRUE);
$available_panes += $handler_panes;
$js_structure += $pane_js;
}
}
}
}
else {
$handlers = page_manager_load_task_handlers($task);
if (count($handlers)) {
foreach ($handlers as $handler) {
$plugin = page_manager_get_task_handler($handler->handler);
$title = page_manager_get_handler_title($plugin, $handler, $task, $subtask['name']);
// If not, then we have an in-code display. Save off the name, so we can get it.
$available_handlers[$task['admin title']][$handler->name . ':' . $handler->task] = $title;
if ($panes) {
list($handler_panes, $pane_js) = _cache_actions_load_panes($handler, $title, TRUE);
$available_panes += $handler_panes;
$js_structure += $pane_js;
}
}
}
}
}
// Return an array if we want the panes.
if ($panes) {
return array(
'handlers' => $available_handlers,
'panes' => $available_panes,
'js' => $js_structure,
);
}
// Otherwise just return the handlers.
return $available_handlers;
}
function _cache_actions_get_page_variants() {
$data = _cache_actions_get_panels_handlers(FALSE);
return $data;
}
/**
* Get Panel Panes.
*/
function _cache_actions_get_panes() {
$data = _cache_actions_get_panels_handlers(TRUE);
// Add javascript for handling the ui controls in the settings form.
drupal_add_js($data['js'], 'setting');
drupal_add_js(drupal_get_path('module', 'cache_actions') . '/js/cache_actions.js');
return $data['panes'];
}
function _cache_actions_get_mini_panels() {
ctools_include('plugins', 'panels');
$available_panels = array();
$mini_panels = panels_mini_load_all();
foreach ($mini_panels as $mini_panel) {
$available_panels[$mini_panel->name] = $mini_panel->name;
}
return $available_panels;
}
function _cache_actions_get_mini_panel_panes() {
ctools_include('plugins', 'panels');
$available_panes = array();
$mini_panels = panels_mini_load_all();
foreach ($mini_panels as $mini_panel) {
foreach ($mini_panel->display->content as $pane) {
// The panes must have rule-based caching on in order for invalidation
// to work properly.
if ($pane->cache['method'] == 'rules') {
$pane_title = panels_get_pane_title($pane);
$available_panes[$mini_panel->name][$pane->pid] = $pane_title;
}
}
}
return $available_panes;
}
function _cache_actions_load_panes($handler, $handler_title, $js = FALSE) {
ctools_include('plugins', 'panels');
$available_panes = array();
$js_panes = array();
if (isset($handler->did)) {
$display = panels_load_display($handler->did);
}
else {
$display = $handler->conf['display'];
}
if (isset($display->content)) {
foreach ($display->content as $pane) {
if ($pane->cache['method'] == 'rules') {
// We can't satisfy a context here, so we only get the title if no context is set.
if (empty($pane->configuration['context'])) {
$pane_title = panels_get_pane_title($pane);
}
else {
$pane_title = $pane->subtype;
}
$available_panes[$handler_title][$pane->pid] = $pane_title;
if (isset($handler->did)) {
$js_panes[$handler->did][$pane->pid] = $pane_title;
}
else {
$js_panes[$handler->name . ':' . $handler->task . (!empty($handler->subtask) ? ':' . $handler->subtask : '')][$pane->pid] = $pane_title;
}
}
}
}
if ($js) {
return array(
$available_panes,
$js_panes,
);
}
return $available_panes;
}
/*
function cache_actions_action_clear_panels_pane_cache_form($settings, &$form) {
if (module_exists('panels') && module_exists('page_manager')) {
drupal_add_js(drupal_get_path('module', 'cache_actions') . '/js/cache_actions.js');
list($available_handlers, $available_panes, $js_structure) = _cache_actions_get_panels_handlers(TRUE);
$js_settings['variants'] = $js_structure;
drupal_add_js($js_settings, 'setting');
$form['settings']['panel'] = array(
'#type' => 'select',
'#title' => t('Panel Variants'),
'#options' => $available_handlers,
'#default_value' => $settings['panel'],
'#description' => t('The panel page to be cleared.
Only panel pages which have caching enabled will be shown here.'),
);
$form['settings']['panes'] = array(
'#type' => 'select',
'#multiple' => TRUE,
'#title' => t('Panel Panes'),
'#options' => $available_panes,
'#default_value' => $settings['panes'],
'#description' => t('The panel page to be cleared.
Only panel pages which have caching enabled will be shown here.'),
);
// Let's make sure the action can't be added if there are no applicable panel pages.
if (!count($available_handlers)) {
form_set_error('panel', t('This action is not applicable, since there are no panel pages with caching enabled.'));
$form['submit']['#disabled'] = TRUE;
}
}
}
*/
function cache_actions_action_clear_varnish_page_form($settings, &$form) {
$form['settings']['paths'] = array(
'#type' => 'textarea',
'#title' => t('Paths'),
'#default_value' => $settings['paths'],
'#description' => t('Enter one page per line as Drupal paths,
<front> is the front page. You can use regular expressions as understood
by varnish on every line.'),
);
}
/**
* Clear the cache of the specified panel page.
* @param int $did the display id
*/
function cache_actions_action_clear_panels_page_cache($handlers) {
if (module_exists('page_manager') && module_exists('panels')) {
// If this is an array, then this is one of the new rules.
if (is_array($handlers)) {
ctools_include('plugins', 'panels');
foreach ($handlers as $handler) {
if (is_numeric($handler)) {
$display = panels_load_display($handler);
}
else {
$display = _cache_actions_get_display_from_key($handler);
}
if (is_object($display)) {
panels_clear_cached_content($display);
}
else {
watchdog('cache_actions', "The panel variant %panel doesn't exist.\n You need to look over your rules and see if there's any rules\n involving that panel.", array(
'%panel' => $handler,
), WATCHDOG_ERROR);
}
}
}
else {
$display = panels_load_display($handlers);
if (is_object($display)) {
panels_clear_cached_content($display);
}
else {
watchdog('cache_actions', "The panel variant %panel doesn't exist.\n You need to look over your rules and see if there's any rules\n involving that panel.", array(
'%panel' => $handler,
), WATCHDOG_ERROR);
}
}
}
}
/**
* Get a display out of a key specified in the UI.
* @param string $key the key
* @return panels_display a display.
*/
function _cache_actions_get_display_from_key($key) {
list($handler, $task, $subtask) = explode(':', $key);
$task = page_manager_get_task($task);
$handler = page_manager_load_task_handler($task, $subtask, $handler);
// In-code handlers have the displays attached to them.
if (isset($handler->conf['display'])) {
$display = $handler->conf['display'];
}
elseif (isset($handler->conf['did'])) {
$display = panels_load_display($handler->conf['did']);
}
_cache_actions_get_cache_key($handler, $display);
return $display;
}
/**
* Clear the cache of panes in a mini panel.
* @param string $handler the handler
* @param array $panes the panes to clear.
*/
function cache_actions_action_clear_panels_pane_cache($handler, $panes) {
if (module_exists('page_manager')) {
ctools_include('plugins', 'panels');
if (!is_numeric($handler)) {
$display = _cache_actions_get_display_from_key($handler);
}
else {
$display = panels_load_display($handler);
}
if (is_array($panes) && isset($display->content)) {
foreach ($panes as $pane_id) {
foreach ($display->content as $pane) {
if ($pane->pid == $pane_id && !empty($pane->cache['method'])) {
_cache_actions_clear_pane_cache($pane, $display);
}
}
}
}
}
}
/**
* Create a unique cache key in the same way as panels does for it's
* internal cache. This is a sure way to get unique cache ids for both
* in-code displays and database displays.
* @param $handler
* @param $display
*/
function _cache_actions_get_cache_key($handler, &$display) {
$task_name = page_manager_make_task_name($handler->task, $handler->subtask);
$display->cache_key = 'panel_context:' . $task_name . ':' . $handler->name;
}
/**
* Clear the content of a pane.
* @param $pane the pane to clear
* @param $display the display it belongs to.
*/
function _cache_actions_clear_pane_cache($pane, $display) {
// Clearing panes can't be used with any other plugin than the rules plugin.
if ($pane->cache['method'] == 'rules') {
$function = panels_plugin_get_function('cache', $pane->cache['method'], 'cache clear');
// Specify the pane to clear. This is specific for the rules plugin.
$display->clear_pane = $pane;
if ($function) {
$function($display);
}
}
}
/**
* Clear the cache of the specified mini panel.
* @param array $mini_panels the name of the mini panels.
*/
function cache_actions_action_clear_panels_mini_cache($mini_panels) {
if (module_exists('panels_mini')) {
ctools_include('plugins', 'panels');
foreach ($mini_panels as $name) {
$panel = panels_mini_load($name);
// Don't load a display if we already have it.
if (isset($panel->display)) {
$display = $panel->display;
}
else {
$display = panels_load_display($panel->did);
}
if (is_object($display)) {
panels_clear_cached_content($display);
}
else {
watchdog('cache_actions', "The mini panel variant %panel doesn't exist.\n You need to look over your rules and see if there's any rules\n involving that panel.", array(
'%panel' => $display,
), WATCHDOG_ERROR);
}
}
}
}
function cache_actions_action_clear_panels_mini_pane_cache($mini_panel, $panes) {
if (module_exists('panels_mini')) {
ctools_include('plugins', 'panels');
$mini = panels_mini_load($mini_panel);
if (is_object($mini)) {
// We need to add the owner name, since this is included when it is cached.
$mini->display->owner->id = $mini->name;
foreach ($panes as $pid) {
foreach ($mini->display->content as $pane) {
if ($pane->pid == $pid) {
_cache_actions_clear_pane_cache($pane, $mini->display);
}
}
}
}
else {
watchdog('cache_actions', "The mini panel %panel doesn't exist.\n You need to look over your rules and see if there's any rules\n involving that panel.", array(
'%panel' => $mini_panel,
), WATCHDOG_ERROR);
}
}
}
function cache_actions_action_clear_varnish_page($paths) {
if (module_exists('varnish')) {
$paths = explode("\r\n", $paths);
// Since varnish don't understand things like <front>, we need to
// Replace that with something it does understand. If <front> exists more than
// once, we ignore it, since the cache will be cleared anyway.
if (($key = array_search('<front>', $paths)) !== FALSE) {
$paths[$key] = '';
}
varnish_expire_cache($paths);
}
}
/**
* Clear out the varnish cache completely.
*/
function cache_actions_action_clear_varnish_cache() {
if (module_exists('varnish')) {
varnish_flush_caches();
}
}
/**
* Get a a keyed array of views with machine name as key and human readable name
* as value.
* @return array an array of views names
*/
function _cache_actions_get_views_list() {
$views = views_get_all_views();
$views_names = array();
foreach ($views as $view) {
$views_names[$view->name] = $view->name;
}
return $views_names;
}
/**
* Get all views and their displays. This is a callback
* for an options list.
*/
function _cache_actions_get_views_displays() {
$views = views_get_all_views();
$displays = array();
foreach ($views as $view) {
$displays[$view->name] = array();
foreach ($view->display as $display) {
// Only list views that actually is shown.
if ($display->id != 'default') {
$displays[$view->name][$view->name . ':' . $display->id] = !empty($display->title) ? $display->title : $display->id;
}
}
}
return $displays;
}
/**
* This action clears the cache of a specific view.
* @param array $views the views to clear.
*/
function cache_actions_action_clear_views_cache($views) {
if (module_exists('views')) {
views_include_handlers();
module_load_include('inc', 'views', 'plugins/views_plugin_cache');
// Is this an array?
if (is_array($views)) {
foreach ($views as $view) {
_cache_actions_clear_view($view);
}
}
else {
// Otherwise it's probably an old rule from 1.x, let's just handle that.
_cache_actions_clear_view($views);
}
}
}
/**
* This action clears the cache of a specific view display.
* @param array $views the views to clear.
*/
function cache_actions_action_clear_views_display_cache($views) {
if (module_exists('views')) {
$loaded_views = array();
views_include_handlers();
module_load_include('inc', 'views', 'plugins/views_plugin_cache');
if (is_array($views)) {
foreach ($views as $entry) {
list($view_name, $display_id) = explode(':', $entry);
// Let´s make sure we don't load views unnecessarily.
if (!isset($loaded_views[$view_name])) {
$loaded_views[$view_name] = views_get_view($view_name);
}
// Check to see that the display has a caching plugin. If it doesn't add the default caching plugin.
$display = $loaded_views[$view_name]->display[$display_id];
if (!isset($display->display_options['cache']['type'])) {
$display->display_options['cache']['type'] = $loaded_views[$view_name]->display['default']->display_options['cache']['type'];
}
// And then clear the cache.
_cache_actions_clear_view_display($loaded_views[$view_name], $display);
}
}
}
}
/**
* Clear out a view and it's displays.
* @param string $view the view machine name
*/
function _cache_actions_clear_view($view) {
$view = views_get_view($view);
if (is_object($view)) {
// Go through all displays and clear the cache.
foreach ($view->display as $display) {
// If we don't have our own cache plugin, then we need to copy
// the cache settings from default.
if (!isset($display->display_options['cache']) && isset($view->display['default'])) {
$display->display_options['cache'] = $view->display['default']->display_options['cache'];
}
_cache_actions_clear_view_display($view, $display);
}
}
else {
watchdog('cache_actions', "The view %view doesn't exist.\n You need to look over your rules and see if there's any rules\n involving that view.", array(
'%view' => $handler,
), WATCHDOG_ERROR);
}
}
/**
* Clear out a specific display in a view.
*/
function _cache_actions_clear_view_display($view, $display) {
// We use the cache plugin to clear the cache.
$view
->set_display($display->id);
$cache_plugin = views_get_plugin('cache', $display->display_options['cache']['type']);
// If we have a cache plugin, then initiate it and flush the cache.
if (isset($cache_plugin)) {
$cache_plugin
->init($view, $display);
$cache_plugin
->cache_flush();
}
}
/**
* This action that clears all cache bins specified.
* @param $bins the bins to be cleared
*/
function cache_actions_action_clear_cache($bins) {
$cache_bins = _cache_actions_get_cache_bins();
foreach ($bins as $bin) {
if (in_array($bin, $cache_bins)) {
cache_clear_all('*', $bin, TRUE);
}
}
}
/**
* This action clears a specific cache sid in a cache bin.
* @param $table the table where the cid should reside.
* @param $cid the cache cid to clear
* @param $wildcard wether or not to use wildcard invalidation.
*/
function cache_actions_action_clear_cache_cid($table, $cid, $wildcard) {
cache_clear_all($cid, $table, $wildcard);
}
/**
* This action clears the css and js cache.
*/
function cache_actions_action_clear_css_js_cache() {
// Change query-strings on css/js files to enforce reload for all users.
_drupal_flush_css_js();
drupal_clear_js_cache();
drupal_clear_css_cache();
}
/**
* Get all cache bins available.
*/
function _cache_actions_get_cache_bins() {
$cache_bins = array(
'cache' => 'cache',
'cache_block' => 'cache_block',
'cache_filter' => 'cache_filter',
'cache_page' => 'cache_page',
);
foreach (module_invoke_all('flush_caches') as $cache_bin) {
$cache_bins[$cache_bin] = $cache_bin;
}
return $cache_bins;
}
Functions
Name | Description |
---|---|
cache_actions_action_clear_cache | This action that clears all cache bins specified. |
cache_actions_action_clear_cache_cid | This action clears a specific cache sid in a cache bin. |
cache_actions_action_clear_css_js_cache | This action clears the css and js cache. |
cache_actions_action_clear_panels_mini_cache | Clear the cache of the specified mini panel. |
cache_actions_action_clear_panels_mini_pane_cache | |
cache_actions_action_clear_panels_page_cache | Clear the cache of the specified panel page. |
cache_actions_action_clear_panels_pane_cache | Clear the cache of panes in a mini panel. |
cache_actions_action_clear_varnish_cache | Clear out the varnish cache completely. |
cache_actions_action_clear_varnish_page | |
cache_actions_action_clear_varnish_page_form | |
cache_actions_action_clear_views_cache | This action clears the cache of a specific view. |
cache_actions_action_clear_views_display_cache | This action clears the cache of a specific view display. |
cache_actions_rules_action_info | Implementation of hook_rules_action_info(). |
_cache_actions_clear_pane_cache | Clear the content of a pane. |
_cache_actions_clear_view | Clear out a view and it's displays. |
_cache_actions_clear_view_display | Clear out a specific display in a view. |
_cache_actions_get_cache_bins | Get all cache bins available. |
_cache_actions_get_cache_key | Create a unique cache key in the same way as panels does for it's internal cache. This is a sure way to get unique cache ids for both in-code displays and database displays. |
_cache_actions_get_display_from_key | Get a display out of a key specified in the UI. |
_cache_actions_get_mini_panels | |
_cache_actions_get_mini_panel_panes | |
_cache_actions_get_page_variants | |
_cache_actions_get_panels_handlers | Get all existing handlers from page manager. This is the ugliest function in the whole module =) |
_cache_actions_get_panes | Get Panel Panes. |
_cache_actions_get_views_displays | Get all views and their displays. This is a callback for an options list. |
_cache_actions_get_views_list | Get a a keyed array of views with machine name as key and human readable name as value. |
_cache_actions_load_panes |