You are here

function ad_cache_file_build in Advertisement 6

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

Build all required cache files when using the file cache.

2 calls to ad_cache_file_build()
ad_cache_file_adcacheapi in cache/file/ad_cache_file.module
Implementation of hook_adcacheapi().
ad_cache_file_rebuild in cache/file/ad_cache_file.inc
Bootstrap drupal, then run ad_cache_file_build() from ad.module which will rebuild all cache files.
1 string reference to 'ad_cache_file_build'
ad_cache_file_rebuild in cache/file/ad_cache_file.inc
Bootstrap drupal, then run ad_cache_file_build() from ad.module which will rebuild all cache files.

File

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

Code

function ad_cache_file_build($new_files = 0, $old_files = 0) {
  $files = max($new_files, $old_files);
  $files = $files ? $files : variable_get('ad_files', 3);
  $new_cache = serialize(_ad_build_cache());
  for ($i = 1; $i <= $files; $i++) {
    $cache_file = file_create_path(".{$i}.ad.cache");
    if (!file_exists($cache_file)) {

      // Create the cache file.
      file_save_data($new_cache, $cache_file, FILE_EXISTS_REPLACE);
    }
    else {
      if (!($fd = @fopen($cache_file, 'r+'))) {
        drupal_set_message(t('Ad module failed to access cache <em>%file</em>.  Verify file permissions.', array(
          '%file' => $cache_file,
        )), 'error');
        continue;
      }

      // Block until we get an exclusive lock on the cache file.
      flock($fd, LOCK_EX);

      // Read the entire cache file into memory.
      $cache = unserialize(file_get_contents($cache_file));
      if ($cache && isset($cache['ad'])) {
        foreach ($cache['ad'] as $aid => $host) {
          foreach ($host as $hostid => $ad) {
            $hostid = $hostid == 'none' ? '' : $hostid;
            if (isset($ad['counts']) && is_array($ad['counts'])) {
              foreach ($ad['counts'] as $action => $counts) {
                foreach ($counts as $timestamp => $count) {
                  db_query("UPDATE {ad_statistics} SET count = count + %d WHERE aid = %d AND action = '%s' AND date = %d AND hostid = '%s'", $count, $aid, $action, $timestamp, $hostid);

                  // If column doesn't already exist, we need to add it.
                  if (!db_affected_rows()) {
                    db_query("INSERT INTO {ad_statistics} (aid, date, action, hostid, count) VALUES(%d, %d, '%s', '%s', %d)", $aid, $timestamp, $action, $hostid, $count);

                    // If another process already added this row our INSERT will
                    // fail, if so we still need to increment it so we don't
                    // loose a count.
                    if (!db_affected_rows()) {
                      db_query("UPDATE {ad_statistics} SET count = count + %d WHERE aid = %d AND action = '%s' AND date = %d AND hostid = '%s'", $count, $aid, $action, $timestamp, $hostid);
                    }
                  }
                }
              }

              // If counting ad impressions, see if we've hit a limit
              if ($action = 'view') {
                $limits = db_fetch_object(db_query('SELECT activated, maxviews, maxclicks, adstatus FROM {ads} WHERE aid = %d', $aid));
                if ($limits->adstatus == 'active') {
                  if ($limits->maxviews) {
                    $views = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d", $aid, date('YmdH', $limits->activated)));
                    if ($views >= $limits->maxviews) {
                      db_query("UPDATE {ads} SET adstatus = 'expired', autoexpire = 0, autoexpired = %d, expired = %d WHERE aid = %d", time(), time(), $aid);
                      ad_statistics_increment($aid, 'autoexpired');
                      ad_statistics_increment($aid, 'expired');
                    }
                  }
                  if ($limits->maxclicks) {
                    $clicks = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date >= %d", $aid, date('YmdH', $limits->activated)));
                    if ($clicks >= $limits->maxclicks) {
                      db_query("UPDATE {ads} SET adstatus = 'expired', autoexpire = 0, autoexpired = %d, expired = %d WHERE aid = %d", time(), time(), $aid);
                      ad_statistics_increment($aid, 'autoexpired');
                      ad_statistics_increment($aid, 'expired');
                    }
                  }
                }
              }
            }
          }
        }
      }

      // This will rebuild a new fresh cache file, and release the lock
      if ($old_files && $i > $new_files) {
        unlink($cache_file);
      }
      else {
        file_save_data($new_cache, $cache_file, FILE_EXISTS_REPLACE);
      }
    }
  }
}