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 @ 59ae487e

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
  // Remove any items from the file type queue if still there.
250
  DrupalQueue::get('file_entity_type_determine')->deleteQueue();
251
}
252

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
544

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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