Skip to content

Instantly share code, notes, and snippets.

@jonhattan
Created September 29, 2015 18:46
Show Gist options
  • Save jonhattan/d533dd5a916485d8a500 to your computer and use it in GitHub Desktop.
Save jonhattan/d533dd5a916485d8a500 to your computer and use it in GitHub Desktop.
Drush command to inspect cache config of blocks, views and panels
<?php
function sb_surgery_drush_command() {
$items = array();
$items['cache-status'] = array(
'description' => 'Show cache status for each block, view, panel or minipanel',
);
return $items;
}
function drush_sb_surgery_cache_status() {
$options = array();
if (module_exists('block')) {
$options['blocks'] = 'Blocks';
}
if (module_exists('views')) {
$options['views'] = 'Views';
}
if (module_exists('panels')) {
$options['panels'] = 'Panels';
}
if (module_exists('panels_mini')) {
$options['panels_mini'] = 'Mini panels';
}
$choice = drush_choice($options, 'Choose component to explore');
$f = 'sb_surgery_cache_status_' . $choice;
$rows = $f();
drush_print_table($rows, TRUE);
}
function sb_surgery_cache_status_blocks() {
$rows = array();
$rows[] = array('Region', 'Block name', 'Cache settings');
// Load all blocks in the default theme.
global $theme_key;
$theme_key = variable_get('theme_default');
$result = _block_load_blocks();
foreach ($result as $region => $blocks) {
foreach ($blocks as $name => $block) {
$cache = $block->cache;
$rows[] = array($region, $name, $cache);
}
}
return $rows;
}
function sb_surgery_cache_status_views() {
$rows = array();
$rows[] = array('View', 'Display', 'Cache type', 'Cache settings');
$views = views_get_all_views();
foreach ($views as $view_name => $view) {
foreach ($view->display as $display_name => $display) {
$cache = $display->display_options['cache'];
$type = ($cache['type'] == '') ? 'none' : $cache['type'];
switch ($type) {
case 'time':
$settings = format_interval($cache['results_lifespan'], 1) . '/' . format_interval($cache['output_lifespan'], 1);
break;
case 'php':
default:
$settings = '';
}
$rows[] = array($view_name, $display_name, $type, $settings);
}
}
return $rows;
}
function sb_surgery_cache_status_panels() {
$rows = array();
$rows[] = array('Panel', 'Pane', 'Cache method', 'Cache settings');
ctools_include('page_manager.admin', 'ctools', 'page_manager');
$tasks = page_manager_get_tasks_by_type('page');
$pages = array('operations' => array());
page_manager_get_pages($tasks, $pages);
$names = array();
foreach ($pages['tasks'] as $page) {
if ($pages['disabled'][$page]) continue;
$names[] = $page;
}
ctools_include('export');
$result = ctools_export_load_object('page_manager_pages', 'names', $names);
print_r($result);
return $rows;
}
function sb_surgery_cache_status_panels_mini() {
$rows = array();
$rows[] = array('Mini panel', 'Pane', 'Cache method', 'Cache settings');
$items = panels_mini_load_all();
foreach ($items as $name => $mini) {
list($method, $settings) = _sb_surgery_cache_status_panels_mini_parse_cache($mini->display->cache);
$rows[] = array($name, 'Global', $method, $settings);
foreach ($mini->display->content as $pane) {
$pane_name = ' ' . $pane->type . ' ' . $pane->subtype;
list($method, $settings) = _sb_surgery_cache_status_panels_mini_parse_cache($pane->cache);
$rows[] = array($name, $pane_name, $method, $settings);
}
}
return $rows;
}
function _sb_surgery_cache_status_panels_mini_parse_cache($cache) {
$method = count($cache) ? $cache['method'] : 'none';
switch ($method) {
case 'simple':
$settings = dt('Lifetime: @lifetime | Granularity: @granularity', array('@lifetime' => format_interval($cache['settings']['lifetime'], 1), '@granularity' => $cache['settings']['granularity']));
break;
default:
$settings = '';
}
return array($method, $settings);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment