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

  ViewVC Help
Powered by ViewVC 1.1.2