Projet

Général

Profil

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

root / drupal7 / sites / all / modules / file_entity / file_entity.install @ 9e88ab34

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

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

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

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

    
333
/**
334
 * Empty update function to trigger a theme registry rebuild.
335
 */
336
function file_entity_update_7100() { }
337

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

    
351
/**
352
 * Empty update function to trigger an entity cache rebuild.
353
 */
354
function file_entity_update_7102() {
355
}
356

    
357
/**
358
 * Empty update function.
359
 */
360
function file_entity_update_7103() {
361
}
362

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

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

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

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

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

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

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

    
545

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
811
/**
812
 * Remove the administration files limit variable.
813
 */
814
function file_entity_update_7207() {
815
  variable_del('file_entity_admin_files_limit');
816
}
817

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

    
825
    foreach ($roles as $rid => $name) {
826
      $new_permissions = array();
827

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

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

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

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

    
846
          case 'download own files':
847
            $new_permissions[] = 'download own ' . $type . ' files';
848
            break;
849

    
850
          case 'download any files':
851
            $new_permissions[] = 'download any ' . $type . ' files';
852
            break;
853
        }
854
      }
855

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

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

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

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

    
904
    db_add_field('file_type', 'mimetypes', $field);
905
  }
906

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

    
917
      if (!empty($mimetypes)) {
918
        $type->mimetypes = $mimetypes;
919
        file_type_save($type);
920
      }
921
    }
922

    
923
    // Remove {file_type_mimetypes} after the information is migrated.
924
    db_drop_table('file_type_mimetypes');
925
  }
926
}
927

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

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

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

    
981
  $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');
982

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

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

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

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

    
1019
  if ($sandbox['#finished'] >= 1) {
1020
    db_drop_table('image_dimensions');
1021
  }
1022
}
1023

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

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

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

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

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