flag.install
22.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
<?php
/**
* @file
* Flag module install/schema/update hooks.
*/
/**
* Implements hook_schema().
*/
function flag_schema() {
$schema = array();
$schema['flag'] = array(
'description' => 'All available flags in the system.',
'fields' => array(
'fid' => array(
'description' => 'The unique ID for this particular flag.',
'type' => 'serial',
'size' => 'small',
'unsigned' => TRUE,
'not null' => TRUE,
),
'entity_type' => array(
'description' => 'The flag type, such as one of "node", "comment", or "user".',
'type' => 'varchar',
'length' => '128',
'not null' => TRUE,
'default' => '',
),
'name' => array(
'description' => 'The machine-name for this flag.',
'type' => 'varchar',
'length' => '32',
'not null' => FALSE,
'default' => '',
),
'title' => array(
'description' => 'The human-readable title for this flag.',
'type' => 'varchar',
'length' => '255',
'not null' => FALSE,
'default' => '',
),
'global' => array(
'description' => 'Whether this flag state should act as a single toggle to all users across the site.',
'type' => 'int',
'size' => 'tiny',
'not null' => FALSE,
'default' => 0,
),
'options' => array(
'description' => 'The options and configuration of this flag.',
'type' => 'text',
'not null' => FALSE,
),
),
'primary key' => array('fid'),
'unique keys' => array(
'name' => array('name'),
),
);
$schema['flagging'] = array(
'description' => 'Objects that have been flagged.',
'fields' => array(
'flagging_id' => array(
'description' => 'The unique ID for this particular tag.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'fid' => array(
'description' => 'The unique flag ID this object has been flagged with, from {flag}.',
'type' => 'int',
'size' => 'small',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'entity_type' => array(
'description' => 'The flag type, eg "node", "comment", "user".',
'type' => 'varchar',
'length' => '128',
'not null' => TRUE,
'default' => '',
),
'entity_id' => array(
'description' => 'The unique ID of the object, such as either the {cid}, {uid}, or {nid}.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'uid' => array(
'description' => 'The user ID by whom this object was flagged.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'sid' => array(
'description' => "The user's numeric sid from the session_api table.",
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'timestamp' => array(
'description' => 'The UNIX time stamp representing when the flag was set.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'disp-size' => 11,
),
),
'primary key' => array('flagging_id'),
'unique keys' => array(
'fid_entity_id_uid_sid' => array('fid', 'entity_id', 'uid', 'sid'),
),
'indexes' => array(
'entity_type_uid_sid' => array('entity_type', 'uid', 'sid'),
'entity_type_entity_id_uid_sid' => array(
'entity_type',
'entity_id',
'uid',
'sid',
),
'entity_id_fid' => array('entity_id', 'fid'),
),
);
$schema['flag_types'] = array(
'description' => 'The entity bundles that are affected by a flag.',
'fields' => array(
'fid' => array(
'description' => 'The unqiue flag ID as defined for the flag in {flag}.',
'type' => 'int',
'size' => 'small',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'type' => array(
'description' => 'The entity bundles that can be flagged by this fid.',
'type' => 'varchar',
'length' => '128',
'not null' => TRUE,
'default' => '',
),
),
'indexes' => array(
'fid' => array('fid'),
),
);
$schema['flag_counts'] = array(
'description' => 'The number of times an item has been flagged.',
'fields' => array(
'fid' => array(
'type' => 'int',
'size' => 'small',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'entity_type' => array(
'description' => 'The flag type, usually one of "node", "comment", "user".',
'type' => 'varchar',
'length' => '128',
'not null' => TRUE,
'default' => '',
),
'entity_id' => array(
'description' => 'The unique ID of the content, usually either the {cid}, {uid}, or {nid}.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'disp-width' => '10',
),
'count' => array(
'description' => 'The number of times this object has been flagged for this flag.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'disp-width' => '10',
),
'last_updated' => array(
'description' => 'The UNIX time stamp representing when the flag was last updated.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'disp-size' => 11,
),
),
'primary key' => array('fid', 'entity_id'),
'indexes' => array(
'fid_entity_type' => array('fid', 'entity_type'),
'entity_type_entity_id' => array('entity_type', 'entity_id'),
'fid_count' => array('fid', 'count'),
'fid_last_updated' => array('fid', 'last_updated'),
),
);
return $schema;
}
/**
* Implements hook_uninstall().
*/
function flag_uninstall() {
$result = db_select('variable', 'v')
->fields('v', array('name'))
->condition('name', 'flag_%', 'LIKE')
->execute();
foreach ($result as $row) {
variable_del($row->name);
}
drupal_set_message(t('Flag has been uninstalled.'));
}
/**
* Implements hook_requirements().
*/
function flag_requirements($phase) {
$requirements = array();
$t = get_t();
if ($phase == 'runtime') {
if (module_exists('translation') && !module_exists('translation_helpers')) {
$requirements['flag_translation'] = array(
'title' => $t('Flag'),
'severity' => REQUIREMENT_ERROR,
'description' => $t('To have the flag module work with translations, you need to install and enable the <a href="http://drupal.org/project/translation_helpers">Translation helpers</a> module.'),
'value' => $t('Translation helpers module not found.'),
);
}
if (module_exists('session_api')) {
if (file_exists('./robots.txt')) {
$flag_path = url('flag') . '/';
// We don't use url() because this may return an absolute URL when
// language negotiation is set to 'domain'.
$flag_path = parse_url($flag_path, PHP_URL_PATH);
$robots_string = 'Disallow: ' . $flag_path;
$contents = file_get_contents('./robots.txt');
if (strpos($contents, $robots_string) === FALSE) {
$requirements['flag_robots'] = array(
'title' => $t('Flag robots.txt problem'),
'severity' => REQUIREMENT_WARNING,
'description' => $t('Flag module may currently be used with anonymous users, however the robots.txt file does not exclude the "@flag-path" path, which may cause search engines to randomly flag and unflag content when they index the site. It is highly recommended to add "@robots-string" to your robots.txt file (located in the root of your Drupal installation).', array('@flag-path' => $flag_path, '@robots-string' => $robots_string)),
'value' => $t('Search engines flagging content'),
);
}
}
}
}
return $requirements;
}
function flag_update_last_removed() {
return 6004;
}
/**
* Convert role access to have separate "flag" and "unflag" permissions.
*/
function flag_update_6200() {
if (db_field_exists('flags', 'roles')) {
$result = db_select('flags', 'f')
->fields('f')
->execute();
foreach ($result as $flag) {
$roles = array_filter(explode(',', $flag->roles));
$options = unserialize($flag->options);
$options['roles'] = array(
'flag' => $roles,
'unflag' => $roles,
);
db_update('flags')
->fields(array(
'options' => serialize($options),
))
->condition('fid', $flag->fid)
->execute();
}
db_drop_field('flags', 'roles');
}
}
/**
* Refine the indexes.
*
* The content type inclusion actually slowed down on unique key. And a count
* index would be helpful for sorting by counts.
*/
function flag_update_6201() {
// Remove "content type" from one key, see http://drupal.org/node/612602.
db_drop_unique_key('flag_content', 'fid_content_type_content_id_uid');
db_add_unique_key('flag_content', 'fid_content_id_uid', array(
'fid',
'content_id',
'uid',
));
// Add a count index, see http://drupal.org/node/489610.
db_add_index('flag_counts', 'count', array('count'));
}
/**
* Add the sid column and unique index on the flag_content table.
*/
function flag_update_6202() {
// Drop the keys affected by the addition of the SID column.
db_drop_unique_key('flag_content', 'fid_content_id_uid');
db_drop_index('flag_content', 'content_type_uid');
// Add the column.
db_add_field('flag_content', 'sid', array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
));
// Re-add the removed keys.
db_add_unique_key('flag_content', 'fid_content_id_uid_sid', array(
'fid',
'content_id',
'uid',
'sid',
));
db_add_index('flag_content', 'content_type_uid_sid', array(
'content_type',
'uid',
'sid',
));
}
/**
* Remove count = 0 rows from the count tables.
*/
function flag_update_6203() {
db_delete('flag_counts')
->condition('count', 0)
->execute();
}
/**
* Remove "content type" from the flag_counts primary key.
*/
function flag_update_6204() {
db_drop_primary_key('flag_counts');
db_add_primary_key('flag_counts', array('fid', 'content_id'));
}
/**
* Provide a better index on the flag_content table including 'uid' and 'sid'.
*/
function flag_update_6205() {
// This update has been removed and corrected in flag_update_6206.
// See http://drupal.org/node/1105490.
}
/**
* Correction to flag_update_6205(). Convert unique key to an index.
*/
function flag_update_6206() {
// Remove the old index that did not include UID or SID.
if (db_index_exists('flag_content', 'content_type_content_id')) {
db_drop_index('flag_content', 'content_type_content_id');
}
// Remove the erroneous unique key that was added in flag_update_6205().
if (db_index_exists('flag_content', 'content_type_content_id_uid_sid')) {
db_drop_unique_key('flag_content', 'content_type_content_id_uid_sid');
}
db_add_index('flag_content', 'content_type_content_id_uid_sid', array(
'content_type',
'content_id',
'uid',
'sid',
));
}
/**
* Adds column last_updated to flag_counts table.
*/
function flag_update_6207() {
db_add_field('flag_counts', 'last_updated', array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'disp-size' => 11),
array('indexes' => array('last_updated' => array('last_updated'))));
}
/**
* Convert flag_count indexes to include FID for more efficient indexing.
*/
function flag_update_6208() {
db_drop_index('flag_counts', 'count');
db_drop_index('flag_counts', 'last_updated');
db_add_index('flag_counts', 'fid_count', array('fid', 'count'));
db_add_index('flag_counts', 'fid_last_updated', array('fid', 'last_updated'));
}
/**
* Clear caches.
*/
function flag_update_7201() {
// Do nothing. Update.php is going to clear caches for us.
}
/**
* Clean-up flag records for deleted nodes and comments.
*/
function flag_update_7202() {
// These queries can't use db_delete() because that doesn't support a
// subquery: see http://drupal.org/node/1267508.
// Clean-up for nodes.
db_query("DELETE FROM {flag_content} WHERE content_type = 'node' AND NOT EXISTS (SELECT 1 FROM {node} n WHERE content_id = n.nid)");
db_query("DELETE FROM {flag_counts} WHERE content_type = 'node' AND NOT EXISTS (SELECT 1 FROM {node} n WHERE content_id = n.nid)");
// Clean-up for comments. Do not use module_exists() because comment module
// could be disabled.
if (db_table_exists('comment')) {
db_query("DELETE FROM {flag_content} WHERE content_type = 'comment' AND NOT EXISTS (SELECT 1 FROM {comment} c WHERE content_id = c.cid)");
db_query("DELETE FROM {flag_counts} WHERE content_type = 'comment' AND NOT EXISTS (SELECT 1 FROM {comment} c WHERE content_id = c.cid)");
}
}
/**
* Add an index to help with view's flag_handler_relationship when not required.
*/
function flag_update_7203() {
// Skip if this index was also added by the 6.x-2.x branch.
if (!db_index_exists('flag_content', 'content_id_fid')) {
db_add_index('flag_content', 'content_id_fid', array('content_id', 'fid'));
}
}
/**
* Rebuild the class registry due to classes moving files.
*/
function flag_update_7300() {
registry_rebuild();
}
/**
* Rename {flag_content} table to {flagging} and {flags} table to {flag}.
*/
function flag_update_7301() {
db_rename_table('flag_content', 'flagging');
db_rename_table('flags', 'flag');
// A second cache clear appears to be required here...
cache_clear_all();
// ...which in fact isn't enough, as the schema cache appears to need explicit
// clearing to prevent the next updates failing to get the schema for the new
// table names.
drupal_get_schema(NULL, TRUE);
}
/**
* Rename database columns on the {flag} table.
*/
function flag_update_7302() {
// No keys or indexes are affected.
// Change field 'content_type' to 'entity_type'.
db_change_field('flag', 'content_type', 'entity_type',
// Spec of the field. Identical to our current hook_schema(): we're not
// changing anything except the name.
array(
'description' => 'The flag type, such as one of "node", "comment", or "user".',
'type' => 'varchar',
'length' => '32',
'not null' => TRUE,
'default' => '',
)
// No keys to re-add.
);
}
/**
* Rename database columns on the {flagging} table.
*/
function flag_update_7303() {
// Drop affected keys and indexes.
db_drop_unique_key('flagging', 'fid_content_id_uid_sid');
db_drop_index('flagging', 'content_type_uid_sid');
db_drop_index('flagging', 'content_type_content_id_uid_sid');
db_drop_index('flagging', 'content_id_fid');
// Change field 'content_type' to 'entity_type'.
db_change_field('flagging', 'content_type', 'entity_type',
// Spec of the field. Identical to our current hook_schema(): we're not
// changing anything except the name.
array(
'description' => 'The flag type, eg "node", "comment", "user".',
'type' => 'varchar',
'length' => '32',
'not null' => TRUE,
'default' => '',
),
// Keys spec. Some are short-lived, as they are about to be dropped again
// and have hybrid names that refer to 'content_id' still.
array(
'unique keys' => array(
'fid_content_id_uid_sid' => array('fid', 'content_id', 'uid', 'sid'),
),
'indexes' => array(
'entity_type_uid_sid' => array('entity_type', 'uid', 'sid'),
'entity_type_content_id_uid_sid' => array(
'entity_type',
'content_id',
'uid',
'sid',
),
'content_id_fid' => array('content_id', 'fid'),
),
)
);
// Now we have to drop keys and indexes all over again!
db_drop_unique_key('flagging', 'fid_content_id_uid_sid');
db_drop_index('flagging', 'entity_type_content_id_uid_sid');
db_drop_index('flagging', 'content_id_fid');
// Change field 'content_id' to 'entity_id'.
db_change_field('flagging', 'content_id', 'entity_id',
// Spec of the field. Identical to our current hook_schema(): we're not
// changing anything except the name.
array(
'description' => 'The unique ID of the content, such as either the {cid}, {uid}, or {nid}.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
// Keys spec. Identical to current hook_schema().
array(
'unique keys' => array(
'fid_entity_id_uid_sid' => array('fid', 'entity_id', 'uid', 'sid'),
),
'indexes' => array(
'entity_type_entity_id_uid_sid' => array(
'entity_type',
'entity_id',
'uid',
'sid',
),
'entity_id_fid' => array('entity_id', 'fid'),
),
)
);
// A serial field must be defined as a key, so make a temporary index on
// 'fcid' so we can safely drop the primary key.
// @see http://drupal.org/node/190027
db_add_index('flagging', 'temp', array('fcid'));
// Drop the primary key so we can rename the field.
db_drop_primary_key('flagging');
// Change field 'fcid' to 'flagging_id'.
db_change_field('flagging', 'fcid', 'flagging_id',
// Spec of the field. Identical to our current hook_schema(): we're not
// changing anything except the name.
array(
'description' => 'The unique ID for this particular tag.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
// Keys spec. Identical to current hook_schema().
array(
'primary key' => array('flagging_id'),
)
);
// Drop our temporary index.
db_drop_index('flagging', 'temp');
cache_clear_all();
}
/**
* Rename database columns on the {flag_counts} table.
*/
function flag_update_7304() {
// Drop keys and indexes using 'content_type'.
db_drop_index('flag_counts', 'fid_content_type');
db_drop_index('flag_counts', 'content_type_content_id');
// Change field 'content_type' to 'entity_type'.
db_change_field('flag_counts', 'content_type', 'entity_type',
// Spec of the field. Identical to our current hook_schema(): we're not
// changing anything except the name.
array(
'description' => 'The flag type, usually one of "node", "comment", "user".',
'type' => 'varchar',
'length' => '32',
'not null' => TRUE,
'default' => '',
),
// Keys spec. Some are short-lived, as they are about to be dropped again.
// Note the hybrid names refer to 'content_id' still.
array(
'indexes' => array(
'fid_entity_type' => array('fid', 'entity_type'),
'entity_type_content_id' => array('entity_type', 'content_id'),
),
)
);
// Now drop keys and indexes using 'content_id'.
db_drop_primary_key('flag_counts');
db_drop_index('flag_counts', 'entity_type_content_id');
// Change field 'content_id' to 'entity_id'.
db_change_field('flag_counts', 'content_id', 'entity_id',
// Spec of the field. Identical to our current hook_schema(): we're not
// changing anything except the name.
array(
'description' => 'The unique ID of the content, usually either the {cid}, {uid}, or {nid}.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'disp-width' => '10',
),
// Keys spec. Identical to current hook_schema() now we're finished.
array(
'primary key' => array('fid', 'entity_id'),
'indexes' => array(
'entity_type_entity_id' => array('entity_type', 'entity_id'),
),
)
);
}
/**
* Convert flag roles to permissions.
*/
function flag_update_7305() {
// We can't use flag_get_flags() to get all flags to act on, because that
// now looks for user permissions and we want the old roles array to convert.
// Hence we need to get flags directly from the database.
// Flags defined in code are saved in the database by flag_get_flags(), so
// this will get them too, unless the module providing them was *only just*
// installed before update.php was run. This edge case is not covered.
$result = db_query("SELECT name, options FROM {flag}");
$flag_data = $result->fetchAllKeyed();
// Note we don't call hook_flag_alter() because we don't have a complete flag.
// If your custom module does something to flag roles, it is your
// responsibility to handle upgrading your extra role data.
foreach ($flag_data as $flag_name => $flag_options) {
$flag_options = unserialize($flag_options);
$flag_roles = $flag_options['roles'];
foreach ($flag_roles['flag'] as $rid) {
$permission = "flag $flag_name";
user_role_grant_permissions($rid, array($permission));
}
foreach ($flag_roles['unflag'] as $rid) {
$permission = "unflag $flag_name";
user_role_grant_permissions($rid, array($permission));
}
// Save the flag options with the roles array removed.
unset($flag_options['roles']);
db_update('flag')
->fields(array(
'options' => serialize($flag_options),
))
->condition('name', $flag_name)
->execute();
}
// Flags in code will now report as overridden because the roles option is no
// longer output. Notify the user that they should update them.
if (count(module_implements('flag_default_flags'))) {
drupal_set_message(t('Flags which are defined in code with hook_flag_default_flags() or Features need to be re-exported.'));
}
// Direct the user to read the change notice, which has more details of how
// access to flag objects has been affected.
return t('Flag roles have been converted to user permissions. Permissions have been granted to each flag based on flag roles. You should review the consequences of this in the <a href="!url">change record</a>.', array(
'!url' => 'http://drupal.org/node/1724256',
));
}
/**
* Convert flag view modes settings.
*/
function flag_update_7306() {
foreach (flag_get_flags() as $flag) {
// Update show_on_teaser property to use new view mode settings.
if (!empty($flag->show_on_teaser)) {
$flag->show_in_links['teaser'] = TRUE;
unset($flag->show_on_teaser);
}
// Update show_on_page property to use new view mode settings.
if (!empty($flag->show_on_page)) {
$flag->show_in_links['full'] = TRUE;
unset($flag->show_on_page);
}
// Update show_on_comment and show_on_entity properties to use new view
// mode settings. Since the old logic was to show on all view modes, do
// that.
if (!empty($flag->show_on_entity) || !empty($flag->show_on_comment)) {
if ($entity_info = entity_get_info($flag->entity_type)) {
foreach ($entity_info['view modes'] as $view_mode => $value) {
$flag->show_in_links[$view_mode] = TRUE;
}
}
unset($flag->show_on_entity, $flag->show_on_comment);
}
$flag->save();
}
}