You are here

function prod_monitor_update_6103 in Production check & Production monitor 6

Update 6103 - Update xmlrpc settings for all sites.

File

prod_monitor/prod_monitor.install, line 205

Code

function prod_monitor_update_6103(&$sandbox) {
  $prefix = '_prod_check_';
  $ret = array();

  // Update 5 sites at a time
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['current_site'] = 0;
    $sandbox['max'] = db_result(db_query('SELECT COUNT(DISTINCT id) FROM {prod_monitor_sites}'));
  }
  $sites = db_query_range("SELECT id, settings FROM {prod_monitor_sites} WHERE id > %d ORDER BY id ASC", $sandbox['current_site'], 0, 5);
  while ($site = db_fetch_object($sites)) {
    $change = FALSE;
    $site->settings = unserialize($site->settings);

    // Adjust functions
    foreach ($site->settings['functions'] as $set => &$data) {
      foreach ($data['functions'] as $function => $title) {
        if (stripos($function, $prefix) === FALSE) {

          // Can't 'rename' keys, so we remove them and add a new one.
          unset($data['functions'][$function]);
          $data['functions'][$prefix . $function] = $title;
          $change = TRUE;
        }
      }
    }

    // Prevent any chance of the next loop from going kaka cuckoo.
    // See warning at http://php.net/manual/en/control-structures.foreach.php
    unset($data, $function);

    // Adjust checks
    foreach ($site->settings['checks'] as $set => &$calls) {
      foreach ($calls as $key => &$function) {
        if (stripos($function, $prefix) === FALSE) {
          $function = $prefix . $function;
          $change = TRUE;
        }
      }
    }

    // Only update record if there were changes. Added to prevent loss of data
    // when (accidentally) running this update twice.
    if ($change) {
      $site->settings = serialize($site->settings);

      // We want the site ID to be displayed after the update has completed,
      // hence the inclusion in the query and not passed as an argument to
      // db_query()
      $sql = "UPDATE {prod_monitor_sites} SET settings = '%s' WHERE id = {$site->id}";

      // No use of update_sql() because we use serialised data.
      $result = db_query($sql, $site->settings);
      $ret[] = array(
        'success' => $result !== FALSE,
        'query' => check_plain($sql),
      );
    }
    $sandbox['progress']++;
    $sandbox['current_site'] = $site->id;
  }
  $ret['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
  return $ret;
}