Commit 43a421e4 authored by Joep Bär's avatar Joep Bär Committed by Jeroen Tubex
Browse files

Issue #3200123 by PROMES, JeroenT: Make purging more efficient

parent b8badecc
Loading
Loading
Loading
Loading
+9 −4
Original line number Diff line number Diff line
@@ -36,19 +36,24 @@ function webform_purge_cron() {
      // Get the records elegible for purging.
      $limit = variable_get('webform_purge_cron_limit', 100000);
      if ($purge_all == 1) {
        $query = db_query('SELECT * FROM {webform_submissions} WHERE submitted < :purge_timestamp' . ($limit ? ' LIMIT ' . check_plain($limit) : ''), array(':purge_timestamp' => $purge_timestamp));
        $query = db_query('SELECT nid, sid FROM {webform_submissions} WHERE submitted < :purge_timestamp ORDER BY nid' . ($limit ? ' LIMIT ' . check_plain($limit) : ''), array(':purge_timestamp' => $purge_timestamp));
      }
      else {
        $query = db_query('SELECT * FROM {webform_submissions} WHERE submitted < :purge_timestamp AND nid IN (:nid)' . ($limit ? ' LIMIT ' . check_plain($limit) : ''), array(':purge_timestamp' => $purge_timestamp, ':nid' => $checkboxes));
        $query = db_query('SELECT nid, sid FROM {webform_submissions} WHERE submitted < :purge_timestamp AND nid IN (:nid) ORDER BY nid' . ($limit ? ' LIMIT ' . check_plain($limit) : ''), array(':purge_timestamp' => $purge_timestamp, ':nid' => $checkboxes));
      }

      $rows = $query->fetchAll();

      // If submissions found to purge, get on with it.
      if (count($rows) > 0) {
        $deleted = 0;
        $deleted = $nid = 0;
        foreach ($rows as $row) {
          // Results are sorted by the node ID. When the node ID is the same,
          // don't load the node again from the database.
          if ($row->nid != $nid) {
            $node = node_load($row->nid);
            $nid = $row->nid;
          }
          $submission = webform_menu_submission_load($row->sid, $row->nid);
          webform_submission_delete($node, $submission);
          $deleted++;