Projet

Général

Profil

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

root / drupal7 / sites / all / modules / file_entity / file_entity.install @ 8e7483ab

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
 * Implements hook_install().
172
 */
173
function file_entity_install() {
174
  $schema = array();
175
  file_entity_schema_alter($schema);
176
  $spec = $schema['file_managed']['fields']['type'];
177
  $indexes_new = array('indexes' => $schema['file_managed']['indexes']);
178

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

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

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

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

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

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

    
219
  // Warn users that existing files will not have a file type until the queue
220
  // has been processed.
221
  if ($queue->numberOfItems()) {
222
    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.'));
223
  }
224
}
225

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

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

    
238
  // Remove variables.
239
  variable_del('file_entity_max_filesize');
240
  variable_del('file_entity_default_allowed_extensions');
241
  variable_del('file_entity_default_file_directory');
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
  variable_del('file_entity_protect_repeated_render');
249

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

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

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

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

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

    
340
/**
341
 * Empty update function to trigger a theme registry rebuild.
342
 */
343
function file_entity_update_7100() {
344
}
345

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

    
359
/**
360
 * Empty update function to trigger an entity cache rebuild.
361
 */
362
function file_entity_update_7102() {
363
}
364

    
365
/**
366
 * Empty update function.
367
 */
368
function file_entity_update_7103() {
369
}
370

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

    
398
/**
399
 * Create the {image_dimensions} database table.
400
 */
401
function file_entity_update_7200() {
402
  if (db_table_exists('image_dimensions')) {
403
    return t('The table {image_dimensions} already exists.');
404
  }
405

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

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

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

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

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

    
553
/**
554
 * Add title and alt text to image file types.
555
 */
556
function file_entity_update_7204() {
557
  _file_entity_create_alt_title_fields();
558
}
559

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

    
594
  // As long as the alt text field doesn't already exist create it.
595
  if (!field_info_field($alt_text_field['field_name'])) {
596
    field_create_field($alt_text_field);
597
  }
598

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

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

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

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

    
704
  // As long as the title text field doesn't exist create it.
705
  if (!field_info_field($title_text_field['field_name'])) {
706
    field_create_field($title_text_field);
707
  }
708

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

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

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

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

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

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

    
818
/**
819
 * Remove the administration files limit variable.
820
 */
821
function file_entity_update_7207() {
822
  variable_del('file_entity_admin_files_limit');
823
}
824

    
825
/**
826
 * Add expanded file type permissions to roles with existing file permissions.
827
 */
828
function file_entity_update_7208() {
829
  foreach (array(
830
    'edit own files', 'edit any files',
831
    'delete own files', 'delete any files',
832
    'download own files', 'download any files',
833
  ) as $old_permission) {
834
    $roles = user_roles(FALSE, $old_permission);
835

    
836
    foreach ($roles as $rid => $name) {
837
      $new_permissions = array();
838

    
839
      foreach (file_type_get_enabled_types() as $type => $info) {
840
        switch ($old_permission) {
841
          case 'edit own files':
842
            $new_permissions[] = 'edit own ' . $type . ' files';
843
            break;
844

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

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

    
853
          case 'delete any files':
854
            $new_permissions[] = 'delete any ' . $type . ' files';
855
            break;
856

    
857
          case 'download own files':
858
            $new_permissions[] = 'download own ' . $type . ' files';
859
            break;
860

    
861
          case 'download any files':
862
            $new_permissions[] = 'download any ' . $type . ' files';
863
            break;
864
        }
865
      }
866

    
867
      if (!empty($new_permissions)) {
868
        // Grant new permissions for the role.
869
        foreach ($new_permissions as $name) {
870
          db_merge('role_permission')
871
            ->key(array(
872
              'rid' => $rid,
873
              'permission' => $name,
874
            ))
875
            ->fields(array(
876
              'module' => 'file_entity',
877
            ))
878
            ->execute();
879
        }
880
      }
881

    
882
      // Remove old permission from the role.
883
      db_delete('role_permission')
884
        ->condition('rid', $rid)
885
        ->condition('permission', $old_permission)
886
        ->condition('module', 'file_entity')
887
        ->execute();
888
    }
889
  }
890
}
891

    
892
/**
893
 * Remove the {file_type_streams} table if it exists.
894
 */
895
function file_entity_update_7209() {
896
  if (db_table_exists('file_type_streams')) {
897
    db_drop_table('file_type_streams');
898
  }
899
}
900

    
901
/**
902
 * Merge MIME types into the {file_type} table.
903
 */
904
function file_entity_update_7210() {
905
  // Add the new mimetypes field if it doesn't already exist.
906
  if (!db_field_exists('file_type', 'mimetypes')) {
907
    $field = array(
908
      'description' => 'Mimetypes mapped to this file type.',
909
      'type' => 'blob',
910
      'size' => 'big',
911
      'not null' => FALSE,
912
      'serialize' => TRUE,
913
    );
914

    
915
    db_add_field('file_type', 'mimetypes', $field);
916
  }
917

    
918
  // Migrate any existing MIME type information into {file_type}.
919
  if (db_table_exists('file_type_mimetypes')) {
920
    module_load_include('inc', 'file_entity', 'file_entity.file_api');
921
    $types = file_type_load_all(TRUE);
922
    foreach ($types as $type) {
923
      $mimetypes = db_select('file_type_mimetypes', 'ftm')
924
        ->fields('ftm', array('mimetype'))
925
        ->condition('type', $type->type)
926
        ->execute()->fetchCol();
927

    
928
      if (!empty($mimetypes)) {
929
        $type->mimetypes = $mimetypes;
930
        file_type_save($type);
931
      }
932
    }
933

    
934
    // Remove {file_type_mimetypes} after the information is migrated.
935
    db_drop_table('file_type_mimetypes');
936
  }
937
}
938

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

    
978
/**
979
 * Migrate the image_dimensions table to the new file_metadata table.
980
 */
981
function file_entity_update_7212(&$sandbox) {
982
  if (!db_table_exists('image_dimensions')) {
983
    return;
984
  }
985

    
986
  if (!isset($sandbox['progress'])) {
987
    $sandbox['progress'] = 0;
988
    $sandbox['current_fid'] = 0;
989
    $sandbox['max'] = db_query('SELECT COUNT(DISTINCT fid) FROM {image_dimensions}')->fetchField();
990
  }
991

    
992
  $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');
993

    
994
  // Clear any existing records in the metadata table in case they exist
995
  // because we only want to do one insert.
996
  if (!empty($results)) {
997
    db_delete('file_metadata')
998
      ->condition('fid', array_keys($results), 'IN')
999
      ->condition(db_or()
1000
        ->condition('name', 'width')
1001
        ->condition('name', 'height')
1002
      )
1003
      ->execute();
1004
  }
1005

    
1006
  $values = array();
1007
  foreach ($results as $result) {
1008
    foreach (array('width', 'height') as $key) {
1009
      $values[] = array(
1010
        'fid' => $result->fid,
1011
        'name' => $key,
1012
        'value' => serialize((int) $result->{$key}),
1013
      );
1014
    }
1015
    $sandbox['current_fid'] = $result->fid;
1016
  }
1017

    
1018
  $sandbox['progress'] += count($results);
1019

    
1020
  if (!empty($values)) {
1021
    $query = db_insert('file_metadata');
1022
    $query->fields(array('fid', 'name', 'value'));
1023
    foreach ($values as $value) {
1024
      $query->values($value);
1025
    }
1026
    $query->execute();
1027
  }
1028

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

    
1031
  if ($sandbox['#finished'] >= 1) {
1032
    db_drop_table('image_dimensions');
1033
  }
1034
}
1035

    
1036
/**
1037
 * Update default alt text and title image field descriptions.
1038
 */
1039
function file_entity_update_7213() {
1040
  if ($title_text_instance = field_info_instance('file', 'field_file_image_title_text', 'image')) {
1041
    if ($title_text_instance['description'] == 'Title text attribute') {
1042
      $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.');
1043
      field_update_instance($title_text_instance);
1044
    }
1045
  }
1046

    
1047
  if ($alt_text_instance = field_info_instance('file', 'field_file_image_alt_text', 'image')) {
1048
    if ($alt_text_instance['description'] == '') {
1049
      $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.');
1050
      field_update_instance($alt_text_instance);
1051
    }
1052
  }
1053
}
1054

    
1055
/**
1056
 * Fix the default value in {file_managed}.type to match the field schema.
1057
 */
1058
function file_entity_update_7214() {
1059
  db_drop_index('file_managed', 'file_type');
1060
  db_change_field('file_managed', 'type', 'type', array(
1061
    'description' => 'The type of this file.',
1062
    'type' => 'varchar',
1063
    'length' => 50,
1064
    'not null' => TRUE,
1065
    'default' => 'undefined',
1066
  ));
1067
  db_add_index('file_managed', 'file_type', array('type'));
1068
}
1069

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

    
1090
/**
1091
 * This update has been removed and will not run.
1092
 */
1093
function file_entity_update_7216() {
1094
  // This update function previously saved default file displays into the
1095
  // database. It has been removed due to reported problems and is better
1096
  // addressed by adding support for ctools default content to features.
1097
}
1098

    
1099
/**
1100
 * Remove deprecated views_plugin_access_menu plugin.
1101
 */
1102
function file_entity_update_7227() {
1103
  $result = db_query("SELECT vid, id, display_options FROM {views_display}");
1104
  foreach ($result as $record) {
1105
    $options = unserialize($record->display_options);
1106
    if (isset($options['access']) && is_array($options['access']) && isset($options['access']['type']) && $options['access']['type'] === 'menu') {
1107
      $options['access'] = array(
1108
        'type' => 'perm',
1109
        'perm' => 'access administration pages',
1110
      );
1111

    
1112
      db_query("UPDATE {views_display} SET display_options = :options WHERE vid = :vid AND id = :id", array(
1113
        'vid' => $record->vid,
1114
        'id' => $record->id,
1115
        'options' => serialize($options),
1116
      ));
1117
    }
1118
  }
1119
}
1120