Version 1
[yaffs-website] / web / core / modules / file / src / Plugin / migrate / source / d6 / UploadInstance.php
1 <?php
2
3 namespace Drupal\file\Plugin\migrate\source\d6;
4
5 use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
6 use Drupal\migrate\Plugin\migrate\source\DummyQueryTrait;
7
8 /**
9  * Drupal 6 upload instance source from database.
10  *
11  * @MigrateSource(
12  *   id = "d6_upload_instance",
13  *   source_provider = "upload"
14  * )
15  */
16 class UploadInstance extends DrupalSqlBase {
17
18   use DummyQueryTrait;
19
20   /**
21    * {@inheritdoc}
22    */
23   protected function initializeIterator() {
24     $node_types = $this->select('node_type', 'nt')
25       ->fields('nt', ['type'])
26       ->execute()
27       ->fetchCol();
28     $variables = array_map(function($type) { return 'upload_' . $type; }, $node_types);
29
30     $max_filesize = $this->variableGet('upload_uploadsize_default', 1);
31     $max_filesize = $max_filesize ? $max_filesize . 'MB' : '';
32     $file_extensions = $this->variableGet('upload_extensions_default', 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp');
33     $return = [];
34     $values = $this->select('variable', 'v')
35       ->fields('v', ['name', 'value'])
36       ->condition('v.name', $variables, 'IN')
37       ->execute()
38       ->fetchAllKeyed();
39     foreach ($node_types as $node_type) {
40       $name = 'upload_' . $node_type;
41       // By default, file attachments in D6 are enabled unless upload_<type> is
42       // false, so include types where the upload-variable is not set.
43       $enabled = !isset($values[$name]) || unserialize($values[$name]);
44       if ($enabled) {
45         $return[$node_type]['node_type'] = $node_type;
46         $return[$node_type]['max_filesize'] = $max_filesize;
47         $return[$node_type]['file_extensions'] = $file_extensions;
48       }
49     }
50
51     return new \ArrayIterator($return);
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function getIds() {
58     return [
59       'node_type' => [
60         'type' => 'string',
61       ],
62     ];
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public function fields() {
69     return [
70       'node_type' => $this->t('Node type'),
71       'max_filesize' => $this->t('Max filesize'),
72       'file_extensions' => $this->t('File extensions'),
73     ];
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   public function count() {
80     return count($this->initializeIterator());
81   }
82
83 }