Commit bc23bfaa authored by Dries's avatar Dries

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

parent 0d9816b8
......@@ -406,7 +406,7 @@ function text_summary($text, $format = NULL, $size = NULL) {
// parse errors.
if (isset($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;
}
}
......@@ -445,7 +445,7 @@ function text_summary($text, $format = NULL, $size = NULL) {
$line_breaks = array('<br />' => 6, '<br>' => 4);
// Newline only indicates a line break if line break converter
// filter is present.
if (isset($filters['filter/1'])) {
if (isset($filters['filter/filter_autop'])) {
$line_breaks["\n"] = 1;
}
$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 (isset($filters['filter/3'])) {
if (isset($filters['filter/filter_htmlcorrector'])) {
$summary = _filter_htmlcorrector($summary);
}
......
......@@ -154,9 +154,9 @@ function filter_admin_format_form(&$form_state, $format) {
foreach ($all as $id => $filter) {
$filter_info = module_invoke($filter->module, 'filter_info');
$form['filters'][$id] = array('#type' => 'checkbox',
'#title' => $filter->name,
'#title' => $filter->title,
'#default_value' => isset($enabled[$id]),
'#description' => $filter_info[$filter->delta]['description'],
'#description' => $filter_info[$filter->name]['description'],
);
}
if (!empty($format->format)) {
......@@ -188,7 +188,7 @@ function filter_admin_format_form_validate($form, &$form_state) {
$name = trim($form_state['values']['name']);
$result = db_query("SELECT format FROM {filter_format} WHERE name = :name", array(':name' => $name))->fetchField();
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')
->condition('format', $format)
->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) {
if ($checked) {
list($module, $delta) = explode('/', $id);
list($module, $filter_name) = explode('/', $id);
// Add new filters to the bottom.
$weight = isset($current[$id]->weight) ? $current[$id]->weight : 10;
$query->values(array(
'format' => $format,
'module' => $module,
'delta' => $delta,
'name' => $filter_name,
'weight' => $weight,
));
// 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();
}
......@@ -352,8 +352,8 @@ function filter_admin_configure(&$form_state, $format) {
$form = array();
foreach ($list as $filter) {
$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'])) {
$form_module = call_user_func($filter_info[$filter->delta]['settings callback'], $format->format);
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->name]['settings callback'], $format->format);
}
if (isset($form_module) && is_array($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) {
foreach ($form_state['values']['weights'] as $id => $weight) {
list($module, $delta) = explode('/', $id);
list($module, $name) = explode('/', $id);
db_update('filter')
->fields(array('weight' => $weight))
->condition('format', $form_state['values']['format'])
->condition('module', $module)
->condition('delta', $delta)
->condition('name', $name)
->execute();
}
drupal_set_message(t('The filter ordering has been saved.'));
......
......@@ -31,12 +31,12 @@ function filter_schema() {
'default' => '',
'description' => 'The origin module of the filter.',
),
'delta' => array(
'type' => 'int',
'name' => array(
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => 0,
'size' => 'tiny',
'description' => 'ID to identify which filter within module is being referenced.',
'default' => '',
'description' => 'Name of the filter being referenced.',
),
'weight' => array(
'type' => 'int',
......@@ -48,10 +48,10 @@ function filter_schema() {
),
'primary key' => array('fid'),
'unique keys' => array(
'fmd' => array('format', 'module', 'delta'),
'fmn' => array('format', 'module', 'name'),
),
'indexes' => array(
'list' => array('format', 'weight', 'module', 'delta'),
'list' => array('format', 'weight', 'module', 'name'),
),
);
$schema['filter_format'] = array(
......@@ -137,3 +137,53 @@ function filter_update_7002() {
db_rename_table($ret, 'filter_formats', 'filter_format');
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;
}
......@@ -334,10 +334,10 @@ function filter_list_all() {
$function = $module . '_filter_info';
$info = $function('list');
if (isset($info) && is_array($info)) {
foreach ($info as $delta => $filter) {
$filters[$module . '/' . $delta] = (object)($filter + array(
foreach ($info as $name => $filter) {
$filters[$module . '/' . $name] = (object)($filter + array(
'module' => $module,
'delta' => $delta,
'name' => $name,
));
}
}
......@@ -381,12 +381,12 @@ function filter_list_format($format) {
if (!isset($filters[$format])) {
$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) {
$info = module_invoke($filter->module, 'filter_info');
if (isset($info) && is_array($info) && isset($info[$filter->delta])) {
$filter->name = $info[$filter->delta]['name'];
$filters[$format][$filter->module . '/' . $filter->delta] = $filter;
if (isset($info) && is_array($info) && isset($info[$filter->name])) {
$filter->title = $info[$filter->name]['title'];
$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.
foreach ($filters as $filter) {
$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'])) {
$text = call_user_func($filter_info[$filter->delta]['prepare callback'], $text, $format, $langcode, $cache_id);
if (isset($filter_info[$filter->name]['prepare callback']) && drupal_function_exists($filter_info[$filter->name]['prepare callback'])) {
$text = call_user_func($filter_info[$filter->name]['prepare callback'], $text, $format, $langcode, $cache_id);
}
}
// Perform filtering.
foreach ($filters as $filter) {
$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'])) {
$text = call_user_func($filter_info[$filter->delta]['process callback'], $text, $format, $langcode, $cache_id);
if (isset($filter_info[$filter->name]['process callback']) && drupal_function_exists($filter_info[$filter->name]['process callback'])) {
$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();
foreach ($filters as $id => $filter) {
$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'])) {
$tip = call_user_func($filter_info[$filter->delta]['tips callback'],$format->format, $long);
if (isset($filter_info[$filter->name]['tips callback']) && drupal_function_exists($filter_info[$filter->name]['tips callback'])) {
$tip = call_user_func($filter_info[$filter->name]['tips callback'],$format->format, $long);
$tips[$format->name][] = array('tip' => $tip, 'id' => $id);
}
}
......@@ -647,33 +647,33 @@ function theme_filter_guidelines($format) {
*/
function filter_filter_info() {
$filters[0] = array(
'name' => t('Limit allowed HTML tags'),
$filters['filter_html'] = array(
'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.'),
'process callback' => '_filter_html',
'settings callback' => '_filter_html_settings',
'tips callback' => '_filter_html_tips'
);
$filters[1] = array(
'name' => t('Convert line breaks'),
$filters['filter_autop'] = array(
'title' => t('Convert line breaks'),
'description' => t('Converts line breaks into HTML (i.e. &lt;br&gt; and &lt;p&gt;) tags.'),
'process callback' => '_filter_autop',
'tips callback' => '_filter_autop_tips'
);
$filters[2] = array(
'name' => t('Convert URLs into links'),
$filters['filter_url'] = array(
'title' => t('Convert URLs into links'),
'description' => t('Turns web and e-mail addresses into clickable links.'),
'process callback' => '_filter_url',
'settings callback' => '_filter_url_settings',
'tips callback' => '_filter_url_tips'
);
$filters[3] = array(
'name' => t('Correct broken HTML'),
$filters['filter_htmlcorrector'] = array(
'title' => t('Correct broken HTML'),
'description' => t('Corrects faulty and chopped off HTML in postings.'),
'process callback' => '_filter_htmlcorrector',
);
$filters[4] = array(
'name' => t('Escape all HTML'),
$filters['filter_html_escape'] = array(
'title' => t('Escape all HTML'),
'description' => t('Escapes all HTML tags, so they will be visible instead of being effective.'),
'process callback' => '_filter_html_escape',
'tips callback' => '_filter_html_escape_tips'
......
......@@ -15,9 +15,9 @@ class FilterAdminTestCase extends DrupalWebTestCase {
*/
function testFilterAdmin() {
// URL filter.
$first_filter = 2;
$first_filter = 'filter_url';
// Line filter.
$second_filter = 1;
$second_filter = 'filter_autop';
// Create users.
$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));
$filters = array();
foreach ($result as $filter) {
if ($filter->delta == $second_filter || $filter->delta == $first_filter) {
if ($filter->name == $second_filter || $filter->name == $first_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.
$edit = array();
......@@ -74,6 +74,7 @@ class FilterAdminTestCase extends DrupalWebTestCase {
$this->assertNotNull($format, t('Format found in database.'));
if ($format !== NULL) {
debug($format);
$this->assertFieldByName('roles[2]', '', t('Role found.'));
$this->assertFieldByName('filters[filter/' . $second_filter . ']', '', t('Line break filter found.'));
$this->assertFieldByName('filters[filter/' . $first_filter . ']', '', t('Url filter found.'));
......
......@@ -29,7 +29,7 @@ function php_install() {
->fields(array(
'format' => $format,
'module' => 'php',
'delta' => 0,
'name' => 'php_code',
'weight' => 0,
))
->execute();
......
......@@ -127,8 +127,8 @@ function _php_filter_tips($format, $long = FALSE) {
*/
function php_filter_info() {
return array(
array(
'name' => t('PHP evaluator'),
'php_code' => array(
'title' => t('PHP evaluator'),
'description' => t('Executes a piece of PHP code. The usage of this filter should be restricted to administrators only!'),
'cache' => FALSE,
'process callback' => 'php_eval',
......
......@@ -461,7 +461,7 @@ class SearchCommentTestCase extends DrupalWebTestCase {
variable_set('comment_preview_article', COMMENT_PREVIEW_OPTIONAL);
// Enable check_plain() for 'Filtered HTML' text format.
$edit = array(
'filters[filter/4]' => 1,
'filters[filter/filter_html_escape]' => 1,
);
$this->drupalPost('admin/settings/formats/1', $edit, t('Save configuration'));
// Allow anonymous users to search content.
......
......@@ -440,33 +440,33 @@ function system_install() {
// Filtered HTML:
db_insert('filter')
->fields(array('format', 'module', 'delta', 'weight'))
->fields(array('format', 'module', 'name', 'weight'))
// URL filter.
->values(array(
'format' => $filtered_html_format,
'module' => 'filter',
'delta' => 2,
'name' => 'filter_url',
'weight' => 0,
))
// HTML filter.
->values(array(
'format' => $filtered_html_format,
'module' => 'filter',
'delta' => 0,
'name' => 'filter_html',
'weight' => 1,
))
// Line break filter.
->values(array(
'format' => $filtered_html_format,
'module' => 'filter',
'delta' => 1,
'name' => 'filter_autop',
'weight' => 2,
))
// HTML corrector filter.
->values(array(
'format' => $filtered_html_format,
'module' => 'filter',
'delta' => 3,
'name' => 'filter_htmlcorrector',
'weight' => 10,
))
// Full HTML:
......@@ -474,21 +474,21 @@ function system_install() {
->values(array(
'format' => $full_html_format,
'module' => 'filter',
'delta' => 2,
'weight' => 0,
'name' => 'filter_url',
'weight' => 0,
))
// Line break filter.
->values(array(
'format' => $full_html_format,
'module' => 'filter',
'delta' => 1,
'name' => 'filter_autop',
'weight' => 1,
))
// HTML corrector filter.
->values(array(
'format' => $full_html_format,
'module' => 'filter',
'delta' => 3,
'name' => 'filter_htmlcorrector',
'weight' => 10,
))
->execute();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment