sql_drush_sql_sync_sanitize

7 sync.sql.inc sql_drush_sql_sync_sanitize($site)

Sql sync sanitization function. This hook function will sanitize usernames and passwords in the user table when the --sanitize option is used. It is also an example of how to write a database sanitizer for sql sync.

To write your own sync hook function, define mymodule_drush_sql_sync_sanitize() and follow the form of this function to add your own database sanitization operations via the register post-sync op function; sync hook function needs to do; sql-sync takes care of the rest.

The function below has a lot of logic to process user preferences and generate the correct SQL regardless of whether Postgres, Mysql, Drupal 6 or Drupal 7 is in use. A simpler sanitize function that always used default values and only worked with Drupal 6 + mysql appears in the drush.api.php.

See also

drush_sql_register_post_sync_op(). This is the only thing that the

hook_drush_sql_sync_sanitize().

File

contributions/drush/commands/sql/sync.sql.inc, line 70

Code

<?php
function sql_drush_sql_sync_sanitize($site) {
  $site_settings = drush_sitealias_get_record($site);
  $user_table_updates = array();
  $message_list = array();

  // Sanitize passwords.
  $newpassword = drush_get_option(array('sanitize-password', 'destination-sanitize-password'), 'password');
  if ($newpassword != 'no') {
    $major_version = drush_drupal_major_version();
    $pw_op = "";

    // In Drupal 6, passwords are hashed via the MD5 algorithm.
    if ($major_version == 6) {
      $pw_op = "MD5('$newpassword')";
    }
    // In Drupal 7, passwords are hashed via a more complex algorithm,
    // available via the user_hash_password function.
    elseif ($major_version >= 7) {
      include_once DRUPAL_ROOT . '/includes/password.inc';
      include_once DRUPAL_ROOT . '/includes/bootstrap.inc';
      $hash = user_hash_password($newpassword);
      $pw_op = "'$hash'";
    }
    if (!empty($pw_op)) {
      $user_table_updates[] = "pass = $pw_op";
      $message_list[] =  "passwords";
    }
  }

  // Sanitize email addresses.
  $newemail = drush_get_option(array('sanitize-email', 'destination-sanitize-email'), 'user+%uid@localhost');
  if ($newemail != 'no') {
    if (strpos($newemail, '%') !== FALSE) {
      // We need a different sanitization query for Postgres and Mysql.
      $db_driver = $site_settings['databases']['default']['default']['driver'];
      if ($db_driver == 'pgsql') {
        $email_map = array(
          '%uid' => "' || uid || '",
          '%mail' => "' || replace(mail, '@', '_') || '",
          '%name' => "' || replace(name, ' ', '_') || '",
        );
        $newmail =  "'" . str_replace(array_keys($email_map), array_values($email_map), $newemail) . "'";
      }
      else {
        $email_map = array(
          '%uid' => "', uid, '",
          '%mail' => "', replace(mail, '@', '_'), '",
          '%name' => "', replace(name, ' ', '_'), '",
        );
        $newmail =  "concat('" . str_replace(array_keys($email_map), array_values($email_map), $newemail) . "')";
      }
    }
    $user_table_updates[] = "mail = $newmail";
    $message_list[] = 'email addresses';
  }

  if (!empty($user_table_updates)) {
    $sanitize_query = "update users set " . implode(', ', $user_table_updates) . " where uid > 0;";
    drush_sql_register_post_sync_op('user-email', dt('Reset !message in user table', array('!message' => implode(' and ', $message_list))), $sanitize_query);
  }

}
?>