Projet

Général

Profil

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

root / drupal7 / sites / all / modules / file_entity / file_entity.install @ 66c11afc

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_default_file_directory');
243
  variable_del('file_entity_alt');
244
  variable_del('file_entity_title');
245
  variable_del('file_entity_allow_insecure_download');
246
  variable_del('file_entity_file_upload_wizard_skip_file_type');
247
  variable_del('file_entity_file_upload_wizard_skip_scheme');
248
  variable_del('file_entity_file_upload_wizard_skip_fields');
249

    
250
  // Remove any items from the file type queue if still there.
251
  DrupalQueue::get('file_entity_type_determine')->deleteQueue();
252

    
253
  cache_clear_all('entity_info', 'cache', TRUE);
254
  cache_clear_all('schema', 'cache', TRUE);
255
}
256

    
257
/**
258
 * Create the {file_display} database table.
259
 */
260
function file_entity_update_7000() {
261
  if (db_table_exists('file_display')) {
262
    return t('The table {file_display} already exists.');
263
  }
264

    
265
  $schema['file_display'] = array(
266
    'description' => 'Stores configuration options for file displays.',
267
    'fields' => array(
268
      'name' => array(
269
        '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.',
270
        'type' => 'varchar',
271
        'length' => '255',
272
        'not null' => TRUE,
273
      ),
274
      'weight' => array(
275
        'type' => 'int',
276
        'not null' => TRUE,
277
        'default' => 0,
278
        '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.',
279
      ),
280
      'status' => array(
281
        'type' => 'int',
282
        'unsigned' => TRUE,
283
        'not null' => TRUE,
284
        'default' => 0,
285
        'size' => 'tiny',
286
        'description' => 'The status of the display. (1 = enabled, 0 = disabled)',
287
      ),
288
      'settings' => array(
289
        'type' => 'blob',
290
        'not null' => FALSE,
291
        'size' => 'big',
292
        'serialize' => TRUE,
293
        'description' => 'A serialized array of name value pairs that store the formatter settings for the display.',
294
      ),
295
    ),
296
    'primary key' => array('name'),
297
  );
298
  db_create_table('file_display', $schema['file_display']);
299
}
300

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

    
336
/**
337
 * Empty update function to trigger a theme registry rebuild.
338
 */
339
function file_entity_update_7100() { }
340

    
341
/**
342
 * Update all files with empty types to use the first part of filemime.
343
 *
344
 * For example, an png image with filemime 'image/png' will be assigned a file
345
 * type of 'image'.
346
 */
347
function file_entity_update_7101() {
348
  db_update('file_managed')
349
    ->expression('type', "SUBSTRING_INDEX(filemime, '/', 1)")
350
    ->condition('type', '')
351
    ->execute();
352
}
353

    
354
/**
355
 * Empty update function to trigger an entity cache rebuild.
356
 */
357
function file_entity_update_7102() {
358
}
359

    
360
/**
361
 * Empty update function.
362
 */
363
function file_entity_update_7103() {
364
}
365

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

    
393
/**
394
 * Create the {image_dimensions} database table.
395
 */
396
function file_entity_update_7200() {
397
  if (db_table_exists('image_dimensions')) {
398
    return t('The table {image_dimensions} already exists.');
399
  }
400

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

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

    
489
  $schema = array(
490
    'description' => 'Maps mimetypes to file types.',
491
    'fields' => array(
492
      'type' => array(
493
        'description' => 'The machine name of the file type.',
494
        'type' => 'varchar',
495
        'length' => 255,
496
        'not null' => TRUE,
497
        'default' => '',
498
      ),
499
      'mimetype' => array(
500
        'description' => 'Mimetypes mapped to this file type.',
501
        'type' => 'varchar',
502
        'length' => 255,
503
        'not null' => TRUE,
504
        'default' => '',
505
      ),
506
    ),
507
    'indexes' => array(
508
      'file_type' => array('type'),
509
      'file_type_mimetype' => array('mimetype'),
510
    ),
511
  );
512
  if (!db_table_exists('file_type_mimetypes')) {
513
    db_create_table('file_type_mimetypes', $schema);
514
  }
515
}
516

    
517
/**
518
 * Update empty {file_managed}.type records to 'undefined'.
519
 *
520
 * Drupal 7.8 disallows empty string as the value for a bundle key, so update
521
 * empty {file_managed}.type records to 'undefined' instead.
522
 */
523
function file_entity_update_7202() {
524
  db_update('file_managed')
525
    // Using 'undefined' instead of FILE_TYPE_NONE, because update functions can
526
    // run for disabled modules.
527
    ->fields(array('type' => 'undefined'))
528
    ->condition('type', '')
529
    ->execute();
530
}
531

    
532
/**
533
 * Update permission names.
534
 */
535
function file_entity_update_7203() {
536
  $permissions = array(
537
    'view file' => 'view files',
538
    'edit file' => 'edit any files',
539
  );
540
  foreach ($permissions as $old => $new) {
541
    db_update('role_permission')
542
      ->fields(array('permission' => $new))
543
      ->condition('permission', $old)
544
      ->execute();
545
  }
546
}
547

    
548

    
549
/**
550
 * Add title and alt text to image file types.
551
 */
552
function file_entity_update_7204() {
553
  _file_entity_create_alt_title_fields();
554
}
555

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

    
590
  // As long as the alt text field doesn't already exist create it.
591
  if (!field_info_field($alt_text_field['field_name'])) {
592
    field_create_field($alt_text_field);
593
  }
594

    
595
  // Define the alt text instance.
596
  $alt_text_instance = array(
597
    'bundle' => 'image',
598
    'default_value' => NULL,
599
    'deleted' => '0',
600
    '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.'),
601
    'display' => array(
602
      'default' => array(
603
        'label' => 'above',
604
        'settings' => array(),
605
        'type' => 'hidden',
606
        'weight' => 0,
607
      ),
608
      'full' => array(
609
        'label' => 'above',
610
        'settings' => array(),
611
        'type' => 'hidden',
612
        'weight' => 0,
613
      ),
614
      'preview' => array(
615
        'label' => 'above',
616
        'settings' => array(),
617
        'type' => 'hidden',
618
        'weight' => 0,
619
      ),
620
      'teaser' => array(
621
        'label' => 'above',
622
        'settings' => array(),
623
        'type' => 'hidden',
624
        'weight' => 0,
625
      ),
626
    ),
627
    'entity_type' => 'file',
628
    'field_name' => 'field_file_image_alt_text',
629
    'label' => 'Alt Text',
630
    'required' => 0,
631
    'settings' => array(
632
      'text_processing' => '0',
633
      'user_register_form' => FALSE,
634
    ),
635
    'widget' => array(
636
      'active' => 1,
637
      'module' => 'text',
638
      'settings' => array(
639
        'size' => '60',
640
      ),
641
      'type' => 'text_textfield',
642
      'weight' => '-4',
643
    ),
644
  );
645

    
646
  // For sites that updated from Media 1.x, continue to provide these deprecated
647
  // view modes.
648
  // @see http://drupal.org/node/1051090
649
  if (variable_get('media__show_deprecated_view_modes')) {
650
    $alt_text_instance['display'] += array(
651
      'media_link' => array(
652
        'label' => 'above',
653
        'settings' => array(),
654
        'type' => 'hidden',
655
        'weight' => 0,
656
      ),
657
      'media_original' => array(
658
        'label' => 'above',
659
        'settings' => array(),
660
        'type' => 'hidden',
661
        'weight' => 0,
662
      ),
663
    );
664
  }
665

    
666
  // As long as the alt text instance doesn't already exist create it.
667
  if (!field_info_instance($alt_text_instance['entity_type'], $alt_text_instance['field_name'], $alt_text_instance['bundle'])) {
668
    field_create_instance($alt_text_instance);
669
  }
670

    
671
  // Create the title text field and instance.
672
  // Define the title text field.
673
  $title_text_field = array(
674
    'active' => '1',
675
    'cardinality' => '1',
676
    'deleted' => '0',
677
    'entity_types' => array(),
678
    'field_name' => 'field_file_image_title_text',
679
    'foreign keys' => array(
680
      'format' => array(
681
        'columns' => array(
682
          'format' => 'format',
683
        ),
684
        'table' => 'filter_format',
685
      ),
686
    ),
687
    'indexes' => array(
688
      'format' => array(
689
        0 => 'format',
690
      ),
691
    ),
692
    'module' => 'text',
693
    'settings' => array(
694
      'max_length' => '255',
695
    ),
696
    'translatable' => '0',
697
    'type' => 'text',
698
  );
699

    
700
  // As long as the title text field doesn't exist create it.
701
  if (!field_info_field($title_text_field['field_name'])) {
702
    field_create_field($title_text_field);
703
  }
704

    
705
  // Define the title text instance.
706
  $title_text_instance = array(
707
    'bundle' => 'image',
708
    'default_value' => NULL,
709
    'deleted' => '0',
710
    '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.'),
711
    'display' => array(
712
      'default' => array(
713
        'label' => 'above',
714
        'settings' => array(),
715
        'type' => 'hidden',
716
        'weight' => 1,
717
      ),
718
      'full' => array(
719
        'label' => 'above',
720
        'settings' => array(),
721
        'type' => 'hidden',
722
        'weight' => 0,
723
      ),
724
      'preview' => array(
725
        'label' => 'above',
726
        'settings' => array(),
727
        'type' => 'hidden',
728
        'weight' => 0,
729
      ),
730
      'teaser' => array(
731
        'label' => 'above',
732
        'settings' => array(),
733
        'type' => 'hidden',
734
        'weight' => 0,
735
      ),
736
    ),
737
    'entity_type' => 'file',
738
    'field_name' => 'field_file_image_title_text',
739
    'label' => 'Title Text',
740
    'required' => 0,
741
    'settings' => array(
742
      'text_processing' => '0',
743
      'user_register_form' => FALSE,
744
    ),
745
    'widget' => array(
746
      'active' => 1,
747
      'module' => 'text',
748
      'settings' => array(
749
        'size' => '60',
750
      ),
751
      'type' => 'text_textfield',
752
      'weight' => '-3',
753
    ),
754
  );
755

    
756
  // For sites that updated from Media 1.x, continue to provide these deprecated
757
  // view modes.
758
  // @see http://drupal.org/node/1051090
759
  if (variable_get('media__show_deprecated_view_modes')) {
760
    $title_text_instance['display'] += array(
761
      'media_link' => array(
762
        'label' => 'above',
763
        'settings' => array(),
764
        'type' => 'hidden',
765
        'weight' => 0,
766
      ),
767
      'media_original' => array(
768
        'label' => 'above',
769
        'settings' => array(),
770
        'type' => 'hidden',
771
        'weight' => 0,
772
      ),
773
    );
774
  }
775

    
776
  // As long as the title text instance doesn't already exist create it.
777
  if (!field_info_instance($title_text_instance['entity_type'], $title_text_instance['field_name'], $title_text_instance['bundle'])) {
778
    field_create_instance($title_text_instance);
779
  }
780
}
781

    
782
/**
783
 * Fix broken indexes caused by incorrect index definitions in update 7201.
784
 */
785
function file_entity_update_7205() {
786
  // Drop broken file type indexes. These may not exist if the broken version
787
  // of update 7201 was never run.
788
  if (db_index_exists('file_type_mimetypes', 0)) {
789
    db_drop_index('file_type_mimetypes', 0);
790
  }
791
  if (db_index_exists('file_type_mimetypes', 1)) {
792
    db_drop_index('file_type_mimetypes', 1);
793
  }
794

    
795
  // Add file type indexes. These may already exist if the fixed version of
796
  // update 7201 was run.
797
  if (!db_index_exists('file_type_mimetypes', 'file_type')) {
798
    db_add_index('file_type_mimetypes', 'file_type', array('type'));
799
  }
800
  if (!db_index_exists('file_type_mimetypes', 'file_type_mimetype')) {
801
    db_add_index('file_type_mimetypes', 'file_type_mimetype', array('mimetype'));
802
  }
803
}
804

    
805
/**
806
 * Configure default pathauto variables if it is currently installed.
807
 */
808
function file_entity_update_7206() {
809
  if (module_exists('pathauto')) {
810
    variable_set('pathauto_file_pattern', 'file/[file:name]');
811
  }
812
}
813

    
814
/**
815
 * Remove the administration files limit variable.
816
 */
817
function file_entity_update_7207() {
818
  variable_del('file_entity_admin_files_limit');
819
}
820

    
821
/**
822
 * Add expanded file type permissions to roles with existing file permissions.
823
 */
824
function file_entity_update_7208() {
825
  foreach (array('edit own files', 'edit any files', 'delete own files', 'delete any files', 'download own files', 'download any files') as $old_permission) {
826
    $roles = user_roles(FALSE, $old_permission);
827

    
828
    foreach ($roles as $rid => $name) {
829
      $new_permissions = array();
830

    
831
      foreach (file_type_get_enabled_types() as $type => $info) {
832
        switch ($old_permission) {
833
          case 'edit own files':
834
            $new_permissions[] = 'edit own ' . $type . ' files';
835
            break;
836

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

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

    
845
          case 'delete any files':
846
            $new_permissions[] = 'delete any ' . $type . ' files';
847
            break;
848

    
849
          case 'download own files':
850
            $new_permissions[] = 'download own ' . $type . ' files';
851
            break;
852

    
853
          case 'download any files':
854
            $new_permissions[] = 'download any ' . $type . ' files';
855
            break;
856
        }
857
      }
858

    
859
      if (!empty($new_permissions)) {
860
        // Grant new permissions for the role.
861
        foreach ($new_permissions as $name) {
862
          db_merge('role_permission')
863
            ->key(array(
864
              'rid' => $rid,
865
              'permission' => $name,
866
            ))
867
            ->fields(array(
868
              'module' => 'file_entity',
869
            ))
870
            ->execute();
871
        }
872
      }
873

    
874
      // Remove old permission from the role.
875
      db_delete('role_permission')
876
        ->condition('rid', $rid)
877
        ->condition('permission', $old_permission)
878
        ->condition('module', 'file_entity')
879
        ->execute();
880
    }
881
  }
882
}
883

    
884
/**
885
 * Remove the {file_type_streams} table if it exists.
886
 */
887
function file_entity_update_7209() {
888
  if (db_table_exists('file_type_streams')) {
889
    db_drop_table('file_type_streams');
890
  }
891
}
892

    
893
/**
894
 * Merge MIME types into the {file_type} table.
895
 */
896
function file_entity_update_7210() {
897
  // Add the new mimetypes field if it doesn't already exist.
898
  if (!db_field_exists('file_type', 'mimetypes')) {
899
    $field = array(
900
      'description' => 'Mimetypes mapped to this file type.',
901
      'type' => 'blob',
902
      'size' => 'big',
903
      'not null' => FALSE,
904
      'serialize' => TRUE,
905
    );
906

    
907
    db_add_field('file_type', 'mimetypes', $field);
908
  }
909

    
910
  // Migrate any existing MIME type information into {file_type}.
911
  if (db_table_exists('file_type_mimetypes')) {
912
    module_load_include('inc', 'file_entity', 'file_entity.file_api');
913
    $types = file_type_load_all(TRUE);
914
    foreach ($types as $type) {
915
      $mimetypes = db_select('file_type_mimetypes', 'ftm')
916
        ->fields('ftm', array('mimetype'))
917
        ->condition('type', $type->type)
918
        ->execute()->fetchCol();
919

    
920
      if (!empty($mimetypes)) {
921
        $type->mimetypes = $mimetypes;
922
        file_type_save($type);
923
      }
924
    }
925

    
926
    // Remove {file_type_mimetypes} after the information is migrated.
927
    db_drop_table('file_type_mimetypes');
928
  }
929
}
930

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

    
970
/**
971
 * Migrate the image_dimensions table to the new file_metadata table.
972
 */
973
function file_entity_update_7212(&$sandbox) {
974
  if (!db_table_exists('image_dimensions')) {
975
    return;
976
  }
977

    
978
  if (!isset($sandbox['progress'])) {
979
    $sandbox['progress'] = 0;
980
    $sandbox['current_fid'] = 0;
981
    $sandbox['max'] = db_query('SELECT COUNT(DISTINCT fid) FROM {image_dimensions}')->fetchField();
982
  }
983

    
984
  $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');
985

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

    
998
  $values = array();
999
  foreach ($results as $result) {
1000
    foreach (array('width', 'height') as $key) {
1001
      $values[] = array(
1002
        'fid' => $result->fid,
1003
        'name' => $key,
1004
        'value' => serialize((int) $result->{$key}),
1005
      );
1006
    }
1007
    $sandbox['progress'] += count($results);
1008
    $sandbox['current_fid'] = $result->fid;
1009
  }
1010

    
1011
  if (!empty($values)) {
1012
    $query = db_insert('file_metadata');
1013
    $query->fields(array('fid', 'name', 'value'));
1014
    foreach ($values as $value) {
1015
      $query->values($value);
1016
    }
1017
    $query->execute();
1018
  }
1019

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

    
1022
  if ($sandbox['#finished'] >= 1) {
1023
    db_drop_table('image_dimensions');
1024
  }
1025
}
1026

    
1027
/**
1028
 * Update default alt text and title image field descriptions.
1029
 */
1030
function file_entity_update_7213() {
1031
  if ($title_text_instance = field_info_instance('file', 'field_file_image_title_text', 'image')) {
1032
    if ($title_text_instance['description'] == 'Title text attribute') {
1033
      $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.');
1034
      field_update_instance($title_text_instance);
1035
    }
1036
  }
1037

    
1038
  if ($alt_text_instance = field_info_instance('file', 'field_file_image_alt_text', 'image')) {
1039
    if ($alt_text_instance['description'] == '') {
1040
      $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.');
1041
      field_update_instance($alt_text_instance);
1042
    }
1043
  }
1044
}
1045

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

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

    
1081
/**
1082
 * This update has been removed and will not run.
1083
 */
1084
function file_entity_update_7216() {
1085
  // This update function previously saved default file displays into the
1086
  // database. It has been removed due to reported problems and is better
1087
  // addressed by adding support for ctools default content to features.
1088
}