You are here

function _ad_build_cache in Advertisement 6.3

Same name and namespace in other branches
  1. 5.2 cache/file/ad_cache_file.module \_ad_build_cache()
  2. 5 cache/file/ad_cache_file.module \_ad_build_cache()
  3. 6 cache/file/ad_cache_file.module \_ad_build_cache()
  4. 6.2 cache/file/ad_cache_file.module \_ad_build_cache()
  5. 7 cache/file/ad_cache_file.module \_ad_build_cache()

Returns the cache structure:

// The ad html. $cache['ad'][$aid]['display'] = $ad // Impressions counter. $cache['ad'][$aid][$hostid]['view'] // Ad type. $cache['ad'][$aid]['adtype'] = $adtype // Synchronization timestamp. $cache['last_sync'] = $timestamp

// Owner ID index. $cache['uid'][$uid]['aid'][] = $aid $cache['ad'][$aid]['uid'][] = $uid; // Host ID index. $cache['uid'][$uid]['hostid'] = $hostid

1 call to _ad_build_cache()
ad_cache_file_build in cache/file/ad_cache_file.module
Build all required cache files when using the file cache.

File

cache/file/ad_cache_file.module, line 227
A plug in for the ad.module, providing a file cache mechanism for improved performance when displaying ads.

Code

function _ad_build_cache() {
  _debug_echo('File cache: _ad_build_cache.');
  $cache = array();
  _debug_echo("SELECT aid FROM ads WHERE adstatus = 'active' OR adstatus = 'approved' OR adstatus = 'offline'");
  $result = db_query("SELECT aid FROM {ads} WHERE adstatus = 'active' OR adstatus = 'approved' OR adstatus = 'offline'");
  $counter = 1;
  while ($ad = db_fetch_object($result)) {
    _debug_echo("File cache: loading node {$ad->aid}.");
    $node = node_load($ad->aid);
    _debug_echo("File cache: advertisement {$counter}: aid({$ad->aid}) type({$node->adtype})");
    $counter++;

    // Ad information.
    _debug_echo("File cache: invoking ad_{$node->adtype}('display_ad').");
    $cache['ad'][$ad->aid]['display'] = module_invoke("ad_{$node->adtype}", 'display_ad', $node);
    $cache['ad'][$ad->aid]['adtype'] = $node->adtype;
    $cache['ad'][$ad->aid]['none']['counts']['view'] = array();
    $cache['ad']['aid'][] = $node->aid;

    // Owner indexes.
    // TODO: Disable this query if ad_remote isn't enabled?
    $owners = db_query('SELECT o.uid, h.hostid FROM {ad_owners} o LEFT JOIN {ad_hosts} h ON o.uid = h.uid WHERE aid = %d', $ad->aid);
    $counter2 = 1;
    while ($owner = db_fetch_object($owners)) {
      $cache['uid'][$owner->uid]['aid'][] = $ad->aid;
      $cache['ad'][$ad->aid]['uid'][] = $owner->uid;
      $cache['ad'][$ad->aid][$owner->hostid]['view'] = array();
      _debug_echo("File cache: owner {$counter2}: uid({$owner->uid})");
      $counter2++;
    }

    // Taxonomy index.
    $terms = db_query('SELECT tid FROM {term_node} WHERE nid = %d', $ad->aid);
    $match = FALSE;
    $counter2 = 1;
    while ($term = db_fetch_object($terms)) {
      $cache['tid'][$term->tid]['aid'][$ad->aid] = $ad->aid;
      $match = TRUE;
      _debug_echo("File cache: term {$counter2}: tid({$term->tid})");
      $counter2++;
    }
    if (!$match) {
      $cache['tid'][0]['aid'][] = $ad->aid;
    }
  }

  // HostID index
  $owners = db_query('SELECT uid, hostid FROM {ad_hosts}');
  $counter = 1;
  while ($owner = db_fetch_object($owners)) {
    $cache['uid'][$owner->uid]['hostid'] = $owner->hostid;
    $cache['ad'][0][$owner->hostid]['count'] = array();
    if (($user = user_load(array(
      'uid' => $owner->uid,
    ))) && user_access('host remote advertisements', $user)) {
      $cache['hostid'][$owner->hostid] = TRUE;
    }
    _debug_echo("File cache: owner {$counter}: uid({$owner->uid}) hostid({$owner->hostid})");
    $counter++;
  }
  _debug_echo('File cache: invoking external ad_build_cache hooks.');
  $external = module_invoke_all('ad_build_cache');

  // TODO: Move helper function adserve_cache_build_hooks from adcache.inc to
  // ad.module to share.
  foreach ($external as $module => $return) {

    // supported cache hooks
    foreach (array(
      'hook_init',
      'hook_filter',
      'hook_weight',
      'hook_select',
      'hook_init_text',
      'hook_exit_text',
      'hook_increment_extra',
    ) as $hook) {
      if (isset($return[$hook]) && is_array($return[$hook])) {
        $weight = isset($return[$hook]['weight']) ? (int) $return[$hook]['weight'] : 0;
        $cache[$hook]['file'][$weight][] = $return[$hook]['file'];
        $cache[$hook]['function'][$weight][] = $return[$hook]['function'];
        unset($external[$module][$hook]);
      }
    }
  }
  $cache = array_merge($cache, $external);
  $cache['last_sync'] = time();
  $cache['lifetime'] = variable_get('ad_cache_file_lifetime', 60);
  return $cache;
}