You are here

function ad_cache_file_build in Advertisement 6.3

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 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.

4 calls to ad_cache_file_build()
ad_cache_file_adcacheapi in cache/file/ad_cache_file.module
Implementation of hook_adcacheapi().
ad_cache_file_cron in cache/file/ad_cache_file.module
Implementation of hook_adcacheapi().
ad_cache_file_force_flush in cache/file/ad_cache_file.module
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 123
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) {
  _debug_echo('File cache: ad_cache_file_build.');
  $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++) {
    _debug_echo("File cache: file {$i} of {$files}.");
    $cache_file = file_create_path(".{$i}.ad.cache");
    if (!file_exists($cache_file)) {

      // Create the cache file.
      _debug_echo('File cache: creating 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.
      _debug_echo('File cache: locking 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 => $counts) {
          if (isset($counts['counts']) && is_array($counts['counts'])) {
            foreach ($counts['counts'] as $adgroup => $tag) {
              foreach ($tag as $extra => $host) {
                foreach ($host as $hostid => $ad) {
                  $hostid = $hostid == 'none' ? '' : $hostid;
                  foreach ($ad as $action => $counts) {
                    foreach ($counts as $timestamp => $count) {
                      _debug_echo("File cache: aid({$aid}) adgroup({$adgroup}) extra({$extra}) hostid({$hostid}) action({$action}) timestamp({$timestamp}) count({$count}).");
                      db_query("UPDATE {ad_statistics} SET count = count + %d WHERE aid = %d AND action = '%s' AND date = %d AND hostid = '%s' AND adgroup = '%s' AND extra = '%s'", $count, $aid, $action, $timestamp, $hostid, $adgroup, $extra);

                      // 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, adgroup, extra, count) VALUES(%d, %d, '%s', '%s', '%s', '%s', %d)", $aid, $timestamp, $action, $hostid, $adgroup, $extra, $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' AND adgroup = '%s' AND extra = '%s'", $count, $aid, $action, $timestamp, $hostid, $adgroup, $extra);
                        }
                      }
                    }
                  }

                  // If counting ad views, 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);
      }
    }
  }
}