Commit 021885c3 authored by merlinofchaos's avatar merlinofchaos

Some code re-org, putting stuff in cache.inc where it belongs

parent c01f577a
<?php
// $Id$
/**
* @file cache.inc
*
* Functions to load Views' data so that it knows what is available to
* build queries from.
*/
/**
* Load views files on behalf of modules.
*/
function _views_include_handlers() {
$views_path = drupal_get_path('module', 'views') . '/modules';
foreach (module_list() as $module) {
$module_path = drupal_get_path('module', $module);
if (file_exists("$module_path/$module.views.inc")) {
include_once "$module_path/$module.views.inc";
}
else if (file_exists("$views_path/$module.views.inc")) {
include_once "$views_path/$module.views.inc";
}
}
}
/**
* Load default views files on behalf of modules.
*/
function _views_include_default_views() {
// Now include the views_default files specifically.
$views_path = drupal_get_path('module', 'views') . '/modules';
foreach (module_list() as $module) {
$module_path = drupal_get_path('module', $module);
if (file_exists("$module_path/$module.views_default.inc")) {
include_once "$module_path/$module.views_default.inc";
}
else if (file_exists("$views_path/$module.views_default.inc")) {
include_once "$views_path/$module.views_default.inc";
}
}
}
/**
* Fetch Views' data from the cache
*/
function _views_fetch_data($table = NULL) {
static $cache = NULL;
if (!isset($cache)) {
$start = microtime();
// NOTE: This happens whether we retrieve them from cache or otherwise.
views_include_handlers();
// @todo: We can probably cache the views_data longer than just the current page run.
$cache = module_invoke_all('views_data');
if (module_exists('devel')) {
dsm('Views data build time: ' . (microtime() - $start) * 1000 . ' ms');
}
}
if (!$table) {
return $cache;
}
if (isset($cache[$table])) {
return $cache[$table];
}
// Return an empty array if there is no match.
return array();
}
/**
* Fetch the plugin data from cache.
*/
function _views_fetch_plugin_data($type = NULL, $plugin = NULL) {
static $cache = NULL;
if (!isset($cache)) {
$start = microtime();
views_include('plugins');
views_include_handlers();
$cache = views_discover_plugins();
if (module_exists('devel')) {
dsm('Views plugins build time: ' . (microtime() - $start) * 1000 . ' ms');
}
}
if (!$type && !$plugin) {
return $cache;
}
else if (!$plugin) {
// Not in the if above so the else below won't run
if (isset($cache[$type])) {
return $cache[$type];
}
}
else if (isset($cache[$type][$plugin])) {
return $cache[$type][$plugin];
}
// Return an empty array if there is no match.
return array();
}
/**
* Scan all modules for default views and rebuild the default views cache.
*
* @return An associative array of all known default views.
*/
function _views_discover_default_views() {
global $language;
views_include_default_views();
$defaults = module_invoke_all('views_default_views');
foreach ($defaults as $name => $view) {
if ($should_cache) {
views_cache_set('views_default_views:'. $name, $view, TRUE);
}
$cache[$name] = $view;
}
return $cache;
}
/**
* Set a cached item in the views cache.
*
* This is just a convenience wrapper around cache_set().
*
* @param $cid
* The cache ID of the data to store.
* @param $data
* The data to store in the cache. Complex data types will be automatically serialized before insertion.
* Strings will be stored as plain text and not serialized.
* @param $use_language
* If TRUE, the data will be cached specific to the currently active language.
*/
function views_cache_set($cid, $data, $use_language = FALSE) {
global $language;
if (variable_get('views_skip_cache', FALSE)) {
return;
}
if ($use_language) {
$cid .= ':'. $language->language;
}
cache_set($cid, $data, 'cache_views');
}
/**
* Return data from the persistent views cache.
*
* This is just a convenience wrapper around cache_get().
*
* @param $cid
* The cache ID of the data to retrieve.
* @param $use_language
* If TRUE, the data will be requested specific to the currently active language.
*/
function views_cache_get($cid, $use_language = FALSE) {
global $language;
if (variable_get('views_skip_cache', FALSE)) {
return 0;
}
if ($use_language) {
$cid .= ':'. $language->language;
}
return cache_get($cid, 'cache_views');
}
......@@ -5,6 +5,61 @@
* Defines the various handler objects to help build and display views.
*/
/**
* Instantiate and construct a new handler
*
* @todo: move to handlers.inc
*/
function _views_create_handler($definition) {
// dpr('Instantiating handler ' . $definition['handler']);
$handler = new $definition['handler'];
if (isset($definition['arguments'])) {
call_user_func_array(array($handler, 'construct'), $definition['arguments']);
}
$handler->set_definition($definition);
return $handler;
}
/**
* Prepare a handler's data by checking defaults and such.
*
* @todo: move to handlers.inc
*/
function _views_prepare_handler($definition, $data, $field) {
foreach (array('group', 'title', 'help') as $key) {
// First check the field level
if (!isset($definition[$key]) && !empty($data[$field][$key])) {
$definition[$key] = $data[$field][$key];
}
// Then if that doesn't work, check the table level
if (!isset($definition['table'][$key]) && !empty($data['table'][$key])) {
$definition[$key] = $data['table'][$key];
}
}
return _views_create_handler($definition);
}
/**
* Fetch a handler to join one table to a primary table from the data cache
*
* @todo: move to cache.inc (maybe)
*/
function views_get_table_join($table, $primary_table) {
$data = views_fetch_data($table);
if (isset($data['table']['join'][$primary_table])) {
$h = $data['table']['join'][$primary_table];
$handler = new $h['handler'];
if (isset($h['arguments'])) {
call_user_func_array(array($handler, 'construct'), $h['arguments']);
}
return $handler;
}
// DEBUG -- identify missing handlers
dsm("missing join: $table $primary_table");
}
/**
* @defgroup views_join_handlers Views' join handlers
* @{
......
......@@ -76,6 +76,48 @@ function views_views_plugins() {
}
/**
* Builds and return a list of all plugins available in the system.
*
* @return Nested array of plugins, grouped by type and
*/
function views_discover_plugins() {
$cache = array('display' => array(), 'style' => array(), 'row' => array());
// Get plugins from all mdoules.
foreach (module_implements('views_plugins') as $module) {
$function = $module . '_views_plugins';
$result = $function();
if (!is_array($result)) {
continue;
}
$module_dir = isset($result['module']) ? $result['module'] : $module;
// Setup automatic path/file finding for theme registration
if ($module_dir == 'views') {
$path = drupal_get_path('module', $module_dir) . '/theme';
$file = 'theme.inc';
}
else {
$path = drupal_get_path('module', $module_dir);
$file = "$module.views.inc";
}
foreach ($result as $type => $info) {
if ($type == 'module') {
continue;
}
foreach ($info as $plugin => $def) {
if (isset($def['theme']) && !isset($def['path'])) {
$def['path'] = $path;
$def['file'] = $file;
}
// merge the new data in
$cache[$type][$plugin] = $def;
}
}
}
return $cache;
}
/**
* @defgroup views_display_plugins Views' display plugins
* @{
......@@ -159,7 +201,7 @@ class views_display_plugin extends views_object {
/**
* Render the view's title for display
* @todo: Necessary? Hm.
* @todo Necessary? Hm.
*/
function render_title() { }
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment