- Patch #546350 by dropcube: remove hardcoded numeric deltas from hook_filter_info().

merge-requests/26/head
Dries Buytaert 2009-08-21 17:28:27 +00:00
parent 0d9816b8fd
commit bc23bfaa11
9 changed files with 112 additions and 61 deletions

View File

@ -406,7 +406,7 @@ function text_summary($text, $format = NULL, $size = NULL) {
// parse errors. // parse errors.
if (isset($format)) { if (isset($format)) {
$filters = filter_list_format($format); $filters = filter_list_format($format);
if (isset($filters['php/0']) && strpos($text, '<?') !== FALSE) { if (isset($filters['php/php_code']) && strpos($text, '<?') !== FALSE) {
return $text; return $text;
} }
} }
@ -445,7 +445,7 @@ function text_summary($text, $format = NULL, $size = NULL) {
$line_breaks = array('<br />' => 6, '<br>' => 4); $line_breaks = array('<br />' => 6, '<br>' => 4);
// Newline only indicates a line break if line break converter // Newline only indicates a line break if line break converter
// filter is present. // filter is present.
if (isset($filters['filter/1'])) { if (isset($filters['filter/filter_autop'])) {
$line_breaks["\n"] = 1; $line_breaks["\n"] = 1;
} }
$break_points[] = $line_breaks; $break_points[] = $line_breaks;
@ -473,7 +473,7 @@ function text_summary($text, $format = NULL, $size = NULL) {
} }
// If the htmlcorrector filter is present, apply it to the generated summary. // If the htmlcorrector filter is present, apply it to the generated summary.
if (isset($filters['filter/3'])) { if (isset($filters['filter/filter_htmlcorrector'])) {
$summary = _filter_htmlcorrector($summary); $summary = _filter_htmlcorrector($summary);
} }

View File

@ -154,9 +154,9 @@ function filter_admin_format_form(&$form_state, $format) {
foreach ($all as $id => $filter) { foreach ($all as $id => $filter) {
$filter_info = module_invoke($filter->module, 'filter_info'); $filter_info = module_invoke($filter->module, 'filter_info');
$form['filters'][$id] = array('#type' => 'checkbox', $form['filters'][$id] = array('#type' => 'checkbox',
'#title' => $filter->name, '#title' => $filter->title,
'#default_value' => isset($enabled[$id]), '#default_value' => isset($enabled[$id]),
'#description' => $filter_info[$filter->delta]['description'], '#description' => $filter_info[$filter->name]['description'],
); );
} }
if (!empty($format->format)) { if (!empty($format->format)) {
@ -188,7 +188,7 @@ function filter_admin_format_form_validate($form, &$form_state) {
$name = trim($form_state['values']['name']); $name = trim($form_state['values']['name']);
$result = db_query("SELECT format FROM {filter_format} WHERE name = :name", array(':name' => $name))->fetchField(); $result = db_query("SELECT format FROM {filter_format} WHERE name = :name", array(':name' => $name))->fetchField();
if ($result) { if ($result) {
form_set_error('name', t('Text format names must be unique. A format named %name already exists.', array('%name' => $name))); form_set_error('name', t('Text format names must be unique. A format named %name already exists.', array('%name' => $format_name)));
} }
} }
} }
@ -217,20 +217,20 @@ function filter_admin_format_form_submit($form, &$form_state) {
db_delete('filter') db_delete('filter')
->condition('format', $format) ->condition('format', $format)
->execute(); ->execute();
$query = db_insert('filter')->fields(array('format', 'module', 'delta', 'weight')); $query = db_insert('filter')->fields(array('format', 'module', 'name', 'weight'));
foreach ($form_state['values']['filters'] as $id => $checked) { foreach ($form_state['values']['filters'] as $id => $checked) {
if ($checked) { if ($checked) {
list($module, $delta) = explode('/', $id); list($module, $filter_name) = explode('/', $id);
// Add new filters to the bottom. // Add new filters to the bottom.
$weight = isset($current[$id]->weight) ? $current[$id]->weight : 10; $weight = isset($current[$id]->weight) ? $current[$id]->weight : 10;
$query->values(array( $query->values(array(
'format' => $format, 'format' => $format,
'module' => $module, 'module' => $module,
'delta' => $delta, 'name' => $filter_name,
'weight' => $weight, 'weight' => $weight,
)); ));
// Check if there are any 'no cache' filters. // Check if there are any 'no cache' filters.
$cache &= !module_invoke($module, 'filter', 'no cache', $delta); $cache &= !module_invoke($module, 'filter', 'no cache', $filter_name);
} }
$query->execute(); $query->execute();
} }
@ -352,8 +352,8 @@ function filter_admin_configure(&$form_state, $format) {
$form = array(); $form = array();
foreach ($list as $filter) { foreach ($list as $filter) {
$filter_info = module_invoke($filter->module, 'filter_info'); $filter_info = module_invoke($filter->module, 'filter_info');
if (isset($filter_info[$filter->delta]['settings callback']) && drupal_function_exists($filter_info[$filter->delta]['settings callback'])) { if (isset($filter_info[$filter->name]['settings callback']) && drupal_function_exists($filter_info[$filter->name]['settings callback'])) {
$form_module = call_user_func($filter_info[$filter->delta]['settings callback'], $format->format); $form_module = call_user_func($filter_info[$filter->name]['settings callback'], $format->format);
} }
if (isset($form_module) && is_array($form_module)) { if (isset($form_module) && is_array($form_module)) {
$form = array_merge($form, $form_module); $form = array_merge($form, $form_module);
@ -440,12 +440,12 @@ function theme_filter_admin_order($form) {
*/ */
function filter_admin_order_submit($form, &$form_state) { function filter_admin_order_submit($form, &$form_state) {
foreach ($form_state['values']['weights'] as $id => $weight) { foreach ($form_state['values']['weights'] as $id => $weight) {
list($module, $delta) = explode('/', $id); list($module, $name) = explode('/', $id);
db_update('filter') db_update('filter')
->fields(array('weight' => $weight)) ->fields(array('weight' => $weight))
->condition('format', $form_state['values']['format']) ->condition('format', $form_state['values']['format'])
->condition('module', $module) ->condition('module', $module)
->condition('delta', $delta) ->condition('name', $name)
->execute(); ->execute();
} }
drupal_set_message(t('The filter ordering has been saved.')); drupal_set_message(t('The filter ordering has been saved.'));

View File

@ -31,12 +31,12 @@ function filter_schema() {
'default' => '', 'default' => '',
'description' => 'The origin module of the filter.', 'description' => 'The origin module of the filter.',
), ),
'delta' => array( 'name' => array(
'type' => 'int', 'type' => 'varchar',
'length' => 32,
'not null' => TRUE, 'not null' => TRUE,
'default' => 0, 'default' => '',
'size' => 'tiny', 'description' => 'Name of the filter being referenced.',
'description' => 'ID to identify which filter within module is being referenced.',
), ),
'weight' => array( 'weight' => array(
'type' => 'int', 'type' => 'int',
@ -48,10 +48,10 @@ function filter_schema() {
), ),
'primary key' => array('fid'), 'primary key' => array('fid'),
'unique keys' => array( 'unique keys' => array(
'fmd' => array('format', 'module', 'delta'), 'fmn' => array('format', 'module', 'name'),
), ),
'indexes' => array( 'indexes' => array(
'list' => array('format', 'weight', 'module', 'delta'), 'list' => array('format', 'weight', 'module', 'name'),
), ),
); );
$schema['filter_format'] = array( $schema['filter_format'] = array(
@ -137,3 +137,53 @@ function filter_update_7002() {
db_rename_table($ret, 'filter_formats', 'filter_format'); db_rename_table($ret, 'filter_formats', 'filter_format');
return $ret; return $ret;
} }
/**
* Remove hardcoded numeric deltas from all filters in core.
*/
function filter_update_7003() {
$ret = array();
// Get an array of the renamed filter deltas, organized by module.
$renamed_deltas = array(
'filter' => array(
'0' => 'filter_html',
'1' => 'filter_autop',
'2' => 'filter_url',
'3' => 'filter_htmlcorrector',
'4' => 'filter_html_escape',
),
'php' => array(
'0' => 'php_code'
)
);
// Rename field 'delta' to 'name'.
db_drop_unique_key($ret, 'filter', 'fmd');
db_drop_index($ret, 'filter', 'list');
db_change_field($ret, 'filter', 'delta', 'name',
array(
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => '',
'description' => 'Name of the filter being referenced.',
),
array(
'unique keys' => array(
'fmn' => array('format', 'module', 'name'),
),
'indexes' => array(
'list' => array('format', 'weight', 'module', 'name'),
)
)
);
// Loop through each filter and make changes to the core filter table.
foreach ($renamed_deltas as $module => $deltas) {
foreach ($deltas as $old_delta => $new_delta) {
$ret[] = update_sql("UPDATE {filter} SET name = '" . $new_delta . "' WHERE module = '" . $module . "' AND name = '" . $old_delta . "'");
}
}
return $ret;
}

View File

@ -334,10 +334,10 @@ function filter_list_all() {
$function = $module . '_filter_info'; $function = $module . '_filter_info';
$info = $function('list'); $info = $function('list');
if (isset($info) && is_array($info)) { if (isset($info) && is_array($info)) {
foreach ($info as $delta => $filter) { foreach ($info as $name => $filter) {
$filters[$module . '/' . $delta] = (object)($filter + array( $filters[$module . '/' . $name] = (object)($filter + array(
'module' => $module, 'module' => $module,
'delta' => $delta, 'name' => $name,
)); ));
} }
} }
@ -381,12 +381,12 @@ function filter_list_format($format) {
if (!isset($filters[$format])) { if (!isset($filters[$format])) {
$filters[$format] = array(); $filters[$format] = array();
$result = db_query("SELECT * FROM {filter} WHERE format = :format ORDER BY weight, module, delta", array(':format' => (int) $format)); $result = db_query("SELECT * FROM {filter} WHERE format = :format ORDER BY weight, module, name", array(':format' => (int) $format));
foreach ($result as $filter) { foreach ($result as $filter) {
$info = module_invoke($filter->module, 'filter_info'); $info = module_invoke($filter->module, 'filter_info');
if (isset($info) && is_array($info) && isset($info[$filter->delta])) { if (isset($info) && is_array($info) && isset($info[$filter->name])) {
$filter->name = $info[$filter->delta]['name']; $filter->title = $info[$filter->name]['title'];
$filters[$format][$filter->module . '/' . $filter->delta] = $filter; $filters[$format][$filter->module . '/' . $filter->name] = $filter;
} }
} }
} }
@ -446,16 +446,16 @@ function check_markup($text, $format = FILTER_FORMAT_DEFAULT, $langcode = '', $c
// Give filters the chance to escape HTML-like data such as code or formulas. // Give filters the chance to escape HTML-like data such as code or formulas.
foreach ($filters as $filter) { foreach ($filters as $filter) {
$filter_info = module_invoke($filter->module, 'filter_info'); $filter_info = module_invoke($filter->module, 'filter_info');
if (isset($filter_info[$filter->delta]['prepare callback']) && drupal_function_exists($filter_info[$filter->delta]['prepare callback'])) { if (isset($filter_info[$filter->name]['prepare callback']) && drupal_function_exists($filter_info[$filter->name]['prepare callback'])) {
$text = call_user_func($filter_info[$filter->delta]['prepare callback'], $text, $format, $langcode, $cache_id); $text = call_user_func($filter_info[$filter->name]['prepare callback'], $text, $format, $langcode, $cache_id);
} }
} }
// Perform filtering. // Perform filtering.
foreach ($filters as $filter) { foreach ($filters as $filter) {
$filter_info = module_invoke($filter->module, 'filter_info'); $filter_info = module_invoke($filter->module, 'filter_info');
if (isset($filter_info[$filter->delta]['process callback']) && drupal_function_exists($filter_info[$filter->delta]['process callback'])) { if (isset($filter_info[$filter->name]['process callback']) && drupal_function_exists($filter_info[$filter->name]['process callback'])) {
$text = call_user_func($filter_info[$filter->delta]['process callback'], $text, $format, $langcode, $cache_id); $text = call_user_func($filter_info[$filter->name]['process callback'], $text, $format, $langcode, $cache_id);
} }
} }
@ -566,8 +566,8 @@ function _filter_tips($format, $long = FALSE) {
$tips[$format->name] = array(); $tips[$format->name] = array();
foreach ($filters as $id => $filter) { foreach ($filters as $id => $filter) {
$filter_info = module_invoke($filter->module, 'filter_info'); $filter_info = module_invoke($filter->module, 'filter_info');
if (isset($filter_info[$filter->delta]['tips callback']) && drupal_function_exists($filter_info[$filter->delta]['tips callback'])) { if (isset($filter_info[$filter->name]['tips callback']) && drupal_function_exists($filter_info[$filter->name]['tips callback'])) {
$tip = call_user_func($filter_info[$filter->delta]['tips callback'],$format->format, $long); $tip = call_user_func($filter_info[$filter->name]['tips callback'],$format->format, $long);
$tips[$format->name][] = array('tip' => $tip, 'id' => $id); $tips[$format->name][] = array('tip' => $tip, 'id' => $id);
} }
} }
@ -647,33 +647,33 @@ function theme_filter_guidelines($format) {
*/ */
function filter_filter_info() { function filter_filter_info() {
$filters[0] = array( $filters['filter_html'] = array(
'name' => t('Limit allowed HTML tags'), 'title' => t('Limit allowed HTML tags'),
'description' => t('Allows you to restrict the HTML tags the user can use. It will also remove harmful content such as JavaScript events, JavaScript URLs and CSS styles from those tags that are not removed.'), 'description' => t('Allows you to restrict the HTML tags the user can use. It will also remove harmful content such as JavaScript events, JavaScript URLs and CSS styles from those tags that are not removed.'),
'process callback' => '_filter_html', 'process callback' => '_filter_html',
'settings callback' => '_filter_html_settings', 'settings callback' => '_filter_html_settings',
'tips callback' => '_filter_html_tips' 'tips callback' => '_filter_html_tips'
); );
$filters[1] = array( $filters['filter_autop'] = array(
'name' => t('Convert line breaks'), 'title' => t('Convert line breaks'),
'description' => t('Converts line breaks into HTML (i.e. &lt;br&gt; and &lt;p&gt;) tags.'), 'description' => t('Converts line breaks into HTML (i.e. &lt;br&gt; and &lt;p&gt;) tags.'),
'process callback' => '_filter_autop', 'process callback' => '_filter_autop',
'tips callback' => '_filter_autop_tips' 'tips callback' => '_filter_autop_tips'
); );
$filters[2] = array( $filters['filter_url'] = array(
'name' => t('Convert URLs into links'), 'title' => t('Convert URLs into links'),
'description' => t('Turns web and e-mail addresses into clickable links.'), 'description' => t('Turns web and e-mail addresses into clickable links.'),
'process callback' => '_filter_url', 'process callback' => '_filter_url',
'settings callback' => '_filter_url_settings', 'settings callback' => '_filter_url_settings',
'tips callback' => '_filter_url_tips' 'tips callback' => '_filter_url_tips'
); );
$filters[3] = array( $filters['filter_htmlcorrector'] = array(
'name' => t('Correct broken HTML'), 'title' => t('Correct broken HTML'),
'description' => t('Corrects faulty and chopped off HTML in postings.'), 'description' => t('Corrects faulty and chopped off HTML in postings.'),
'process callback' => '_filter_htmlcorrector', 'process callback' => '_filter_htmlcorrector',
); );
$filters[4] = array( $filters['filter_html_escape'] = array(
'name' => t('Escape all HTML'), 'title' => t('Escape all HTML'),
'description' => t('Escapes all HTML tags, so they will be visible instead of being effective.'), 'description' => t('Escapes all HTML tags, so they will be visible instead of being effective.'),
'process callback' => '_filter_html_escape', 'process callback' => '_filter_html_escape',
'tips callback' => '_filter_html_escape_tips' 'tips callback' => '_filter_html_escape_tips'

View File

@ -15,9 +15,9 @@ class FilterAdminTestCase extends DrupalWebTestCase {
*/ */
function testFilterAdmin() { function testFilterAdmin() {
// URL filter. // URL filter.
$first_filter = 2; $first_filter = 'filter_url';
// Line filter. // Line filter.
$second_filter = 1; $second_filter = 'filter_autop';
// Create users. // Create users.
$admin_user = $this->drupalCreateUser(array('administer filters')); $admin_user = $this->drupalCreateUser(array('administer filters'));
@ -55,11 +55,11 @@ class FilterAdminTestCase extends DrupalWebTestCase {
$result = db_query('SELECT * FROM {filter} WHERE format = :format ORDER BY weight ASC', array(':format' => $filtered)); $result = db_query('SELECT * FROM {filter} WHERE format = :format ORDER BY weight ASC', array(':format' => $filtered));
$filters = array(); $filters = array();
foreach ($result as $filter) { foreach ($result as $filter) {
if ($filter->delta == $second_filter || $filter->delta == $first_filter) { if ($filter->name == $second_filter || $filter->name == $first_filter) {
$filters[] = $filter; $filters[] = $filter;
} }
} }
$this->assertTrue(($filters[0]->delta == $second_filter && $filters[1]->delta == $first_filter), t('Order confirmed.')); $this->assertTrue(($filters[0]->name == $second_filter && $filters[1]->name == $first_filter), t('Order confirmed.'));
// Add filter. // Add filter.
$edit = array(); $edit = array();
@ -74,6 +74,7 @@ class FilterAdminTestCase extends DrupalWebTestCase {
$this->assertNotNull($format, t('Format found in database.')); $this->assertNotNull($format, t('Format found in database.'));
if ($format !== NULL) { if ($format !== NULL) {
debug($format);
$this->assertFieldByName('roles[2]', '', t('Role found.')); $this->assertFieldByName('roles[2]', '', t('Role found.'));
$this->assertFieldByName('filters[filter/' . $second_filter . ']', '', t('Line break filter found.')); $this->assertFieldByName('filters[filter/' . $second_filter . ']', '', t('Line break filter found.'));
$this->assertFieldByName('filters[filter/' . $first_filter . ']', '', t('Url filter found.')); $this->assertFieldByName('filters[filter/' . $first_filter . ']', '', t('Url filter found.'));

View File

@ -29,7 +29,7 @@ function php_install() {
->fields(array( ->fields(array(
'format' => $format, 'format' => $format,
'module' => 'php', 'module' => 'php',
'delta' => 0, 'name' => 'php_code',
'weight' => 0, 'weight' => 0,
)) ))
->execute(); ->execute();

View File

@ -127,8 +127,8 @@ else {
*/ */
function php_filter_info() { function php_filter_info() {
return array( return array(
array( 'php_code' => array(
'name' => t('PHP evaluator'), 'title' => t('PHP evaluator'),
'description' => t('Executes a piece of PHP code. The usage of this filter should be restricted to administrators only!'), 'description' => t('Executes a piece of PHP code. The usage of this filter should be restricted to administrators only!'),
'cache' => FALSE, 'cache' => FALSE,
'process callback' => 'php_eval', 'process callback' => 'php_eval',

View File

@ -461,7 +461,7 @@ class SearchCommentTestCase extends DrupalWebTestCase {
variable_set('comment_preview_article', COMMENT_PREVIEW_OPTIONAL); variable_set('comment_preview_article', COMMENT_PREVIEW_OPTIONAL);
// Enable check_plain() for 'Filtered HTML' text format. // Enable check_plain() for 'Filtered HTML' text format.
$edit = array( $edit = array(
'filters[filter/4]' => 1, 'filters[filter/filter_html_escape]' => 1,
); );
$this->drupalPost('admin/settings/formats/1', $edit, t('Save configuration')); $this->drupalPost('admin/settings/formats/1', $edit, t('Save configuration'));
// Allow anonymous users to search content. // Allow anonymous users to search content.

View File

@ -440,33 +440,33 @@ function system_install() {
// Filtered HTML: // Filtered HTML:
db_insert('filter') db_insert('filter')
->fields(array('format', 'module', 'delta', 'weight')) ->fields(array('format', 'module', 'name', 'weight'))
// URL filter. // URL filter.
->values(array( ->values(array(
'format' => $filtered_html_format, 'format' => $filtered_html_format,
'module' => 'filter', 'module' => 'filter',
'delta' => 2, 'name' => 'filter_url',
'weight' => 0, 'weight' => 0,
)) ))
// HTML filter. // HTML filter.
->values(array( ->values(array(
'format' => $filtered_html_format, 'format' => $filtered_html_format,
'module' => 'filter', 'module' => 'filter',
'delta' => 0, 'name' => 'filter_html',
'weight' => 1, 'weight' => 1,
)) ))
// Line break filter. // Line break filter.
->values(array( ->values(array(
'format' => $filtered_html_format, 'format' => $filtered_html_format,
'module' => 'filter', 'module' => 'filter',
'delta' => 1, 'name' => 'filter_autop',
'weight' => 2, 'weight' => 2,
)) ))
// HTML corrector filter. // HTML corrector filter.
->values(array( ->values(array(
'format' => $filtered_html_format, 'format' => $filtered_html_format,
'module' => 'filter', 'module' => 'filter',
'delta' => 3, 'name' => 'filter_htmlcorrector',
'weight' => 10, 'weight' => 10,
)) ))
// Full HTML: // Full HTML:
@ -474,21 +474,21 @@ function system_install() {
->values(array( ->values(array(
'format' => $full_html_format, 'format' => $full_html_format,
'module' => 'filter', 'module' => 'filter',
'delta' => 2, 'name' => 'filter_url',
'weight' => 0, 'weight' => 0,
)) ))
// Line break filter. // Line break filter.
->values(array( ->values(array(
'format' => $full_html_format, 'format' => $full_html_format,
'module' => 'filter', 'module' => 'filter',
'delta' => 1, 'name' => 'filter_autop',
'weight' => 1, 'weight' => 1,
)) ))
// HTML corrector filter. // HTML corrector filter.
->values(array( ->values(array(
'format' => $full_html_format, 'format' => $full_html_format,
'module' => 'filter', 'module' => 'filter',
'delta' => 3, 'name' => 'filter_htmlcorrector',
'weight' => 10, 'weight' => 10,
)) ))
->execute(); ->execute();