<?php
/**
 * Update permission names to allow non-English characters.
 * See http://drupal.org/node/510054.
 */
function role_delegation_update_6000() {
  $ret = array();
  $role_results = db_query('SELECT name FROM {role}');
  while ($role = db_fetch_object($role_results)) {
    // Check whether the assign role permission name has changed.
    if (preg_match('/[^a-zA-Z0-9 \\-_]/', $role->name)) {
      $old_role_name = preg_replace('/[^a-zA-Z0-9 \\-_]/', '', $role->name);
      $old_perm = "assign $old_role_name role";
      $new_perm = "assign {$role->name} role";

      // Update permissions with the new name.
      $perm_results = db_query('SELECT * FROM {permission}');
      while ($perm_row = db_fetch_object($perm_results)) {
        $perms = explode(', ', $perm_row->perm);
        $changed = FALSE;
        foreach ($perms as $key => $perm) {
          if ($perm == $old_perm) {
            $perms[$key] = $new_perm;
            $changed = TRUE;
          }
        }
        if ($changed) {
          $perms = db_escape_string(implode(', ', $perms));
          $ret[] = update_sql("UPDATE {permission} SET perm = '$perms' WHERE pid = {$perm_row->pid}");
        }
      }
    }
  }
  return $ret;
}
