2007-10-05 16:07:22 +00:00
<?php
2009-05-13 19:42:18 +00:00
/**
* @file
* Install, update and uninstall functions for the filter module.
*/
2007-10-05 16:07:22 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_schema().
2007-10-05 16:07:22 +00:00
*/
function filter_schema() {
2008-12-03 16:32:22 +00:00
$schema['filter'] = array(
2009-01-21 16:58:42 +00:00
'description' => 'Table that maps filters (HTML corrector) to text formats (Filtered HTML).',
2007-10-05 16:07:22 +00:00
'fields' => array(
2007-10-10 11:39:35 +00:00
'format' => array(
2010-10-20 01:15:58 +00:00
'type' => 'varchar',
'length' => 255,
2010-10-23 00:43:48 +00:00
'not null' => TRUE,
2008-12-03 16:32:22 +00:00
'description' => 'Foreign key: The {filter_format}.format to which this filter is assigned.',
2007-10-10 11:39:35 +00:00
),
'module' => array(
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => '',
2008-11-15 13:01:11 +00:00
'description' => 'The origin module of the filter.',
2007-10-10 11:39:35 +00:00
),
2009-08-21 17:28:27 +00:00
'name' => array(
'type' => 'varchar',
'length' => 32,
2007-10-10 11:39:35 +00:00
'not null' => TRUE,
2009-08-21 17:28:27 +00:00
'default' => '',
'description' => 'Name of the filter being referenced.',
2007-10-10 11:39:35 +00:00
),
'weight' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
2008-11-15 13:01:11 +00:00
'description' => 'Weight of filter within format.',
2009-08-27 21:18:20 +00:00
),
'status' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Filter enabled status. (1 = enabled, 0 = disabled)',
),
'settings' => array(
2010-06-25 17:47:22 +00:00
'type' => 'blob',
2009-08-27 21:18:20 +00:00
'not null' => FALSE,
'size' => 'big',
'serialize' => TRUE,
'description' => 'A serialized array of name value pairs that store the filter settings for the specific format.',
),
2007-10-05 16:07:22 +00:00
),
2009-08-27 21:18:20 +00:00
'primary key' => array('format', 'name'),
2007-12-18 12:59:22 +00:00
'indexes' => array(
2009-12-03 15:33:42 +00:00
'list' => array('weight', 'module', 'name'),
2007-12-18 12:59:22 +00:00
),
2007-10-05 16:07:22 +00:00
);
2008-12-03 16:32:22 +00:00
$schema['filter_format'] = array(
2009-01-21 16:58:42 +00:00
'description' => 'Stores text formats: custom groupings of filters, such as Filtered HTML.',
2007-10-05 16:07:22 +00:00
'fields' => array(
2007-10-10 11:39:35 +00:00
'format' => array(
2010-10-20 01:15:58 +00:00
'type' => 'varchar',
'length' => 255,
2007-10-10 11:39:35 +00:00
'not null' => TRUE,
2010-10-20 01:15:58 +00:00
'description' => 'Primary Key: Unique machine name of the format.',
2007-10-10 11:39:35 +00:00
),
'name' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
2009-01-21 16:58:42 +00:00
'description' => 'Name of the text format (Filtered HTML).',
2009-10-16 19:06:25 +00:00
'translatable' => TRUE,
2007-10-10 11:39:35 +00:00
),
'cache' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'size' => 'tiny',
2009-05-16 16:06:04 +00:00
'description' => 'Flag to indicate whether format is cacheable. (1 = cacheable, 0 = not cacheable)',
2007-10-10 11:39:35 +00:00
),
2010-09-18 02:18:35 +00:00
'status' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 1,
'size' => 'tiny',
'description' => 'The status of the text format. (1 = enabled, 0 = disabled)',
),
2008-02-19 14:07:21 +00:00
'weight' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
2009-01-21 16:58:42 +00:00
'description' => 'Weight of text format to use when listing.',
2010-09-18 02:18:35 +00:00
),
2007-10-05 16:07:22 +00:00
),
'primary key' => array('format'),
2008-03-15 12:31:29 +00:00
'unique keys' => array(
'name' => array('name'),
),
2009-11-07 22:14:58 +00:00
'indexes' => array(
2010-09-18 02:18:35 +00:00
'status_weight' => array('status', 'weight'),
2009-11-07 22:14:58 +00:00
),
2007-10-05 16:07:22 +00:00
);
$schema['cache_filter'] = drupal_get_schema_unprocessed('system', 'cache');
2010-05-01 08:12:23 +00:00
$schema['cache_filter']['description'] = 'Cache table for the Filter module to store already filtered pieces of text, identified by text format and hash of the text.';
2007-10-05 16:07:22 +00:00
return $schema;
}
2010-03-04 21:42:01 +00:00
/**
* Implements hook_install().
*/
function filter_install() {
// All sites require at least one text format (the fallback format) that all
// users have access to, so add it here. We initialize it as a simple, safe
// plain text format with very basic formatting, but it can be modified by
// installation profiles to have other properties.
$plain_text_format = array(
2010-10-20 01:15:58 +00:00
'format' => 'plain_text',
2010-03-04 21:42:01 +00:00
'name' => 'Plain text',
'weight' => 10,
'filters' => array(
// Escape all HTML.
'filter_html_escape' => array(
'weight' => 0,
'status' => 1,
),
// URL filter.
'filter_url' => array(
'weight' => 1,
'status' => 1,
),
// Line break filter.
'filter_autop' => array(
'weight' => 2,
'status' => 1,
),
),
);
$plain_text_format = (object) $plain_text_format;
filter_format_save($plain_text_format);
// Set the fallback format to plain text.
variable_set('filter_fallback_format', $plain_text_format->format);
}
2010-07-01 15:14:27 +00:00
/**
* Implements hook_update_dependencies().
*/
function filter_update_dependencies() {
2011-08-06 23:29:31 +00:00
// filter_update_7005() migrates role permissions and therefore must run
// after the {role} and {role_permission} tables are properly set up, which
// happens in user_update_7007().
2010-07-01 15:14:27 +00:00
$dependencies['filter'][7005] = array(
'user' => 7007,
);
return $dependencies;
}
- Patch #11218 by David_Rothstein, sun, quicksketch, duncf, awood456, dropcube, mgifford | pwolanin, dww, RobRoy, Crell, webchick, beginner, ray007, bjaspan, chx, Gábor Hojtsy, Steven, Dries, lutegrass, sym, guardian, matt2000, geerlingguy, SeanBannister, matt westgate, com2, praseodym: allow default text formats per role, and integrate text format permissions.
2009-09-20 07:32:19 +00:00
/**
2011-01-02 17:26:40 +00:00
* @addtogroup updates-6.x-to-7.x
- Patch #11218 by David_Rothstein, sun, quicksketch, duncf, awood456, dropcube, mgifford | pwolanin, dww, RobRoy, Crell, webchick, beginner, ray007, bjaspan, chx, Gábor Hojtsy, Steven, Dries, lutegrass, sym, guardian, matt2000, geerlingguy, SeanBannister, matt westgate, com2, praseodym: allow default text formats per role, and integrate text format permissions.
2009-09-20 07:32:19 +00:00
* @{
*/
2008-02-19 14:07:21 +00:00
/**
2010-09-05 01:05:06 +00:00
* Upgrade the {filter_formats} table and rename it to {filter_format}.
2008-02-19 14:07:21 +00:00
*/
function filter_update_7000() {
2010-09-05 01:05:06 +00:00
db_rename_table('filter_formats', 'filter_format');
2010-03-12 14:31:01 +00:00
2010-09-18 02:18:35 +00:00
// Add the new {filter_format}.status and {filter_format}.weight column.
db_add_field('filter_format', 'status', array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 1,
'size' => 'tiny',
'description' => 'The status of the text format. (1 = enabled, 0 = disabled)',
));
2010-09-05 01:05:06 +00:00
db_add_field('filter_format', 'weight', array(
2010-03-12 14:31:01 +00:00
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Weight of text format to use when listing.',
), array(
'indexes' => array(
2010-09-18 02:18:35 +00:00
'status_weight' => array('status', 'weight'),
2010-03-12 14:31:01 +00:00
),
));
2008-02-19 14:07:21 +00:00
}
2008-04-11 02:55:55 +00:00
/**
* Break out "escape HTML filter" option to its own filter.
*/
function filter_update_7001() {
2010-09-05 01:05:06 +00:00
$result = db_query("SELECT format FROM {filter_format}")->fetchCol();
2009-09-29 15:13:57 +00:00
$insert = db_insert('filters')->fields(array('format', 'module', 'delta', 'weight'));
2009-10-13 15:39:41 +00:00
foreach ($result as $format_id) {
2008-04-11 02:55:55 +00:00
// Deprecated constants FILTER_HTML_STRIP = 1 and FILTER_HTML_ESCAPE = 2.
2009-10-13 15:39:41 +00:00
if (variable_get('filter_html_' . $format_id, 1) == 2) {
$insert->values(array(
'format' => $format_id,
2010-12-30 03:44:39 +00:00
'module' => 'filter',
2009-10-13 15:39:41 +00:00
'delta' => 4,
'weight' => 0,
));
2008-04-11 02:55:55 +00:00
}
2009-10-13 15:39:41 +00:00
variable_del('filter_html_' . $format_id);
2008-04-11 02:55:55 +00:00
}
2009-09-29 15:13:57 +00:00
$insert->execute();
2008-04-11 02:55:55 +00:00
}
2008-12-03 16:32:22 +00:00
/**
2010-09-05 01:05:06 +00:00
* Upgrade the {filter} table for core filters.
2009-08-21 17:28:27 +00:00
*/
function filter_update_7003() {
2010-09-05 01:05:06 +00:00
// Duplicates the {filters} table since core cannot take care of the potential
2010-04-22 05:44:41 +00:00
// contributed module filters.
2010-09-05 01:05:06 +00:00
db_rename_table('filters', 'd6_upgrade_filter');
2010-04-22 05:44:41 +00:00
// Creates the Drupal 7 filter table.
2010-09-05 01:05:06 +00:00
$filter_table = array(
'description' => 'Table that maps filters (HTML corrector) to text formats (Filtered HTML).',
'fields' => array(
'format' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Foreign key: The {filter_format}.format to which this filter is assigned.',
),
'module' => array(
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => '',
'description' => 'The origin module of the filter.',
),
'name' => array(
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => '',
'description' => 'Name of the filter being referenced.',
),
'weight' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Weight of filter within format.',
),
'status' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Filter enabled status. (1 = enabled, 0 = disabled)',
),
'settings' => array(
'type' => 'blob',
'not null' => FALSE,
'size' => 'big',
'serialize' => TRUE,
'description' => 'A serialized array of name value pairs that store the filter settings for the specific format.',
),
),
'primary key' => array('format', 'name'),
'indexes' => array(
'list' => array('weight', 'module', 'name'),
),
);
db_create_table('filter', $filter_table);
2010-04-22 05:44:41 +00:00
2009-08-21 17:28:27 +00:00
// 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(
2009-08-26 10:17:54 +00:00
'0' => 'php_code',
),
2009-08-21 17:28:27 +00:00
);
2009-08-26 10:17:54 +00:00
2010-04-22 05:44:41 +00:00
// Loop through each filter and make changes to the core filter table by
// each record from the old to the new table.
2009-08-21 17:28:27 +00:00
foreach ($renamed_deltas as $module => $deltas) {
2010-04-22 05:44:41 +00:00
foreach ($deltas as $old_delta => $new_name) {
2010-09-05 01:05:06 +00:00
$query = db_select('d6_upgrade_filter')
->fields('d6_upgrade_filter', array('format', 'weight'))
->condition('module', $module)
->condition('delta', $old_delta)
->distinct();
foreach ($query->execute() as $record) {
// Port the filter settings.
$settings = array();
if ($new_name == 'filter_html') {
if ($setting = variable_get("allowed_html_{$record->format}", NULL)) {
$settings['allowed_html'] = $setting;
variable_del("allowed_html_{$record->format}");
}
if ($setting = variable_get("filter_html_help_{$record->format}", NULL)) {
$settings['filter_html_help'] = $setting;
variable_del("filter_html_help_{$record->format}");
}
if ($setting = variable_get("filter_html_nofollow_{$record->format}", NULL)) {
$settings['filter_html_nofollow'] = $setting;
variable_del("filter_html_nofollow_{$record->format}");
}
}
elseif ($new_name == 'filter_url') {
if ($setting = variable_get("filter_url_length_{$record->format}", NULL)) {
$settings['filter_url_length'] = $setting;
variable_del("filter_url_length_{$record->format}");
}
}
2010-04-22 05:44:41 +00:00
db_insert('filter')
->fields(array(
'format' => $record->format,
'module' => $module,
'name' => $new_name,
'weight' => $record->weight,
2010-09-05 01:05:06 +00:00
'settings' => serialize($settings),
'status' => 1,
2010-04-22 05:44:41 +00:00
))
->execute();
}
db_delete('d6_upgrade_filter')
2009-12-08 06:52:38 +00:00
->condition('module', $module)
2010-04-22 05:44:41 +00:00
->condition('delta', $old_delta)
2009-12-08 06:52:38 +00:00
->execute();
2009-08-21 17:28:27 +00:00
}
}
}
- Patch #11218 by David_Rothstein, sun, quicksketch, duncf, awood456, dropcube, mgifford | pwolanin, dww, RobRoy, Crell, webchick, beginner, ray007, bjaspan, chx, Gábor Hojtsy, Steven, Dries, lutegrass, sym, guardian, matt2000, geerlingguy, SeanBannister, matt westgate, com2, praseodym: allow default text formats per role, and integrate text format permissions.
2009-09-20 07:32:19 +00:00
/**
* Integrate text formats with the user permissions system.
*
* This function converts text format role assignments to use the new text
* format permissions introduced in Drupal 7, creates a fallback (plain text)
* format that is available to all users, and explicitly sets the text format
* in cases that used to rely on a single site-wide default.
*/
function filter_update_7005() {
// Move role data from the filter system to the user permission system.
$all_roles = array_keys(user_roles());
$default_format = variable_get('filter_default_format', 1);
$result = db_query("SELECT * FROM {filter_format}");
foreach ($result as $format) {
// We need to assign the default format to all roles (regardless of what
// was stored in the database) to preserve the behavior of the site at the
// moment of the upgrade.
$format_roles = ($format->format == $default_format ? $all_roles : explode(',', $format->roles));
foreach ($format_roles as $format_role) {
if (in_array($format_role, $all_roles)) {
2010-09-13 05:50:09 +00:00
_update_7000_user_role_grant_permissions($format_role, array('use text format ' . $format->format), 'filter');
- Patch #11218 by David_Rothstein, sun, quicksketch, duncf, awood456, dropcube, mgifford | pwolanin, dww, RobRoy, Crell, webchick, beginner, ray007, bjaspan, chx, Gábor Hojtsy, Steven, Dries, lutegrass, sym, guardian, matt2000, geerlingguy, SeanBannister, matt westgate, com2, praseodym: allow default text formats per role, and integrate text format permissions.
2009-09-20 07:32:19 +00:00
}
}
}
// Drop the roles field from the {filter_format} table.
2009-09-29 15:13:57 +00:00
db_drop_field('filter_format', 'roles');
- Patch #11218 by David_Rothstein, sun, quicksketch, duncf, awood456, dropcube, mgifford | pwolanin, dww, RobRoy, Crell, webchick, beginner, ray007, bjaspan, chx, Gábor Hojtsy, Steven, Dries, lutegrass, sym, guardian, matt2000, geerlingguy, SeanBannister, matt westgate, com2, praseodym: allow default text formats per role, and integrate text format permissions.
2009-09-20 07:32:19 +00:00
// Add a fallback text format which outputs plain text and appears last on
// the list for all users. Generate a unique name for it, starting with
// "Plain text".
$start_name = 'Plain text';
$format_name = $start_name;
while ($format = db_query('SELECT format FROM {filter_format} WHERE name = :name', array(':name' => $format_name))->fetchField()) {
2010-03-04 21:42:01 +00:00
$id = empty($id) ? 2 : $id + 1;
- Patch #11218 by David_Rothstein, sun, quicksketch, duncf, awood456, dropcube, mgifford | pwolanin, dww, RobRoy, Crell, webchick, beginner, ray007, bjaspan, chx, Gábor Hojtsy, Steven, Dries, lutegrass, sym, guardian, matt2000, geerlingguy, SeanBannister, matt westgate, com2, praseodym: allow default text formats per role, and integrate text format permissions.
2009-09-20 07:32:19 +00:00
$format_name = $start_name . ' ' . $id;
}
2010-09-05 01:05:06 +00:00
// Insert the filter format.
$format_id = db_insert('filter_format')
->fields(array(
'name' => $format_name,
'cache' => 1,
'weight' => 1,
2010-09-18 02:18:35 +00:00
'status' => 1,
2010-09-05 01:05:06 +00:00
))
->execute();
- Patch #11218 by David_Rothstein, sun, quicksketch, duncf, awood456, dropcube, mgifford | pwolanin, dww, RobRoy, Crell, webchick, beginner, ray007, bjaspan, chx, Gábor Hojtsy, Steven, Dries, lutegrass, sym, guardian, matt2000, geerlingguy, SeanBannister, matt westgate, com2, praseodym: allow default text formats per role, and integrate text format permissions.
2009-09-20 07:32:19 +00:00
// This format should output plain text, so we escape all HTML and apply the
2010-03-04 21:42:01 +00:00
// line break and URL filters only.
2010-09-05 01:05:06 +00:00
db_insert('filter')
->fields(array(
'format',
'name',
'weight',
'status',
'module',
'settings',
))
->values(array(
'format' => $format_id,
'name' => 'filter_html_escape',
2010-03-04 21:42:01 +00:00
'weight' => 0,
'status' => 1,
2010-09-05 01:05:06 +00:00
'module' => 'filter',
'settings' => serialize(array()),
))
->values(array(
'format' => $format_id,
'name' => 'filter_url',
2010-03-04 21:42:01 +00:00
'weight' => 1,
'status' => 1,
2010-09-05 01:05:06 +00:00
'module' => 'filter',
'settings' => serialize(array()),
))
->values(array(
'format' => $format_id,
'name' => 'filter_autop',
2010-03-04 21:42:01 +00:00
'weight' => 2,
'status' => 1,
2010-09-05 01:05:06 +00:00
'module' => 'filter',
'settings' => serialize(array()),
))
->execute();
variable_set('filter_fallback_format', $format_id);
drupal_set_message('A new <em>Plain text</em> format has been created which will be available to all users. You can configure this text format on the <a href="' . url('admin/config/content/formats/' . $format) . '">text format configuration page</a>.');
- Patch #11218 by David_Rothstein, sun, quicksketch, duncf, awood456, dropcube, mgifford | pwolanin, dww, RobRoy, Crell, webchick, beginner, ray007, bjaspan, chx, Gábor Hojtsy, Steven, Dries, lutegrass, sym, guardian, matt2000, geerlingguy, SeanBannister, matt westgate, com2, praseodym: allow default text formats per role, and integrate text format permissions.
2009-09-20 07:32:19 +00:00
// Move the former site-wide default text format to the top of the list, so
// that it continues to be the default text format for all users.
db_update('filter_format')
->fields(array('weight' => -1))
->condition('format', $default_format)
->execute();
// We do not delete the 'filter_default_format' variable, since other modules
2010-09-28 03:30:37 +00:00
// need it in their update functions; for an example, see user_update_7010().
- Patch #11218 by David_Rothstein, sun, quicksketch, duncf, awood456, dropcube, mgifford | pwolanin, dww, RobRoy, Crell, webchick, beginner, ray007, bjaspan, chx, Gábor Hojtsy, Steven, Dries, lutegrass, sym, guardian, matt2000, geerlingguy, SeanBannister, matt westgate, com2, praseodym: allow default text formats per role, and integrate text format permissions.
2009-09-20 07:32:19 +00:00
// @todo This variable can be deleted in Drupal 8.
}
2010-03-05 13:32:10 +00:00
/**
* Grant usage of all text formats to user roles having the 'administer filters' permission.
*/
function filter_update_7008() {
// Build the list of permissions to grant.
$permissions = array();
2010-09-05 01:05:06 +00:00
foreach (db_query('SELECT format FROM {filter_format}')->fetchCol() as $format_id) {
if ($format_id != variable_get('filter_fallback_format')) {
$permissions[] = 'use text format ' . $format_id;
2010-03-05 13:32:10 +00:00
}
}
// Grant text format permissions to all roles that can 'administer filters'.
// Albeit anonymous users *should not* have the permission, we cannot presume
// that they do not or must not.
if ($roles = user_roles(FALSE, 'administer filters')) {
foreach ($roles as $rid => $name) {
2010-09-13 05:50:09 +00:00
_update_7000_user_role_grant_permissions($rid, $permissions, 'filter');
2010-03-05 13:32:10 +00:00
}
}
}
2010-06-25 17:47:22 +00:00
/**
* Converts fields that store serialized variables from text to blob.
*/
function filter_update_7009() {
$schema = system_schema_cache_7054();
db_drop_table('cache_filter');
db_create_table('cache_filter', $schema);
}
2010-10-20 01:15:58 +00:00
/**
* Change {filter_format}.format and {filter}.format into varchar.
*/
function filter_update_7010() {
db_change_field('filter_format', 'format', 'format', array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'Primary Key: Unique machine name of the format.',
));
db_change_field('filter', 'format', 'format', array(
'type' => 'varchar',
'length' => 255,
2010-10-23 00:43:48 +00:00
'not null' => TRUE,
2010-10-20 01:15:58 +00:00
'description' => 'Foreign key: The {filter_format}.format to which this filter is assigned.',
));
}
- Patch #11218 by David_Rothstein, sun, quicksketch, duncf, awood456, dropcube, mgifford | pwolanin, dww, RobRoy, Crell, webchick, beginner, ray007, bjaspan, chx, Gábor Hojtsy, Steven, Dries, lutegrass, sym, guardian, matt2000, geerlingguy, SeanBannister, matt westgate, com2, praseodym: allow default text formats per role, and integrate text format permissions.
2009-09-20 07:32:19 +00:00
/**
2011-01-02 17:26:40 +00:00
* @} End of "addtogroup updates-6.x-to-7.x"
- Patch #11218 by David_Rothstein, sun, quicksketch, duncf, awood456, dropcube, mgifford | pwolanin, dww, RobRoy, Crell, webchick, beginner, ray007, bjaspan, chx, Gábor Hojtsy, Steven, Dries, lutegrass, sym, guardian, matt2000, geerlingguy, SeanBannister, matt westgate, com2, praseodym: allow default text formats per role, and integrate text format permissions.
2009-09-20 07:32:19 +00:00
*/