D6EdlpFile.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Drupal\edlp_migrate\Plugin\migrate\source;
  3. use Drupal\migrate\Row;
  4. use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
  5. /**
  6. * Drupal 6 file source from database.
  7. *
  8. * @MigrateSource(
  9. * id = "d6_edlp_file"
  10. * )
  11. */
  12. class D6EdlpFile extends DrupalSqlBase {
  13. /**
  14. * The file directory path.
  15. *
  16. * @var string
  17. */
  18. protected $filePath;
  19. /**
  20. * The temporary file path.
  21. *
  22. * @var string
  23. */
  24. protected $tempFilePath;
  25. /**
  26. * Flag for private or public file storage.
  27. *
  28. * @var bool
  29. */
  30. protected $isPublic;
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function query() {
  35. return $this->select('files', 'f')
  36. ->fields('f')
  37. ->orderBy('timestamp')
  38. // If two or more files have the same timestamp, they'll end up in a
  39. // non-deterministic order. Ordering by fid (or any other unique field)
  40. // will prevent this.
  41. ->orderBy('f.fid');
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. protected function initializeIterator() {
  47. $site_path = isset($this->configuration['site_path']) ? $this->configuration['site_path'] : 'sites/default';
  48. // $filepath = $this->variableGet('file_directory_path', $site_path . '/files') . '/';
  49. // $filepath = str_replace('//', '/', $filepath);
  50. // drush_print('filepath : '.$filepath);
  51. $this->filePath = $this->variableGet('file_directory_path', $site_path . '/files') . '/';
  52. $this->tempFilePath = $this->variableGet('file_directory_temp', '/tmp') . '/';
  53. // FILE_DOWNLOADS_PUBLIC == 1 and FILE_DOWNLOADS_PRIVATE == 2.
  54. $this->isPublic = $this->variableGet('file_downloads', 1) == 1;
  55. return parent::initializeIterator();
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function prepareRow(Row $row) {
  61. drush_print("D6EdlpFile");
  62. // drush_print_r($row);
  63. $filepath = $row->getSourceProperty('filepath');
  64. $filepath = str_replace('//', '/', $filepath);
  65. $filepath = preg_replace('/^\//', '', $filepath);
  66. drush_print('filePath : '.$filepath);
  67. $row->setSourceProperty('filepath', $filepath);
  68. $row->setSourceProperty('file_directory_path', $this->filePath);
  69. $row->setSourceProperty('temp_directory_path', $this->tempFilePath);
  70. $row->setSourceProperty('is_public', $this->isPublic);
  71. return parent::prepareRow($row);
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function fields() {
  77. return [
  78. 'fid' => $this->t('File ID'),
  79. 'uid' => $this->t('The {users}.uid who added the file. If set to 0, this file was added by an anonymous user.'),
  80. 'filename' => $this->t('File name'),
  81. 'filepath' => $this->t('File path'),
  82. 'filemime' => $this->t('File MIME Type'),
  83. 'status' => $this->t('The published status of a file.'),
  84. 'timestamp' => $this->t('The time that the file was added.'),
  85. 'file_directory_path' => $this->t('The Drupal files path.'),
  86. 'is_public' => $this->t('TRUE if the files directory is public otherwise FALSE.'),
  87. ];
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function getIds() {
  93. $ids['fid']['type'] = 'integer';
  94. return $ids;
  95. }
  96. }