Merge branch '7.x-1.x' of git.drupal.org:project/message into 7.x-1.x
[project/message.git] / message.install
1 <?php
2
3 /**
4 * @file
5 * Install, update, and uninstall functions for the message module.
6 */
7
8 /**
9 * Implements hook_install().
10 */
11 function message_install() {
12 // Create the field holding the message text.
13 $field = array(
14 'field_name' => MESSAGE_FIELD_MESSAGE_TEXT,
15 'type' => 'text_long',
16 'entity_types' => array('message_type'),
17 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
18 'translatable' => TRUE,
19 'locked' => TRUE,
20 );
21 $field = field_create_field($field);
22 $instance = array(
23 'field_name' => MESSAGE_FIELD_MESSAGE_TEXT,
24 'bundle' => 'message_type',
25 'entity_type' => 'message_type',
26 'label' => t('Message text'),
27 'description' => t('This is the text of all messages of this type.'),
28 'required' => TRUE,
29 'settings' => array(
30 'text_processing' => 1,
31 ),
32 );
33 field_create_instance($instance);
34
35 variable_del('message_entity_delete_cleanup');
36 }
37
38 /**
39 * Implements hook_uninstall().
40 */
41 function message_uninstall() {
42 $instance = field_info_instance('message_type', 'message_text', 'message_type');
43 field_delete_instance($instance);
44 field_delete_field('message_text');
45
46 variable_del('message_delete_cron_limit');
47 variable_del('message_purge_enable');
48 variable_del('message_purge_quota');
49 variable_del('message_purge_days');
50 }
51
52 /**
53 * Implements hook_schema()
54 */
55 function message_schema() {
56 $schema['message_type_category'] = array(
57 'description' => 'Storage for user-defined message category.',
58 'fields' => array(
59 // Although the "category" should be enough as the primary key, the
60 // numeric ID is required for the internal use of entity API.
61 'id' => array(
62 'type' => 'serial',
63 'not null' => TRUE,
64 'description' => 'Primary Key: Numeric message type category ID.',
65 ),
66 'category' => array(
67 'description' => 'The unified identifier for a message type category.',
68 'type' => 'varchar',
69 'length' => 255,
70 'not null' => TRUE,
71 'default' => '',
72 ),
73 'description' => array(
74 'description' => 'Description for this message type category.',
75 'type' => 'varchar',
76 'length' => 255,
77 'not null' => TRUE,
78 'default' => '',
79 ),
80 'language' => array(
81 'description' => 'The {languages}.language of this message type category.',
82 'type' => 'varchar',
83 'length' => 12,
84 'not null' => TRUE,
85 'default' => '',
86 ),
87 'status' => array(
88 'type' => 'int',
89 'not null' => TRUE,
90 // Set the default to ENTITY_CUSTOM without using the constant as it is
91 // not safe to use it at this point.
92 'default' => 0x01,
93 'size' => 'tiny',
94 'description' => 'The exportable status of the entity.',
95 ),
96 'module' => array(
97 'description' => 'The name of the providing module if the entity has been defined in code.',
98 'type' => 'varchar',
99 'length' => 255,
100 'not null' => FALSE,
101 ),
102 ),
103 'primary key' => array('id'),
104 'unique keys' => array(
105 'category' => array('category'),
106 ),
107 );
108
109 $schema['message_type'] = array(
110 'description' => 'Storage for user-defined message templates.',
111 'fields' => array(
112 // Although the "name" should be enough as the primary key, the numeric ID
113 // is required for the internal use of entity API.
114 'id' => array(
115 'type' => 'serial',
116 'not null' => TRUE,
117 'description' => 'Primary Key: Numeric message type ID.',
118 ),
119 'name' => array(
120 'description' => 'The unified identifier for a message type.',
121 'type' => 'varchar',
122 'length' => 255,
123 'not null' => TRUE,
124 'default' => '',
125 ),
126 'category' => array(
127 'description' => 'Reference to a message type category.',
128 'type' => 'varchar',
129 'length' => 255,
130 'not null' => TRUE,
131 'default' => 'message_type',
132 ),
133 'description' => array(
134 'description' => 'Description for this message type.',
135 'type' => 'varchar',
136 'length' => 255,
137 'not null' => TRUE,
138 'default' => '',
139 ),
140 'argument_keys' => array(
141 'description' => 'Serialized array with the argument keys',
142 'type' => 'text',
143 'serialize' => TRUE,
144 ),
145 'language' => array(
146 'description' => 'The {languages}.language of this message type.',
147 'type' => 'varchar',
148 'length' => 12,
149 'not null' => TRUE,
150 'default' => '',
151 ),
152 'status' => array(
153 'type' => 'int',
154 'not null' => TRUE,
155 // Set the default to ENTITY_CUSTOM without using the constant as it is
156 // not safe to use it at this point.
157 'default' => 0x01,
158 'size' => 'tiny',
159 'description' => 'The exportable status of the entity.',
160 ),
161 'module' => array(
162 'description' => 'The name of the providing module if the entity has been defined in code.',
163 'type' => 'varchar',
164 'length' => 255,
165 'not null' => FALSE,
166 ),
167 'arguments' => array(
168 'description' => 'Serialized array with the arguments.',
169 'type' => 'text',
170 'serialize' => TRUE,
171 ),
172 'data' => array(
173 'description' => 'Serialized array with general data.',
174 'type' => 'text',
175 'serialize' => TRUE,
176 'size' => 'big',
177 ),
178 ),
179 'primary key' => array('id'),
180 'unique keys' => array(
181 'name' => array('name'),
182 ),
183 );
184
185 $schema['message'] = array(
186 'description' => 'An instance of a message type (e.g. like a node is an instance of a node type).',
187 'fields' => array(
188 'mid' => array(
189 'type' => 'serial',
190 'unsigned' => TRUE,
191 'description' => 'The Unique ID of the message.',
192 ),
193 'type' => array(
194 'description' => 'Reference to a message a type.',
195 'type' => 'varchar',
196 'length' => 255,
197 'not null' => TRUE,
198 'default' => '',
199 ),
200 'arguments' => array(
201 'description' => 'Serialized array with the arguments',
202 'type' => 'text',
203 'serialize' => TRUE,
204 ),
205 'uid' => array(
206 'description' => 'The user ID of the acting user.',
207 'type' => 'int',
208 'default value' => NULL,
209 'unsigned' => TRUE,
210 ),
211 'timestamp' => array(
212 'description' => 'When the message instance was recorded.',
213 'type' => 'int',
214 'not null' => TRUE,
215 'unsigned' => TRUE,
216 ),
217 'language' => array(
218 'description' => 'The {languages}.language of this message.',
219 'type' => 'varchar',
220 'length' => 12,
221 'not null' => TRUE,
222 'default' => '',
223 ),
224 ),
225 'foreign keys' => array(
226 'message_type' => array(
227 'table' => 'message_type',
228 'columns' => array('type' => 'name'),
229 ),
230 'owner' => array(
231 'table' => 'users',
232 'columns' => array('uid' => 'uid'),
233 ),
234 ),
235 'primary key' => array('mid'),
236 );
237 return $schema;
238 }
239
240
241 /**
242 * Add in the exportable entity db columns as required by the entity API.
243 */
244 function message_update_7000() {
245 db_add_field('message_type', 'status', array(
246 'type' => 'int',
247 'not null' => TRUE,
248 // Set the default to ENTITY_CUSTOM without using the constant as it is
249 // not safe to use it at this point.
250 'default' => 0x01,
251 'size' => 'tiny',
252 'description' => 'The exportable status of the entity.',
253 ));
254 db_add_field('message_type', 'module', array(
255 'description' => 'The name of the providing module if the entity has been defined in code.',
256 'type' => 'varchar',
257 'length' => 255,
258 'not null' => FALSE,
259 ));
260 }
261
262 /**
263 * Add the argument keys column of a message type.
264 */
265 function message_update_7001() {
266 db_add_field('message_type', 'argument_keys', array(
267 'description' => 'Serialized array with the argument keys',
268 'type' => 'text',
269 'serialize' => TRUE,
270 ));
271 }
272
273 /**
274 * Update the message type text field to have text processing.
275 */
276 function message_update_7002() {
277 $instance = array(
278 'field_name' => MESSAGE_FIELD_MESSAGE_TEXT,
279 'bundle' => 'message_type',
280 'entity_type' => 'message_type',
281 'label' => t('Message text'),
282 'description' => t('This is the text of all messages of this type.'),
283 'required' => TRUE,
284 'settings' => array(
285 'text_processing' => 1,
286 ),
287 );
288 field_update_instance($instance);
289 }
290
291 /**
292 * Add message type category.
293 */
294 function message_update_7003() {
295 $schema['message_type_category'] = array(
296 'description' => 'Storage for user-defined message category.',
297 'fields' => array(
298 // Although the "category" should be enough as the primary key, the
299 // numeric ID is required for the internal use of entity API.
300 'id' => array(
301 'type' => 'serial',
302 'not null' => TRUE,
303 'description' => 'Primary Key: Numeric message type category ID.',
304 ),
305 'category' => array(
306 'description' => 'The unified identifier for a message type category.',
307 'type' => 'varchar',
308 'length' => 255,
309 'not null' => TRUE,
310 'default' => '',
311 ),
312 'description' => array(
313 'description' => 'Description for this message type category.',
314 'type' => 'varchar',
315 'length' => 255,
316 'not null' => TRUE,
317 'default' => '',
318 ),
319 'status' => array(
320 'type' => 'int',
321 'not null' => TRUE,
322 // Set the default to ENTITY_CUSTOM without using the constant as it is
323 // not safe to use it at this point.
324 'default' => 0x01,
325 'size' => 'tiny',
326 'description' => 'The exportable status of the entity.',
327 ),
328 'module' => array(
329 'description' => 'The name of the providing module if the entity has been defined in code.',
330 'type' => 'varchar',
331 'length' => 255,
332 'not null' => FALSE,
333 ),
334 ),
335 'primary key' => array('id'),
336 'unique keys' => array(
337 'category' => array('category'),
338 ),
339 );
340 db_create_table('message_type_category', $schema['message_type_category']);
341
342 db_add_field('message_type', 'category', array(
343 'description' => 'Reference to a message type category.',
344 'type' => 'varchar',
345 'length' => 255,
346 'not null' => TRUE,
347 'default' => 'message_type',
348 ));
349 }
350
351 /**
352 * Rename the 'name' column to 'type' in {Message}.
353 */
354 function message_update_7004() {
355 $column = array(
356 'description' => 'Reference to a message a type.',
357 'type' => 'varchar',
358 'length' => 255,
359 'not null' => TRUE,
360 'default' => '',
361 );
362 db_change_field('message', 'name', 'type', $column);
363 }
364
365
366 /**
367 * Add "arguments" column to message-type.
368 */
369 function message_update_7005() {
370 $column = array(
371 'description' => 'Serialized array with the arguments',
372 'type' => 'text',
373 'serialize' => TRUE,
374 );
375 db_add_field('message_type', 'arguments', $column);
376 }
377
378 /**
379 * Add "data" column to message-type.
380 */
381 function message_update_7006() {
382 $column = array(
383 'description' => 'Serialized array with general data.',
384 'type' => 'text',
385 'serialize' => TRUE,
386 'size' => 'big',
387 );
388 db_add_field('message_type', 'data', $column);
389 }
390
391 /**
392 * Add "language" column to all Message's related entities.
393 */
394 function message_update_7007(&$sandbox) {
395 // Update the existing entities, with the default language.
396 $langcode = language_default()->language;
397
398 $tables = array(
399 'message_type_category',
400 'message_type',
401 'message',
402 );
403
404 if (!isset($sandbox['total'])) {
405 foreach ($tables as $key => $table) {
406 $name = str_replace('_', ' ', $table);
407 $column = array(
408 'description' => "The {languages}.language of this $name.",
409 'type' => 'varchar',
410 'length' => 12,
411 'not null' => TRUE,
412 'default' => '',
413 );
414 db_add_field($table, 'language', $column);
415
416 if ($table == 'message') {
417 // We don't want to time out when updating messages, so we will
418 // process it using batches.
419 $query = db_select($table);
420 $sandbox['last'] = 0;
421 $sandbox['total'] = $query->countQuery()->execute()->fetchField();
422 $sandbox['#finished'] = 0;
423 }
424 else {
425 db_update($table)
426 ->fields(array(
427 'language' => $langcode,
428 ))
429 ->execute();
430 }
431 }
432 }
433 elseif ($sandbox['total'] && $sandbox['last'] <= $sandbox['total']) {
434 // Update messages.
435 $batch_size = 200;
436 db_update('message')
437 ->fields(array(
438 'language' => $langcode,
439 ))
440 ->condition('mid', $sandbox['last'], '>')
441 ->execute();
442
443 $sandbox['last'] += $batch_size;
444 $sandbox['#finished'] = min(0.99, $sandbox['last'] / $sandbox['total']);
445 }
446 else {
447 // Finished processing.
448 $sandbox['#finished'] = 1;
449 }
450 }
451
452 /**
453 * Make message-text field cardinality unlimited.
454 */
455 function message_update_7008() {
456 $field = field_info_field(MESSAGE_FIELD_MESSAGE_TEXT);
457 $field['cardinality'] = FIELD_CARDINALITY_UNLIMITED;
458 field_update_field($field);
459 }