Preparing new release.
[project/wysiwyg.git] / wysiwyg.install
1 <?php
2 // $Id$
3
4 /**
5 * Implementation of hook_schema().
6 */
7 function wysiwyg_schema() {
8 $schema = array();
9 $schema['wysiwyg'] = array(
10 'description' => t('Stores Wysiwyg profiles.'),
11 'fields' => array(
12 'format' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
13 'editor' => array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'default' => ''),
14 'settings' => array('type' => 'text', 'size' => 'normal'),
15 ),
16 'primary key' => array('format'),
17 );
18 return $schema;
19 }
20
21 /**
22 * Implementation of hook_install().
23 */
24 function wysiwyg_install() {
25 drupal_install_schema('wysiwyg');
26 // Import settings from old editor modules.
27 wysiwyg_migrate_tinymce();
28 }
29
30 /**
31 * Implementation of hook_uninstall()
32 */
33 function wysiwyg_uninstall() {
34 drupal_uninstall_schema('wysiwyg');
35 }
36
37 /**
38 * Migrate from TinyMCE.
39 */
40 function wysiwyg_migrate_tinymce() {
41 if (db_table_exists('tinymce_settings')) {
42 $schema = db_result(db_query("SELECT schema_version FROM {system} WHERE name = 'tinymce'"));
43 if ($schema >= 1) {
44 // Migrate profile configurations.
45 $profiles = db_query("SELECT settings FROM {tinymce_settings}");
46 while ($profile = db_fetch_array($profiles)) {
47 $settings = unserialize($profile['settings']);
48 // Convert buttons/plugins into an associative array.
49 $old_buttons = (isset($settings['buttons']) ? $settings['buttons'] : array());
50 $settings['buttons'] = array();
51 foreach ($old_buttons as $old_button => $enabled) {
52 list($plugin, $button) = explode('-', $old_button, 2);
53 $settings['buttons'][$plugin][$button] = 1;
54 }
55 foreach (_wysiwyg_install_get_formats() as $format => $name) {
56 // We can't use update_sql() here because of curly braces in serialized
57 // array.
58 db_query("INSERT INTO {wysiwyg} (format, editor, settings) VALUES (%d, 'tinymce', '%s')", $format, serialize($settings));
59 }
60 // We can only migrate one profile.
61 break;
62 }
63 // Disable TinyMCE module.
64 module_disable(array('tinymce'));
65 drupal_set_message('TinyMCE module can be safely uninstalled now.');
66 }
67 else {
68 drupal_set_message('To migrate your existing TinyMCE settings to Wysiwyg Editor, please update TinyMCE module to the latest official release, and re-install Wysiwyg Editor module.');
69 }
70 }
71 }
72
73 /**
74 * Retrieve a list of input formats to associate profiles to.
75 */
76 function _wysiwyg_install_get_formats() {
77 $formats = array();
78 $result = db_query("SELECT format, name FROM {filter_formats}");
79 while ($format = db_fetch_object($result)) {
80 // Build a list of all formats.
81 $formats[$format->format] = $format->name;
82 // Fetch filters.
83 $result2 = db_query("SELECT module, delta FROM {filters} WHERE format = %d", $format->format);
84 while ($filter = db_fetch_object($result2)) {
85 // If PHP filter is enabled, remove this format.
86 if ($filter->module == 'php') {
87 unset($formats[$format->format]);
88 break;
89 }
90 }
91 }
92 return $formats;
93 }
94
95 /**
96 * Associate Wysiwyg profiles with input formats.
97 *
98 * Since there was no association yet, we can only assume that there is one
99 * profile only, and that profile must be duplicated and assigned to all input
100 * formats (except PHP code format). Also, input formats already have
101 * titles/names, so Wysiwyg profiles do not need an own.
102 *
103 * Because input formats are already granted to certain user roles only, we can
104 * remove our custom Wysiwyg profile permissions. A 1:1 relationship between
105 * input formats and permissions makes plugin_count obsolete, too.
106 *
107 * Since the resulting table is completely different, a new schema is installed.
108 */
109 function wysiwyg_update_6001() {
110 $ret = array();
111 if (db_table_exists('wysiwyg')) {
112 return $ret;
113 }
114 // Install new schema.
115 db_create_table($ret, 'wysiwyg', array(
116 'fields' => array(
117 'format' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
118 'editor' => array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'default' => ''),
119 'settings' => array('type' => 'text', 'size' => 'normal'),
120 ),
121 'primary key' => array('format'),
122 ));
123
124 // Fetch all input formats.
125 $formats = _wysiwyg_install_get_formats();
126
127 // Fetch all profiles.
128 $result = db_query("SELECT name, settings FROM {wysiwyg_profile}");
129 while ($profile = db_fetch_object($result)) {
130 $profile->settings = unserialize($profile->settings);
131 // Extract editor name from profile settings.
132 $profile->editor = $profile->settings['editor'];
133 // Clean-up.
134 unset($profile->settings['editor']);
135 unset($profile->settings['old_name']);
136 unset($profile->settings['name']);
137 unset($profile->settings['rids']);
138 // Sorry. There Can Be Only One. ;)
139 break;
140 }
141
142 if ($profile) {
143 // Rebuild profiles and associate with input formats.
144 foreach ($formats as $format => $name) {
145 // Insert profiles.
146 // We can't use update_sql() here because of curly braces in serialized
147 // array.
148 db_query("INSERT INTO {wysiwyg} (format, editor, settings) VALUES (%d, '%s', '%s')", $format, $profile->editor, serialize($profile->settings));
149 $ret[] = array(
150 'success' => TRUE,
151 'query' => strtr('Wysiwyg profile %profile converted and associated with input format %format.', array('%profile' => check_plain($profile->name), '%format' => check_plain($name))),
152 );
153 }
154 }
155
156 // Drop obsolete tables {wysiwyg_profile} and {wysiwyg_role}.
157 db_drop_table($ret, 'wysiwyg_profile');
158 db_drop_table($ret, 'wysiwyg_role');
159
160 return $ret;
161 }
162