You are here

function social_content_delete_old_nodes in Social Content 7.2

Deletes old nodes of all configured instances.

Return value

int The number of deleted nodes.

2 calls to social_content_delete_old_nodes()
drush_social_content_purge in ./social_content.drush.inc
Callback function for drush social-content-purge command.
social_content_cron in ./social_content.module
Implements hook_cron().
1 string reference to 'social_content_delete_old_nodes'
social_content_cronapi in ./social_content.module
Implements hook_cronapi().

File

./social_content.module, line 137
Social Content module.

Code

function social_content_delete_old_nodes() {
  $nids_to_delete = array();
  $instances = SocialContent::getAllInstances();
  foreach ($instances as $instance) {
    if (isset($instance->settings['auto_delete']) && is_numeric($instance->settings['auto_delete']) && $instance->settings['auto_delete'] > 0) {

      // Get all ids and timestamps of nodes imported by this instance.
      $nodes = db_query('SELECT internal_id, stamp FROM {social_content_history} WHERE instance = :instance', array(
        ':instance' => $instance->id,
      ))
        ->fetchAllKeyed(0, 1);
      $delete_date_seconds = $instance->settings['auto_delete'] * 24 * 60 * 60;
      foreach ($nodes as $nid => $created) {
        if (REQUEST_TIME - $created > $delete_date_seconds) {
          $nids_to_delete[] = $nid;
        }
      }
    }
  }
  $count = count($nids_to_delete);
  if ($count) {

    // Do not delete from history, so they are not imported again.
    $auto_delete_current = variable_get('social_content_auto_delete_history', TRUE);
    variable_set('social_content_auto_delete_history', FALSE);
    node_delete_multiple($nids_to_delete);

    // Restore custom value.
    variable_set('social_content_auto_delete_history', $auto_delete_current);
    watchdog('social_content', '%count nodes have been deleted for being too old.', array(
      '%count' => $count,
    ));
  }
  return $count;
}