media.inc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Destination class implementing migration into media entities.
  4. */
  5. class MigrateDestinationMedia extends MigrateDestinationFile {
  6. /**
  7. * Call this from the prepare() method of a migration that contains media
  8. * image files, if you want to rewrite the IMG tags into media references.
  9. *
  10. * @param $entity
  11. * Entity object being built.
  12. * @param $field
  13. * Name of the text field within the entity to be modified. Defaults to 'body'.
  14. */
  15. static public function rewriteImgTags($entity, $field = 'body') {
  16. if (is_array($entity->$field)) {
  17. migrate_instrument_start('MigrateDestinationMedia rewriteImgTags');
  18. foreach ($entity->$field as $language => $values) {
  19. $body = $values[0]['value'];
  20. break;
  21. }
  22. // Quickly skip any non-candidates
  23. if (!stristr($body, '<img')) {
  24. migrate_instrument_stop('MigrateDestinationMedia rewriteImgTags');
  25. return;
  26. }
  27. // Pass full img tags into the callback
  28. $new_body = preg_replace_callback('|<img [^>]*>|i', array(self, 'replaceCallback'),
  29. $body);
  30. $entity->{$field}[$language][0]['value'] = $new_body;
  31. migrate_instrument_stop('MigrateDestinationMedia rewriteImgTags');
  32. }
  33. }
  34. /**
  35. * If a referenced image can be found in the files table, replace the <img> tag
  36. * with a media JSON reference.
  37. *
  38. * @param array $matches
  39. */
  40. static protected function replaceCallback(array $matches) {
  41. $src_matches = array();
  42. // Default to the original <img> tag
  43. $result = $matches[0];
  44. // Extract the src parameter
  45. if (preg_match('|src=[\'"]([^\'"]+)[\'"]|i', $matches[0], $src_matches)) {
  46. // Replace the scheme and host portions of the referenced URI with the
  47. // Drupal scheme as it's saved in the file_unmanaged table
  48. $src = $src_matches[1];
  49. $fid = db_select('file_managed', 'f')
  50. ->fields('f', array('fid'))
  51. ->condition('filename', basename($src))
  52. ->execute()
  53. ->fetchField();
  54. if ($fid) {
  55. $image_info = array(
  56. 'type' => 'media',
  57. 'view_mode' => 'media_large',
  58. 'fid' => $fid,
  59. 'attributes' => array(
  60. 'alt' => '',
  61. 'title' => '',
  62. 'class' => 'media-image',
  63. 'typeof' => 'foaf:Image',
  64. 'wysiwyg' => 1,
  65. ),
  66. );
  67. // Get the height and width parameters if present
  68. preg_match('|width=[\'"]([^\'"]+)[\'"]|i', $matches[0], $width);
  69. preg_match('|height=[\'"]([^\'"]+)[\'"]|i', $matches[0], $height);
  70. // image width
  71. if ($width) {
  72. $image_info['attributes']['width'] = $width[1];
  73. }
  74. // image height
  75. if ($height) {
  76. $image_info['attributes']['height'] = $height[1];
  77. }
  78. $result = '[[' . drupal_json_encode($image_info) . ']]';
  79. }
  80. }
  81. return $result;
  82. }
  83. }
  84. /**
  85. * Class for creating Youtube file entities.
  86. */
  87. class MigrateExtrasFileYoutube implements MigrateFileInterface {
  88. /**
  89. * Implementation of MigrateFileInterface::fields().
  90. *
  91. * @return array
  92. */
  93. static public function fields() {
  94. return array();
  95. }
  96. /**
  97. * Implementation of MigrateFileInterface::processFiles().
  98. *
  99. * @param $value
  100. * The URI or local filespec of a file to be imported.
  101. * @param $owner
  102. * User ID (uid) to be the owner of the file.
  103. * @return object
  104. * The file entity being created or referenced.
  105. */
  106. public function processFile($value, $owner) {
  107. // Convert the Youtube URI into a local stream wrapper.
  108. if (class_exists('MediaInternetYouTubeHandler')) {
  109. $handler = new MediaInternetYouTubeHandler($value);
  110. $destination = $handler->parse($value);
  111. $file = file_uri_to_object($destination, TRUE);
  112. }
  113. elseif (class_exists('MediaInternetOEmbedHandler')) {
  114. $handler = new MediaInternetOEmbedHandler($value);
  115. $file = $handler->getFileObject();
  116. }
  117. else {
  118. MigrationBase::displayMessage(t('Could not find a handler for YouTube videos'));
  119. return FALSE;
  120. }
  121. // Create a file entity object for this Youtube reference, and see if we
  122. // can get the video title.
  123. if (empty($file->fid) && $info = $handler->getOEmbed()) {
  124. $file->filename = truncate_utf8($info['title'], 255);
  125. }
  126. $file = file_save($file);
  127. if (is_object($file)) {
  128. return $file;
  129. }
  130. else {
  131. return FALSE;
  132. }
  133. }
  134. }
  135. // Basic support for the deprecated media field type.
  136. class MigrateMediaFieldHandler extends MigrateFileFieldHandler {
  137. public function __construct() {
  138. $this->registerTypes(array('media'));
  139. }
  140. /**
  141. * Implementation of MigrateFieldHandler::fields().
  142. *
  143. * @param $type
  144. * The field type.
  145. * @param $instance
  146. * Instance info for the field.
  147. * @param Migration $migration
  148. * The migration context for the parent field. We can look at the mappings
  149. * and determine which subfields are relevant.
  150. * @return array
  151. */
  152. public function fields($type, $instance, $migration = NULL) {
  153. $fields = parent::fields($type, $instance, $migration);
  154. unset($fields['description']);
  155. unset($fields['display']);
  156. $fields += array(
  157. 'title' => t('Subfield: String to be used as the title value'),
  158. 'data' => t('Subfield: Additional data about the field'),
  159. );
  160. return $fields;
  161. }
  162. }