# HG changeset patch # User piotre # Date 1243522387 0 # Node ID b7653861e0b49bdf77768c10f254bb19015ce5b5 # Parent cd53a6ba3f977fb00423daa27d0bdd01ea23363d nettoyage modules filefield, ad, panels diff -r cd53a6ba3f97 -r b7653861e0b4 cache/memcache/ad_cache_memcache.inc --- a/cache/memcache/ad_cache_memcache.inc Mon Apr 27 10:27:57 2009 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,401 +0,0 @@ -. - */ - -/** - * TODO: Debug Raw and IFrame display methods, neither currently seem to work - * with this cache type. - */ - -/** - * Called by adserve.inc, display an ad from memcache. - */ -function ad_cache_memcache() { - _debug_echo('Memcache: entering ad_cache_memcache().'); - - // TODO: Move the meat of this function into adserve.php, simplifying what - // cache plugins have to do and removing duplicated logic. - $init_cache = array(); - $init_func = ad_cache_memcache_hook($init_cache, 'include_file_init', 'include_func_init'); - - $hostid = adserve_variable('hostid') ? adserve_variable('hostid') : 'none'; - if ($hostid == 'none' || ad_memcache_get("ad-hostid-$hostid")) { - if (function_exists($init_func)) { - $init = $init_func($init_cache, $hostid); - } - if (!empty($init)) { - if (adserve_variable('debug')) { - echo "Memcache: initialized externally:
\n";
-        print_r($init);
-        echo '
'; - } - $type = $init['type']; - $id = $init['id']; - $group = $init['group']; - $aids = explode(',', $id); - adserve_variable('quantity', $init['quantity']); - } - else { - if ($id = adserve_variable('nids')) { - $type = 'node'; - } - else if ($id = adserve_variable('tids')) { - $type = 'taxonomy'; - } - else { - $type = 'default'; - $id = 0; - } - $aids = ad_cache_memcache_get_ids($type, $id); - $group = $id; - } - adserve_variable('group', $group); - - if (adserve_variable('debug')) { - echo 'Memcache: selecting from the following ad id(s): '; - if (empty($aids)) { - echo 'none.
'; - } - else { - echo implode(', ', $aids) .'.
'; - } - } - - $ids = adserve_variable("$type-ids"); - if ($ids == NULL) { - $ids = array(); - } - - $output = ''; - $selected = adserve_select_ad($aids, adserve_variable('quantity'), $ids); - adserve_variable("$type-ids", array_merge($selected, $ids)); - foreach ($selected as $aid) { - if ($aid = (int)$aid) { - $ad = ad_cache_memcache_get_ad($aid); - - if (!empty($output)) { - $display_count++; - $output .= "
"; - } - - $output .= $ad->display; - } - else { - $ad = array(); - } - - _debug_echo("Displaying AID: $aid."); - $action = $aid ? 'view' : 'count'; - ad_cache_memcache_increment($action, $aid, $group, $hostid, $ad); - } - if (empty($output)) { - adserve_variable('error', TRUE); - $output = 'No active ads were found in the '. (empty($nids) ? 'tids' : 'nids') ." '$id'."; - _debug_echo("Memcache: {$output}"); - } - } - else { - _debug_echo("Memcache: invalid hostid: '$hostid'."); - $output = 'You do not have permission to display ads.'; - } - - return $output; -} - -function ad_cache_memcache_hook(&$cache, $hook, $func) { - - if (empty($cache)) { - _debug_echo('Memcache: retrieving hook info from cache.'); - $cache = ad_memcache_get('ad-cache-hook'); - } - $include_func = NULL; - if (is_array($cache) && !empty($cache)) { - $include_file = adserve_variable('root_dir') .'/'. $cache[$hook]; - if (file_exists($include_file) && is_file($include_file)) { - _debug_echo("Memcache: including external file: '$include_file'."); - include_once($include_file); - } - else if (is_file($include_file)) { - _debug_echo("Memcache: unable to find external file: '$include_file'."); - } - else { - _debug_echo('Memcache: no include file defined in cache.'); - } - $include_func = $cache[$func]; - if ($include_func) { - _debug_echo("Memcache: returning requested func($func): '$include_func'."); - } - } - return ($include_func); -} - -function ad_cache_memcache_get_ids($op = 'default', $id = 0) { - switch ($op) { - - case 'node': { - $ids = explode(',', $id); - break; - } - - case 'taxonomy': { - $ids = ad_memcache_get("ad-taxonomy-cache-$id"); - if (!$ids || empty($ids)) { - $taxonomy = ad_memcache_get('ad-taxonomy'); - $cache = array(); - $ids = explode(',', $id); - foreach ($ids as $tid) { - if (is_array($taxonomy[$tid])) { - $cache += $taxonomy[$tid]; - } - } - // Rebuild keys from 0, cache for quick re-use on next ad display. - $ids = array_values($cache); - ad_memcache_set("ad-taxonomy-cache-$id", $ids); - } - break; - } - - default: { - $taxonomy = ad_memcache_get('ad-taxonomy'); - $ids = $taxonomy[0]; - break; - } - - } - - return $ids; -} - -function ad_cache_memcache_get_ad($aid) { - static $load = FALSE; - - $ad = ad_memcache_get("ad-aid-$aid"); - - if (!$load && !is_object($ad)) { - $load = TRUE; - adserve_bootstrap(); - $ad_memcache_build = variable_get('ad_memcache_build', ''); - if ((time() - $ad_memcache_build) >= 60) { - ad_cache_memcache_build(); - } - } - - return $ad; -} - -/** - * Increment impressions counter in memcache. - */ -function ad_cache_memcache_increment($action, $aid, $group = '', $hostid = '', $ad = array()) { - static $timestamp = NULL; - - _debug_echo("Memcache: increment action($action) aid($aid) group($group) hostid($hostid)."); - - if ($aid && !is_object($ad)) { - _debug_echo("Invalid ad id: $aid."); - return (0); - } - - if (!isset($timestamp)) { - $timestamp = date('YmdH'); - } - $counters = ad_memcache_get("ad-counters-$aid"); - - $update = TRUE; - if (!is_array($counters) || !isset($counters["$action:$group:$hostid:$timestamp"])) { - _debug_echo("Memcache: adding map: action($action) aid($aid) group($group) hostid($hostid) timestamp($timestamp)"); - ad_memcache_increment_map($action, $aid, $group, $hostid, $timestamp); - } - - $rc = ad_memcache_increment("ad-$action-$aid-$group-$hostid-$timestamp"); - _debug_echo("Memcache: incrementing ad-$action-$aid-$group-$hostid-$timestamp ($rc)"); -} - -/** - * The maximum time any process can hold a given lock, in seconds. - */ -define('AD_MEMCACHE_LOCK_LIMIT', 2); - -/** - * Store a value in memcache. - */ -function ad_memcache_set($key, $value, $timeout = 86400) { - $memcache = ad_memcache_init(); - - return $memcache->set($key, $value, MEMCACHE_COMPRESSED, $timeout); -} - -/** - * Store a value in memcache. - */ -function ad_memcache_add($key, $value, $timeout = 86400) { - $memcache = ad_memcache_init(); - - return $memcache->add($key, $value, MEMCACHE_COMPRESSED, $timeout); -} - -/** - * Get a value from memcache. - */ -function ad_memcache_get($key) { - $memcache = ad_memcache_init(); - - return $memcache->get($key); -} - -/** - * Delete a value from memcache. - */ -function ad_memcache_delete($key) { - $memcache = ad_memcache_init(); - - return $memcache->delete($key); -} - -/** - * Get a lock in memcache. - */ -function ad_memcache_lock($key, $wait = TRUE) { - $loop = 0; - $lock = FALSE; - while ($lock == FALSE) { - $lock = ad_memcache_add("LOCK-$key-LOCK", TRUE, AD_MEMCACHE_LOCK_LIMIT); - if (!$lock && $wait) { - if ($loop++ > 50) { - // Hard limit of 5 seconds, after which we fail to grab a lock. - return FALSE; - } - // Wait 1/10th of a second and try again. - usleep(100000); - } - else if (!$lock && !$wait) { - return FALSE; - } - } - return TRUE; -} - -/** - * Release a lock in memcache. - */ -function ad_memcache_unlock($key) { - ad_memcache_delete("LOCK-$key-LOCK"); -} - -/** - * Increment a numerical value in memcache. - */ -function ad_memcache_increment($key, $value = 1) { - $memcache = ad_memcache_init(); - - $rc = $memcache->increment($key, $value); - if ($rc === FALSE) { - // We tried incrementing a counter that hasn't yet been initialized. - $rc = $memcache->set($key, $value); - if ($rc === FALSE) { - // Another process already initialized the counter, increment it. - $rc = $memcache->increment($key); - } - } - return $rc; -} - -/** - * Decrement a numerical value in memcache. - */ -function ad_memcache_decrement($key, $value = 1) { - $memcache = ad_memcache_init(); - - $rc = $memcache->decrement($key, $value); - if ($rc === FALSE) { - // We tried incrementing a counter that hasn't yet been initialized. - $rc = $memcache->set($key, $value); - if ($rc === FALSE) { - // Another process already initialized the counter, increment it. - $rc = $memcache->decrement($key); - } - } - return $rc; -} - -/** - * Update mapping which allows us to quickly find stats in memcache when - * feeding them into the database. - */ -function ad_memcache_increment_map($action, $aid, $group, $hostid, $timestamp) { - $key = "ad-counters-$aid"; - if (ad_memcache_lock($key)) { - $counters = ad_memcache_get($key); - if (!is_array($counters) || !isset($counters["$action:$group:$hostid:$timestamp"])) { - $counters["$action:$group:$hostid:$timestamp"] = "$action:$group:$hostid:$timestamp"; - ad_memcache_set($key, $counters); - } - ad_memcache_unlock($key); - } -} - -/** - * Decrement a numerical value in memcache. - * TODO: Use the same configuration style as Drupal's memcache module, - * supporting multiple memcache servers, etc. - */ -function ad_memcache_init() { - static $memcache = NULL; - - if (!$memcache) { - $memcache = new Memcache; - $memcache->addServer('localhost', 11211); - } - return $memcache; -} - -/** - * Allow external ad selection logic. - */ -function ad_cache_memcache_adserve_select($ads, $invalid) { - $cache = array(); - if ($select_func = ad_cache_memcache_hook($cache, 'include_file_select', 'include_func_select')) { - _debug_echo("Memcache: adserve_select: invoking '$select_func()'"); - if (function_exists($select_func)) { - if (is_array($cache) && !empty($cache)) { - return $select_func($ads, $invalid, $cache); - } - else { - _debug_echo("Memcache: unexpected error: cache empty."); - } - } - else { - _debug_echo("Memcache: adserve_select: '$include_func_select()' not found"); - } - } - else { - _debug_echo("Memcache: adserve_select: no select function defined"); - } -} - -/** - * Allow external exit text. - */ -function ad_cache_memcache_adserve_exit_text() { - $cache = array(); - if ($exit_text_func = ad_cache_memcache_hook($cache, 'include_file_exit_text', 'include_func_exit_text')) { - _debug_echo("Memcache: adserve_exit_text: invoking '$exit_text_func()'"); - if (function_exists($exit_text_func)) { - return $exit_text_func(); - } - else { - _debug_echo("Memcache: adserve_exit_text: '$exit_text_func()' not found"); - } - } - else { - _debug_echo("Memcache: adserve_exit_text: no exit_text function defined"); - } -} - diff -r cd53a6ba3f97 -r b7653861e0b4 cache/memcache/ad_cache_memcache.info --- a/cache/memcache/ad_cache_memcache.info Mon Apr 27 10:27:57 2009 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,12 +0,0 @@ -; $Id: ad_cache_memcache.info,v 1.1.2.2.2.1 2008/08/11 21:15:26 jeremy Exp $ -name = Ad memcache cache -package = Ad -dependencies[] = ad -description = Integrates the ad module with memcached, allowing ads to be served directly out of RAM. -core = 6.x -; Information added by drupal.org packaging script on 2009-02-17 -version = "6.x-1.1" -core = "6.x" -project = "ad" -datestamp = "1234899607" - diff -r cd53a6ba3f97 -r b7653861e0b4 cache/memcache/ad_cache_memcache.module --- a/cache/memcache/ad_cache_memcache.module Mon Apr 27 10:27:57 2009 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,282 +0,0 @@ -. - */ - -/** - * Implementation of hook_requirements(). - */ -function ad_cache_memcache_requirements($phase = NULL) { - include_once './includes/install.inc'; - // Connect to memcached so we can retrieve its version. - if (function_exists('memcache_add_server')) { - require_once(drupal_get_path('module', 'ad_cache_memcache') .'/ad_cache_memcache.inc'); - $memcache = ad_memcache_init(); - // Retrieve the version of memcache. - if (function_exists('memcache_get_version')) { - $severity = REQUIREMENT_OK; - $value = memcache_get_version($memcache); - } - else { - $severity = REQUIREMENT_ERROR; - $value = t('Memcache installation not valid, %function not found.', array('%function' => 'memcache_get_version')); - } - } - else { - $severity = REQUIREMENT_ERROR; - $value = t('Memcache is not installed, %function not found.', array('%function' => 'memcache_add_server')); - } - - if ($phase) { - return array( - 'memcache' => array( - 'title' => t('Memcache'), - 'value' => $value, - 'severity' => $severity, - ), - ); - } - else { - if ($severity == REQUIREMENT_OK) { - return TRUE; - } - else { - return $value; - } - } -} - -/** - * Implementation of hook_help(). - */ -function ad_cache_memcache_help($path, $arg) { - $output = ''; - switch ($path) { - case 'admin/modules#description': - $output = t('Utilize memcached to improve the performance of the ad module.'); - break; - - } - return $output; -} - -/** - * Implementation of hook_adcacheapi(). - */ -function ad_cache_memcache_adcacheapi($op, &$node = array()) { - switch ($op) { - case 'method': - ad_cache_memcache_sync(); - ad_cache_memcache_build(); - return array('memcache' => t('Memcache')); - case 'description': - return t('Memcache allows improved performance by caching data directly in RAM.'); - case 'settings': - $form['memcache'] = array( - '#type' => 'fieldset', - '#title' => t('Memcache settings'), - '#collapsible' => TRUE, - '#collapsed' => (variable_get('ad_cache', 'none') == 'memcache') ? FALSE : TRUE, - ); - $period = drupal_map_assoc(array(60,120,180,240,300,600,900,1800,2700,3600,10800,21600,43200,86400), 'format_interval'); - $form['memcache']['ad_cache_memcache_sync'] = array( - '#type' => 'select', - '#title' => t('Sync frequency'), - '#default_value' => variable_get('ad_cache_memcache_sync', 600), - '#options' => $period, - '#description' => t('Specify how often statistics stored in RAM should be synced to the database (requires cron runs with the same or greater frequency). The longer you store data in memcache, the more data you risk loosing in the event of a system failure. This configuration option is only relevant if memcache is enabled.'), - ); - // Sanity tests... - if (variable_get('ad_cache', 'none') == 'memcache') { - $sync = variable_get('ad_cache_memcache_sync', 600); - $cron_last = variable_get('cron_last', NULL); - if (is_numeric($cron_last)) { - if (time() - $cron_last > $sync) { - drupal_set_message(t('Memcache warning: your last cron run was !time ago. Advertisement impressions data is only synchronized into the database when cron runs. You are risking data loss. To learn more about how Drupal cron works, please check the online help pages for configuring cron jobs.', array('@url' => 'http://drupal.org/cron', '!sync' => format_interval($sync), '!time' => format_interval(time() - $cron_last))), 'error'); - } - } - else { - drupal_set_message(t('Memcache warning: Cron has not run. Advertisement impressions data is only synchronized into the database when cron runs. You are risking data loss. It appears cron jobs have not been setup on your system. Please check the help pages for configuring cron jobs.', array('@url' => 'http://drupal.org/cron')), 'error'); - } - } - return $form; - case 'settings_submit': - variable_set('ad_cache_memcache_sync', $node['ad_cache_memcache_sync']); - break; - - case 'insert': - case 'update': - case 'delete': - if (variable_get('ad_cache', 'none') == 'memcache') { - ad_cache_memcache_sync_ad($node->nid); - ad_cache_memcache_build($node); - } - break; - } -} - -/** - * Regularily syncronize counters into RAM. - */ -function ad_cache_memcache_cron() { - $ad_memcache_timestamp = variable_get('ad_memcache_timestamp', ''); - if ((time() - $ad_memcache_timestamp) >= variable_get('ad_cache_memcache_sync', 600)) { - ad_cache_memcache_sync(); - } - - $ad_memcache_build = variable_get('ad_memcache_build', ''); - // rebuild cache every 12 hours - // TODO: Make configurable - if ((time() - $ad_memcache_build) >= 43200) { - ad_cache_memcache_build(); - } -} - -/** - * Load advertisements into memory. - */ -function ad_cache_memcache_sync() { - variable_set('ad_memcache_timestamp', time()); - if (($error = ad_cache_memcache_requirements()) === TRUE) { - $result = db_query("SELECT aid, adtype FROM {ads} WHERE adstatus = 'active'"); - while ($ad = db_fetch_object($result)) { - ad_cache_memcache_sync_ad($ad->aid); - } - // Sync counters. - ad_cache_memcache_sync_ad(0); - } - else { - drupal_set_message(t('!module: Unable to syncronize cache: !error', array('!module' => 'ad_cache_memcache.module', '!error' => $error)), 'error'); - } -} - -/** - * Syncronize counts for given advertisement with database. - */ -function ad_cache_memcache_sync_ad($aid) { - if (($error = ad_cache_memcache_requirements()) !== TRUE) { - drupal_set_message(t('!module: Unable to syncronize cache: !error', array('!module' => 'ad_cache_memcache.module', '!error' => $error)), 'error'); - return; - } - - if (!ad_memcache_lock("ad-counters-$aid")) { - // Another process is already updating these values. - return; - } - $counters = ad_memcache_get("ad-counters-$aid"); - if (!is_array($counters)) { - ad_memcache_unlock("ad-counters-$aid"); - // There's nothing currently in memory for this ad. - return; - } - ad_memcache_delete("ad-counters-$aid"); - ad_memcache_unlock("ad-counters-$aid"); - foreach ($counters as $map) { - list($action, $group, $hostid, $timestamp) = explode(':', $map); - if ($action && $group && $hostid && $timestamp) { - $count = ad_memcache_get("ad-$action-$aid-$group-$hostid-$timestamp"); - if ($count) { - ad_memcache_decrement("ad-$action-$aid-$group-$hostid-$timestamp", $count); - db_query("UPDATE {ad_statistics} SET count = count + %d WHERE aid = %d AND action = '%s' AND date = %d AND adgroup = '%s' AND hostid = '%s'", $count, $aid, $action, $timestamp, $group, $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, adgroup, hostid, count) VALUES(%d, %d, '%s', '%s', '%s', %d)", $aid, $timestamp, $action, $group, $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 adgroup = '%s' AND hostid = '%s'", $count, $aid, $action, $timestamp, $group, $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'); - } - } - } - } - } - } -} - -/** - * Caches ad information into memory. -*/ -function ad_cache_memcache_build($changed = NULL) { - variable_set('ad_memcache_build', time()); - if (($error = ad_cache_memcache_requirements()) !== TRUE) { - drupal_set_message(t('!module: Unable to build cache: !error', array('!module' => 'ad_cache_memcache.module', '!error' => $error)), 'error'); - return; - } - - if (is_object($changed) && isset($changed->aid)) { - // An advertisement has changed, rebuild cache on next cron run. - variable_set('ad_memcache_build', ''); - } - else { - // Rebuilding entire cache. - $result = db_query("SELECT aid, adtype, redirect FROM {ads} WHERE adstatus = 'active' OR adstatus = 'approved' OR adstatus = 'offline'"); - while ($ad = db_fetch_object($result)) { - $node = node_load($ad->aid); - $ad->display = module_invoke("ad_$ad->adtype", 'display_ad', $node); - ad_memcache_set("ad-aid-$ad->aid", $ad); - $ads[] = $ad->aid; - - // Owner indexes. - $ad_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); - $counter = 0; - while ($owner = db_fetch_object($ad_owners)) { - $owners[$owner->uid][$ad->aid] = $ad->aid; - ad_memcache_set("ad-$ad->aid-uid", $owner->uid); - } - - $match = FALSE; - // Taxonomy index. - $terms = db_query('SELECT tid FROM {term_node} WHERE nid = %d', $ad->aid); - while ($term = db_fetch_object($terms)) { - $taxonomy[$term->tid][$ad->aid] = $ad->aid; - $match = TRUE; - } - if (!$match) { - $taxonomy[0][] = $ad->aid; - } - } - ad_memcache_set("ad-ads", $ads); - ad_memcache_set("ad-owners", $owners); - ad_memcache_set("ad-taxonomy", $taxonomy); - - // HostID index - $owners = db_query('SELECT uid, hostid FROM {ad_hosts}'); - while ($owner = db_fetch_object($owners)) { - ad_memcache_set("ad-hosts-$owner->uid", $owner->hostid); - if (($user = user_load(array('uid' => $owner->uid))) && - (user_access('host remote advertisements', $user))) { - ad_memcache_set("ad-hostid-$owner->hostid", TRUE); - } - } - // Always invoke hooks, they can decide to queue or act immediately. - ad_memcache_set('ad-cache-hook', module_invoke_all('ad_build_cache')); - } -} -