Same name in other branches
  1. 7.x-1.x advagg_bundler/advagg_bundler.module \advagg_bundler_analysis()

Given a filename return a bundle key.

Parameters

string $filename: Filename.

bool $force: Bypass the cache and get a fresh version of the analysis.

bool $safesql: Turn off SQL language that might cause errors.

int $depth: Used to prevent endless loops.

Return value

string String to be used for the grouping key.

3 calls to advagg_bundler_analysis()
advagg_admin_get_file_info in ./advagg.admin.inc
Get detailed info about the given filename.
advagg_bundler_admin_settings_form in advagg_bundler/advagg_bundler.admin.inc
Form builder; Configure advagg settings.
advagg_bundler_advagg_build_aggregate_plans_alter in advagg_bundler/advagg_bundler.advagg.inc
Implements hook_advagg_build_aggregate_plans_alter().

File

advagg_bundler/advagg_bundler.module, line 136

Code

function advagg_bundler_analysis($filename = '', $force = FALSE, $safesql = FALSE, $depth = 0) {
    // Cache query in a static.
    static $analysis = array();
    if (empty($analysis)) {
        // See if we have a cached version of this. Generate cache ID.
        $query = db_select('advagg_aggregates_versions', 'aav')->condition('aav.root', 1)
            ->condition('aav.atime', REQUEST_TIME - max(172800, variable_get('advagg_bundler_outdated', ADVAGG_BUNDLER_OUTDATED), '>'), '>');
        $query->addExpression('COUNT(aggregate_filenames_hash)', 'counter');
        $count = $query->execute()
            ->fetchField();
        $ideal_cid = 'advagg:bundler_analysis:' . $count;
        if (!$force) {
            // Generate cache IDs.
            $counts = range(max(0, $count - 3), $count + 3);
            foreach ($counts as $count) {
                $cache_ids[] = 'advagg:bundler_analysis:' . $count;
            }
            // Get a range of cached bundler_analysis data.
            $cache_hits = cache_get_multiple($cache_ids, 'cache_advagg_aggregates');
            if (!empty($cache_hits)) {
                if (isset($cache_hits[$ideal_cid])) {
                    $cache = $cache_hits[$ideal_cid];
                }
                elseif (!$force && module_exists('httprl') && httprl_is_background_callback_capable()) {
                    // Setup callback options array.
                    $callback_options = array(
                        array(
                            'function' => 'advagg_bundler_analysis',
                        ),
                        $filename,
                        TRUE,
                    );
                    // Queue up the request.
                    httprl_queue_background_callback($callback_options);
                    // Execute request.
                    httprl_send_request();
                    // Use most recent bundler_analysis data.
                    $max = 0;
                    foreach ($cache_hits as $data) {
                        if ($data->created > $max) {
                            $max = $data->created;
                            $cache = $data;
                        }
                    }
                }
            }
        }
        if ($force || empty($cache->data)) {
            try {
                $analysis = advagg_bundler_analyisis_query($safesql);
                // Save results to the cache.
                cache_set($ideal_cid, $analysis, 'cache_advagg_aggregates', CACHE_TEMPORARY);
            } catch (PDOException $e) {
                if ($depth > 2) {
                    throw $e;
                }
                $depth++;
                return advagg_bundler_analysis($filename, TRUE, TRUE, $depth);
            }
        }
        else {
            $analysis = $cache->data;
        }
    }
    // If no filename is given pass back then entire query results.
    if (empty($filename)) {
        return $analysis;
    }
    // Return a key to be used in groupings.
    if (!empty($analysis[$filename])) {
        return $analysis[$filename];
    }
    // We need to return a value that can be used as an array key if the query
    // didn't give us anything.
    return 0;
}