Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\migrate_plus;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Plugin\PluginBase;
|
Chris@0
|
6 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Defines a base data parser implementation.
|
Chris@0
|
10 *
|
Chris@0
|
11 * @see \Drupal\migrate_plus\Annotation\DataParser
|
Chris@0
|
12 * @see \Drupal\migrate_plus\DataParserPluginInterface
|
Chris@0
|
13 * @see \Drupal\migrate_plus\DataParserPluginManager
|
Chris@0
|
14 * @see plugin_api
|
Chris@0
|
15 */
|
Chris@0
|
16 abstract class DataParserPluginBase extends PluginBase implements DataParserPluginInterface {
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * List of source urls.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @var string[]
|
Chris@0
|
22 */
|
Chris@0
|
23 protected $urls;
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * Index of the currently-open url.
|
Chris@0
|
27 *
|
Chris@0
|
28 * @var int
|
Chris@0
|
29 */
|
Chris@0
|
30 protected $activeUrl;
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * String indicating how to select an item's data from the source.
|
Chris@0
|
34 *
|
Chris@0
|
35 * @var string
|
Chris@0
|
36 */
|
Chris@0
|
37 protected $itemSelector;
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * Current item when iterating.
|
Chris@0
|
41 *
|
Chris@0
|
42 * @var mixed
|
Chris@0
|
43 */
|
Chris@0
|
44 protected $currentItem = NULL;
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * Value of the ID for the current item when iterating.
|
Chris@0
|
48 *
|
Chris@0
|
49 * @var string
|
Chris@0
|
50 */
|
Chris@0
|
51 protected $currentId = NULL;
|
Chris@0
|
52
|
Chris@0
|
53 /**
|
Chris@0
|
54 * The data retrieval client.
|
Chris@0
|
55 *
|
Chris@0
|
56 * @var \Drupal\migrate_plus\DataFetcherPluginInterface
|
Chris@0
|
57 */
|
Chris@0
|
58 protected $dataFetcher;
|
Chris@0
|
59
|
Chris@0
|
60 /**
|
Chris@0
|
61 * {@inheritdoc}
|
Chris@0
|
62 */
|
Chris@0
|
63 public function __construct(array $configuration, $plugin_id, $plugin_definition) {
|
Chris@0
|
64 parent::__construct($configuration, $plugin_id, $plugin_definition);
|
Chris@0
|
65 $this->urls = $configuration['urls'];
|
Chris@0
|
66 $this->itemSelector = $configuration['item_selector'];
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 /**
|
Chris@0
|
70 * {@inheritdoc}
|
Chris@0
|
71 */
|
Chris@0
|
72 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
Chris@0
|
73 return new static($configuration, $plugin_id, $plugin_definition);
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76
|
Chris@0
|
77 /**
|
Chris@0
|
78 * Returns the initialized data fetcher plugin.
|
Chris@0
|
79 *
|
Chris@0
|
80 * @return \Drupal\migrate_plus\DataFetcherPluginInterface
|
Chris@0
|
81 * The data fetcher plugin.
|
Chris@0
|
82 */
|
Chris@0
|
83 public function getDataFetcherPlugin() {
|
Chris@0
|
84 if (!isset($this->dataFetcherPlugin)) {
|
Chris@0
|
85 $this->dataFetcherPlugin = \Drupal::service('plugin.manager.migrate_plus.data_fetcher')->createInstance($this->configuration['data_fetcher_plugin'], $this->configuration);
|
Chris@0
|
86 }
|
Chris@0
|
87 return $this->dataFetcherPlugin;
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 /**
|
Chris@0
|
91 * {@inheritdoc}
|
Chris@0
|
92 */
|
Chris@0
|
93 public function rewind() {
|
Chris@0
|
94 $this->activeUrl = NULL;
|
Chris@0
|
95 $this->next();
|
Chris@0
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 /**
|
Chris@0
|
99 * Implementation of Iterator::next().
|
Chris@0
|
100 */
|
Chris@0
|
101 public function next() {
|
Chris@0
|
102 $this->currentItem = $this->currentId = NULL;
|
Chris@0
|
103 if (is_null($this->activeUrl)) {
|
Chris@0
|
104 if (!$this->nextSource()) {
|
Chris@0
|
105 // No data to import.
|
Chris@0
|
106 return;
|
Chris@0
|
107 }
|
Chris@0
|
108 }
|
Chris@0
|
109 // At this point, we have a valid open source url, try to fetch a row from
|
Chris@0
|
110 // it.
|
Chris@0
|
111 $this->fetchNextRow();
|
Chris@0
|
112 // If there was no valid row there, try the next url (if any).
|
Chris@0
|
113 if (is_null($this->currentItem)) {
|
Chris@0
|
114 if ($this->nextSource()) {
|
Chris@0
|
115 $this->fetchNextRow();
|
Chris@0
|
116 }
|
Chris@0
|
117 }
|
Chris@0
|
118 if ($this->valid()) {
|
Chris@0
|
119 foreach ($this->configuration['ids'] as $id_field_name => $id_info) {
|
Chris@0
|
120 $this->currentId[$id_field_name] = $this->currentItem[$id_field_name];
|
Chris@0
|
121 }
|
Chris@0
|
122 }
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 /**
|
Chris@0
|
126 * Opens the specified URL.
|
Chris@0
|
127 *
|
Chris@0
|
128 * @param $url
|
Chris@0
|
129 * URL to open.
|
Chris@0
|
130 *
|
Chris@0
|
131 * @return bool
|
Chris@0
|
132 * TRUE if the URL was successfully opened, FALSE otherwise.
|
Chris@0
|
133 */
|
Chris@0
|
134 abstract protected function openSourceUrl($url);
|
Chris@0
|
135
|
Chris@0
|
136 /**
|
Chris@0
|
137 * Retrieves the next row of data from the open source URL, populating
|
Chris@0
|
138 * currentItem.
|
Chris@0
|
139 */
|
Chris@0
|
140 abstract protected function fetchNextRow();
|
Chris@0
|
141
|
Chris@0
|
142 /**
|
Chris@0
|
143 * Advances the data parser to the next source url.
|
Chris@0
|
144 *
|
Chris@0
|
145 * @return bool
|
Chris@0
|
146 * TRUE if a valid source URL was opened
|
Chris@0
|
147 */
|
Chris@0
|
148 protected function nextSource() {
|
Chris@0
|
149 while ($this->activeUrl === NULL || (count($this->urls) - 1) > $this->activeUrl) {
|
Chris@0
|
150 if (is_null($this->activeUrl)) {
|
Chris@0
|
151 $this->activeUrl = 0;
|
Chris@0
|
152 }
|
Chris@0
|
153 else {
|
Chris@0
|
154 // Increment the activeUrl so we try to load the next source.
|
Chris@0
|
155 $this->activeUrl = $this->activeUrl + 1;
|
Chris@0
|
156 if ($this->activeUrl >= count($this->urls)) {
|
Chris@0
|
157 return FALSE;
|
Chris@0
|
158 }
|
Chris@0
|
159 }
|
Chris@0
|
160
|
Chris@0
|
161 if ($this->openSourceUrl($this->urls[$this->activeUrl])) {
|
Chris@0
|
162 // We have a valid source.
|
Chris@0
|
163 return TRUE;
|
Chris@0
|
164 }
|
Chris@0
|
165 }
|
Chris@0
|
166
|
Chris@0
|
167 return FALSE;
|
Chris@0
|
168 }
|
Chris@0
|
169
|
Chris@0
|
170 /**
|
Chris@0
|
171 * {@inheritdoc}
|
Chris@0
|
172 */
|
Chris@0
|
173 public function current() {
|
Chris@0
|
174 return $this->currentItem;
|
Chris@0
|
175 }
|
Chris@0
|
176
|
Chris@0
|
177 /**
|
Chris@0
|
178 * {@inheritdoc}
|
Chris@0
|
179 */
|
Chris@0
|
180 public function key() {
|
Chris@0
|
181 return $this->currentId;
|
Chris@0
|
182 }
|
Chris@0
|
183
|
Chris@0
|
184 /**
|
Chris@0
|
185 * {@inheritdoc}
|
Chris@0
|
186 */
|
Chris@0
|
187 public function valid() {
|
Chris@0
|
188 return !empty($this->currentItem);
|
Chris@0
|
189 }
|
Chris@0
|
190
|
Chris@0
|
191 /**
|
Chris@0
|
192 * {@inheritdoc}
|
Chris@0
|
193 */
|
Chris@0
|
194 public function count() {
|
Chris@0
|
195 $count = 0;
|
Chris@0
|
196 foreach ($this as $item) {
|
Chris@0
|
197 $count++;
|
Chris@0
|
198 }
|
Chris@0
|
199 return $count;
|
Chris@0
|
200 }
|
Chris@0
|
201
|
Chris@0
|
202 /**
|
Chris@0
|
203 * Return the selectors used to populate each configured field.
|
Chris@0
|
204 *
|
Chris@0
|
205 * @return string[]
|
Chris@0
|
206 * Array of selectors, keyed by field name.
|
Chris@0
|
207 */
|
Chris@0
|
208 protected function fieldSelectors() {
|
Chris@0
|
209 $fields = [];
|
Chris@0
|
210 foreach ($this->configuration['fields'] as $field_info) {
|
Chris@0
|
211 if (isset($field_info['selector'])) {
|
Chris@0
|
212 $fields[$field_info['name']] = $field_info['selector'];
|
Chris@0
|
213 }
|
Chris@0
|
214 }
|
Chris@0
|
215 return $fields;
|
Chris@0
|
216 }
|
Chris@0
|
217
|
Chris@0
|
218 }
|