D7PublicFile.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace Drupal\materio_migrate\Plugin\migrate\source;
  3. use Drupal\Core\Database\Query\Condition;
  4. use Drupal\migrate\Row;
  5. use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
  6. /**
  7. * Drupal 7 file source from database.
  8. *
  9. * @MigrateSource(
  10. * id = "d7_pubic_file",
  11. * source_module = "file"
  12. * )
  13. */
  14. class D7PublicFile extends DrupalSqlBase {
  15. /**
  16. * The public file directory path.
  17. *
  18. * @var string
  19. */
  20. protected $publicPath;
  21. /**
  22. * The private file directory path, if any.
  23. *
  24. * @var string
  25. */
  26. protected $privatePath;
  27. /**
  28. * The temporary file directory path.
  29. *
  30. * @var string
  31. */
  32. protected $temporaryPath;
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function query() {
  37. $query = $this->select('file_managed', 'f')
  38. ->fields('f')
  39. ->condition('uri', 'temporary://%', 'NOT LIKE')
  40. ->orderBy('f.timestamp');
  41. // Filter by scheme(s), if configured.
  42. if (isset($this->configuration['scheme'])) {
  43. $schemes = [];
  44. // Remove 'temporary' scheme.
  45. $valid_schemes = array_diff((array) $this->configuration['scheme'], ['temporary']);
  46. // Accept either a single scheme, or a list.
  47. foreach ((array) $valid_schemes as $scheme) {
  48. $schemes[] = rtrim($scheme) . '://';
  49. }
  50. $schemes = array_map([$this->getDatabase(), 'escapeLike'], $schemes);
  51. // Add conditions, uri LIKE 'public://%' OR uri LIKE 'private://%'.
  52. $conditions = new Condition('OR');
  53. foreach ($schemes as $scheme) {
  54. $conditions->condition('uri', $scheme . '%', 'LIKE');
  55. }
  56. $query->condition($conditions);
  57. }
  58. return $query;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. protected function initializeIterator() {
  64. $this->publicPath = $this->variableGet('file_public_path', 'sites/default/files');
  65. $this->privatePath = $this->variableGet('file_private_path', NULL);
  66. $this->temporaryPath = $this->variableGet('file_temporary_path', '/tmp');
  67. return parent::initializeIterator();
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function prepareRow(Row $row) {
  73. // Compute the filepath property, which is a physical representation of
  74. // the URI relative to the Drupal root.
  75. $path = str_replace(['public:/', 'private:/', 'temporary:/'], [$this->publicPath, $this->privatePath, $this->temporaryPath], $row->getSourceProperty('uri'));
  76. // At this point, $path could be an absolute path or a relative path,
  77. // depending on how the scheme's variable was set. So we need to shear out
  78. // the source_base_path in order to make them all relative.
  79. $path = str_replace($this->configuration['constants']['source_base_path'], NULL, $path);
  80. $row->setSourceProperty('filepath', $path);
  81. return parent::prepareRow($row);
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function fields() {
  87. return [
  88. 'fid' => $this->t('File ID'),
  89. 'uid' => $this->t('The {users}.uid who added the file. If set to 0, this file was added by an anonymous user.'),
  90. 'filename' => $this->t('File name'),
  91. 'filepath' => $this->t('File path'),
  92. 'filemime' => $this->t('File MIME Type'),
  93. 'status' => $this->t('The published status of a file.'),
  94. 'timestamp' => $this->t('The time that the file was added.'),
  95. ];
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function getIds() {
  101. $ids['fid']['type'] = 'integer';
  102. return $ids;
  103. }
  104. }