Projet

Général

Profil

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

root / drupal7 / sites / all / modules / file_entity / file_entity.install @ 2b3c8cc1

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
          ->condition('rid', $rid)
379
          ->condition('permission', 'view file')
380
          ->execute();
381
      }
382
    }
383
  }
384
}
385

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

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

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

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

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

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

    
541

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
821
    foreach ($roles as $rid => $name) {
822
      $new_permissions = array();
823

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

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

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

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

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

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

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

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

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

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

    
900
    db_add_field('file_type', 'mimetypes', $field);
901
  }
902

    
903
  // Migrate any existing MIME type information into {file_type}.
904
  if (db_table_exists('file_type_mimetypes')) {
905
    module_load_include('inc', 'file_entity', 'file_entity.file_api');
906
    $types = file_type_load_all(TRUE);
907
    foreach ($types as $type) {
908
      $mimetypes = db_select('file_type_mimetypes', 'ftm')
909
        ->fields('ftm', array('mimetype'))
910
        ->condition('type', $type->type)
911
        ->execute()->fetchCol();
912

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

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

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

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

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

    
977
  $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');
978

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

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

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

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

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

    
1020
/**
1021
 * Update default alt text and title image field descriptions.
1022
 */
1023
function file_entity_update_7213() {
1024
  if ($title_text_instance = field_info_instance('file', 'field_file_image_title_text', 'image')) {
1025
    if ($title_text_instance['description'] == 'Title text attribute') {
1026
      $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.');
1027
      field_update_instance($title_text_instance);
1028
    }
1029
  }
1030

    
1031
  if ($alt_text_instance = field_info_instance('file', 'field_file_image_alt_text', 'image')) {
1032
    if ($alt_text_instance['description'] == '') {
1033
      $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.');
1034
      field_update_instance($alt_text_instance);
1035
    }
1036
  }
1037
}
1038

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

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

    
1074
/**
1075
 * This update has been removed and will not run.
1076
 */
1077
function file_entity_update_7216() {
1078
  // This update function previously saved default file displays into the
1079
  // database. It has been removed due to reported problems and is better
1080
  // addressed by adding support for ctools default content to features.
1081
}