You are here

function apdqc_init in Asynchronous Prefetch Database Query Cache 7

Implements hook_init().

File

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

Code

function apdqc_init() {
  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';
    }
  }
  $lang_shorthand = apdqc_get_lang();
  $table_keys_cache_prefetch = array();
  if (apdqc_get_bin_class_name('cache') === 'APDQCache') {
    if (module_exists('filter')) {
      $table_keys_cache_prefetch['cache'][] = 'filter_list_format';
    }
    if (module_exists('image')) {
      $table_keys_cache_prefetch['cache'][] = 'image_styles';
    }
    if (module_exists('cdn')) {
      $table_keys_cache_prefetch['cache'][] = 'cdn_blacklist';
    }
    if (module_exists('entity')) {
      $table_keys_cache_prefetch['cache'][] = 'entity_property_info:' . $lang_shorthand;
    }
    if (module_exists('ctools')) {
      $table_keys_cache_prefetch['cache'][] = 'ctools_plugin_files:ctools:access';
      $table_keys_cache_prefetch['cache'][] = 'ctools_plugin_files:ctools:content_types';
      $table_keys_cache_prefetch['cache'][] = 'ctools_plugin_files:ctools:relationships';
      if (module_exists('panels')) {
        $table_keys_cache_prefetch['cache'][] = 'ctools_plugin_files:panels:styles';
        $table_keys_cache_prefetch['cache'][] = 'ctools_plugin_files:panels:layouts';
        $table_keys_cache_prefetch['cache'][] = 'ctools_plugin_files:panels:display_renderers';
      }
      if (module_exists('page_manager')) {
        $table_keys_cache_prefetch['cache'][] = 'ctools_plugin_files:page_manager:tasks';
        $table_keys_cache_prefetch['cache'][] = 'ctools_plugin_files:page_manager:task_handlers';
      }
    }
    $default_theme = apdqc_escape_string(variable_get('theme_default', 'bartik'));
    $global_theme = apdqc_escape_string($GLOBALS['theme']);
    if ($default_theme != $global_theme) {
      $table_keys_cache_prefetch['cache'][] = 'theme_registry:runtime:' . $global_theme;
    }
    $table_keys_cache_prefetch['cache'][] = 'theme_registry:' . $global_theme;
  }
  if (apdqc_get_bin_class_name('cache_rules') === 'APDQCache') {
    if (module_exists('rules')) {
      $table_keys_cache_prefetch['cache_rules'][] = 'data:' . $lang_shorthand;
    }
  }
  if (apdqc_get_bin_class_name('cache_token') === 'APDQCache') {
    if (module_exists('token')) {
      $table_keys_cache_prefetch['cache_token'][] = 'info:' . $lang_shorthand;
      $table_keys_cache_prefetch['cache_token'][] = 'field:info';
    }
  }
  if (apdqc_get_bin_class_name('cache_field') === 'APDQCache') {
    if (function_exists('menu_get_item')) {
      $item = menu_get_item();
    }
    if (!empty($item) && !empty($item['page_callback']) && $item['page_callback'] === 'drupalorg_home') {
      $table_keys_cache_prefetch['cache_field'][] = "field_info:bundle:node:post";
    }
    if (!empty($item) && !empty($item['page_callback']) && $item['page_callback'] === 'project_solr_browse_page') {
      if (apdqc_get_bin_class_name('cache_views') === 'APDQCache') {
        $table_keys_cache_prefetch += apdqc_views_get_unpack_options();
        if (!empty($item['path']) && ($item['path'] === 'project/project_module' || $item['path'] === 'project/project_distribution' || $item['path'] === 'project/project_theme')) {
          $table_keys_cache_prefetch['cache_views'][] = 'ctools_export:views_view:project_issue_user_projects';
          $table_keys_cache_prefetch['cache_views'][] = 'views_data:field_data_field_issue_assigned:' . $lang_shorthand;
          $table_keys_cache_prefetch['cache_views'][] = 'views_data:field_data_field_project:' . $lang_shorthand;
          $table_keys_cache_prefetch['cache_views'][] = 'views_data:field_data_field_project_machine_name:' . $lang_shorthand;
          $table_keys_cache_prefetch['cache_views'][] = 'views_data:history:' . $lang_shorthand;
          $table_keys_cache_prefetch['cache_views'][] = 'views_data:field_data_field_issue_status:' . $lang_shorthand;
          $table_keys_cache_prefetch['cache_views'][] = 'views_data:field_data_field_issue_priority:' . $lang_shorthand;
          $table_keys_cache_prefetch['cache_views'][] = 'views_data:field_data_field_issue_category:' . $lang_shorthand;
          $table_keys_cache_prefetch['cache_views'][] = 'views_data:field_data_field_issue_version:' . $lang_shorthand;
          $table_keys_cache_prefetch['cache_views'][] = 'views_data:node_comment_statistics:' . $lang_shorthand;
          $table_keys_cache_prefetch['cache_views'][] = 'views_data:users:' . $lang_shorthand;
          $table_keys_cache_prefetch['cache_views'][] = 'views_data:search_index:' . $lang_shorthand;
          $table_keys_cache_prefetch['cache_views'][] = 'views_data:project_maintainer:' . $lang_shorthand;
        }
      }
      if (!empty($item['path']) && $item['path'] === 'project/project_module') {
        $table_keys_cache_prefetch['cache_field'][] = "field_info:bundle:node:project_module";
        $table_keys_cache_prefetch['cache_field'][] = "field_info:bundle_extra:node:project_module";
      }
    }
    if (!empty($item) && !empty($item['page_arguments'][0]->type) && ($item['page_arguments'][0]->type === 'project_module' || $item['page_arguments'][0]->type === 'project_distribution' || $item['page_arguments'][0]->type === 'project_theme') && !empty($item['page_arguments'][0]->versioncontrol_project) && !empty($item['page_arguments'][0]->project)) {
      if (apdqc_get_bin_class_name('cache_views') === 'APDQCache') {
        $table_keys_cache_prefetch += apdqc_views_get_unpack_options();
        $table_keys_cache_prefetch['cache_views'][] = 'ctools_export:views_view:project_issue_metrics_new_issues';
        $table_keys_cache_prefetch['cache_views'][] = 'views_data:sampler_project_issue_new_issues_comments_by_project:' . $lang_shorthand;
        $table_keys_cache_prefetch['cache_views'][] = 'ctools_export:views_view:project_issue_metrics_response_rate';
        $table_keys_cache_prefetch['cache_views'][] = 'views_data:sampler_project_issue_responses_by_project:' . $lang_shorthand;
        $table_keys_cache_prefetch['cache_views'][] = 'ctools_export:views_view:project_issue_metrics_open_bugs';
        $table_keys_cache_prefetch['cache_views'][] = 'views_data:sampler_project_issue_opened_vs_closed_by_category:' . $lang_shorthand;
        $table_keys_cache_prefetch['cache_views'][] = 'ctools_export:views_view:project_issue_metrics_participants';
        $table_keys_cache_prefetch['cache_views'][] = 'views_data:sampler_project_issue_reporters_participants_by_project:' . $lang_shorthand;
        $table_keys_cache_prefetch['cache_views'][] = 'ctools_export:views_view:project_issue_metrics_first_response';
      }
      $table_keys_cache_prefetch['cache_field'][] = "field_info:bundle:node:organization";
    }
    if (!empty($item) && !empty($item['page_arguments'][0]) && is_string($item['page_arguments'][0]) && $item['page_arguments'][0] === 'project_issue_project_searchapi' && !empty($item['page_arguments'][1]) && is_string($item['page_arguments'][1]) && $item['page_arguments'][1] === 'page') {
      $table_keys_cache_prefetch['cache_field'][] = "field_info:bundle:node:page";
      $table_keys_cache_prefetch['cache_field'][] = "field_info:bundle:node:project_release";
      $table_keys_cache_prefetch['cache_field'][] = "field_info:bundle:node:section";
      $table_keys_cache_prefetch['cache_field'][] = "field_info:bundle:node:changenotice";
    }
    if (!empty($item) && !empty($item['page_arguments'][0]) && is_object($item['page_arguments'][0]) && !empty($item['page_arguments'][0]->type) && $item['page_arguments'][0]->type === 'project_issue') {
      $table_keys_cache_prefetch['cache_field'][] = "field_info:bundle:node:page";
      $table_keys_cache_prefetch['cache_field'][] = "field_info:bundle:node:project_release";
      $table_keys_cache_prefetch['cache_field'][] = "field_info:bundle:node:section";
      $table_keys_cache_prefetch['cache_field'][] = "field_info:bundle:node:changenotice";
      if (apdqc_get_bin_class_name('cache_views') === 'APDQCache') {
        $table_keys_cache_prefetch += apdqc_views_get_unpack_options();
        $table_keys_cache_prefetch['cache_views'][] = 'ctools_export:views_view:change_records';
      }
    }
    $source = $_GET['q'];
    $matched = apdqc_entity_urls($source);
    if (!empty($matched)) {
      $entity_name = $matched[0];
      $entity_id = substr($source, strlen($matched[1]));
      $entity_bundle = '';
      if (!empty($item) && !empty($item['original_map'])) {
        $map_key = array_search($entity_id, $item['original_map']);
        if (!empty($item['map'][$map_key]->type)) {
          $entity_bundle = $item['map'][$map_key]->type;
        }
        elseif ($entity_name === 'user') {
          $entity_bundle = 'user';
        }
      }
      if (!empty($entity_bundle)) {
        $table_keys_cache_prefetch['cache_field'][] = "field_info:bundle:{$entity_name}:{$entity_bundle}";
        $table_keys_cache_prefetch['cache_field'][] = "field_info:bundle_extra:{$entity_name}:{$entity_bundle}";
        if (module_exists('comment') && !empty($item['map'][$map_key]->comment)) {

          // Add in comment pre-loading if this entity has comments enabled.
          $table_keys_cache_prefetch['cache_field'][] = "field_info:bundle:comment:comment_{$entity_name}_{$entity_bundle}";
          $table_keys_cache_prefetch['cache_field'][] = "field_info:bundle_extra:comment:comment_{$entity_name}_{$entity_bundle}";
        }
        if (module_exists('field_collection')) {
          if (!empty($item['page_arguments'][0]) && is_object($item['page_arguments'][0])) {
            $prefetch = apdqc_async_data(FALSE, 'cache_field', array(
              'field_info:field_map',
            ));
            $parameters = array();
            if (!empty($prefetch['field_info:field_map']['data'])) {
              apdqc_inflate_unserialize($prefetch['field_info:field_map']);
              $parameters = $prefetch['field_info:field_map']['data'];
            }
            foreach ($parameters as $key => $values) {
              if ($values['type'] === 'field_collection' && !empty($item['page_arguments'][0]->{$key}[LANGUAGE_NONE])) {
                $table_keys_cache_prefetch['cache_field'][] = 'field_info:bundle:field_collection_item:' . apdqc_escape_string($key);
                $table_keys_cache_prefetch['cache_field'][] = 'field_info:bundle_extra:field_collection_item:' . apdqc_escape_string($key);
                foreach ($item['page_arguments'][0]->{$key}[LANGUAGE_NONE] as $values) {
                  if (!empty($values['value'])) {
                    $table_keys_cache_prefetch['cache_field'][] = "field:field_collection_item:" . apdqc_escape_string($values['value']);
                  }
                }
              }
            }
            if (apdqc_get_bin_class_name('cache_views') === 'APDQCache') {
              $table_keys_cache_prefetch['cache_views'][] = 'views_data:field_collection_item:' . $lang_shorthand;
            }
          }
        }
      }
    }
    if (module_exists('googleanalytics')) {
      $table_keys_cache_prefetch['cache_field'][] = "field_info:bundle:user:user";
    }
  }
  if (module_exists('admin_menu')) {
    $cid = 'admin_menu:' . apdqc_escape_string($GLOBALS['user']->uid . ':' . session_id() . ':' . $GLOBALS['language']->language);
    if (apdqc_get_bin_class_name('cache_menu') === 'APDQCache') {
      $table_keys_cache_prefetch['cache_menu'][] = $cid;
    }
    if (apdqc_get_bin_class_name('cache_admin_menu') === 'APDQCache') {
      $table_keys_cache_prefetch['cache_admin_menu'][] = $cid;
    }
  }
  if (apdqc_get_bin_class_name('cache_views') === 'APDQCache') {
    if (module_exists('views')) {
      $table_keys_cache_prefetch['cache_views'][] = 'ctools_export_index:views_view';
    }
  }
  if (apdqc_get_bin_class_name('cache_field') === 'APDQCache') {
    if (module_exists('field_group')) {
      $table_keys_cache_prefetch['cache_field'][] = 'field_groups';
    }
  }
  if (apdqc_get_bin_class_name('cache_menu') === 'APDQCache') {
    $table_keys_cache_prefetch['cache_menu'][] = 'menu_custom';

    // Get router item.
    if (function_exists('menu_get_item')) {
      $router_item = menu_get_item();
    }

    // If this router item points to its parent, start from the parents to
    // compute tabs and actions.
    if (!empty($router_item) && $router_item['type'] & MENU_LINKS_TO_PARENT) {
      $router_item = menu_get_item($router_item['tab_parent_href']);
    }

    // If we failed to fetch a router item or the current user doesn't have
    // access to it, don't bother computing the tabs.
    if (!empty($router_item) && $router_item['access']) {

      // Get all tabs (also known as local tasks) and the root page.
      $table_keys_cache_prefetch['cache_menu'][] = 'local_tasks:' . apdqc_escape_string($router_item['tab_root']);
    }

    // Load up system blocks.
    if (module_exists('block')) {

      // 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';
      }
      $cids = array();
      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);
          $href = apdqc_escape_string($item['href']);
          $cids[] = 'links:' . $menu_name . ':page:' . $href . ':' . $lang_shorthand . ':' . (int) $item['access'] . ':1';
          $cids[] = 'links:' . $menu_name . ':page:' . $href . ':' . $lang_shorthand . ':' . (int) $item['access'] . ':0';
          $cids[] = 'links:' . $menu_name . ':page:' . $href . ':' . $lang_shorthand . ':' . (int) $item['access'] . ':1:trail';
          $cids[] = 'links:' . $menu_name . ':page:' . $href . ':' . $lang_shorthand . ':' . (int) $item['access'] . ':0:trail';
        }
      }
      $prefetch = apdqc_async_data(FALSE, 'cache_menu', $cids);
      foreach ($prefetch as $cid => $cache) {
        if (empty($cache)) {
          continue;
        }
        $menu_name = substr($cid, 6, strpos($cid, ':', 6) - 6);
        apdqc_inflate_unserialize($cache);
        $parameters = $cache['data'];
        if (isset($parameters['expanded'])) {
          sort($parameters['expanded']);
        }
        $hash = hash('sha256', serialize($parameters));
        $table_keys_cache_prefetch['cache_menu'][] = "links:{$menu_name}:tree-data:{$lang_shorthand}:{$hash}";
      }
    }
  }

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