You are here

function privatemsg_filter_inbox_rebuild_process in Privatemsg 7

Same name and namespace in other branches
  1. 6.2 privatemsg_filter/privatemsg_filter.admin.inc \privatemsg_filter_inbox_rebuild_process()
  2. 7.2 privatemsg_filter/privatemsg_filter.admin.inc \privatemsg_filter_inbox_rebuild_process()

Batch processing function for rebuilding the index.

1 string reference to 'privatemsg_filter_inbox_rebuild_process'
privatemsg_filter_inbox_rebuid_form_submit in privatemsg_filter/privatemsg_filter.admin.inc
Submit callback for inbox rebuild form.

File

privatemsg_filter/privatemsg_filter.admin.inc, line 222
Admin menu callbacks for privatemsg_filter module.

Code

function privatemsg_filter_inbox_rebuild_process(&$context) {

  // First run, initialize sandbox.
  if (!isset($context['sandbox']['current_thread_id'])) {
    $context['sandbox']['current_thread_id'] = 0;

    // Assume that the thread ids are distributed more or less equally over the
    // whole data set. This allows us to calculate the approximate progress.
    $context['sandbox']['max'] = db_query('SELECT MAX(thread_id) FROM {pm_index}')
      ->fetchField();
    $context['results']['count'] = 0;
  }

  // Fetch the 10 next thread_ids.
  $threads = db_query_range('SELECT DISTINCT thread_id FROM {pm_index} WHERE thread_id > :thread_id ORDER BY thread_id ASC', 0, 20, array(
    ':thread_id' => $context['sandbox']['current_thread_id'],
  ))
    ->fetchCol();
  if (!empty($threads)) {

    // By limiting this slow query (having condition with 2 depending subqueries)
    // on a specific set of threads, this allows us to process the slow having
    // part on a relatively small subset of pm_index that can be selected based on
    // the thread_id index.
    $sql = "SELECT pmi.thread_id, pmi.recipient, pmi.type FROM {pm_index} pmi WHERE thread_id IN (:threads) GROUP BY pmi.thread_id, pmi.recipient, pmi.type HAVING ((SELECT pmf.author FROM {pm_message} pmf WHERE pmf.mid = pmi.thread_id) = pmi.recipient AND pmi.type IN ('user', 'hidden') AND COUNT(pmi.thread_id) > 1) OR (SELECT COUNT(*) FROM {pm_message} pmf INNER JOIN {pm_index} pmif ON (pmf.mid = pmif.mid) WHERE pmif.thread_id = pmi.thread_id AND pmf.author <> pmi.recipient AND pmi.type IN ('user', 'hidden')) > 0";
    $result = db_query($sql, array(
      ':threads' => $threads,
    ));
    foreach ($result as $row) {
      $row->uid = $row->recipient;

      // $row is an object with uid property, so we pass it to the function as a
      // pseudo user object.
      privatemsg_filter_add_tags(array(
        $row->thread_id,
      ), variable_get('privatemsg_filter_inbox_tag', ''), $row);
      $context['results']['count']++;
    }
    $context['sandbox']['current_thread_id'] = max($threads);
  }

  // Set #finished based on sandbox.
  $context['finished'] = empty($context['sandbox']['max']) ? 1 : $context['sandbox']['current_thread_id'] / $context['sandbox']['max'];
}