You are here

function apdqc_stream_wrappers in Asynchronous Prefetch Database Query Cache 7

Implements hook_stream_wrappers().

File

./apdqc.module, line 1211
Asynchronous Prefetch Database Query Cache module.

Code

function apdqc_stream_wrappers() {
  if (!function_exists('apdqc_run_prefetch_array') || !function_exists('apdqc_escape_string')) {
    require_once 'apdqc.cache.inc';
    $db_type = apdqc_fast_get_db_type();
    if ($db_type === 'mysql') {
      require_once 'apdqc.mysql.inc';
    }
  }
  static $run_once;
  if (!empty($run_once)) {
    return;
  }
  $run_once = TRUE;

  // Run before menu_get_item() && hook_init but inside full bootstrap.
  $lang_shorthand = apdqc_get_lang();
  $table_keys_cache_prefetch = array();
  if (apdqc_get_bin_class_name('cache_menu') === 'APDQCache') {
    $wrappers = stream_get_wrappers();
    if (module_exists('block') && in_array('public', $wrappers)) {
      drupal_path_initialize();

      // Preload menus in the blocks.
      $system_menus = menu_list_system_menus();
      $main = variable_get('menu_main_links_source', 'main-menu');
      $secondary = variable_get('menu_secondary_links_source', 'user-menu');
      if (!array_key_exists($main, $system_menus)) {
        $system_menus[$main] = 'Main Menu';
      }
      if ($main !== $secondary) {
        $system_menus[$secondary] = 'Secondary Menu';
      }
      foreach ($system_menus as $menu_name => $title) {
        $active_path = menu_tree_get_path($menu_name);

        // Load the menu item corresponding to the current page.
        $item = menu_get_item($active_path);
        if ($item) {
          $menu_name = apdqc_escape_string($menu_name);

          // Generate a cache ID (cid) specific for this page.
          $table_keys_cache_prefetch['cache_menu'][] = 'links:' . $menu_name . ':page:' . apdqc_escape_string($item['href']) . ':' . $lang_shorthand . ':' . (int) $item['access'] . ':1';
          $table_keys_cache_prefetch['cache_menu'][] = 'links:' . $menu_name . ':page:' . apdqc_escape_string($item['href']) . ':' . $lang_shorthand . ':' . (int) $item['access'] . ':0';
          $table_keys_cache_prefetch['cache_menu'][] = 'links:' . $menu_name . ':page:' . apdqc_escape_string($item['href']) . ':' . $lang_shorthand . ':' . (int) $item['access'] . ':1:trail';
          $table_keys_cache_prefetch['cache_menu'][] = 'links:' . $menu_name . ':page:' . apdqc_escape_string($item['href']) . ':' . $lang_shorthand . ':' . (int) $item['access'] . ':0:trail';
        }
      }
    }
  }
  if (apdqc_get_bin_class_name('cache') === 'APDQCache') {
    $table_keys_cache_prefetch['cache'][] = 'schema:runtime:1';
    if (module_exists('ctools')) {
      $table_keys_cache_prefetch['cache'][] = 'ctools_plugin_type_info';
    }
    if (module_exists('context')) {
      $table_keys_cache_prefetch['cache'][] = 'plugins:context:plugins';
    }
    if (module_exists('mailsystem')) {
      $table_keys_cache_prefetch['cache'][] = 'mailsystem_get_classes';
    }
    if (module_exists('filter')) {
      $table_keys_cache_prefetch['cache'][] = 'filter_formats:' . $lang_shorthand;
    }
    if (module_exists('context')) {
      $table_keys_cache_prefetch['cache'][] = 'context';
    }
    if (module_exists('ctools')) {
      $table_keys_cache_prefetch['cache'][] = 'ctools_plugin_files:ctools:arguments';
      $table_keys_cache_prefetch['cache'][] = 'ctools_plugin_files:ctools:contexts';
      if (module_exists('page_manager')) {
        $table_keys_cache_prefetch['cache'][] = 'ctools_plugin_files:page_manager:tasks';
      }
    }
  }

  // Get cache_path bin keys.
  $page = $_GET['q'];
  if (empty($page)) {
    $page = variable_get('site_frontpage', 'node');
  }
  $source = drupal_lookup_path('source', $page);
  if (empty($source)) {
    $source = $page;
  }
  if (apdqc_get_bin_class_name('cache') === 'APDQCache') {
    $table_keys_cache_prefetch['cache_path'][] = apdqc_escape_string($source);
  }
  if (apdqc_get_bin_class_name('cache_field') === 'APDQCache') {
    $matched = apdqc_entity_urls($source);
    if (!empty($matched)) {

      // Use the advanced drupal_static() pattern, since this is called very
      // often.
      static $drupal_static_fast;
      if (!isset($drupal_static_fast)) {
        $drupal_static_fast['cache_field'] =& drupal_static('apdqc_cache_prefetch_ids');
      }
      $string = 'field:' . $matched[0] . ':' . apdqc_escape_string(substr($source, strlen($matched[1])));
      if (!isset($drupal_static_fast['cache_field'][$string])) {
        $drupal_static_fast['cache_field'][$string] = TRUE;
        $table_keys_cache_prefetch['cache_field'][] = $string;
      }
      if ($matched[0] == 'user') {
        $table_keys_cache_prefetch['cache_field'][] = 'field_info:bundle:user:user';
      }
    }
    $table_keys_cache_prefetch['cache_field'][] = 'field_info_types:' . $lang_shorthand;
  }
  if (apdqc_get_bin_class_name('cache_token') === 'APDQCache') {
    if (module_exists('token')) {
      $table_keys_cache_prefetch['cache_token'][] = 'info:' . $lang_shorthand;
    }
  }

  // Prefetch cache.
  apdqc_run_prefetch_array($table_keys_cache_prefetch);
}