/[drupal]/drupal/modules/filter/filter.install
ViewVC logotype

Contents of /drupal/modules/filter/filter.install

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.24 - (show annotations) (download) (as text)
Sat Nov 7 22:14:58 2009 UTC (2 weeks, 2 days ago) by dries
Branch: MAIN
CVS Tags: DRUPAL-7-0-UNSTABLE-10, HEAD
Changes since 1.23: +5 -1 lines
File MIME type: text/x-php
- Patch #625942 by catch: added index to avoid filesort in filter_formats().
1 <?php
2 // $Id: filter.install,v 1.23 2009/10/16 19:06:23 dries Exp $
3
4 /**
5 * @file
6 * Install, update and uninstall functions for the filter module.
7 */
8
9 /**
10 * Implement hook_schema().
11 */
12 function filter_schema() {
13 $schema['filter'] = array(
14 'description' => 'Table that maps filters (HTML corrector) to text formats (Filtered HTML).',
15 'fields' => array(
16 'format' => array(
17 'type' => 'int',
18 'not null' => TRUE,
19 'default' => 0,
20 'description' => 'Foreign key: The {filter_format}.format to which this filter is assigned.',
21 ),
22 'module' => array(
23 'type' => 'varchar',
24 'length' => 64,
25 'not null' => TRUE,
26 'default' => '',
27 'description' => 'The origin module of the filter.',
28 ),
29 'name' => array(
30 'type' => 'varchar',
31 'length' => 32,
32 'not null' => TRUE,
33 'default' => '',
34 'description' => 'Name of the filter being referenced.',
35 ),
36 'weight' => array(
37 'type' => 'int',
38 'not null' => TRUE,
39 'default' => 0,
40 'size' => 'tiny',
41 'description' => 'Weight of filter within format.',
42 ),
43 'status' => array(
44 'type' => 'int',
45 'not null' => TRUE,
46 'default' => 0,
47 'description' => 'Filter enabled status. (1 = enabled, 0 = disabled)',
48 ),
49 'settings' => array(
50 'type' => 'text',
51 'not null' => FALSE,
52 'size' => 'big',
53 'serialize' => TRUE,
54 'description' => 'A serialized array of name value pairs that store the filter settings for the specific format.',
55 ),
56 ),
57 'primary key' => array('format', 'name'),
58 'unique keys' => array(
59 'fmn' => array('format', 'module', 'name'),
60 ),
61 'indexes' => array(
62 'list' => array('format', 'weight', 'module', 'name'),
63 ),
64 );
65 $schema['filter_format'] = array(
66 'description' => 'Stores text formats: custom groupings of filters, such as Filtered HTML.',
67 'fields' => array(
68 'format' => array(
69 'type' => 'serial',
70 'not null' => TRUE,
71 'description' => 'Primary Key: Unique ID for format.',
72 ),
73 'name' => array(
74 'type' => 'varchar',
75 'length' => 255,
76 'not null' => TRUE,
77 'default' => '',
78 'description' => 'Name of the text format (Filtered HTML).',
79 'translatable' => TRUE,
80 ),
81 'cache' => array(
82 'type' => 'int',
83 'not null' => TRUE,
84 'default' => 0,
85 'size' => 'tiny',
86 'description' => 'Flag to indicate whether format is cacheable. (1 = cacheable, 0 = not cacheable)',
87 ),
88 'weight' => array(
89 'type' => 'int',
90 'not null' => TRUE,
91 'default' => 0,
92 'size' => 'tiny',
93 'description' => 'Weight of text format to use when listing.',
94 )
95 ),
96 'primary key' => array('format'),
97 'unique keys' => array(
98 'name' => array('name'),
99 ),
100 'indexes' => array(
101 'weight' => array('weight'),
102 ),
103 );
104
105 $schema['cache_filter'] = drupal_get_schema_unprocessed('system', 'cache');
106 $schema['cache_filter']['description'] = 'Cache table for the Filter module to store already filtered pieces of text, identified by text format and md5 hash of the text.';
107
108 return $schema;
109 }
110
111 /**
112 * @defgroup updates-6.x-to-7.x Filter updates from 6.x to 7.x
113 * @{
114 */
115
116 /**
117 * Add a weight column to the filter formats table.
118 */
119 function filter_update_7000() {
120 db_add_field('filter_formats', 'weight', array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'tiny'));
121 db_add_index('filter_formats', 'weight', array('weight'));
122 }
123
124 /**
125 * Break out "escape HTML filter" option to its own filter.
126 */
127 function filter_update_7001() {
128 $result = db_query("SELECT format FROM {filter_formats}")->fetchCol();
129 $insert = db_insert('filters')->fields(array('format', 'module', 'delta', 'weight'));
130
131 foreach ($result as $format_id) {
132 // Deprecated constants FILTER_HTML_STRIP = 1 and FILTER_HTML_ESCAPE = 2.
133 if (variable_get('filter_html_' . $format_id, 1) == 2) {
134 $insert->values(array(
135 'format' => $format_id,
136 'filter' => 'filter',
137 'delta' => 4,
138 'weight' => 0,
139 ));
140 }
141 variable_del('filter_html_' . $format_id);
142 }
143
144 $insert->execute();
145 }
146
147 /**
148 * Rename {filters} table to {filter} and {filter_formats} table to {filter_format}.
149 */
150 function filter_update_7002() {
151 db_rename_table('filters', 'filter');
152 db_rename_table('filter_formats', 'filter_format');
153 }
154
155 /**
156 * Remove hardcoded numeric deltas from all filters in core.
157 */
158 function filter_update_7003() {
159 // Get an array of the renamed filter deltas, organized by module.
160 $renamed_deltas = array(
161 'filter' => array(
162 '0' => 'filter_html',
163 '1' => 'filter_autop',
164 '2' => 'filter_url',
165 '3' => 'filter_htmlcorrector',
166 '4' => 'filter_html_escape',
167 ),
168 'php' => array(
169 '0' => 'php_code',
170 ),
171 );
172
173 // Rename field 'delta' to 'name'.
174 db_drop_unique_key('filter', 'fmd');
175 db_drop_index('filter', 'list');
176 db_change_field('filter', 'delta', 'name',
177 array(
178 'type' => 'varchar',
179 'length' => 32,
180 'not null' => TRUE,
181 'default' => '',
182 'description' => 'Name of the filter being referenced.',
183 ),
184 array(
185 'unique keys' => array(
186 'fmn' => array('format', 'module', 'name'),
187 ),
188 'indexes' => array(
189 'list' => array('format', 'weight', 'module', 'name'),
190 ),
191 )
192 );
193
194 // Loop through each filter and make changes to the core filter table.
195 foreach ($renamed_deltas as $module => $deltas) {
196 foreach ($deltas as $old_delta => $new_delta) {
197 db_update('filter')
198 ->fields(array('name', $new_delta))
199 ->condition('module', $module)
200 ->condition('name', $old_delta)
201 ->execute();
202 }
203 }
204 }
205
206 /**
207 * Move filter settings storage into {filter} table.
208 *
209 * - Remove {filter}.fid.
210 * - Add (format, name) as primary key for {filter}.
211 * - Add {filter}.status.
212 * - Add {filter}.settings.
213 */
214 function filter_update_7004() {
215 db_drop_field('filter', 'fid');
216 db_add_primary_key('filter', array('format', 'name'));
217 db_add_field('filter', 'status',
218 array(
219 'type' => 'int',
220 'not null' => TRUE,
221 'default' => 0,
222 'description' => 'Filter enabled status. (1 = enabled, 0 = disabled)',
223 )
224 );
225 db_add_field('filter', 'settings',
226 array(
227 'type' => 'text',
228 'not null' => FALSE,
229 'size' => 'big',
230 'serialize' => TRUE,
231 'description' => 'A serialized array of name value pairs that store the filter settings for the specific format.',
232 )
233 );
234
235 // Enable all existing filters ({filter} contained only enabled previously).
236 db_update('filter')
237 ->fields('status', '1')
238 ->execute();
239
240 // Move filter settings from system variables into {filter}.settings.
241 $filters = db_query("SELECT * FROM {filter} WHERE module = :name", array(':name' => 'filter'));
242 foreach ($filters as $filter) {
243 $settings = array();
244 if ($filter->name == 'filter_html') {
245 if ($setting = variable_get("allowed_html_{$filter->format}", NULL)) {
246 $settings['allowed_html'] = $setting;
247 variable_del("allowed_html_{$filter->format}");
248 }
249 if ($setting = variable_get("filter_html_help_{$filter->format}", NULL)) {
250 $settings['filter_html_help'] = $setting;
251 variable_del("filter_html_help_{$filter->format}");
252 }
253 if ($setting = variable_get("filter_html_nofollow_{$filter->format}", NULL)) {
254 $settings['filter_html_nofollow'] = $setting;
255 variable_del("filter_html_nofollow_{$filter->format}");
256 }
257 }
258 elseif ($filter->name == 'filter_url') {
259 if ($setting = variable_get("filter_url_length_{$filter->format}", NULL)) {
260 $settings['filter_url_length'] = $setting;
261 variable_del("filter_url_length_{$filter->format}");
262 }
263 }
264 if (!empty($settings)) {
265 db_upddate('filter')
266 ->fields(array('settings' => serialize($settings)))
267 ->condition('format', $filter->format)
268 ->condition('name', $filter->name)
269 ->execute();
270 }
271 }
272 }
273
274 /**
275 * Integrate text formats with the user permissions system.
276 *
277 * This function converts text format role assignments to use the new text
278 * format permissions introduced in Drupal 7, creates a fallback (plain text)
279 * format that is available to all users, and explicitly sets the text format
280 * in cases that used to rely on a single site-wide default.
281 */
282 function filter_update_7005() {
283
284 // Move role data from the filter system to the user permission system.
285 $all_roles = array_keys(user_roles());
286 $default_format = variable_get('filter_default_format', 1);
287 $result = db_query("SELECT * FROM {filter_format}");
288 foreach ($result as $format) {
289 // We need to assign the default format to all roles (regardless of what
290 // was stored in the database) to preserve the behavior of the site at the
291 // moment of the upgrade.
292 $format_roles = ($format->format == $default_format ? $all_roles : explode(',', $format->roles));
293 foreach ($format_roles as $format_role) {
294 if (in_array($format_role, $all_roles)) {
295 user_role_grant_permissions($format_role, array(filter_permission_name($format)));
296 }
297 }
298 }
299
300 // Drop the roles field from the {filter_format} table.
301 db_drop_field('filter_format', 'roles');
302
303 // Add a fallback text format which outputs plain text and appears last on
304 // the list for all users. Generate a unique name for it, starting with
305 // "Plain text".
306 $start_name = 'Plain text';
307 $format_name = $start_name;
308 while ($format = db_query('SELECT format FROM {filter_format} WHERE name = :name', array(':name' => $format_name))->fetchField()) {
309 $id = empty($id) ? 1 : $id + 1;
310 $format_name = $start_name . ' ' . $id;
311 }
312 $fallback_format = new stdClass();
313 $fallback_format->name = $format_name;
314 $fallback_format->cache = 1;
315 $fallback_format->weight = 1;
316 // This format should output plain text, so we escape all HTML and apply the
317 // line break filter only.
318 $fallback_format->filters = array(
319 'filter_html_escape' => array('status' => 1),
320 'filter_autop' => array('status' => 1),
321 );
322 filter_format_save($fallback_format);
323 variable_set('filter_fallback_format', $fallback_format->format);
324 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>.');
325
326 // Move the former site-wide default text format to the top of the list, so
327 // that it continues to be the default text format for all users.
328 db_update('filter_format')
329 ->fields(array('weight' => -1))
330 ->condition('format', $default_format)
331 ->execute();
332
333 // It was previously possible for a value of "0" to be stored in database
334 // tables to indicate that a particular piece of text should be filtered
335 // using the default text format. Therefore, we have to convert all such
336 // instances (in Drupal core) to explicitly use the appropriate format.
337 // Note that the update of the node body field is handled separately, in
338 // node_update_7006().
339 foreach (array('block_custom', 'comment') as $table) {
340 if (db_table_exists($table)) {
341 db_update($table)
342 ->fields(array('format' => $default_format))
343 ->condition('format', 0)
344 ->execute();
345 }
346 }
347
348 // We do not delete the 'filter_default_format' variable, since other modules
349 // may need it in their update functions.
350 // @todo This variable can be deleted in Drupal 8.
351 }
352
353 /**
354 * @} End of "defgroup updates-6.x-to-7.x"
355 * The next series of updates should start at 8000.
356 */

  ViewVC Help
Powered by ViewVC 1.1.2