You are here

function message_cron in Message 7

Same name and namespace in other branches
  1. 8 message.module \message_cron()

Implements hook_cron().

Fetch all message types and purge old messages.

3 calls to message_cron()
MessageCron::testPurge in tests/message.test
Test purging of messages upon message_cron according to message type purge settings.
MessageCron::testPurgeGlobalSettings in tests/message.test
Test global purge settings and overriding them.
MessageCron::testPurgeRequestLimit in tests/message.test
Test compliance with MESSAGE_DELETE_PER_PURGE.

File

./message.module, line 317
API functions to manipulate messages.

Code

function message_cron() {

  // The maximal amount of messages to purge per cron run.
  $purge_limit = variable_get('message_delete_cron_limit', MESSAGE_PURGE_LIMIT);

  // Messages to be deleted.
  $purge_messages = array();

  // Names of non global-purge-settings overriding message types.
  $no_override_type_names = array();

  // Message types that override global purge settings.
  $override_types = array();

  // Iterate all message types to distinguish between overriding and non-
  // overriding types.
  foreach (message_type_load() as $message_type) {
    if (empty($message_type->data['purge']['override'])) {
      $no_override_type_names[] = $message_type->name;
    }
    else {

      // For overriding types, store the type and not its name to later extract
      // the specific purge settings.
      $override_types[] = $message_type;
    }
  }

  // Gather purgeable messages of overriding types.
  foreach ($override_types as $message_type) {

    // Ignore message type with unchecked "Purge messages". Also make sure that
    // purging settings are present.
    if (empty($message_type->data['purge']) || empty($message_type->data['purge']['enabled'])) {
      continue;
    }
    $purge_messages += message_get_purgeable_by_type($purge_limit, $message_type->name, $message_type->data['purge']);
  }

  // Gather purgeable messages of non-overriding types according to global
  // settings.
  if (!empty($no_override_type_names)) {

    // Do nothing if purge isn't enabled.
    if (variable_get('message_purge_enable', FALSE)) {
      $purge_settings = array(
        'quota' => variable_get('message_purge_quota', NULL),
        'days' => variable_get('message_purge_days', NULL),
      );
      $purge_messages += message_get_purgeable_by_type($purge_limit, $no_override_type_names, $purge_settings);
    }
  }

  // Delete all gathered messages.
  if (!empty($purge_messages)) {
    message_delete_multiple(array_keys($purge_messages));
  }
}