Newer
Older
<?php
// $Id$

Angie Byron
committed
/**
* @file
* Install, update and uninstall functions for the filter module.
*/

Dries Buytaert
committed
* Implements hook_schema().
*/
function filter_schema() {

Dries Buytaert
committed
$schema['filter'] = array(

Dries Buytaert
committed
'description' => 'Table that maps filters (HTML corrector) to text formats (Filtered HTML).',
'fields' => array(
'format' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,

Dries Buytaert
committed
'description' => 'Foreign key: The {filter_format}.format to which this filter is assigned.',
),
'module' => array(
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => '',

Dries Buytaert
committed
'description' => 'The origin module of the filter.',

Dries Buytaert
committed
'name' => array(
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,

Dries Buytaert
committed
'default' => '',
'description' => 'Name of the filter being referenced.',
),
'weight' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,

Dries Buytaert
committed
'description' => 'Weight of filter within format.',

Dries Buytaert
committed
),
'status' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Filter enabled status. (1 = enabled, 0 = disabled)',
),
'settings' => array(

Dries Buytaert
committed
'type' => 'blob',

Dries Buytaert
committed
'not null' => FALSE,
'size' => 'big',
'serialize' => TRUE,
'description' => 'A serialized array of name value pairs that store the filter settings for the specific format.',
),

Dries Buytaert
committed
'primary key' => array('format', 'name'),

Dries Buytaert
committed
'indexes' => array(

Dries Buytaert
committed
'list' => array('weight', 'module', 'name'),

Dries Buytaert
committed
),

Dries Buytaert
committed
$schema['filter_format'] = array(

Dries Buytaert
committed
'description' => 'Stores text formats: custom groupings of filters, such as Filtered HTML.',
'fields' => array(
'format' => array(
'type' => 'serial',
'not null' => TRUE,

Dries Buytaert
committed
'description' => 'Primary Key: Unique ID for format.',
),
'name' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',

Dries Buytaert
committed
'description' => 'Name of the text format (Filtered HTML).',

Dries Buytaert
committed
'translatable' => TRUE,
),
'cache' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'size' => 'tiny',

Dries Buytaert
committed
'description' => 'Flag to indicate whether format is cacheable. (1 = cacheable, 0 = not cacheable)',

Dries Buytaert
committed
'weight' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,

Dries Buytaert
committed
'description' => 'Weight of text format to use when listing.',

Dries Buytaert
committed
)
),
'primary key' => array('format'),
'unique keys' => array(
'name' => array('name'),
),

Dries Buytaert
committed
'indexes' => array(
'weight' => array('weight'),
),
);
$schema['cache_filter'] = drupal_get_schema_unprocessed('system', 'cache');

Dries Buytaert
committed
$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.';
return $schema;
}
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* 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(
'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);
}

Angie Byron
committed
/**
* Implements hook_update_dependencies().
*/
function filter_update_dependencies() {
// Filter update 7007 migrates permissions and therefore needs to run after
// the {role} table is properly set up.
$dependencies['filter'][7005] = array(
'user' => 7007,
);
return $dependencies;
}
/**
* @defgroup updates-6.x-to-7.x Filter updates from 6.x to 7.x
* @{
*/

Dries Buytaert
committed
/**

Dries Buytaert
committed
* Increase the size of {filters}.weight and add {filter_formats}.weight.

Dries Buytaert
committed
*/
function filter_update_7000() {

Dries Buytaert
committed
// The list index will be recreated by filter_update_7003().
db_drop_index('filters', 'list');
// Change the weight column of the filter table to normal (ie. non tiny) int.
db_change_field('filters', 'weight', 'weight', array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Weight of filter within format.',
));
// Add a new filter_format.weight column.
db_add_field('filter_formats', 'weight', array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Weight of text format to use when listing.',
), array(
'indexes' => array(
'weight' => array('weight'),
),
));

Dries Buytaert
committed
}
/**
* Break out "escape HTML filter" option to its own filter.
*/
function filter_update_7001() {
$result = db_query("SELECT format FROM {filter_formats}")->fetchCol();

Dries Buytaert
committed
$insert = db_insert('filters')->fields(array('format', 'module', 'delta', 'weight'));
foreach ($result as $format_id) {
// Deprecated constants FILTER_HTML_STRIP = 1 and FILTER_HTML_ESCAPE = 2.
if (variable_get('filter_html_' . $format_id, 1) == 2) {
$insert->values(array(
'format' => $format_id,
'filter' => 'filter',
'delta' => 4,
'weight' => 0,
));
variable_del('filter_html_' . $format_id);

Dries Buytaert
committed
$insert->execute();

Dries Buytaert
committed
/**
* Rename {filters} table to {filter} and {filter_formats} table to {filter_format}.
*/
function filter_update_7002() {

Dries Buytaert
committed
db_rename_table('filters', 'filter');
db_rename_table('filter_formats', 'filter_format');

Dries Buytaert
committed
}

Dries Buytaert
committed
/**
* Remove hardcoded numeric deltas from all filters in core.
*/
function filter_update_7003() {
// Duplicates the filter table since core cannot take care of the potential
// contributed module filters.
db_rename_table('filter', 'd6_upgrade_filter');
// Creates the Drupal 7 filter table.
$schema = filter_schema();
db_create_table('filter', $schema['filter']);

Dries Buytaert
committed
// 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(

Dries Buytaert
committed
'0' => 'php_code',
),

Dries Buytaert
committed
);

Dries Buytaert
committed
// Loop through each filter and make changes to the core filter table by
// each record from the old to the new table.

Dries Buytaert
committed
foreach ($renamed_deltas as $module => $deltas) {
foreach ($deltas as $old_delta => $new_name) {
$query = db_select('d6_upgrade_filter');
$query->fields('d6_upgrade_filter', array('format', 'weight'));
$query->condition('module', $module);
$query->condition('delta', $old_delta);

Angie Byron
committed
$query->distinct();
$result = $query->execute();
foreach ($result as $record) {
db_insert('filter')
->fields(array(
'format' => $record->format,
'module' => $module,
'name' => $new_name,
'weight' => $record->weight,
))
->execute();
}
db_delete('d6_upgrade_filter')
->condition('module', $module)
->condition('delta', $old_delta)

Dries Buytaert
committed
}
}
}

Dries Buytaert
committed
/**
* Move filter settings storage into {filter} table.
*/
function filter_update_7004() {
// Enable all existing filters ({filter} contained only enabled previously).

Dries Buytaert
committed
db_update('filter')

Angie Byron
committed
->fields(array('status' => '1'))

Dries Buytaert
committed
->execute();

Dries Buytaert
committed
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Move filter settings from system variables into {filter}.settings.
$filters = db_query("SELECT * FROM {filter} WHERE module = :name", array(':name' => 'filter'));
foreach ($filters as $filter) {
$settings = array();
if ($filter->name == 'filter_html') {
if ($setting = variable_get("allowed_html_{$filter->format}", NULL)) {
$settings['allowed_html'] = $setting;
variable_del("allowed_html_{$filter->format}");
}
if ($setting = variable_get("filter_html_help_{$filter->format}", NULL)) {
$settings['filter_html_help'] = $setting;
variable_del("filter_html_help_{$filter->format}");
}
if ($setting = variable_get("filter_html_nofollow_{$filter->format}", NULL)) {
$settings['filter_html_nofollow'] = $setting;
variable_del("filter_html_nofollow_{$filter->format}");
}
}
elseif ($filter->name == 'filter_url') {
if ($setting = variable_get("filter_url_length_{$filter->format}", NULL)) {
$settings['filter_url_length'] = $setting;
variable_del("filter_url_length_{$filter->format}");
}
}
if (!empty($settings)) {

Angie Byron
committed
db_update('filter')
->fields(array('settings' => serialize($settings)))
->condition('format', $filter->format)
->condition('name', $filter->name)
->execute();

Dries Buytaert
committed
}
}
}
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/**
* 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)) {
user_role_grant_permissions($format_role, array(filter_permission_name($format)));
}
}
}
// Drop the roles field from the {filter_format} table.

Dries Buytaert
committed
db_drop_field('filter_format', 'roles');
// 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()) {
$id = empty($id) ? 2 : $id + 1;
$format_name = $start_name . ' ' . $id;
}
$fallback_format = new stdClass();
$fallback_format->name = $format_name;
$fallback_format->weight = 1;
// This format should output plain text, so we escape all HTML and apply the
// line break and URL filters only.
$fallback_format->filters = array(
'filter_html_escape' => array(
'weight' => 0,
'status' => 1,
),
'filter_url' => array(
'weight' => 1,
'status' => 1,
),
'filter_autop' => array(
'weight' => 2,
'status' => 1,
),
);
filter_format_save($fallback_format);
variable_set('filter_fallback_format', $fallback_format->format);
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/' . $fallback_format->format) . '">text format configuration page</a>.');
// 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
// may need it in their update functions.
// @todo This variable can be deleted in Drupal 8.
}

Dries Buytaert
committed
/**
* 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();
foreach (filter_formats() as $format_id => $format) {
if ($permission = filter_permission_name($format)) {
$permissions[] = $permission;
}
}
// 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) {
user_role_grant_permissions($rid, $permissions);
}
}
}

Dries Buytaert
committed
/**
* Converts fields that store serialized variables from text to blob.
*/
function filter_update_7009() {
$spec = 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.',
);
db_change_field('filter', 'settings', 'settings', $spec);
$schema = system_schema_cache_7054();
db_drop_table('cache_filter');
db_create_table('cache_filter', $schema);
}
/**
* @} End of "defgroup updates-6.x-to-7.x"
* The next series of updates should start at 8000.
*/