Added support for JavaScript Function references and Regular Expressions in settings.
[project/wysiwyg.git] / wysiwyg.install
1 <?php
2
3 /**
4 * Implementation of hook_install().
5 */
6 function wysiwyg_install() {
7 switch ($GLOBALS['db_type']) {
8 case 'mysql':
9 case 'mysqli':
10 db_query("CREATE TABLE {wysiwyg} (
11 format int NOT NULL default '0',
12 editor varchar(128) NOT NULL default '',
13 settings text,
14 PRIMARY KEY (format)
15 ) /*!40100 DEFAULT CHARACTER SET utf8 */");
16 break;
17
18 case 'pgsql':
19 db_query("CREATE TABLE {wysiwyg} (
20 format int NOT NULL default '0',
21 editor varchar(128) NOT NULL default '',
22 settings text,
23 PRIMARY KEY (format)
24 )");
25 break;
26 }
27 }
28
29 /**
30 * Implementation of hook_uninstall()
31 */
32 function wysiwyg_uninstall() {
33 db_query('DROP TABLE {wysiwyg}');
34 }
35
36 /**
37 * Implementation of hook_enable().
38 */
39 function wysiwyg_enable() {
40 // Disable conflicting, obsolete editor integration modules whenever this
41 // module is enabled. This is crude, but the only way to ensure no conflicts.
42 module_disable(array(
43 'ckeditor',
44 'editarea',
45 'editonpro',
46 'editor',
47 'fckeditor',
48 'freerte',
49 'htmlarea',
50 'htmlbox',
51 'jwysiwyg',
52 'markitup',
53 'nicedit',
54 'openwysiwyg',
55 'pegoeditor',
56 'quicktext',
57 'tinymce',
58 'tinymce_autoconf',
59 'tinytinymce',
60 'whizzywig',
61 'widgeditor',
62 'wymeditor',
63 'xstandard',
64 'yui_editor',
65 ));
66 }
67
68 /**
69 * Retrieve a list of input formats to associate profiles to.
70 */
71 function _wysiwyg_install_get_formats() {
72 $formats = array();
73 $result = db_query("SELECT format, name FROM {filter_formats}");
74 while ($format = db_fetch_object($result)) {
75 // Build a list of all formats.
76 $formats[$format->format] = $format->name;
77 // Fetch filters.
78 $result2 = db_query("SELECT module, delta FROM {filters} WHERE format = %d", $format->format);
79 while ($filter = db_fetch_object($result2)) {
80 // If PHP filter is enabled, remove this format.
81 if ($filter->module == 'filter' && $filter->delta == 1) {
82 unset($formats[$format->format]);
83 break;
84 }
85 }
86 }
87 return $formats;
88 }
89
90 /**
91 * Associate Wysiwyg profiles with input formats.
92 *
93 * Since there was no association yet, we can only assume that there is one
94 * profile only, and that profile must be duplicated and assigned to all input
95 * formats (except PHP code format). Also, input formats already have
96 * titles/names, so Wysiwyg profiles do not need an own.
97 *
98 * Because input formats are already granted to certain user roles only, we can
99 * remove our custom Wysiwyg profile permissions. A 1:1 relationship between
100 * input formats and permissions makes plugin_count obsolete, too.
101 *
102 * Since the resulting table is completely different, a new schema is installed.
103 */
104 function wysiwyg_update_5001() {
105 $ret = array();
106 if (db_table_exists('wysiwyg')) {
107 return $ret;
108 }
109 // Install new schema.
110 switch ($GLOBALS['db_type']) {
111 case 'mysql':
112 case 'mysqli':
113 $ret[] = update_sql("CREATE TABLE {wysiwyg} (
114 format int NOT NULL default '0',
115 editor varchar(128) NOT NULL default '',
116 settings text,
117 PRIMARY KEY (format)
118 ) /*!40100 DEFAULT CHARACTER SET utf8 */");
119 break;
120
121 case 'pgsql':
122 $ret[] = update_sql("CREATE TABLE {wysiwyg} (
123 format int NOT NULL default '0',
124 editor varchar(128) NOT NULL default '',
125 settings text,
126 PRIMARY KEY (format)
127 )");
128 break;
129 }
130
131 // Fetch all input formats.
132 $formats = _wysiwyg_install_get_formats();
133
134 // Fetch all profiles.
135 $result = db_query("SELECT name, settings FROM {wysiwyg_profile}");
136 while ($profile = db_fetch_object($result)) {
137 $profile->settings = unserialize($profile->settings);
138 // Extract editor name from profile settings.
139 $profile->editor = $profile->settings['editor'];
140 // Clean-up.
141 unset($profile->settings['editor']);
142 unset($profile->settings['old_name']);
143 unset($profile->settings['name']);
144 unset($profile->settings['rids']);
145 // Sorry. There Can Be Only One. ;)
146 break;
147 }
148
149 if ($profile) {
150 // Rebuild profiles and associate with input formats.
151 foreach ($formats as $format => $name) {
152 // Insert profiles.
153 // We can't use update_sql() here because of curly braces in serialized
154 // array.
155 db_query("INSERT INTO {wysiwyg} (format, editor, settings) VALUES (%d, '%s', '%s')", $format, $profile->editor, serialize($profile->settings));
156 $ret[] = array(
157 'success' => TRUE,
158 'query' => strtr('Wysiwyg profile %profile converted and associated with input format %format.', array('%profile' => check_plain($profile->name), '%format' => check_plain($name))),
159 );
160 }
161 }
162
163 // Drop obsolete tables {wysiwyg_profile} and {wysiwyg_role}.
164 $ret[] = update_sql("DROP TABLE {wysiwyg_profile}");
165 $ret[] = update_sql("DROP TABLE {wysiwyg_role}");
166
167 return $ret;
168 }
169
170 /**
171 * Clear JS/CSS caches to ensure that clients load fresh copies.
172 */
173 function wysiwyg_update_5200() {
174 $ret = array();
175 drupal_clear_css_cache();
176
177 // Rebuild the menu to remove old admin/settings/wysiwyg/profile item.
178 menu_rebuild();
179
180 // Flush content caches.
181 cache_clear_all();
182
183 $ret[] = array(
184 'success' => TRUE,
185 'query' => 'Caches have been flushed.',
186 );
187 return $ret;
188 }
189
190 /**
191 * Update enabled font plugin buttons to default plugin in TinyMCE profiles.
192 */
193 function wysiwyg_update_5201() {
194 $ret = array();
195 $results = db_query("SELECT format, settings FROM {wysiwyg} WHERE editor = 'tinymce'");
196
197 while ($profile = db_fetch_object($results)) {
198 $settings = unserialize($profile->settings);
199 // Move enabled 'font' buttons into 'default' plugin buttons.
200 $changed = FALSE;
201 foreach (array('formatselect', 'fontselect', 'fontsizeselect', 'styleselect') as $button) {
202 if (isset($settings['buttons']['font'][$button])) {
203 $settings['buttons']['default'][$button] = $settings['buttons']['font'][$button];
204 unset($settings['buttons']['font'][$button]);
205 $changed = TRUE;
206 }
207 }
208 if ($changed) {
209 // We can't use update_sql() here because of curly braces in serialized
210 // array.
211 db_query("UPDATE {wysiwyg} SET settings='%s' WHERE format = '%s'", array(serialize($settings), $profile->format));
212 }
213 }
214 $ret[] = array(
215 'success' => TRUE,
216 'query' => 'TinyMCE profiles have been updated.',
217 );
218 return $ret;
219 }