media.install 22.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
<?php

/**
 * @file
 * Install, update and uninstall functions for the Media module.
 */

define('MEDIA_UPDATE_RECORDS_ON_INSTALL', 200);

/**
 * Implements hook_schema().
 */
function media_schema() {
  $schema['media_type'] = array(
    'description' => 'Stores the settings for media types.',
    'fields' => array(
      'name' => array(
        'description' => 'The machine name of the media type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'label' => array(
        'description' => 'The label of the media type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'base' => array(
        'description' => 'If this is a base type (i.e. cannot be deleted)',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
      ),
      'weight' => array(
        'description' => 'Weight of media type. Determines which one wins when claiming a piece of media (first wins)',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'normal',
      ),
      'type_callback' => array(
        'description' => 'Callback to determine if provided media is of this type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'default' => '',
      ),
      'type_callback_args' => array(
        'type' => 'text',
        'not null' => FALSE,
        'size' => 'big',
        'serialize' => TRUE,
        'description' => 'A serialized array of name value pairs that will be passed to the callback function',
      ),
    ),
    'primary key' => array('name'),
  );

  $schema['media_list_type'] = array(
    'description' => 'Stores the user preference for whether to list as table or images.',
    'fields' => array(
      'uid' => array(
        'description' => 'The {user}.uid of the user.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'type' => array(
        'description' => 'The type of display (table or images).',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'primary key' => array('uid'),
  );

  $schema['media_filter_usage'] = array(
    'description' => 'Stores fids that have been included in the media tag in formatted textareas.',
    'fields' => array(
      'fid' => array(
        'description' => 'The media {file_managed}.fid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'timestamp' => array(
        'description' => 'The timestamp the fid was last recorded by media_filter()',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'foreign keys' => array(
      'file_managed' => array(
        'table' => 'file_managed',
        'columns' => array('fid' => 'fid'),
      ),
    ),
    'primary key' => array('fid'),
    'indexes' => array(
      'timestamp' => array('timestamp'),
    ),
  );

  $schema['cache_media_xml'] = drupal_get_schema_unprocessed('system', 'cache');
  $schema['cache_media_xml']['description'] = 'Cache table for the the results of retreived XML documents for remote streams.';

  return $schema;
}

/**
 * Implements hook_field_schema().
 */
function media_field_schema($field) {
  return array(
    'columns' => array(
      'fid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
      ),
      'title' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
      'data' => array(
        'type' => 'text',
        'not null' => FALSE,
        'size' => 'big',
        'serialize' => TRUE,
        //'description' => 'Used for storing additional information.  Can be harnessed by widgets',
      ),
    ),
    'indexes' => array(
      'fid' => array('fid'),
    ),
    'foreign keys' => array(
      'file_managed' => array(
        'table' => 'file_managed',
        'columns' => array('fid' => 'fid'),
      ),
    ),
  );
}

/**
 * Implements hook_install().
 */
function media_install() {
  // @todo We may need to disable the media bundle & field in hook_disable.

  // Define the default type to be used if no other type is found. Give it a
  // high weight to ensure it runs last.
  $types['default'] = new stdClass();
  $types['default']->name = 'default';
  $types['default']->label = "Other";
  $types['default']->base = TRUE;
  $types['default']->weight = 1000;
  $types['default']->type_callback_args = array(
    'match_type' => 'any',
    'mimetypes' => array('/.*/'),
  );

  // Define the common media types: image, audio, and video.
  $types['image'] = new stdClass();
  $types['image']->name = 'image';
  $types['image']->label = "Image";
  $types['image']->base = TRUE;
  $types['image']->type_callback_args = array(
    'match_type' => 'all',
    'mimetypes' => array('/^image/'),
    'extensions' => array('jpg', 'jpeg', 'gif', 'png', 'tiff'),
    'streams' => array('public', 'private'),
  );

  $types['audio'] = new stdClass();
  $types['audio']->name = 'audio';
  $types['audio']->label = "Audio";
  $types['audio']->base = TRUE;
  $types['audio']->type_callback_args = array(
    'match_type' => 'all',
    'mimetypes' => array('/^audio/'),
    'extensions' => array('mp3', 'ogg', 'wma'),
    'streams' => array('public', 'private'),
  );

  $types['video'] = new stdClass();
  $types['video']->name = 'video';
  $types['video']->label = "Video";
  $types['video']->base = TRUE;
  $types['video']->type_callback_args = array(
    'match_type' => 'all',
    'mimetypes' => array('/^video/'),
    'extensions' => array('mov', 'mp4', 'avi'),
    'streams' => array('public', 'private'),
  );

  // Create the defined types.
  foreach ($types as $name => $type) {
    media_type_save($type);

    // @todo By default, we hide the file display in the 'small' view mode for
    //   legacy reasons. Does it still make sense to do so? See
    //   http://drupal.org/node/1051090.
    $bundle_settings = field_bundle_settings('file', $name);
    $bundle_settings['extra_fields']['display']['file']['media_small'] = array('weight' => 0, 'visible' => FALSE);
    field_bundle_settings('file', $name, $bundle_settings);
  }

  // Set permissions.
  $roles = user_roles();
  foreach ($roles as $rid => $role) {
    user_role_grant_permissions($rid, array('view media'));
  }

  // Updates the type field for the first MEDIA_UPDATE_RECORDS_ON_INSTALL files.
  $invalid_files = media_type_invalid_files_count();
  if ($invalid_files <= MEDIA_UPDATE_RECORDS_ON_INSTALL) {
    media_type_batch_update(FALSE, MEDIA_UPDATE_RECORDS_ON_INSTALL);
  }

  $invalid_files = media_type_invalid_files_count();
  if ($invalid_files > 0) {
    // Not all files could be converted. Display a persistant nag message on
    // every page for the administrator, urging them to finish the process.
    media_variable_set('show_file_type_rebuild_nag', TRUE);
  }
}

/**
 * Implements hook_uninstall().
 */
function media_uninstall() {
  drupal_load('module', 'media');
  foreach (media_variable_default() as $name => $value) {
    media_variable_del($name);
  }
}

/**
 * Implements hook_update_dependencies().
 */
function media_update_dependencies() {
  // This update function requires field_update_7002() to run before it since
  // the field_bundle_settings variable has been split into separate variables
  // per entity type and bundle.
  $dependencies['mymodule'][7016] = array(
    'field' => 7002,
  );

  return $dependencies;
}

/**
 * Create the media_list_type table.
 */
function media_update_7000() {
  $schema['media_list_type'] = array(
    'description' => 'Stores the user preference for whether to list as table or images.',
    'fields' => array(
      'uid' => array(
        'description' => 'The {user}.uid of the user.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'type' => array(
        'description' => 'The type of display (table or images).',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'primary key' => array('uid'),
  );
  db_create_table('media_list_type', $schema['media_list_type']);
}

/**
 * Create the cache_media_xml table.
 */
function media_update_7001() {
  $schema['cache_media_xml'] = drupal_get_schema_unprocessed('system', 'cache');
  $schema['cache_media_xml']['description'] = 'Cache table for the the results of retreived XML documents for remote streams.';
  db_create_table('cache_media_xml', $schema['cache_media_xml']);
}

/**
 * Create the media_type table from the media_types variable.
 */
function media_update_7002() {
  $schema['media_type'] = array(
    'description' => 'Stores the settings for media types.',
    'fields' => array(
      'name' => array(
        'description' => 'The machine name of the media type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'label' => array(
        'description' => 'The label of the media type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'base' => array(
        'description' => 'If this is a base type (i.e. cannot be deleted)',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
      ),
      'weight' => array(
        'description' => 'Weight of media type. Determines which one wins when claiming a piece of media (first wins)',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'normal',
      ),
      'type_callback' => array(
        'description' => 'Callback to determine if provided media is of this type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'default' => '',
      ),
      'type_callback_args' => array(
        'type' => 'text',
        'not null' => FALSE,
        'size' => 'big',
        'serialize' => TRUE,
        'description' => 'A serialized array of name value pairs that will be passed to the callback function',
      ),
    ),
    'primary key' => array('name'),
  );

  db_create_table('media_type', $scheme['media_type']);
  drupal_load('module', 'media');
  $old_types = variable_get('media_types');
  foreach ($old_types as $type) {
    // Was an error in the original creation
    if (isset($type->callbacks)) {
      unset($type->callbacks);
    }
    $type->name = $type->machine_name;
    unset($type->machine_name);
    media_type_save($type);
  }
  variable_del('media_types');
}

/**
 * We now prefix media namespaced variables with media__, so fix old variables.
 */
function media_update_7003() {
  drupal_load('module', 'media');
  foreach (media_variable_default() as $variable => $value) {
    if (($test = variable_get('media_' . $variable, TRUE)) == variable_get('media_' . $variable, FALSE)) {
      media_variable_set($variable, $test);
      variable_del('media_' . $variable);
    }
  }
}

/**
 * Removed /media from the menu.
 */
function media_update_7004() {
  // Do nothing. Running an update function will trigger a menu rebuild.
}

/**
 * Deprecated update function.
 */
function media_update_7005() {
}

/**
 * Rename the file table to file_managed in case head2head was used.
 */
function media_update_7006() {
  if (db_table_exists('file') && !db_table_exists('file_managed')) {
    db_rename_table('file', 'file_managed');
  }
}

/**
 * Changes the preview formatter for non-image types to the icon view.
 */
function media_update_7007() {
  // @todo media_type_configure_formatters() is a deprecated function, so remove
  //   this code entirely?
  drupal_load('module', 'media');
  drupal_load('module', 'field');

  foreach (media_type_get_types() as $type => $info) {
    if ($type != 'image') {
      media_type_configure_formatters($type, array('media_preview' => 'media_large_icon'));
    }
  }
}

/**
 * Give all users view media perm by default
 */
function media_update_7008() {
  $roles = user_roles();
  foreach ($roles as $rid => $role) {
    user_role_grant_permissions($rid, array('view media'));
  }
}

/**
 * Changes the preview formatter for video types to a square thumbnail, like for images.
 */
function media_update_7009() {
  // @todo media_type_configure_formatters() is a deprecated function, so remove
  //   this code entirely?
  drupal_load('module', 'media');
  drupal_load('module', 'field');
  media_type_configure_formatters('video', array('media_preview' => 'styles_file_square_thumbnail'));
}

/**
 * Changes the large formatter for video types to the large file style.
 */
function media_update_7010() {
  // @todo media_type_configure_formatters() is a deprecated function, so remove
  //   this code entirely?
  // This formatter association was added to media_enable() at one point, but
  // without a corresponding update function, so here's that update function.
  drupal_load('module', 'media');
  drupal_load('module', 'field');
  media_type_configure_formatters('video', array('media_large' => 'styles_file_large'));
}

/**
 * Empty update function.
 */
function media_update_7011() {
}

/**
 * Create the media_filter_usage table.
 */
function media_update_7012() {
  $schema['media_filter_usage'] = array(
    'description' => 'Stores fids that have been included in the media tag in formatted textareas.',
    'fields' => array(
      'fid' => array(
        'description' => 'The media {file_managed}.fid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'timestamp' => array(
        'description' => 'The timestamp the fid was last recorded by media_filter()',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'foreign keys' => array(
      'file_managed' => array(
        'table' => 'file_managed',
        'columns' => array('fid' => 'fid'),
      ),
    ),
    'primary key' => array('fid'),
    'indexes' => array(
      'timestamp' => array('timestamp'),
    ),
  );
  db_create_table('media_filter_usage', $schema['media_filter_usage']);
}

/**
 * Work around a core bug where text format cacheability is not updated.
 *
 * @see http://drupal.org/node/993230
 */
function media_update_7013() {
  $formats = filter_formats();
  foreach ($formats as $format) {
    $format->filters = filter_list_format($format->format);
    // filter_format_save() expects filters to be an array, however
    // filter_list_format() gives us objects.
    foreach ($format->filters as $key => $value) {
      $format->filters[$key] = (array) $value;
    }
    filter_format_save($format);
  }
}

/**
 * Rename the media__dialog_get_theme_name variable to media__dialog_theme.
 */
function media_update_7014() {
  if ($old_value = variable_get('media__dialog_get_theme_name')) {
    variable_del('media__dialog_get_theme_name');
    variable_set('media__dialog_theme', $old_value);
  }
}

/**
 * Empty update function to trigger a registry rebuild.
 */
function media_update_7015() {
}

/**
 * Convert Media entities to File entities.
 *
 * This update function requires field_update_7002() to run before it since
 * the field_bundle_settings variable has been split into separate variables
 * per entity type and bundle.
 *
 * @see http://drupal.org/node/1418708
 * @see http://drupal.org/node/1211008
 */
function media_update_7016() {
  // Allow File Entity module to take over the {file_managed}.type field. It
  // will create new indexes as it needs to, but it doesn't know about old ones,
  // so delete them.
  if (db_index_exists('file_managed', 'file_type')) {
    db_drop_index('file_managed', 'file_type');
  }
  module_enable(array('file_entity'));

  // Move all field instances from Media entity to File entity.
  $instances = field_read_instances(array('entity_type' => 'media'), array('include_inactive' => TRUE, 'include_deleted' => TRUE));
  foreach ($instances as $instance) {
    // Skip the old self-referencing file field. It will be deleted later in
    // this function.
    if ($instance['field_name'] === 'file') {
      continue;
    }

    // @todo Convert this to use _update_7000_field_read_fields()
    $fields = field_read_fields(array('id' => $instance['field_id']), array('include_inactive' => TRUE, 'include_deleted' => TRUE));
    $field = $fields[$instance['field_id']];

    // There is no API for updating the entity_type foreign key within field
    // data storage. We can do a direct db_update() for when the default SQL
    // storage back-end is being used, but must skip updating fields that use a
    // different storage type.
    // @todo What else should be added here (e.g., logging and/or a message to
    //   the administrator)?
    if ($field['storage']['type'] !== 'field_sql_storage' || !module_exists('field_sql_storage') || !$field['storage']['active']) {
      $messages[] = t('Cannot update field %id (%field_name) because it does not use the field_sql_storage storage type.', array(
        '%id' => $field['id'],
        '%field_name' => $field['field_name'],
      ));
      continue;
    }

    // Update the data tables.
    $table_name = _field_sql_storage_tablename($field);
    $revision_name = _field_sql_storage_revision_tablename($field);
    db_update($table_name)
      ->fields(array('entity_type' => 'file'))
      ->condition('entity_type', 'media')
      ->condition('bundle', $instance['bundle'])
      ->execute();
    db_update($revision_name)
      ->fields(array('entity_type' => 'file'))
      ->condition('entity_type', 'media')
      ->condition('bundle', $instance['bundle'])
      ->execute();

    // Once all the data has been updated, update the {field_config_instance}
    // record.
    db_update('field_config_instance')
      ->fields(array('entity_type' => 'file'))
      ->condition('id', $instance['id'])
      ->execute();
  }

  // Update the field_bundle_settings configuration variable: move media bundle
  // settings to file bundles, and move settings of the old self-referencing
  // file field to the new file pseudo-field.
  foreach ($instances as $instance) {
    if ($instance['field_name'] === 'file' && !$instance['deleted']) {
      $file_settings = field_bundle_settings('file', $instance['bundle']);
      $media_settings = field_bundle_settings('media', $instance['bundle']);
      $file_settings = array_merge($file_settings, $media_settings);
      if (isset($instance['widget']['weight'])) {
        $file_settings['extra_fields']['form']['file']['weight'] = $instance['widget']['weight'];
      }
      if (isset($instance['display'])) {
        foreach ($instance['display'] as $view_mode => $display) {
          if (isset($display['weight'])) {
            $file_settings['extra_fields']['display']['file'][$view_mode]['weight'] = $display['weight'];
          }
          if (isset($display['type'])) {
            $file_settings['extra_fields']['display']['file'][$view_mode]['visible'] = ($display['type'] != 'hidden');
          }
        }
      }
      field_bundle_settings('file', $instance['bundle'], $file_settings);
    }
  }
  // Delete old media bundle settings.
  db_delete('variable')
    ->condition('name', db_like('field_bundle_settings_media__') . '%', 'LIKE')
    ->execute();

  // Copy field formatter settings of old self-referencing file field to file
  // pseudo-field formatter settings.
  $file_displays = variable_get('file_displays', array());
  foreach ($instances as $instance) {
    if ($instance['field_name'] === 'file' && !$instance['deleted']) {
      if (isset($instance['display'])) {
        foreach ($instance['display'] as $view_mode => $display) {
          if (isset($display['type']) && $display['type'] != 'hidden') {
            $file_formatter = 'file_field_' . $display['type'];
            $file_displays[$instance['bundle']][$view_mode][$file_formatter]['status'] = TRUE;
            if (isset($display['settings'])) {
              $file_displays[$instance['bundle']][$view_mode][$file_formatter]['settings'] = $display['settings'];
            }
          }
        }
      }
    }
  }
  variable_set('file_displays', $file_displays);

  // Delete the old self-referencing file field instances. If all instances are
  // deleted, field_delete_instance() will delete the field too.
  foreach ($instances as $instance) {
    if ($instance['field_name'] === 'file' && !$instance['deleted']) {
      field_delete_instance($instance);
    }
  }

  field_cache_clear();
}

/**
 * Move file display configurations from the 'file_displays' variable to the
 * {file_display} table.
 */
function media_update_7017() {
  // If the {file_display} table doesn't exist, then the File Entity module's
  // update functions will automatically take care of migrating the
  // configurations. However, if file_entity_update_7001() has already run
  // prior to media_update_7016(), run it again in order to capture those
  // configurations too.
  if (db_table_exists('file_display')) {
    module_load_include('install', 'file_entity', 'file_entity');
    file_entity_update_7001();
  }
}

/**
 * Empty update function to trigger a menu rebuild.
 */
function media_update_7018() {
}

/**
 * Update old per-view-mode media field formatters to the generic media formatter with a setting.
 */
function media_update_7019() {
  $instances = array();
  $fields = field_read_fields(array('type' => 'media'), array('include_inactive' => TRUE));
  foreach ($fields as $field) {
    $instances = array_merge($instances, field_read_instances(array('field_id' => $field['id']), array('include_inactive' => TRUE)));
  }
  foreach ($instances as $instance) {
    $update_instance = FALSE;
    foreach ($instance['display'] as $view_mode => $display) {
      if (in_array($display['type'], array('media_link', 'media_preview', 'media_small', 'media_large', 'media_original'))) {
        $update_instance = TRUE;
        $instance['display'][$view_mode]['type'] = 'media';
        $instance['display'][$view_mode]['settings'] = array('file_view_mode' => $display['type']);
      }
    }
    if ($update_instance) {
      field_update_instance($instance);
    }
  }
}

/**
 * Delete the wysiwyg_allowed_types variable if it is the same as default.
 */
function media_update_7020() {
  if (variable_get('media__wysiwyg_allowed_types') == array('image', 'video')) {
    variable_del('media__wysiwyg_allowed_types');
  }
}