comparison modules/filter/filter.install @ 0:ff03f76ab3fe

initial version
author danieleb <danielebarchiesi@me.com>
date Wed, 21 Aug 2013 18:51:11 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:ff03f76ab3fe
1 <?php
2
3 /**
4 * @file
5 * Install, update, and uninstall functions for the Filter module.
6 */
7
8 /**
9 * Implements hook_schema().
10 */
11 function filter_schema() {
12 $schema['filter'] = array(
13 'description' => 'Table that maps filters (HTML corrector) to text formats (Filtered HTML).',
14 'fields' => array(
15 'format' => array(
16 'type' => 'varchar',
17 'length' => 255,
18 'not null' => TRUE,
19 'description' => 'Foreign key: The {filter_format}.format to which this filter is assigned.',
20 ),
21 'module' => array(
22 'type' => 'varchar',
23 'length' => 64,
24 'not null' => TRUE,
25 'default' => '',
26 'description' => 'The origin module of the filter.',
27 ),
28 'name' => array(
29 'type' => 'varchar',
30 'length' => 32,
31 'not null' => TRUE,
32 'default' => '',
33 'description' => 'Name of the filter being referenced.',
34 ),
35 'weight' => array(
36 'type' => 'int',
37 'not null' => TRUE,
38 'default' => 0,
39 'description' => 'Weight of filter within format.',
40 ),
41 'status' => array(
42 'type' => 'int',
43 'not null' => TRUE,
44 'default' => 0,
45 'description' => 'Filter enabled status. (1 = enabled, 0 = disabled)',
46 ),
47 'settings' => array(
48 'type' => 'blob',
49 'not null' => FALSE,
50 'size' => 'big',
51 'serialize' => TRUE,
52 'description' => 'A serialized array of name value pairs that store the filter settings for the specific format.',
53 ),
54 ),
55 'primary key' => array('format', 'name'),
56 'indexes' => array(
57 'list' => array('weight', 'module', 'name'),
58 ),
59 );
60 $schema['filter_format'] = array(
61 'description' => 'Stores text formats: custom groupings of filters, such as Filtered HTML.',
62 'fields' => array(
63 'format' => array(
64 'type' => 'varchar',
65 'length' => 255,
66 'not null' => TRUE,
67 'description' => 'Primary Key: Unique machine name of the format.',
68 ),
69 'name' => array(
70 'type' => 'varchar',
71 'length' => 255,
72 'not null' => TRUE,
73 'default' => '',
74 'description' => 'Name of the text format (Filtered HTML).',
75 'translatable' => TRUE,
76 ),
77 'cache' => array(
78 'type' => 'int',
79 'not null' => TRUE,
80 'default' => 0,
81 'size' => 'tiny',
82 'description' => 'Flag to indicate whether format is cacheable. (1 = cacheable, 0 = not cacheable)',
83 ),
84 'status' => array(
85 'type' => 'int',
86 'unsigned' => TRUE,
87 'not null' => TRUE,
88 'default' => 1,
89 'size' => 'tiny',
90 'description' => 'The status of the text format. (1 = enabled, 0 = disabled)',
91 ),
92 'weight' => array(
93 'type' => 'int',
94 'not null' => TRUE,
95 'default' => 0,
96 'description' => 'Weight of text format to use when listing.',
97 ),
98 ),
99 'primary key' => array('format'),
100 'unique keys' => array(
101 'name' => array('name'),
102 ),
103 'indexes' => array(
104 'status_weight' => array('status', 'weight'),
105 ),
106 );
107
108 $schema['cache_filter'] = drupal_get_schema_unprocessed('system', 'cache');
109 $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.';
110
111 return $schema;
112 }
113
114 /**
115 * Implements hook_install().
116 */
117 function filter_install() {
118 // All sites require at least one text format (the fallback format) that all
119 // users have access to, so add it here. We initialize it as a simple, safe
120 // plain text format with very basic formatting, but it can be modified by
121 // installation profiles to have other properties.
122 $plain_text_format = array(
123 'format' => 'plain_text',
124 'name' => 'Plain text',
125 'weight' => 10,
126 'filters' => array(
127 // Escape all HTML.
128 'filter_html_escape' => array(
129 'weight' => 0,
130 'status' => 1,
131 ),
132 // URL filter.
133 'filter_url' => array(
134 'weight' => 1,
135 'status' => 1,
136 ),
137 // Line break filter.
138 'filter_autop' => array(
139 'weight' => 2,
140 'status' => 1,
141 ),
142 ),
143 );
144 $plain_text_format = (object) $plain_text_format;
145 filter_format_save($plain_text_format);
146
147 // Set the fallback format to plain text.
148 variable_set('filter_fallback_format', $plain_text_format->format);
149 }
150
151 /**
152 * Implements hook_update_dependencies().
153 */
154 function filter_update_dependencies() {
155 // filter_update_7005() migrates role permissions and therefore must run
156 // after the {role} and {role_permission} tables are properly set up, which
157 // happens in user_update_7007().
158 $dependencies['filter'][7005] = array(
159 'user' => 7007,
160 );
161
162 return $dependencies;
163 }
164 /**
165 * @addtogroup updates-6.x-to-7.x
166 * @{
167 */
168
169 /**
170 * Upgrade the {filter_formats} table and rename it to {filter_format}.
171 */
172 function filter_update_7000() {
173 db_rename_table('filter_formats', 'filter_format');
174
175 // Add the new {filter_format}.status and {filter_format}.weight column.
176 db_add_field('filter_format', 'status', array(
177 'type' => 'int',
178 'unsigned' => TRUE,
179 'not null' => TRUE,
180 'default' => 1,
181 'size' => 'tiny',
182 'description' => 'The status of the text format. (1 = enabled, 0 = disabled)',
183 ));
184 db_add_field('filter_format', 'weight', array(
185 'type' => 'int',
186 'not null' => TRUE,
187 'default' => 0,
188 'description' => 'Weight of text format to use when listing.',
189 ), array(
190 'indexes' => array(
191 'status_weight' => array('status', 'weight'),
192 ),
193 ));
194 }
195
196 /**
197 * Break out "escape HTML filter" option to its own filter.
198 */
199 function filter_update_7001() {
200 $result = db_query("SELECT format FROM {filter_format}")->fetchCol();
201 $insert = db_insert('filters')->fields(array('format', 'module', 'delta', 'weight'));
202
203 foreach ($result as $format_id) {
204 // Deprecated constants FILTER_HTML_STRIP = 1 and FILTER_HTML_ESCAPE = 2.
205 if (variable_get('filter_html_' . $format_id, 1) == 2) {
206 $insert->values(array(
207 'format' => $format_id,
208 'module' => 'filter',
209 'delta' => 4,
210 'weight' => 0,
211 ));
212 }
213 variable_del('filter_html_' . $format_id);
214 }
215
216 $insert->execute();
217 }
218
219 /**
220 * Upgrade the {filter} table for core filters.
221 */
222 function filter_update_7003() {
223 // Duplicates the {filters} table since core cannot take care of the potential
224 // contributed module filters.
225 db_rename_table('filters', 'd6_upgrade_filter');
226 // Creates the Drupal 7 filter table.
227 $filter_table = array(
228 'description' => 'Table that maps filters (HTML corrector) to text formats (Filtered HTML).',
229 'fields' => array(
230 'format' => array(
231 'type' => 'int',
232 'not null' => TRUE,
233 'default' => 0,
234 'description' => 'Foreign key: The {filter_format}.format to which this filter is assigned.',
235 ),
236 'module' => array(
237 'type' => 'varchar',
238 'length' => 64,
239 'not null' => TRUE,
240 'default' => '',
241 'description' => 'The origin module of the filter.',
242 ),
243 'name' => array(
244 'type' => 'varchar',
245 'length' => 32,
246 'not null' => TRUE,
247 'default' => '',
248 'description' => 'Name of the filter being referenced.',
249 ),
250 'weight' => array(
251 'type' => 'int',
252 'not null' => TRUE,
253 'default' => 0,
254 'description' => 'Weight of filter within format.',
255 ),
256 'status' => array(
257 'type' => 'int',
258 'not null' => TRUE,
259 'default' => 0,
260 'description' => 'Filter enabled status. (1 = enabled, 0 = disabled)',
261 ),
262 'settings' => array(
263 'type' => 'blob',
264 'not null' => FALSE,
265 'size' => 'big',
266 'serialize' => TRUE,
267 'description' => 'A serialized array of name value pairs that store the filter settings for the specific format.',
268 ),
269 ),
270 'primary key' => array('format', 'name'),
271 'indexes' => array(
272 'list' => array('weight', 'module', 'name'),
273 ),
274 );
275 db_create_table('filter', $filter_table);
276
277 // Get an array of the renamed filter deltas, organized by module.
278 $renamed_deltas = array(
279 'filter' => array(
280 '0' => 'filter_html',
281 '1' => 'filter_autop',
282 '2' => 'filter_url',
283 '3' => 'filter_htmlcorrector',
284 '4' => 'filter_html_escape',
285 ),
286 'php' => array(
287 '0' => 'php_code',
288 ),
289 );
290
291 // Loop through each filter and make changes to the core filter table by
292 // each record from the old to the new table.
293 foreach ($renamed_deltas as $module => $deltas) {
294 foreach ($deltas as $old_delta => $new_name) {
295 $query = db_select('d6_upgrade_filter')
296 ->fields('d6_upgrade_filter', array('format', 'weight'))
297 ->condition('module', $module)
298 ->condition('delta', $old_delta)
299 ->distinct();
300
301 foreach ($query->execute() as $record) {
302 // Port the filter settings.
303 $settings = array();
304 if ($new_name == 'filter_html') {
305 if ($setting = variable_get("allowed_html_{$record->format}", NULL)) {
306 $settings['allowed_html'] = $setting;
307 variable_del("allowed_html_{$record->format}");
308 }
309 if ($setting = variable_get("filter_html_help_{$record->format}", NULL)) {
310 $settings['filter_html_help'] = $setting;
311 variable_del("filter_html_help_{$record->format}");
312 }
313 if ($setting = variable_get("filter_html_nofollow_{$record->format}", NULL)) {
314 $settings['filter_html_nofollow'] = $setting;
315 variable_del("filter_html_nofollow_{$record->format}");
316 }
317 }
318 elseif ($new_name == 'filter_url') {
319 if ($setting = variable_get("filter_url_length_{$record->format}", NULL)) {
320 $settings['filter_url_length'] = $setting;
321 variable_del("filter_url_length_{$record->format}");
322 }
323 }
324
325 db_insert('filter')
326 ->fields(array(
327 'format' => $record->format,
328 'module' => $module,
329 'name' => $new_name,
330 'weight' => $record->weight,
331 'settings' => serialize($settings),
332 'status' => 1,
333 ))
334 ->execute();
335 }
336 db_delete('d6_upgrade_filter')
337 ->condition('module', $module)
338 ->condition('delta', $old_delta)
339 ->execute();
340 }
341 }
342 }
343
344 /**
345 * Integrate text formats with the user permissions system.
346 *
347 * This function converts text format role assignments to use the new text
348 * format permissions introduced in Drupal 7, creates a fallback (plain text)
349 * format that is available to all users, and explicitly sets the text format
350 * in cases that used to rely on a single site-wide default.
351 */
352 function filter_update_7005() {
353 // Move role data from the filter system to the user permission system.
354 $all_roles = array_keys(user_roles());
355 $default_format = variable_get('filter_default_format', 1);
356 $result = db_query("SELECT * FROM {filter_format}");
357 foreach ($result as $format) {
358 // We need to assign the default format to all roles (regardless of what
359 // was stored in the database) to preserve the behavior of the site at the
360 // moment of the upgrade.
361 $format_roles = ($format->format == $default_format ? $all_roles : explode(',', $format->roles));
362 foreach ($format_roles as $format_role) {
363 if (in_array($format_role, $all_roles)) {
364 _update_7000_user_role_grant_permissions($format_role, array('use text format ' . $format->format), 'filter');
365 }
366 }
367 }
368
369 // Drop the roles field from the {filter_format} table.
370 db_drop_field('filter_format', 'roles');
371
372 // Add a fallback text format which outputs plain text and appears last on
373 // the list for all users. Generate a unique name for it, starting with
374 // "Plain text".
375 $start_name = 'Plain text';
376 $format_name = $start_name;
377 while ($format = db_query('SELECT format FROM {filter_format} WHERE name = :name', array(':name' => $format_name))->fetchField()) {
378 $id = empty($id) ? 2 : $id + 1;
379 $format_name = $start_name . ' ' . $id;
380 }
381
382 // Insert the filter format.
383 $format_id = db_insert('filter_format')
384 ->fields(array(
385 'name' => $format_name,
386 'cache' => 1,
387 'weight' => 1,
388 'status' => 1,
389 ))
390 ->execute();
391
392 // This format should output plain text, so we escape all HTML and apply the
393 // line break and URL filters only.
394 db_insert('filter')
395 ->fields(array(
396 'format',
397 'name',
398 'weight',
399 'status',
400 'module',
401 'settings',
402 ))
403 ->values(array(
404 'format' => $format_id,
405 'name' => 'filter_html_escape',
406 'weight' => 0,
407 'status' => 1,
408 'module' => 'filter',
409 'settings' => serialize(array()),
410 ))
411 ->values(array(
412 'format' => $format_id,
413 'name' => 'filter_url',
414 'weight' => 1,
415 'status' => 1,
416 'module' => 'filter',
417 'settings' => serialize(array()),
418 ))
419 ->values(array(
420 'format' => $format_id,
421 'name' => 'filter_autop',
422 'weight' => 2,
423 'status' => 1,
424 'module' => 'filter',
425 'settings' => serialize(array()),
426 ))
427 ->execute();
428
429 variable_set('filter_fallback_format', $format_id);
430 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>.');
431
432 // Move the former site-wide default text format to the top of the list, so
433 // that it continues to be the default text format for all users.
434 db_update('filter_format')
435 ->fields(array('weight' => -1))
436 ->condition('format', $default_format)
437 ->execute();
438
439 // We do not delete the 'filter_default_format' variable, since other modules
440 // need it in their update functions; for an example, see user_update_7010().
441 // @todo This variable can be deleted in Drupal 8.
442 }
443
444 /**
445 * Grant usage of all text formats to user roles having the 'administer filters' permission.
446 */
447 function filter_update_7008() {
448 // Build the list of permissions to grant.
449 $permissions = array();
450 foreach (db_query('SELECT format FROM {filter_format}')->fetchCol() as $format_id) {
451 if ($format_id != variable_get('filter_fallback_format')) {
452 $permissions[] = 'use text format ' . $format_id;
453 }
454 }
455 // Grant text format permissions to all roles that can 'administer filters'.
456 // Albeit anonymous users *should not* have the permission, we cannot presume
457 // that they do not or must not.
458 if ($roles = user_roles(FALSE, 'administer filters')) {
459 foreach ($roles as $rid => $name) {
460 _update_7000_user_role_grant_permissions($rid, $permissions, 'filter');
461 }
462 }
463 }
464
465 /**
466 * Converts fields that store serialized variables from text to blob.
467 */
468 function filter_update_7009() {
469 $schema = system_schema_cache_7054();
470 db_drop_table('cache_filter');
471 db_create_table('cache_filter', $schema);
472 }
473
474 /**
475 * Change {filter_format}.format and {filter}.format into varchar.
476 */
477 function filter_update_7010() {
478 db_change_field('filter_format', 'format', 'format', array(
479 'type' => 'varchar',
480 'length' => 255,
481 'not null' => TRUE,
482 'description' => 'Primary Key: Unique machine name of the format.',
483 ));
484 db_change_field('filter', 'format', 'format', array(
485 'type' => 'varchar',
486 'length' => 255,
487 'not null' => TRUE,
488 'description' => 'Foreign key: The {filter_format}.format to which this filter is assigned.',
489 ));
490 }
491
492 /**
493 * @} End of "addtogroup updates-6.x-to-7.x".
494 */