f4182a927e19b7ec6a4027b53c46a8d0b6da9795
[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_module = "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) {
29       return 'upload_' . $type;
30     }, $node_types);
31
32     $max_filesize = $this->variableGet('upload_uploadsize_default', 1);
33     $max_filesize = $max_filesize ? $max_filesize . 'MB' : '';
34     $file_extensions = $this->variableGet('upload_extensions_default', 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp');
35     $return = [];
36     $values = $this->select('variable', 'v')
37       ->fields('v', ['name', 'value'])
38       ->condition('v.name', $variables, 'IN')
39       ->execute()
40       ->fetchAllKeyed();
41     foreach ($node_types as $node_type) {
42       $name = 'upload_' . $node_type;
43       // By default, file attachments in D6 are enabled unless upload_<type> is
44       // false, so include types where the upload-variable is not set.
45       $enabled = !isset($values[$name]) || unserialize($values[$name]);
46       if ($enabled) {
47         $return[$node_type]['node_type'] = $node_type;
48         $return[$node_type]['max_filesize'] = $max_filesize;
49         $return[$node_type]['file_extensions'] = $file_extensions;
50       }
51     }
52
53     return new \ArrayIterator($return);
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function getIds() {
60     return [
61       'node_type' => [
62         'type' => 'string',
63       ],
64     ];
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function fields() {
71     return [
72       'node_type' => $this->t('Node type'),
73       'max_filesize' => $this->t('Max filesize'),
74       'file_extensions' => $this->t('File extensions'),
75     ];
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public function count($refresh = FALSE) {
82     return count($this->initializeIterator());
83   }
84
85 }