You are here

function boost_block in Boost 6

Implementation of hook_block().

File

./boost.module, line 2069
Provides static file caching for Drupal text output. Pages, Feeds, ect...

Code

function boost_block($op = 'list', $delta = 0, $edit = array()) {
  global $user;
  switch ($op) {
    case 'list':
      return array(
        'status' => array(
          'info' => t('Boost: Pages cache status'),
          'weight' => 10,
          'cache' => BLOCK_NO_CACHE,
        ),
        'config' => array(
          'info' => t('Boost: Pages cache configuration'),
          'weight' => 10,
          'cache' => BLOCK_NO_CACHE,
        ),
        'stats' => array(
          'info' => t('Boost: AJAX core statistics'),
          'weight' => 10,
          'cache' => BLOCK_NO_CACHE,
        ),
        'relationship' => array(
          'info' => t('Boost: Expiration Relationship'),
          'weight' => 10,
          'cache' => BLOCK_NO_CACHE,
        ),
      );
      break;
    case 'configure':
      if ($delta == 'stats') {
        $form['boost_block_show_stats'] = array(
          '#type' => 'checkbox',
          '#title' => t('Display Statistics.'),
          '#default_value' => variable_get('boost_block_show_stats', FALSE),
          '#description' => t('If false, uses Javascript to hide the block via "parent().parent().hide()".'),
        );
        $form['boost_block_cache_stats_block'] = array(
          '#type' => 'checkbox',
          '#title' => t('Cache Statistics Block'),
          '#default_value' => variable_get('boost_block_cache_stats_block', FALSE),
        );
        return $form;
      }
      break;
    case 'save':
      if ($delta == 'stats') {
        variable_set('boost_block_show_stats', $edit['boost_block_show_stats']);
        variable_set('boost_block_cache_stats_block', $edit['boost_block_cache_stats_block']);
      }
      break;
    case 'view':
      $block = array();
      switch ($delta) {
        case 'relationship':

          // Don't show the block to anonymous users, nor on any pages that
          // aren't even cacheable to begin with (e.g. admin/*).
          if (!empty($user->uid) && boost_is_cacheable($GLOBALS['_boost_path'])) {
            $output = '';
            $router_item = _boost_get_menu_router();
            if ($router_item['page_callback'] == 'node') {
              $node = node_load(arg(1));
              $data = boost_expire_node($node, $node->nid, TRUE);
              if (BOOST_FLUSH_VIEWS_INSERT && module_exists('views')) {
                $data['view'] = _boost_views_runit(TRUE, $node->nid);
              }
              $output .= str_replace('    ', '    ', nl2br(htmlentities(print_r($data, TRUE))));
            }
            $block['subject'] = '';
            $block['content'] = $output;
          }
          break;
        case 'status':

          // Don't show the block to anonymous users, nor on any pages that
          // aren't even cacheable to begin with (e.g. admin/*).
          if (!empty($user->uid) && boost_is_cacheable($GLOBALS['_boost_path'])) {
            $output = '';

            // Has anything on the site changed?
            if (BOOST_CHECK_BEFORE_CRON_EXPIRE) {
              $output .= t('Site Has Changed: %old<br />', array(
                '%old' => boost_has_site_changed() ? 'True' : 'False',
              ));
            }

            // Display Cached File Info
            $filename = boost_file_path($GLOBALS['_boost_path']);
            $ttl = boost_db_get_ttl($filename);
            $generate = boost_get_generation_time($filename);
            if ($generate !== FALSE) {
              if (boost_is_cached($GLOBALS['_boost_path'])) {
                $output .= $filename . ' <br />';
              }
              else {
                $output .= $filename . '<br />Does not exist. <br />';
              }
              if ($ttl < 0) {
                $output .= t('<strong>Expired: %interval ago</strong><br />', array(
                  '%interval' => format_interval(abs($ttl)),
                ));
              }
              else {
                $output .= t('Expire In: %interval<br />', array(
                  '%interval' => format_interval(abs($ttl)),
                ));
              }
              $tim = boost_db_get_cache_age($filename);
              $output .= t('Cache Age: %time<br />', array(
                '%time' => format_interval($tim),
              ));
              $output .= t('Cache Generated: %time seconds<br />', array(
                '%time' => round($generate, 2),
              )) . ' ';
              if (user_access('administer site configuration')) {
                $output .= drupal_get_form('boost_block_flush_form');
              }
              $output .= '<br />';
            }
            else {
              $output .= t('This page is being served <strong>live</strong> to anonymous visitors, as it is not currently in the static page cache.');
            }

            // Display any info about errors on this page
            $error = _boost_page_have_error();
            $msg_count = empty($GLOBALS['_boost_message_count']) ? 0 : $GLOBALS['_boost_message_count'];
            $drupal_msg = max(count(drupal_get_messages(NULL, FALSE)), $msg_count);
            if ($error || BOOST_HALT_ON_MESSAGES && $drupal_msg != 0) {
              $output .= t('There are <strong>php errors</strong> or <strong>Drupal messages</strong> on this page, preventing boost from caching.') . ' ';
              if ($error) {
                $output .= t('ERROR: <pre>%error</pre> !link <br /> !performance', array(
                  '%error' => boost_print_r($error, TRUE),
                  '!link' => l(t('Lookup Error Type'), 'http://php.net/errorfunc.constants'),
                  '!performance' => l(t('Turn Off Error Checking'), 'admin/settings/performance/boost'),
                ));
              }
              if (BOOST_HALT_ON_MESSAGES && $drupal_msg != 0) {
                $output .= t('MESSAGES: %msg <br /> !performance', array(
                  '%msg' => $drupal_msg,
                  '!performance' => l(t('Turn Off Error Checking'), 'admin/settings/performance/boost'),
                ));
              }
            }
            $block['subject'] = '';
            $block['content'] = theme('boost_cache_status', isset($ttl) ? $ttl : -1, $output);
          }
          break;
        case 'config':

          // Don't show the block to anonymous users, nor on any pages that
          // aren't even cacheable to begin with (e.g. admin/*).
          if (user_access('administer site configuration') && boost_is_cacheable($GLOBALS['_boost_path']) && !BOOST_NO_DATABASE) {
            $block['subject'] = '';
            $block['content'] = theme('boost_cache_status', -1, drupal_get_form('boost_block_db_settings_form'));
          }
          break;
        case 'stats':
          $filename = 'boost_stats.php';
          $block = module_invoke('statistics', 'block', 'view', 0);
          variable_set('boost_statistics_html', $block['content']);
          if (!(strpos($_SERVER['SCRIPT_FILENAME'], 'index.php') === FALSE || variable_get('site_offline', 0) || $_SERVER['REQUEST_METHOD'] != 'GET' && $_SERVER['REQUEST_METHOD'] != 'HEAD' || $_SERVER['SERVER_SOFTWARE'] === 'PHP CLI' || !variable_get('boost_enabled', CACHE_NORMAL) || isset($_GET['nocache']) || !boost_is_cacheable($GLOBALS['_boost_path']) || !empty($user->uid) || !module_exists('statistics'))) {
            $block = array();
            $block['subject'] = t('Popular content');
            $block['content'] = '<div id="boost-stats"></div>' . boost_stats_generate($filename);
          }
          elseif (!variable_get('boost_block_show_stats', FALSE)) {
            $block['content'] .= '<div id="boost-stats"></div>';
            drupal_add_js('$("#boost-stats").parent().parent().hide();', 'inline', 'footer');
          }
          break;
      }
      return $block;
  }
}