Projet

Général

Profil

Paste
Télécharger (32,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / file_entity / file_entity.install @ a2baadd1

1
<?php
2

    
3
/**
4
 * @file
5
 * Install, update and uninstall functions for the file_entity module.
6
 */
7

    
8
/**
9
 * Implements hook_schema().
10
 */
11
function file_entity_schema() {
12
  $schema['file_type'] = array(
13
    'description' => 'Stores the settings for file types.',
14
    'fields' => array(
15
      'type' => array(
16
        'description' => 'The machine name of the file type.',
17
        'type' => 'varchar',
18
        'length' => 255,
19
        'not null' => TRUE,
20
        'default' => '',
21
      ),
22
      'label' => array(
23
        'description' => 'The human readable name of the file type.',
24
        'type' => 'varchar',
25
        'length' => 255,
26
        'not null' => TRUE,
27
        'default' => '',
28
        'translatable' => TRUE,
29
      ),
30
      'description' => array(
31
        'description' => 'A brief description of this file type.',
32
        'type' => 'text',
33
        'not null' => TRUE,
34
        'size' => 'medium',
35
        'translatable' => TRUE,
36
      ),
37
      'mimetypes' => array(
38
        'description' => 'Mimetypes mapped to this file type.',
39
        'type' => 'blob',
40
        'size' => 'big',
41
        'not null' => FALSE,
42
        'serialize' => TRUE,
43
      ),
44
    ),
45
    'primary key' => array('type'),
46
    'export' => array(
47
      'key' => 'type',
48
      'key name' => 'Type',
49
      'primary key' => 'type',
50
      'default hook' => 'file_default_types',
51
      'identifier' => 'file_type',
52
      'export type string' => 'ctools_type',
53
      'save callback' => 'file_type_save',
54
      'delete callback' => 'file_type_delete',
55
      'api' => array(
56
        'owner' => 'file_entity',
57
        'api' => 'file_type',
58
        'minimum_version' => 1,
59
        'current_version' => 1,
60
      ),
61
    ),
62
  );
63
  $schema['file_display'] = array(
64
    'description' => 'Stores configuration options for file displays.',
65
    'fields' => array(
66
      // @todo Can be refactored as a compond primary key after
67
      //   http://drupal.org/node/924236 is implemented.
68
      'name' => array(
69
        'description' => 'A combined string (FILE_TYPE__VIEW_MODE__FILE_FORMATTER) identifying a file display configuration. For integration with CTools Exportables, stored as a single string rather than as a compound primary key.',
70
        'type' => 'varchar',
71
        'length' => '255',
72
        'not null' => TRUE,
73
      ),
74
      'weight' => array(
75
        'type' => 'int',
76
        'not null' => TRUE,
77
        'default' => 0,
78
        'description' => 'Weight of formatter within the display chain for the associated file type and view mode. A file is rendered using the lowest weighted enabled display configuration that matches the file type and view mode and that is capable of displaying the file.',
79
      ),
80
      'status' => array(
81
        'type' => 'int',
82
        'unsigned' => TRUE,
83
        'not null' => TRUE,
84
        'default' => 0,
85
        'size' => 'tiny',
86
        'description' => 'The status of the display. (1 = enabled, 0 = disabled)',
87
      ),
88
      'settings' => array(
89
        'type' => 'blob',
90
        'not null' => FALSE,
91
        'size' => 'big',
92
        'serialize' => TRUE,
93
        'description' => 'A serialized array of name value pairs that store the formatter settings for the display.',
94
      ),
95
    ),
96
    'primary key' => array('name'),
97
    // Exportable support via CTools.
98
    'export' => array(
99
      'key' => 'name',
100
      'key name' => 'Name',
101
      'primary key' => 'name',
102
      // The {file_display}.status field is used to control whether the display
103
      // is active in the display chain. CTools-level disabling is something
104
      // different, and it's not yet clear how to interpret it for file
105
      // displays. Until that's figured out, prevent CTools-level disabling.
106
      'can disable' => FALSE,
107
      'default hook' => 'file_default_displays',
108
      'identifier' => 'file_display',
109
      'api' => array(
110
        'owner' => 'file_entity',
111
        'api' => 'file_default_displays',
112
        'minimum_version' => 1,
113
        'current_version' => 1,
114
      ),
115
    ),
116
  );
117
  $schema['file_metadata'] = array(
118
    'description' => 'Cache images dimensions.',
119
    'fields' => array(
120
      'fid' => array(
121
        'description' => 'The {file_managed}.fid of the metadata.',
122
        'type' => 'int',
123
        'unsigned' => TRUE,
124
        'not null' => TRUE,
125
        'default' => 0,
126
      ),
127
      'name' => array(
128
        'description' => "The name of the metadata (e.g. 'width').",
129
        'type' => 'varchar',
130
        'length' => '255',
131
        'not null' => TRUE,
132
      ),
133
      'value' => array(
134
        'description' => "The value of the metadata (e.g. '200px').",
135
        'type' => 'blob',
136
        'not null' => FALSE,
137
        'size' => 'big',
138
        'serialize' => TRUE,
139
      ),
140
    ),
141
    'primary key' => array('fid', 'name'),
142
    'foreign keys' => array(
143
      'file_managed' => array(
144
        'table' => 'file_managed',
145
        'columns' => array('fid' => 'fid'),
146
      ),
147
    ),
148
  );
149
  return $schema;
150
}
151

    
152
/**
153
 * Implements hook_schema_alter().
154
 */
155
function file_entity_schema_alter(&$schema) {
156
  $schema['file_managed']['fields']['type'] = array(
157
    'description' => 'The type of this file.',
158
    'type' => 'varchar',
159
    'length' => 50,
160
    'not null' => TRUE,
161
    // If the FILE_TYPE_NONE constant ever changes, then change the value here
162
    // too, and add an update function to deal with existing records. The
163
    // constant isn't used here, because there may be cases where this function
164
    // runs without the module file loaded.
165
    'default' => 'undefined',
166
  );
167
  $schema['file_managed']['indexes']['file_type'] = array('type');
168
}
169

    
170

    
171
/**
172
 * Implements hook_install().
173
 */
174
function file_entity_install() {
175
  $schema = array();
176
  file_entity_schema_alter($schema);
177
  $spec = $schema['file_managed']['fields']['type'];
178
  $indexes_new = array('indexes' => $schema['file_managed']['indexes']);
179

    
180
  // If another module (e.g., Media) had added a {file_managed}.type field,
181
  // then change it to the expected specification. Otherwise, add the field.
182
  if (db_field_exists('file_managed', 'type')) {
183
    // db_change_field() will fail if any records have type=NULL, so update
184
    // them to the new default value.
185
    db_update('file_managed')->fields(array('type' => $spec['default']))->isNull('type')->execute();
186

    
187
    // Indexes using a field being changed must be dropped prior to calling
188
    // db_change_field(). However, the database API doesn't provide a way to do
189
    // this without knowing what the old indexes are. Therefore, it is the
190
    // responsibility of the module that added them to drop them prior to
191
    // allowing this module to be installed.
192
    db_change_field('file_managed', 'type', 'type', $spec, $indexes_new);
193
  }
194
  else {
195
    db_add_field('file_managed', 'type', $spec, $indexes_new);
196
  }
197

    
198
  // Set permissions.
199
  $roles = user_roles();
200
  foreach ($roles as $rid => $role) {
201
    user_role_grant_permissions($rid, array('view files'));
202
  }
203

    
204
  // Create the title and alt text fields.
205
  _file_entity_create_alt_title_fields();
206

    
207
  // Configure default pathauto variables if it is currently installed.
208
  if (module_exists('pathauto')) {
209
    variable_set('pathauto_file_pattern', 'file/[file:name]');
210
  }
211

    
212
  // Classify existing files according to the currently defined file types.
213
  // Queue all files to be classified during cron runs using the Queue API.
214
  $queue = DrupalQueue::get('file_entity_type_determine');
215
  $result = db_query('SELECT fid FROM {file_managed}');
216
  foreach ($result as $record) {
217
    $queue->createItem($record->fid);
218
  }
219

    
220
  // Warn users that existing files will not have a file type until the queue
221
  // has been processed.
222
  if ($queue->numberOfItems()) {
223
    drupal_set_message(t('Existing files must be classified according to the currently defined file types. These files have been queued for processing and will have their file type determined during cron runs.'));
224
  }
225
}
226

    
227
/**
228
 * Implements hook_uninstall().
229
 */
230
function file_entity_uninstall() {
231
  drupal_load('module', 'file_entity');
232
  foreach (file_type_load_all(TRUE) as $type) {
233
    file_type_delete($type);
234
  }
235

    
236
  // Remove the added column to the core {file_managed} table.
237
  db_drop_field('file_managed', 'type');
238

    
239
  // Remove variables.
240
  variable_del('file_entity_max_filesize');
241
  variable_del('file_entity_default_allowed_extensions');
242
  variable_del('file_entity_alt');
243
  variable_del('file_entity_title');
244
  variable_del('file_entity_allow_insecure_download');
245
  variable_del('file_entity_file_upload_wizard_skip_file_type');
246
  variable_del('file_entity_file_upload_wizard_skip_scheme');
247
  variable_del('file_entity_file_upload_wizard_skip_fields');
248
}
249

    
250
/**
251
 * Create the {file_display} database table.
252
 */
253
function file_entity_update_7000() {
254
  if (db_table_exists('file_display')) {
255
    return t('The table {file_display} already exists.');
256
  }
257

    
258
  $schema['file_display'] = array(
259
    'description' => 'Stores configuration options for file displays.',
260
    'fields' => array(
261
      'name' => array(
262
        'description' => 'A combined string (FILE_TYPE__VIEW_MODE__FILE_FORMATTER) identifying a file display configuration. For integration with CTools Exportables, stored as a single string rather than as a compound primary key.',
263
        'type' => 'varchar',
264
        'length' => '255',
265
        'not null' => TRUE,
266
      ),
267
      'weight' => array(
268
        'type' => 'int',
269
        'not null' => TRUE,
270
        'default' => 0,
271
        'description' => 'Weight of formatter within the display chain for the associated file type and view mode. A file is rendered using the lowest weighted enabled display configuration that matches the file type and view mode and that is capable of displaying the file.',
272
      ),
273
      'status' => array(
274
        'type' => 'int',
275
        'unsigned' => TRUE,
276
        'not null' => TRUE,
277
        'default' => 0,
278
        'size' => 'tiny',
279
        'description' => 'The status of the display. (1 = enabled, 0 = disabled)',
280
      ),
281
      'settings' => array(
282
        'type' => 'blob',
283
        'not null' => FALSE,
284
        'size' => 'big',
285
        'serialize' => TRUE,
286
        'description' => 'A serialized array of name value pairs that store the formatter settings for the display.',
287
      ),
288
    ),
289
    'primary key' => array('name'),
290
  );
291
  db_create_table('file_display', $schema['file_display']);
292
}
293

    
294
/**
295
 * Move file display configurations.
296
 *
297
 * Move file display configurations from the 'file_displays' variable to the
298
 * {file_display} database table.
299
 */
300
function file_entity_update_7001() {
301
  $file_displays = variable_get('file_displays');
302
  if (!empty($file_displays)) {
303
    foreach ($file_displays as $file_type => $file_type_displays) {
304
      if (!empty($file_type_displays)) {
305
        foreach ($file_type_displays as $view_mode => $view_mode_displays) {
306
          if (!empty($view_mode_displays)) {
307
            foreach ($view_mode_displays as $formatter_name => $display) {
308
              if (!empty($display)) {
309
                db_merge('file_display')
310
                  ->key(array(
311
                    'name' => implode('__', array($file_type, $view_mode, $formatter_name)),
312
                  ))
313
                  ->fields(array(
314
                    'status' => isset($display['status']) ? $display['status'] : 0,
315
                    'weight' => isset($display['weight']) ? $display['weight'] : 0,
316
                    'settings' => isset($display['settings']) ? serialize($display['settings']) : NULL,
317
                  ))
318
                  ->execute();
319
              }
320
            }
321
          }
322
        }
323
      }
324
    }
325
  }
326
  variable_del('file_displays');
327
}
328

    
329
/**
330
 * Empty update function to trigger a theme registry rebuild.
331
 */
332
function file_entity_update_7100() { }
333

    
334
/**
335
 * Update all files with empty types to use the first part of filemime.
336
 *
337
 * For example, an png image with filemime 'image/png' will be assigned a file
338
 * type of 'image'.
339
 */
340
function file_entity_update_7101() {
341
  db_update('file_managed')
342
    ->expression('type', "SUBSTRING_INDEX(filemime, '/', 1)")
343
    ->condition('type', '')
344
    ->execute();
345
}
346

    
347
/**
348
 * Empty update function to trigger an entity cache rebuild.
349
 */
350
function file_entity_update_7102() {
351
}
352

    
353
/**
354
 * Empty update function.
355
 */
356
function file_entity_update_7103() {
357
}
358

    
359
/**
360
 * Assign view file permission when updating without the Media module.
361
 */
362
function file_entity_update_7104() {
363
  if (!module_exists('media')) {
364
    $roles = user_roles(FALSE, 'view file');
365
    if (empty($roles)) {
366
      // Set permissions.
367
      $roles = user_roles();
368
      foreach ($roles as $rid => $role) {
369
        // Do not use user_role_grant_permission() since it relies on
370
        // hook_permission(), which will not run for file entity module if it
371
        // is disabled or the permission is renamed or removed.
372
        db_merge('role_permission')
373
          ->fields(array(
374
            'rid' => $rid,
375
            'permission' => 'view file',
376
            'module' => 'file_entity',
377
          ))
378
          ->execute();
379
      }
380
    }
381
  }
382
}
383

    
384
/**
385
 * Create the {image_dimensions} database table.
386
 */
387
function file_entity_update_7200() {
388
  if (db_table_exists('image_dimensions')) {
389
    return t('The table {image_dimensions} already exists.');
390
  }
391

    
392
  $schema['image_dimensions'] = array(
393
    'description' => 'Cache images dimensions.',
394
    'fields' => array(
395
      'fid' => array(
396
        'description' => 'File ID.',
397
        'type' => 'serial',
398
        'unsigned' => TRUE,
399
        'not null' => TRUE,
400
      ),
401
      'height' => array(
402
        'description' => 'The height of the image in pixels.',
403
        'type' => 'int',
404
        'unsigned' => TRUE,
405
        'not null' => TRUE,
406
        'default' => 0,
407
      ),
408
      'width' => array(
409
        'description' => 'The width of the image in pixels..',
410
        'type' => 'int',
411
        'unsigned' => TRUE,
412
        'not null' => TRUE,
413
        'default' => 0,
414
      ),
415
    ),
416
    'primary key' => array('fid'),
417
    'foreign keys' => array(
418
      'file_managed' => array(
419
        'table' => 'file_managed',
420
        'columns' => array('fid' => 'fid'),
421
      ),
422
    ),
423
  );
424
  db_create_table('image_dimensions', $schema['image_dimensions']);
425
}
426

    
427
/**
428
 * Add the {file_type}, {file_type_mimetypes} tables.
429
 */
430
function file_entity_update_7201() {
431
  $schema = array(
432
    'description' => 'Stores the settings for file types.',
433
    'fields' => array(
434
      'type' => array(
435
        'description' => 'The machine name of the file type.',
436
        'type' => 'varchar',
437
        'length' => 255,
438
        'not null' => TRUE,
439
        'default' => '',
440
      ),
441
      'label' => array(
442
        'description' => 'The human readable name of the file type.',
443
        'type' => 'varchar',
444
        'length' => 255,
445
        'not null' => TRUE,
446
        'default' => '',
447
        'translatable' => TRUE,
448
      ),
449
      'description' => array(
450
        'description' => 'A brief description of this file type.',
451
        'type' => 'text',
452
        'not null' => TRUE,
453
        'size' => 'medium',
454
        'translatable' => TRUE,
455
      ),
456
    ),
457
    'primary key' => array('type'),
458
    'export' => array(
459
      'key' => 'type',
460
      'key name' => 'Type',
461
      'primary key' => 'type',
462
      'default hook' => 'file_default_types',
463
      'identifier' => 'file_type',
464
      'export type string' => 'ctools_type',
465
      'subrecords callback' => 'file_type_load_subrecords',
466
      'save callback' => 'file_type_save',
467
      'delete callback' => 'file_type_delete',
468
      'api' => array(
469
        'owner' => 'file_entity',
470
        'api' => 'file_type',
471
        'minimum_version' => 1,
472
        'current_version' => 1,
473
      ),
474
    ),
475
  );
476
  if (!db_table_exists('file_type')) {
477
    db_create_table('file_type', $schema);
478
  }
479

    
480
  $schema = array(
481
    'description' => 'Maps mimetypes to file types.',
482
    'fields' => array(
483
      'type' => array(
484
        'description' => 'The machine name of the file type.',
485
        'type' => 'varchar',
486
        'length' => 255,
487
        'not null' => TRUE,
488
        'default' => '',
489
      ),
490
      'mimetype' => array(
491
        'description' => 'Mimetypes mapped to this file type.',
492
        'type' => 'varchar',
493
        'length' => 255,
494
        'not null' => TRUE,
495
        'default' => '',
496
      ),
497
    ),
498
    'indexes' => array(
499
      'file_type' => array('type'),
500
      'file_type_mimetype' => array('mimetype'),
501
    ),
502
  );
503
  if (!db_table_exists('file_type_mimetypes')) {
504
    db_create_table('file_type_mimetypes', $schema);
505
  }
506
}
507

    
508
/**
509
 * Update empty {file_managed}.type records to 'undefined'.
510
 *
511
 * Drupal 7.8 disallows empty string as the value for a bundle key, so update
512
 * empty {file_managed}.type records to 'undefined' instead.
513
 */
514
function file_entity_update_7202() {
515
  db_update('file_managed')
516
    // Using 'undefined' instead of FILE_TYPE_NONE, because update functions can
517
    // run for disabled modules.
518
    ->fields(array('type' => 'undefined'))
519
    ->condition('type', '')
520
    ->execute();
521
}
522

    
523
/**
524
 * Update permission names.
525
 */
526
function file_entity_update_7203() {
527
  $permissions = array(
528
    'view file' => 'view files',
529
    'edit file' => 'edit any files',
530
  );
531
  foreach ($permissions as $old => $new) {
532
    db_update('role_permission')
533
      ->fields(array('permission' => $new))
534
      ->condition('permission', $old)
535
      ->execute();
536
  }
537
}
538

    
539

    
540
/**
541
 * Add title and alt text to image file types.
542
 */
543
function file_entity_update_7204() {
544
  _file_entity_create_alt_title_fields();
545
}
546

    
547
/**
548
 * Function to create the title and alt text fields and instances.
549
 */
550
function _file_entity_create_alt_title_fields() {
551
  $t = get_t();
552
  // Create the alt text field and instance.
553
  // Define the alt text field.
554
  $alt_text_field = array(
555
    'active' => '1',
556
    'cardinality' => '1',
557
    'deleted' => '0',
558
    'entity_types' => array(),
559
    'field_name' => 'field_file_image_alt_text',
560
    'foreign keys' => array(
561
      'format' => array(
562
        'columns' => array(
563
          'format' => 'format',
564
        ),
565
        'table' => 'filter_format',
566
      ),
567
    ),
568
    'indexes' => array(
569
      'format' => array(
570
        0 => 'format',
571
      ),
572
    ),
573
    'module' => 'text',
574
    'settings' => array(
575
      'max_length' => '255',
576
    ),
577
    'translatable' => '0',
578
    'type' => 'text',
579
  );
580

    
581
  // As long as the alt text field doesn't already exist create it.
582
  if (!field_info_field($alt_text_field['field_name'])) {
583
    field_create_field($alt_text_field);
584
  }
585

    
586
  // Define the alt text instance.
587
  $alt_text_instance = array(
588
    'bundle' => 'image',
589
    'default_value' => NULL,
590
    'deleted' => '0',
591
    'description' => $t('Alternative text is used by screen readers, search engines, and when the image cannot be loaded. By adding alt text you improve accessibility and search engine optimization.'),
592
    'display' => array(
593
      'default' => array(
594
        'label' => 'above',
595
        'settings' => array(),
596
        'type' => 'hidden',
597
        'weight' => 0,
598
      ),
599
      'full' => array(
600
        'label' => 'above',
601
        'settings' => array(),
602
        'type' => 'hidden',
603
        'weight' => 0,
604
      ),
605
      'preview' => array(
606
        'label' => 'above',
607
        'settings' => array(),
608
        'type' => 'hidden',
609
        'weight' => 0,
610
      ),
611
      'teaser' => array(
612
        'label' => 'above',
613
        'settings' => array(),
614
        'type' => 'hidden',
615
        'weight' => 0,
616
      ),
617
    ),
618
    'entity_type' => 'file',
619
    'field_name' => 'field_file_image_alt_text',
620
    'label' => 'Alt Text',
621
    'required' => 0,
622
    'settings' => array(
623
      'text_processing' => '0',
624
      'user_register_form' => FALSE,
625
    ),
626
    'widget' => array(
627
      'active' => 1,
628
      'module' => 'text',
629
      'settings' => array(
630
        'size' => '60',
631
      ),
632
      'type' => 'text_textfield',
633
      'weight' => '-4',
634
    ),
635
  );
636

    
637
  // For sites that updated from Media 1.x, continue to provide these deprecated
638
  // view modes.
639
  // @see http://drupal.org/node/1051090
640
  if (variable_get('media__show_deprecated_view_modes')) {
641
    $alt_text_instance['display'] += array(
642
      'media_link' => array(
643
        'label' => 'above',
644
        'settings' => array(),
645
        'type' => 'hidden',
646
        'weight' => 0,
647
      ),
648
      'media_original' => array(
649
        'label' => 'above',
650
        'settings' => array(),
651
        'type' => 'hidden',
652
        'weight' => 0,
653
      ),
654
    );
655
  }
656

    
657
  // As long as the alt text instance doesn't already exist create it.
658
  if (!field_info_instance($alt_text_instance['entity_type'], $alt_text_instance['field_name'], $alt_text_instance['bundle'])) {
659
    field_create_instance($alt_text_instance);
660
  }
661

    
662
  // Create the title text field and instance.
663
  // Define the title text field.
664
  $title_text_field = array(
665
    'active' => '1',
666
    'cardinality' => '1',
667
    'deleted' => '0',
668
    'entity_types' => array(),
669
    'field_name' => 'field_file_image_title_text',
670
    'foreign keys' => array(
671
      'format' => array(
672
        'columns' => array(
673
          'format' => 'format',
674
        ),
675
        'table' => 'filter_format',
676
      ),
677
    ),
678
    'indexes' => array(
679
      'format' => array(
680
        0 => 'format',
681
      ),
682
    ),
683
    'module' => 'text',
684
    'settings' => array(
685
      'max_length' => '255',
686
    ),
687
    'translatable' => '0',
688
    'type' => 'text',
689
  );
690

    
691
  // As long as the title text field doesn't exist create it.
692
  if (!field_info_field($title_text_field['field_name'])) {
693
    field_create_field($title_text_field);
694
  }
695

    
696
  // Define the title text instance.
697
  $title_text_instance = array(
698
    'bundle' => 'image',
699
    'default_value' => NULL,
700
    'deleted' => '0',
701
    'description' => $t('Title text is used in the tool tip when a user hovers their mouse over the image. Adding title text makes it easier to understand the context of an image and improves usability.'),
702
    'display' => array(
703
      'default' => array(
704
        'label' => 'above',
705
        'settings' => array(),
706
        'type' => 'hidden',
707
        'weight' => 1,
708
      ),
709
      'full' => array(
710
        'label' => 'above',
711
        'settings' => array(),
712
        'type' => 'hidden',
713
        'weight' => 0,
714
      ),
715
      'preview' => array(
716
        'label' => 'above',
717
        'settings' => array(),
718
        'type' => 'hidden',
719
        'weight' => 0,
720
      ),
721
      'teaser' => array(
722
        'label' => 'above',
723
        'settings' => array(),
724
        'type' => 'hidden',
725
        'weight' => 0,
726
      ),
727
    ),
728
    'entity_type' => 'file',
729
    'field_name' => 'field_file_image_title_text',
730
    'label' => 'Title Text',
731
    'required' => 0,
732
    'settings' => array(
733
      'text_processing' => '0',
734
      'user_register_form' => FALSE,
735
    ),
736
    'widget' => array(
737
      'active' => 1,
738
      'module' => 'text',
739
      'settings' => array(
740
        'size' => '60',
741
      ),
742
      'type' => 'text_textfield',
743
      'weight' => '-3',
744
    ),
745
  );
746

    
747
  // For sites that updated from Media 1.x, continue to provide these deprecated
748
  // view modes.
749
  // @see http://drupal.org/node/1051090
750
  if (variable_get('media__show_deprecated_view_modes')) {
751
    $title_text_instance['display'] += array(
752
      'media_link' => array(
753
        'label' => 'above',
754
        'settings' => array(),
755
        'type' => 'hidden',
756
        'weight' => 0,
757
      ),
758
      'media_original' => array(
759
        'label' => 'above',
760
        'settings' => array(),
761
        'type' => 'hidden',
762
        'weight' => 0,
763
      ),
764
    );
765
  }
766

    
767
  // As long as the title text instance doesn't already exist create it.
768
  if (!field_info_instance($title_text_instance['entity_type'], $title_text_instance['field_name'], $title_text_instance['bundle'])) {
769
    field_create_instance($title_text_instance);
770
  }
771
}
772

    
773
/**
774
 * Fix broken indexes caused by incorrect index definitions in update 7201.
775
 */
776
function file_entity_update_7205() {
777
  // Drop broken file type indexes. These may not exist if the broken version
778
  // of update 7201 was never run.
779
  if (db_index_exists('file_type_mimetypes', 0)) {
780
    db_drop_index('file_type_mimetypes', 0);
781
  }
782
  if (db_index_exists('file_type_mimetypes', 1)) {
783
    db_drop_index('file_type_mimetypes', 1);
784
  }
785

    
786
  // Add file type indexes. These may already exist if the fixed version of
787
  // update 7201 was run.
788
  if (!db_index_exists('file_type_mimetypes', 'file_type')) {
789
    db_add_index('file_type_mimetypes', 'file_type', array('type'));
790
  }
791
  if (!db_index_exists('file_type_mimetypes', 'file_type_mimetype')) {
792
    db_add_index('file_type_mimetypes', 'file_type_mimetype', array('mimetype'));
793
  }
794
}
795

    
796
/**
797
 * Configure default pathauto variables if it is currently installed.
798
 */
799
function file_entity_update_7206() {
800
  if (module_exists('pathauto')) {
801
    variable_set('pathauto_file_pattern', 'file/[file:name]');
802
  }
803
}
804

    
805
/**
806
 * Remove the administration files limit variable.
807
 */
808
function file_entity_update_7207() {
809
  variable_del('file_entity_admin_files_limit');
810
}
811

    
812
/**
813
 * Add expanded file type permissions to roles with existing file permissions.
814
 */
815
function file_entity_update_7208() {
816
  foreach (array('edit own files', 'edit any files', 'delete own files', 'delete any files', 'download own files', 'download any files') as $old_permission) {
817
    $roles = user_roles(FALSE, $old_permission);
818

    
819
    foreach ($roles as $rid => $name) {
820
      $new_permissions = array();
821

    
822
      foreach (file_type_get_enabled_types() as $type => $info) {
823
        switch ($old_permission) {
824
          case 'edit own files':
825
            $new_permissions[] = 'edit own ' . $type . ' files';
826
            break;
827

    
828
          case 'edit any files':
829
            $new_permissions[] = 'edit any ' . $type . ' files';
830
            break;
831

    
832
          case 'delete own files':
833
            $new_permissions[] = 'delete own ' . $type . ' files';
834
            break;
835

    
836
          case 'delete any files':
837
            $new_permissions[] = 'delete any ' . $type . ' files';
838
            break;
839

    
840
          case 'download own files':
841
            $new_permissions[] = 'download own ' . $type . ' files';
842
            break;
843

    
844
          case 'download any files':
845
            $new_permissions[] = 'download any ' . $type . ' files';
846
            break;
847
        }
848
      }
849

    
850
      if (!empty($new_permissions)) {
851
        // Grant new permissions for the role.
852
        foreach ($new_permissions as $name) {
853
          db_merge('role_permission')
854
            ->key(array(
855
              'rid' => $rid,
856
              'permission' => $name,
857
            ))
858
            ->fields(array(
859
              'module' => 'file_entity',
860
            ))
861
            ->execute();
862
        }
863
      }
864

    
865
      // Remove old permission from the role.
866
      db_delete('role_permission')
867
        ->condition('rid', $rid)
868
        ->condition('permission', $old_permission)
869
        ->condition('module', 'file_entity')
870
        ->execute();
871
    }
872
  }
873
}
874

    
875
/**
876
 * Remove the {file_type_streams} table if it exists.
877
 */
878
function file_entity_update_7209() {
879
  if (db_table_exists('file_type_streams')) {
880
    db_drop_table('file_type_streams');
881
  }
882
}
883

    
884
/**
885
 * Merge MIME types into the {file_type} table.
886
 */
887
function file_entity_update_7210() {
888
  // Add the new mimetypes field if it doesn't already exist.
889
  if (!db_field_exists('file_type', 'mimetypes')) {
890
    $field = array(
891
      'description' => 'Mimetypes mapped to this file type.',
892
      'type' => 'blob',
893
      'size' => 'big',
894
      'not null' => FALSE,
895
      'serialize' => TRUE,
896
    );
897

    
898
    db_add_field('file_type', 'mimetypes', $field);
899
  }
900

    
901
  // Migrate any existing MIME type information into {file_type}.
902
  if (db_table_exists('file_type_mimetypes')) {
903
    foreach (file_type_load_all(TRUE) as $type) {
904
      $mimetypes = array();
905
      $result = db_select('file_type_mimetypes', 'ftm')
906
        ->fields('ftm', array('mimetype'))
907
        ->condition('type', $type->type, '=')
908
        ->execute();
909

    
910
      foreach ($result as $record) {
911
        $mimetypes[] = $record->mimetype;
912
      }
913

    
914
      if (!empty($mimetypes)) {
915
        $type->mimetypes = $mimetypes;
916
        file_type_save($type);
917
      }
918
    }
919

    
920
    // Remove {file_type_mimetypes} after the information is migrated.
921
    db_drop_table('file_type_mimetypes');
922
  }
923
}
924

    
925
/**
926
 * Create the {file_metadata} table.
927
 */
928
function file_entity_update_7211() {
929
  $schema = array(
930
    'description' => 'Stores file metadata in a key/value store.',
931
    'fields' => array(
932
      'fid' => array(
933
        'description' => 'The {file_managed}.fid of the metadata.',
934
        'type' => 'int',
935
        'unsigned' => TRUE,
936
        'not null' => TRUE,
937
        'default' => 0,
938
      ),
939
      'name' => array(
940
        'description' => "The name of the metadata (e.g. 'width').",
941
        'type' => 'varchar',
942
        'length' => '255',
943
        'not null' => TRUE,
944
      ),
945
      'value' => array(
946
        'description' => "The value of the metadata (e.g. '200px').",
947
        'type' => 'blob',
948
        'not null' => FALSE,
949
        'size' => 'big',
950
        'serialize' => TRUE,
951
      ),
952
    ),
953
    'primary key' => array('fid', 'name'),
954
    'foreign keys' => array(
955
      'file_managed' => array(
956
        'table' => 'file_managed',
957
        'columns' => array('fid' => 'fid'),
958
      ),
959
    ),
960
  );
961
  db_create_table('file_metadata', $schema);
962
}
963

    
964
/**
965
 * Migrate the image_dimensions table to the new file_metadata table.
966
 */
967
function file_entity_update_7212(&$sandbox) {
968
  if (!db_table_exists('image_dimensions')) {
969
    return;
970
  }
971

    
972
  if (!isset($sandbox['progress'])) {
973
    $sandbox['progress'] = 0;
974
    $sandbox['current_fid'] = 0;
975
    $sandbox['max'] = db_query('SELECT COUNT(DISTINCT fid) FROM {image_dimensions}')->fetchField();
976
  }
977

    
978
  $results = db_query_range("SELECT fid, width, height FROM {image_dimensions} WHERE fid > :fid ORDER BY fid ASC", 0, 20, array(':fid' => $sandbox['current_fid']))->fetchAllAssoc('fid');
979

    
980
  // Clear any existing records in the metadata table in case they exist
981
  // because we only want to do one insert.
982
  if (!empty($results)) {
983
    db_delete('file_metadata')
984
      ->condition('fid', array_keys($results), 'IN')
985
      ->condition(db_or()
986
        ->condition('name', 'width')
987
        ->condition('name', 'height')
988
      )
989
      ->execute();
990
  }
991

    
992
  $values = array();
993
  foreach ($results as $result) {
994
    foreach (array('width', 'height') as $key) {
995
      $values[] = array(
996
        'fid' => $result->fid,
997
        'name' => $key,
998
        'value' => serialize((int) $result->{$key}),
999
      );
1000
    }
1001
    $sandbox['progress'] += count($results);
1002
    $sandbox['current_fid'] = $result->fid;
1003
  }
1004

    
1005
  if (!empty($values)) {
1006
    $query = db_insert('file_metadata');
1007
    $query->fields(array('fid', 'name', 'value'));
1008
    foreach ($values as $value) {
1009
      $query->values($value);
1010
    }
1011
    $query->execute();
1012
  }
1013

    
1014
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
1015

    
1016
  if ($sandbox['#finished'] >= 1) {
1017
    db_drop_table('image_dimensions');
1018
  }
1019
}
1020

    
1021
/**
1022
 * Update default alt text and title image field descriptions.
1023
 */
1024
function file_entity_update_7213() {
1025
  if ($title_text_instance = field_info_instance('file', 'field_file_image_title_text', 'image')) {
1026
    if ($title_text_instance['description'] == 'Title text attribute') {
1027
      $title_text_instance['description'] = t('Title text is used in the tool tip when a user hovers their mouse over the image. Adding title text makes it easier to understand the context of an image and improves usability.');
1028
      field_update_instance($title_text_instance);
1029
    }
1030
  }
1031

    
1032
  if ($alt_text_instance = field_info_instance('file', 'field_file_image_alt_text', 'image')) {
1033
    if ($alt_text_instance['description'] == '') {
1034
      $alt_text_instance['description'] = t('Alternative text is used by screen readers, search engines, and when the image cannot be loaded. By adding alt text you improve accessibility and search engine optimization.');
1035
      field_update_instance($alt_text_instance);
1036
    }
1037
  }
1038
}
1039

    
1040
/**
1041
 * Fix the default value in {file_managed}.type to match the field schema.
1042
 */
1043
function file_entity_update_7214() {
1044
  db_drop_index('file_managed', 'file_type');
1045
  db_change_field('file_managed', 'type', 'type', array(
1046
    'description' => 'The type of this file.',
1047
    'type' => 'varchar',
1048
    'length' => 50,
1049
    'not null' => TRUE,
1050
    'default' => 'undefined',
1051
  ));
1052
  db_add_index('file_managed', 'file_type', array('type'));
1053
}
1054

    
1055
/**
1056
 * Fix the {file_metadata}.fid schema.
1057
 */
1058
function file_entity_update_7215() {
1059
  // When changing a primary key serial field to an int, we need to add a
1060
  // temporary index to make this update work.
1061
  // @see https://drupal.org/node/190027
1062
  db_add_index('file_metadata', 'temp', array('fid'));
1063
  db_drop_primary_key('file_metadata');
1064
  db_change_field('file_metadata', 'fid', 'fid', array(
1065
    'description' => 'The {file_managed}.fid of the metadata.',
1066
    'type' => 'int',
1067
    'unsigned' => TRUE,
1068
    'not null' => TRUE,
1069
    'default' => 0,
1070
  ));
1071
  db_add_primary_key('file_metadata', array('fid', 'name'));
1072
  db_drop_index('file_metadata', 'temp');
1073
}