Index: modules/block/block.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/block/block.module,v
retrieving revision 1.304
diff -u -r1.304 block.module
--- modules/block/block.module	23 Apr 2008 20:01:48 -0000	1.304
+++ modules/block/block.module	4 May 2008 03:12:17 -0000
@@ -199,7 +199,7 @@
       return $blocks;
 
     case 'configure':
-      $box = array('format' => FILTER_FORMAT_DEFAULT);
+      $box = array('format' => filter_default_format());
       if ($delta) {
         $box = block_box_get($delta);
       }
@@ -327,7 +327,7 @@
     '#weight' => -17,
   );
   if (!isset($edit['format'])) {
-    $edit['format'] = FILTER_FORMAT_DEFAULT;
+    $edit['format'] = filter_default_format();
   }
   $form['body_field']['format'] = filter_form($edit['format'], -16);
 
@@ -336,7 +336,7 @@
 
 function block_box_save($edit, $delta) {
   if (!filter_access($edit['format'])) {
-    $edit['format'] = FILTER_FORMAT_DEFAULT;
+    $edit['format'] = filter_default_format();
   }
 
   db_query("UPDATE {boxes} SET body = '%s', info = '%s', format = %d WHERE bid = %d", $edit['body'], $edit['info'], $edit['format'], $delta);
Index: modules/filter/filter.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/filter/filter.module,v
retrieving revision 1.210
diff -u -r1.210 filter.module
--- modules/filter/filter.module	23 Apr 2008 20:01:50 -0000	1.210
+++ modules/filter/filter.module	4 May 2008 03:12:19 -0000
@@ -7,14 +7,6 @@
  */
 
 /**
- * Special format ID which means "use the default format".
- *
- * This value can be passed to the filter APIs as a format ID: this is
- * equivalent to not passing an explicit format at all.
- */
-define('FILTER_FORMAT_DEFAULT', 0);
-
-/**
  * Implementation of hook_help().
  */
 function filter_help($path, $arg) {
@@ -22,12 +14,11 @@
     case 'admin/help#filter':
       $output = '<p>' . t("The filter module allows administrators to configure text input formats for use on your site. An input format defines the HTML tags, codes, and other input allowed in both content and comments, and is a key feature in guarding against potentially damaging input from malicious users. Two input formats included by default are <em>Filtered HTML</em> (which allows only an administrator-approved subset of HTML tags) and <em>Full HTML</em> (which allows the full set of HTML tags). Additional input formats may be created by an administrator.") . '</p>';
       $output .= '<p>' . t('Each input format uses filters to manipulate text, and most input formats apply several different filters to text in a specific order. Each filter is designed for a specific purpose, and generally either adds, removes or transforms elements within user-entered text before it is displayed. A filter does not change the actual content of a post, but instead, modifies it temporarily before it is displayed. A filter may remove unapproved HTML tags, for instance, while another automatically adds HTML to make links referenced in text clickable.') . '</p>';
-      $output .= '<p>' . t('Users with access to more than one input format can use the <em>Input format</em> fieldset to choose between available input formats when creating or editing multi-line content. Administrators determine the input formats available to each user role, select a default input format, and control the order of formats listed in the <em>Input format</em> fieldset.') . '</p>';
+      $output .= '<p>' . t('Users with access to more than one input format can use the <em>Input format</em> fieldset to choose between available input formats when creating or editing multi-line content. Administrators determine the input formats available to each user role and control the order of formats listed in the <em>Input format</em> fieldset.') . '</p>';
       $output .= '<p>' . t('For more information, see the online handbook entry for <a href="@filter">Filter module</a>.', array('@filter' => 'http://drupal.org/handbook/modules/filter/')) . '</p>';
       return $output;
     case 'admin/settings/filters':
-      $output = '<p>' . t('Use the list below to review the input formats available to each user role, to select a default input format, and to control the order of formats listed in the <em>Input format</em> fieldset. (The <em>Input format</em> fieldset is displayed below textareas when users with access to more than one input format create multi-line content.) The input format selected as <em>Default</em> is available to all users and, unless another format is selected, is applied to all content. All input formats are available to users in roles with the "administer filters" permission.') . '</p>';
-      $output .= '<p>' . t('Since input formats, if available, are presented in the same order as the list below, it may be helpful to arrange the formats in descending order of your preference for their use. To change the order of an input format, grab a drag-and-drop handle under the <em>Name</em> column and drag to a new location in the list. (Grab a handle by clicking and holding the mouse while hovering over a handle icon.) Remember that your changes will not be saved until you click the <em>Save changes</em> button at the bottom of the page.') . '</p>';
+      $output = '<p>' . t('Use the list below to review the input formats available to each user role, to select a default input format, and to control the order of formats listed in the <em>Input format</em> fieldset. (The <em>Input format</em> fieldset with textareas when users with access to more than one input format create multi-line content.) All input formats are available to users in roles with the "administer filters" permission.') . '</p>';
       return $output;
     case 'admin/settings/filters/%':
       return '<p>' . t('Every <em>filter</em> performs one particular change on the user input, for example stripping out malicious HTML or making URLs clickable. Choose which filters you want to apply to text in this input format. If you notice some filters are causing conflicts in the output, you can <a href="@rearrange">rearrange them</a>.', array('@rearrange' => url('admin/settings/filters/' . $arg[3] . '/order'))) . '</p>';
@@ -140,7 +131,8 @@
 }
 
 function filter_format_load($arg) {
-  return filter_formats($arg);
+  $formats = filter_formats();
+  return $formats[$arg];
 }
 
 /**
@@ -154,9 +146,16 @@
  * Implementation of hook_perm().
  */
 function filter_perm() {
-  return array(
+  $perms = array(
     'administer filters' => t('Manage input formats and filters, and select which roles may use them. %warning', array('%warning' => t('Warning: Give to trusted roles only; this permission has security implications.'))),
   );
+
+  // Generate permissions for each input format.
+  $result = db_query('SELECT * FROM {filter_formats} ORDER BY weight');
+  while ($format = db_fetch_object($result)) {
+    $perms['use '. check_plain($format->name)] = t('Use %input_format in forms when entering or editing content.', array('%input_format' => $format->name));
+  }
+  return $perms;
 }
 
 /**
@@ -289,41 +288,35 @@
 }
 
 /**
- * Retrieve a list of input formats.
- */
-function filter_formats($index = NULL) {
-  global $user;
-  static $formats;
-
-  // Administrators can always use all input formats.
-  $all = user_access('administer filters');
-
-  if (!isset($formats)) {
-    $formats = array();
-
-    $query = 'SELECT * FROM {filter_formats}';
-
-    // Build query for selecting the format(s) based on the user's roles.
-    $args = array();
-    if (!$all) {
-      $where = array();
-      foreach ($user->roles as $rid => $role) {
-        $where[] = "roles LIKE '%%,%d,%%'";
-        $args[] = $rid;
-      }
-      $query .= ' WHERE ' . implode(' OR ', $where) . ' OR format = %d';
-      $args[] = variable_get('filter_default_format', 1);
-    }
-
-    $result = db_query($query . ' ORDER by weight', $args);
+ * Retrieve a list of input formats the current user can use.
+ *
+ * @param $account
+ *   Optional. If provided, formats for this user account only will be returned.
+ *   All formats will be returned otherwise.
+ */
+function filter_formats($account = NULL) {
+  static $formats = array();
+
+  // Check if we've already loaded format data before doing a query.
+  if (!isset($formats['all'])) {
+    $formats['all'] = array();
+    $result = db_query('SELECT * FROM {filter_formats} ORDER BY weight');
     while ($format = db_fetch_object($result)) {
-      $formats[$format->format] = $format;
+      $formats['all'][$format->format] = $format;
     }
   }
-  if (isset($index)) {
-    return isset($formats[$index]) ? $formats[$index] : FALSE;
+
+  // Build a list of user-specific formats if not already set.
+  if (isset($account) && !isset($formats['user'][$account->uid])) {
+    $formats['user'][$account->uid] = array();
+    foreach ($formats['all'] as $format) {
+      if (filter_access($format, $account)) {
+        $formats['user'][$account->uid][$format->format] = $format;
+      }
+    }
   }
-  return $formats;
+
+  return isset($account) ? $formats['users'][$account->uid] : $formats['all'];
 }
 
 /**
@@ -354,17 +347,21 @@
 }
 
 /**
- * Resolve a format id, including the default format.
+ * Get the default input format ID for the specified role.
+ * 
+ * @param $account
+ *   Optional. The user for which the default ID will be retreived.
  */
-function filter_resolve_format($format) {
-  return $format == FILTER_FORMAT_DEFAULT ? variable_get('filter_default_format', 1) : $format;
+function filter_default_format($account = NULL) {
+  global $user;
+  return !isset($account) ? $user->default_format : $account->default_format;
 }
+
 /**
  * Check if text in a certain input format is allowed to be cached.
  */
 function filter_format_allowcache($format) {
   static $cache = array();
-  $format = filter_resolve_format($format);
   if (!isset($cache[$format])) {
     $cache[$format] = db_result(db_query('SELECT cache FROM {filter_formats} WHERE format = %d', $format));
   }
@@ -413,8 +410,7 @@
  * @param $text
  *    The text to be filtered.
  * @param $format
- *    The format of the text to be filtered. Specify FILTER_FORMAT_DEFAULT for
- *    the default format.
+ *    The format of the text to be filtered.
  * @param $check
  *    Whether to check the $format with filter_access() first. Defaults to TRUE.
  *    Note that this will check the permissions of the current user, so you
@@ -422,11 +418,9 @@
  *    showing content that is not (yet) stored in the database (eg. upon preview),
  *    set to TRUE so the user's permissions are checked.
  */
-function check_markup($text, $format = FILTER_FORMAT_DEFAULT, $check = TRUE) {
+function check_markup($text, $format, $check = TRUE) {
   // When $check = TRUE, do an access check on $format.
   if (isset($text) && (!$check || filter_access($format))) {
-    $format = filter_resolve_format($format);
-
     // Check for a cached version of this piece of text.
     $cache_id = $format . ':' . md5($text);
     if ($cached = cache_get($cache_id, 'cache_filter')) {
@@ -479,9 +473,11 @@
  * @return
  *   HTML for the form element.
  */
-function filter_form($value = FILTER_FORMAT_DEFAULT, $weight = NULL, $parents = array('format')) {
-  $value = filter_resolve_format($value);
-  $formats = filter_formats();
+function filter_form($value = NULL, $weight = NULL, $parents = array('format')) {
+  global $user;
+
+  $value = !isset($value) ? filter_default_format() : $value;
+  $formats = filter_formats($user);
 
   $extra = theme('filter_tips_more_info');
 
@@ -514,7 +510,7 @@
     // Only one format available: use a hidden form item and only show tips.
     $format = array_shift($formats);
     $form[$format->format] = array('#type' => 'value', '#value' => $format->format, '#parents' => $parents);
-    $tips = _filter_tips(variable_get('filter_default_format', 1), FALSE);
+    $tips = _filter_tips(filter_default_format(), FALSE);
     $form['format']['guidelines'] = array(
       '#title' => t('Formatting guidelines'),
       '#value' => theme('filter_tips', $tips, FALSE, $extra),
@@ -535,17 +531,45 @@
 }
 
 /**
- * Returns TRUE if the user is allowed to access this format.
+ * Check if a user has access to a particular filter.
+ *
+ * @param $format
+ *   The format object that will be checked for access.
+ * @param $account
+ *   The user object that access will be checked against.
+ *
+ * @return
+ *   Boolean TRUE if the user has access to the requested filter.
  */
-function filter_access($format) {
-  $format = filter_resolve_format($format);
-  if (user_access('administer filters') || ($format == variable_get('filter_default_format', 1))) {
-    return TRUE;
-  }
-  else {
-    $formats = filter_formats();
-    return isset($formats[$format]);
+function filter_access($format, $account = NULL) {
+  return user_access('use '. check_plain($format->name), $account) || user_access('administer filters', $account);
+}
+
+/**
+ * Retreive a list of a roles that are allowed to use a particular format.
+ *
+ * @param $format
+ *   The format object that will be checked for access.
+ *
+ * @return
+ *   An array of roles structured $rid => $role_name.
+ */
+function filter_format_roles($format) {
+  return user_roles(FALSE, 'use ' . check_plain($format->name));
+}
+
+/**
+ * Retreive a list of formats that are allowed for a particular role.
+ */
+function filter_role_formats($rid) {
+  $formats = array();
+  foreach (filter_formats() as $format) {
+    $roles = filter_format_roles($format);
+    if (isset($roles[$rid])) {
+      $formats[$format->format] = $format->name;
+    }
   }
+  return $formats;
 }
 
 /**
@@ -557,8 +581,10 @@
  * Helper function for fetching filter tips.
  */
 function _filter_tips($format, $long = FALSE) {
+  global $user;
+
   if ($format == -1) {
-    $formats = filter_formats();
+    $formats = filter_formats($user);
   }
   else {
     $formats = array(db_fetch_object(db_query("SELECT * FROM {filter_formats} WHERE format = %d", $format)));
Index: modules/filter/filter.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/filter/filter.install,v
retrieving revision 1.9
diff -u -r1.9 filter.install
--- modules/filter/filter.install	14 Apr 2008 17:48:37 -0000	1.9
+++ modules/filter/filter.install	4 May 2008 03:12:19 -0000
@@ -64,13 +64,6 @@
         'default' => '',
         'description' => t('Name of the input format (Filtered HTML).'),
       ),
-      'roles' => array(
-        'type' => 'varchar',
-        'length' => 255,
-        'not null' => TRUE,
-        'default' => '',
-        'description' => t('A comma-separated string of roles; references {role}.rid.'), // This is bad since you can't use joins, nor index.
-      ),
       'cache' => array(
         'type' => 'int',
         'not null' => TRUE,
@@ -99,6 +92,11 @@
 }
 
 /**
+ * @defgroup updates-6.x-to-7.x Filter updates from 6.x to 7.x
+ * @{
+ */
+
+/**
  * Add a weight column to the filter formats table.
  */
 function filter_update_7000() {
@@ -122,3 +120,34 @@
   }
   return $ret;
 }
+
+/**
+ * Move filter format access to the user permissions handler.
+ */
+function filter_update_7002() {
+  $ret = array();
+  // Get list of roles to work with.
+  $result = db_query("SELECT rid FROM {role}");
+  while ($role = db_result($result)) {
+    $roles[$role] = $role;
+  }
+  
+  // Move role data from filter_formats to user permissions.
+  $result = db_query("SELECT name, roles FROM {filter_formats}");
+  while ($format = db_fetch_object($result)) {
+    $format_roles = explode(',', $format->roles);
+    foreach ($format_roles as $format_role) {
+      if (in_array($format_role, $roles)) {
+        $ret[] = update_sql("UPDATE {permission} SET perm = CONCAT(perm, ', use " . db_escape_string(check_plain($format->name)) . "') WHERE rid = " . $format_role);
+      }
+    }
+  }
+  // Finally, drop the roles field from filter_formats.
+  db_drop_field($ret, 'filter_formats', 'roles');
+  return $ret;
+}
+
+/**
+ * @} End of "defgroup updates-6.x-to-7.x"
+ * The next series of updates should start at 8000.
+ */
Index: modules/filter/filter.admin.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/filter/filter.admin.inc,v
retrieving revision 1.11
diff -u -r1.11 filter.admin.inc
--- modules/filter/filter.admin.inc	14 Apr 2008 17:48:37 -0000	1.11
+++ modules/filter/filter.admin.inc	4 May 2008 03:12:19 -0000
@@ -20,23 +20,42 @@
   $error = FALSE;
 
   $form = array('#tree' => TRUE);
+
+  $form['list'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Input format order'),
+    '#description' => t('Input formats are presented in the same order as the list below. It may be helpful to arrange the formats in the order of your preference for their use.'),
+  );
+
   foreach ($formats as $id => $format) {
-    $roles = array();
-    foreach (user_roles() as $rid => $name) {
-      // Prepare a roles array with roles that may access the filter.
-      if (strstr($format->roles, ",$rid,")) {
-        $roles[] = $name;
-      }
-    }
+    $roles = filter_format_roles($format);
     $default = ($id == variable_get('filter_default_format', 1));
     $options[$id] = '';
-    $form[$id]['name'] = array('#value' => $format->name);
-    $form[$id]['roles'] = array('#value' => $default ? t('All roles may use default format') : ($roles ? implode(', ', $roles) : t('No roles may use this format')));
-    $form[$id]['configure'] = array('#value' => l(t('configure'), 'admin/settings/filters/' . $id));
-    $form[$id]['delete'] = array('#value' => $default ? '' : l(t('delete'), 'admin/settings/filters/delete/' . $id));
-    $form[$id]['weight'] = array('#type' => 'weight', '#default_value' => $format->weight);
+    $form['list'][$id]['name'] = array('#value' => $format->name);
+    $form['list'][$id]['roles'] = array('#value' => $roles ? implode(', ', $roles) : t('No roles may use this format'));
+    $form['list'][$id]['configure'] = array('#value' => l(t('configure'), 'admin/settings/filters/' . $id));
+    $form['list'][$id]['delete'] = array('#value' => $default ? '' : l(t('delete'), 'admin/settings/filters/delete/' . $id));
+    $form['list'][$id]['weight'] = array('#type' => 'weight', '#default_value' => $format->weight);
+  }
+
+  // Add a row of checkboxes for form group.
+  $form['roles'] = array('#type' => 'fieldset',
+    '#title' => t('Role default formats'),
+    '#description' =>
+      '<p>' . t('Choose a default format for each role on your site. If a user has multiple roles assigned to their account, the default format will be determined by the input format order. The first default input format found beyond the anonymous and authenticated roles will be used.') . '</p>' .
+      '<p>' . t('You may grant roles access to more input formats from the <a href="!url">permissions page</a>.', array('!url' => url('admin/user/permissions'))) . '</p>',
+    '#tree' => TRUE,
+  );
+
+  foreach (user_roles(FALSE, NULL, FALSE) as $rid => $role) {
+    $form['roles'][$rid] = array(
+      '#type' => 'select',
+      '#title' => $role->name,
+      '#default_value' => $role->default_format,
+      '#options' => filter_role_formats($rid),
+    );
   }
-  $form['default'] = array('#type' => 'radios', '#options' => $options, '#default_value' => variable_get('filter_default_format', 1));
+
   $form['submit'] = array('#type' => 'submit', '#value' => t('Save changes'));
   return $form;
 }
@@ -62,31 +81,40 @@
  * @ingroup themeable
  */
 function theme_filter_admin_overview($form) {
+  // Generate a table for the input format list.
   $rows = array();
-  foreach ($form as $id => $element) {
-    if (isset($element['roles']) && is_array($element['roles'])) {
-      $element['weight']['#attributes']['class'] = 'input-format-order-weight';
-      $rows[] = array(
-        'data' => array(
-          check_plain($element['name']['#value']),
-          drupal_render($element['roles']),
-          drupal_render($form['default'][$id]),
-          drupal_render($element['weight']),
-          drupal_render($element['configure']),
-          drupal_render($element['delete']),
-        ),
-        'class' => 'draggable',
-      );
-      unset($form[$id]);
-    }
+  foreach (element_children($form['list']) as $key) {
+    $form['list'][$key]['weight']['#attributes']['class'] = 'input-format-order-weight';
+    $rows[] = array(
+      'data' => array(
+        check_plain($form['list'][$key]['name']['#value']),
+        drupal_render($form['list'][$key]['roles']),
+        drupal_render($form['list'][$key]['weight']),
+        drupal_render($form['list'][$key]['configure']),
+        drupal_render($form['list'][$key]['delete']),
+      ),
+      'class' => 'draggable',
+    );
   }
-  $header = array(t('Name'), t('Roles'), t('Default'), t('Weight'), array('data' => t('Operations'), 'colspan' => 2));
-  $output = theme('table', $header, $rows, array('id' => 'input-format-order'));
-  $output .= drupal_render($form);
+  $header = array(t('Name'), t('Roles'), t('Weight'), array('data' => t('Operations'), 'colspan' => 2));
+  $form['list']['#children'] = theme('table', $header, $rows, array('id' => 'input-format-order'));
 
+  // Add table drag for the input format list.
   drupal_add_tabledrag('input-format-order', 'order', 'sibling', 'input-format-order-weight');
 
-  return $output;
+  // Generate a table for the role default formats.
+  $rows = array();
+  foreach (element_children($form['roles']) as $key) {
+    $row = array();
+    $row[] = check_plain($form['roles'][$key]['#title']);
+    unset($form['roles'][$key]['#title']);
+    $row[] = drupal_render($form['roles'][$key]);
+    $rows[] = $row;
+  }
+  $header = array(t('Role'), t('Default format'));
+  $form['roles']['#children'] = theme('table', $header, $rows, array('id' => 'input-format-roles'));
+
+  return drupal_render($form);
 }
 
 /**
@@ -95,7 +123,7 @@
 function filter_admin_format_page($format = NULL) {
   if (!isset($format->name)) {
     drupal_set_title(t("Add input format"));
-    $format = (object)array('name' => '', 'roles' => '', 'format' => '');
+    $format = (object)array('name' => '', 'format' => '');
   }
   return drupal_get_form('filter_admin_format_form', $format);
 }
@@ -108,12 +136,6 @@
  * @see filter_admin_format_form_submit()
  */
 function filter_admin_format_form(&$form_state, $format) {
-  $default = ($format->format == variable_get('filter_default_format', 1));
-  if ($default) {
-    $help = t('All roles for the default format must be enabled and cannot be changed.');
-    $form['default_format'] = array('#type' => 'hidden', '#value' => 1);
-  }
-
   $form['name'] = array('#type' => 'textfield',
     '#title' => t('Name'),
     '#default_value' => $format->name,
@@ -121,23 +143,6 @@
     '#required' => TRUE,
   );
 
-  // Add a row of checkboxes for form group.
-  $form['roles'] = array('#type' => 'fieldset',
-    '#title' => t('Roles'),
-    '#description' => $default ? $help : t('Choose which roles may use this filter format. Note that roles with the "administer filters" permission can always use all the filter formats.'),
-    '#tree' => TRUE,
-  );
-
-  foreach (user_roles() as $rid => $name) {
-    $checked = strstr($format->roles, ",$rid,");
-    $form['roles'][$rid] = array('#type' => 'checkbox',
-      '#title' => $name,
-      '#default_value' => ($default || $checked),
-    );
-    if ($default) {
-      $form['roles'][$rid]['#disabled'] = TRUE;
-    }
-  }
   // Table with filters
   $all = filter_list_all();
   $enabled = filter_list_format($format->format);
@@ -156,6 +161,7 @@
   }
   if (!empty($format->format)) {
     $form['format'] = array('#type' => 'hidden', '#value' => $format->format);
+    $form['previous_name'] = array('#type' => 'hidden', '#value' => $format->name);
 
     // Composition tips (guidelines)
     $tips = _filter_tips($format->format, FALSE);
@@ -195,7 +201,7 @@
   $name = trim($form_state['values']['name']);
   $cache = TRUE;
 
-  // Add a new filter format.
+  // Add a new input format.
   if (!$format) {
     $new = TRUE;
     db_query("INSERT INTO {filter_formats} (name) VALUES ('%s')", $name);
@@ -203,6 +209,8 @@
     drupal_set_message(t('Added input format %format.', array('%format' => $name)));
   }
   else {
+    // Update the name of affected permission.
+    user_rename_permission('use '. check_plain($form_state['values']['previous_name']), 'use '. check_plain($name));
     drupal_set_message(t('The input format settings have been updated.'));
   }
 
@@ -219,25 +227,7 @@
     }
   }
 
-  // We store the roles as a string for ease of use.
-  // We should always set all roles to TRUE when saving a default role.
-  // We use leading and trailing comma's to allow easy substring matching.
-  $roles = array();
-  if (isset($form_state['values']['roles'])) {
-    foreach ($form_state['values']['roles'] as $id => $checked) {
-      if ($checked) {
-        $roles[] = $id;
-      }
-    }
-  }
-  if (!empty($form_state['values']['default_format'])) {
-    $roles = ',' . implode(',', array_keys(user_roles())) . ',';
-  }
-  else {
-    $roles = ',' . implode(',', $roles) . ',';
-  }
-
-  db_query("UPDATE {filter_formats} SET cache = %d, name='%s', roles = '%s' WHERE format = %d", $cache, $name, $roles, $format);
+  db_query("UPDATE {filter_formats} SET cache = %d, name='%s' WHERE format = %d", $cache, $name, $format);
 
   cache_clear_all($format . ':', 'cache_filter', TRUE);
 
@@ -256,14 +246,14 @@
  * @ingroup forms
  * @see filter_admin_delete_submit()
  */
-function filter_admin_delete() {
-  $format = arg(4);
+function filter_admin_delete($form_state, $format) {
   $format = db_fetch_object(db_query('SELECT * FROM {filter_formats} WHERE format = %d', $format));
 
   if ($format) {
     if ($format->format != variable_get('filter_default_format', 1)) {
       $form['format'] = array('#type' => 'hidden', '#value' => $format->format);
       $form['name'] = array('#type' => 'hidden', '#value' => $format->name);
+      $form['destination'] = array('#type' => 'hidden', '#value' => 'admin/settings/filters');
 
       return confirm_form($form, t('Are you sure you want to delete the input format %format?', array('%format' => $format->name)), 'admin/settings/filters', t('If you have any content left in this input format, it will be switched to the default input format. This action cannot be undone.'), t('Delete'), t('Cancel'));
     }
Index: modules/simpletest/drupal_web_test_case.php
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/drupal_web_test_case.php,v
retrieving revision 1.3
diff -u -r1.3 drupal_web_test_case.php
--- modules/simpletest/drupal_web_test_case.php	30 Apr 2008 06:45:43 -0000	1.3
+++ modules/simpletest/drupal_web_test_case.php	4 May 2008 03:12:21 -0000
@@ -48,7 +48,7 @@
       'title'     => $this->randomName(8),
       'comment'   => 2,
       'changed'   => time(),
-      'format'    => FILTER_FORMAT_DEFAULT,
+      'format'    => filter_default_format(),
       'moderate'  => 0,
       'promote'   => 0,
       'revision'  => 1,
Index: modules/system/system.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.install,v
retrieving revision 1.250
diff -u -r1.250 system.install
--- modules/system/system.install	16 Apr 2008 11:35:52 -0000	1.250
+++ modules/system/system.install	4 May 2008 03:12:22 -0000
@@ -369,8 +369,8 @@
   db_query("INSERT INTO {role} (name) VALUES ('%s')", 'anonymous user');
   db_query("INSERT INTO {role} (name) VALUES ('%s')", 'authenticated user');
 
-  db_query("INSERT INTO {permission} (rid, perm, tid) VALUES (%d, '%s', %d)", 1, 'access content', 0);
-  db_query("INSERT INTO {permission} (rid, perm, tid) VALUES (%d, '%s', %d)", 2, 'access comments, access content, post comments, post comments without approval', 0);
+  db_query("INSERT INTO {permission} (rid, perm, tid) VALUES (%d, '%s', %d)", 1, 'access content, use Filtered HTML', 0);
+  db_query("INSERT INTO {permission} (rid, perm, tid) VALUES (%d, '%s', %d)", 2, 'access comments, access content, post comments, post comments without approval, use Filtered HTML', 0);
 
   db_query("INSERT INTO {variable} (name, value) VALUES ('%s', '%s')", 'theme_default', 's:7:"garland";');
   db_query("UPDATE {system} SET status = %d WHERE type = '%s' AND name = '%s'", 1, 'theme', 'garland');
@@ -381,8 +381,8 @@
   db_query("INSERT INTO {node_access} (nid, gid, realm, grant_view, grant_update, grant_delete) VALUES (%d, %d, '%s', %d, %d, %d)", 0, 0, 'all', 1, 0, 0);
 
   // Add input formats.
-  db_query("INSERT INTO {filter_formats} (name, roles, cache) VALUES ('%s', '%s', %d)", 'Filtered HTML', ',1,2,', 1);
-  db_query("INSERT INTO {filter_formats} (name, roles, cache) VALUES ('%s', '%s', %d)", 'Full HTML', '', 1);
+  db_query("INSERT INTO {filter_formats} (name, cache) VALUES ('%s', %d)", 'Filtered HTML', 1);
+  db_query("INSERT INTO {filter_formats} (name, cache) VALUES ('%s', %d)", 'Full HTML', 1);
 
   // Enable filters for each input format.
 
Index: modules/user/user.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/user/user.install,v
retrieving revision 1.11
diff -u -r1.11 user.install
--- modules/user/user.install	16 Apr 2008 11:26:29 -0000	1.11
+++ modules/user/user.install	4 May 2008 03:12:22 -0000
@@ -304,6 +304,20 @@
 }
 
 /**
+ * Add 'default_format' column to the {role} table.
+ * 
+ * Default filter formats are now stored per role instead of site-wide.
+ */
+function user_update_7002() {
+  $ret = array();
+  db_add_field($ret, 'role', 'default_format', array('type' => 'int', 'size' => 'tiny', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
+  // Set all role defaults to the current default.
+  $ret[] = update_sql('UPDATE {role} SET default_format = ' . variable_get('filter_default_format', 0));
+  variable_del('filter_default_format');
+  return $ret;
+}
+
+/**
  * @} End of "defgroup user-updates-6.x-to-7.x"
  * The next series of updates should start at 8000.
  */
Index: modules/user/user.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/user/user.module,v
retrieving revision 1.904
diff -u -r1.904 user.module
--- modules/user/user.module	23 Apr 2008 20:01:56 -0000	1.904
+++ modules/user/user.module	4 May 2008 03:12:23 -0000
@@ -1595,12 +1595,16 @@
  * @param $permission
  *   A string containing a permission. If set, only roles containing that
  *   permission are returned.
+ * @param $names_only
+ *   If TRUE, return a $rid => $name list that is suitable for use as an options
+ *   array in select lists, radios, or checkboxes.
  *
  * @return
- *   An associative array with the role id as the key and the role name as
- *   value.
+ *   If $names_only is TRUE, an associative array with the role id as the key
+ *   and the role name as value. If $names_only is FALSE, return the entire role
+ *   object, including the rid, name, and default_format for the role.
  */
-function user_roles($membersonly = FALSE, $permission = NULL) {
+function user_roles($membersonly = FALSE, $permission = NULL, $names_only = TRUE) {
   // System roles take the first two positions.
   $roles = array(
     DRUPAL_ANONYMOUS_RID => NULL,
@@ -1619,14 +1623,14 @@
       // We only translate the built in role names
       case DRUPAL_ANONYMOUS_RID:
         if (!$membersonly) {
-          $roles[$role->rid] = t($role->name);
+          $roles[$role->rid] = $names_only ? t($role->name) : $role;
         }
         break;
       case DRUPAL_AUTHENTICATED_RID:
-        $roles[$role->rid] = t($role->name);
+        $roles[$role->rid] = $names_only ? t($role->name) : $role;
         break;
       default:
-        $roles[$role->rid] = $role->name;
+        $roles[$role->rid] = $names_only ? t($role->name) : $role;
     }
   }
 
@@ -1635,6 +1639,38 @@
 }
 
 /**
+ * Rename a permission.
+ * 
+ * @param $old_perm
+ *   The current name of the permissions to renamed. May be a single string
+ *   permission or an array of permissions.
+ * @param $new_perm
+ *   The new name of the permissions. May be a single string permission or an
+ *   array of permissions. The type for $old_perm and $new_perm must match.
+ */
+function user_rename_permission($old_perm, $new_perm) {
+  // Convert parameters to arrays if they're not already.
+  if (!is_array($old_perm) && !is_array($new_perm)) {
+    $old_perm = array($old_perm);
+    $new_perm = array($new_perm);
+  }
+
+  $result = db_query('SELECT * FROM {permission}');
+  while ($permission = db_fetch_object($result)) {
+    // Create an associative array for easy mapping.
+    $permissions = drupal_map_assoc(explode(', ', $permission->perm));
+    foreach ($old_perm as $index => $name) {
+      // Give the permission the new name.
+      if (isset($permissions[$name]) && isset($new_perm[$index])) {
+        $permissions[$name] = $new_perm[$index];
+      }
+    }
+    $permission->perm = implode(', ', $permissions);
+    db_query("UPDATE {permission} SET perm = '%s' WHERE pid = %d", $permission->perm, $permission->pid);
+  }
+}
+
+/**
  * Implementation of hook_user_operations().
  */
 function user_user_operations($form_state = array()) {
Index: modules/blogapi/blogapi.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/blogapi/blogapi.module,v
retrieving revision 1.118
diff -u -r1.118 blogapi.module
--- modules/blogapi/blogapi.module	14 Apr 2008 17:48:35 -0000	1.118
+++ modules/blogapi/blogapi.module	4 May 2008 03:12:17 -0000
@@ -200,7 +200,7 @@
   $edit['promote'] = in_array('promote', $node_type_default);
   $edit['comment'] = variable_get('comment_' . $edit['type'], 2);
   $edit['revision'] = in_array('revision', $node_type_default);
-  $edit['format'] = FILTER_FORMAT_DEFAULT;
+  $edit['format'] = filter_default_format();
   $edit['status'] = $publish;
 
   // check for bloggerAPI vs. metaWeblogAPI
@@ -474,7 +474,9 @@
 function blogapi_mt_supported_text_filters() {
   // NOTE: we're only using anonymous' formats because the MT spec
   // does not allow for per-user formats.
-  $formats = filter_formats();
+  $account = new stdClass();
+  $account->uid = 0;
+  $formats = filter_formats($account);
 
   $filters = array();
   foreach ($formats as $format) {
Index: modules/node/node.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/node/node.module,v
retrieving revision 1.959
diff -u -r1.959 node.module
--- modules/node/node.module	23 Apr 2008 20:01:52 -0000	1.959
+++ modules/node/node.module	4 May 2008 03:12:20 -0000
@@ -902,7 +902,7 @@
   $node->changed = time();
 
   $node->timestamp = time();
-  $node->format = isset($node->format) ? $node->format : FILTER_FORMAT_DEFAULT;
+  $node->format = isset($node->format) ? $node->format : filter_default_format();
   $update_node = TRUE;
 
   // Generate the node table query and the node_revisions table query.
Index: modules/profile/profile.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/profile/profile.module,v
retrieving revision 1.240
diff -u -r1.240 profile.module
--- modules/profile/profile.module	28 Apr 2008 09:11:58 -0000	1.240
+++ modules/profile/profile.module	4 May 2008 03:12:20 -0000
@@ -259,7 +259,7 @@
   if (isset($user->{$field->name}) && $value = $user->{$field->name}) {
     switch ($field->type) {
       case 'textarea':
-        return check_markup($value);
+        return check_markup($value, filter_default_format($user), FALSE);
       case 'textfield':
       case 'selection':
         return $browse ? l($value, 'profile/' . $field->name . '/' . $value) : check_plain($value);
Index: modules/comment/comment.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/comment/comment.module,v
retrieving revision 1.630
diff -u -r1.630 comment.module
--- modules/comment/comment.module	25 Apr 2008 18:34:05 -0000	1.630
+++ modules/comment/comment.module	4 May 2008 03:12:19 -0000
@@ -1352,7 +1352,7 @@
     '#required' => TRUE,
   );
   if (!isset($edit['format'])) {
-    $edit['format'] = FILTER_FORMAT_DEFAULT;
+    $edit['format'] = filter_default_format();
   }
   $form['comment_filter']['format'] = filter_form($edit['format']);
 
