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 * Returns the initialized data fetcher plugin.
|
Chris@0
|
78 *
|
Chris@0
|
79 * @return \Drupal\migrate_plus\DataFetcherPluginInterface
|
Chris@0
|
80 * The data fetcher plugin.
|
Chris@0
|
81 */
|
Chris@0
|
82 public function getDataFetcherPlugin() {
|
Chris@0
|
83 if (!isset($this->dataFetcherPlugin)) {
|
Chris@0
|
84 $this->dataFetcherPlugin = \Drupal::service('plugin.manager.migrate_plus.data_fetcher')->createInstance($this->configuration['data_fetcher_plugin'], $this->configuration);
|
Chris@0
|
85 }
|
Chris@0
|
86 return $this->dataFetcherPlugin;
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 /**
|
Chris@0
|
90 * {@inheritdoc}
|
Chris@0
|
91 */
|
Chris@0
|
92 public function rewind() {
|
Chris@0
|
93 $this->activeUrl = NULL;
|
Chris@0
|
94 $this->next();
|
Chris@0
|
95 }
|
Chris@0
|
96
|
Chris@0
|
97 /**
|
Chris@0
|
98 * Implementation of Iterator::next().
|
Chris@0
|
99 */
|
Chris@0
|
100 public function next() {
|
Chris@0
|
101 $this->currentItem = $this->currentId = NULL;
|
Chris@0
|
102 if (is_null($this->activeUrl)) {
|
Chris@0
|
103 if (!$this->nextSource()) {
|
Chris@0
|
104 // No data to import.
|
Chris@0
|
105 return;
|
Chris@0
|
106 }
|
Chris@0
|
107 }
|
Chris@0
|
108 // At this point, we have a valid open source url, try to fetch a row from
|
Chris@0
|
109 // it.
|
Chris@0
|
110 $this->fetchNextRow();
|
Chris@0
|
111 // If there was no valid row there, try the next url (if any).
|
Chris@0
|
112 if (is_null($this->currentItem)) {
|
Chris@4
|
113 while ($this->nextSource()) {
|
Chris@0
|
114 $this->fetchNextRow();
|
Chris@4
|
115 if ($this->valid()) {
|
Chris@4
|
116 break;
|
Chris@4
|
117 }
|
Chris@0
|
118 }
|
Chris@0
|
119 }
|
Chris@0
|
120 if ($this->valid()) {
|
Chris@0
|
121 foreach ($this->configuration['ids'] as $id_field_name => $id_info) {
|
Chris@0
|
122 $this->currentId[$id_field_name] = $this->currentItem[$id_field_name];
|
Chris@0
|
123 }
|
Chris@0
|
124 }
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@0
|
127 /**
|
Chris@0
|
128 * Opens the specified URL.
|
Chris@0
|
129 *
|
Chris@4
|
130 * @param string $url
|
Chris@0
|
131 * URL to open.
|
Chris@0
|
132 *
|
Chris@0
|
133 * @return bool
|
Chris@0
|
134 * TRUE if the URL was successfully opened, FALSE otherwise.
|
Chris@0
|
135 */
|
Chris@0
|
136 abstract protected function openSourceUrl($url);
|
Chris@0
|
137
|
Chris@0
|
138 /**
|
Chris@4
|
139 * Retrieves the next row of data. populating currentItem.
|
Chris@4
|
140 *
|
Chris@4
|
141 * Retrieves from the open source URL.
|
Chris@0
|
142 */
|
Chris@0
|
143 abstract protected function fetchNextRow();
|
Chris@0
|
144
|
Chris@0
|
145 /**
|
Chris@0
|
146 * Advances the data parser to the next source url.
|
Chris@0
|
147 *
|
Chris@0
|
148 * @return bool
|
Chris@0
|
149 * TRUE if a valid source URL was opened
|
Chris@0
|
150 */
|
Chris@0
|
151 protected function nextSource() {
|
Chris@0
|
152 while ($this->activeUrl === NULL || (count($this->urls) - 1) > $this->activeUrl) {
|
Chris@0
|
153 if (is_null($this->activeUrl)) {
|
Chris@0
|
154 $this->activeUrl = 0;
|
Chris@0
|
155 }
|
Chris@0
|
156 else {
|
Chris@0
|
157 // Increment the activeUrl so we try to load the next source.
|
Chris@0
|
158 $this->activeUrl = $this->activeUrl + 1;
|
Chris@0
|
159 if ($this->activeUrl >= count($this->urls)) {
|
Chris@0
|
160 return FALSE;
|
Chris@0
|
161 }
|
Chris@0
|
162 }
|
Chris@0
|
163
|
Chris@0
|
164 if ($this->openSourceUrl($this->urls[$this->activeUrl])) {
|
Chris@0
|
165 // We have a valid source.
|
Chris@0
|
166 return TRUE;
|
Chris@0
|
167 }
|
Chris@0
|
168 }
|
Chris@0
|
169
|
Chris@0
|
170 return FALSE;
|
Chris@0
|
171 }
|
Chris@0
|
172
|
Chris@0
|
173 /**
|
Chris@0
|
174 * {@inheritdoc}
|
Chris@0
|
175 */
|
Chris@0
|
176 public function current() {
|
Chris@0
|
177 return $this->currentItem;
|
Chris@0
|
178 }
|
Chris@0
|
179
|
Chris@0
|
180 /**
|
Chris@0
|
181 * {@inheritdoc}
|
Chris@0
|
182 */
|
Chris@0
|
183 public function key() {
|
Chris@0
|
184 return $this->currentId;
|
Chris@0
|
185 }
|
Chris@0
|
186
|
Chris@0
|
187 /**
|
Chris@0
|
188 * {@inheritdoc}
|
Chris@0
|
189 */
|
Chris@0
|
190 public function valid() {
|
Chris@0
|
191 return !empty($this->currentItem);
|
Chris@0
|
192 }
|
Chris@0
|
193
|
Chris@0
|
194 /**
|
Chris@0
|
195 * {@inheritdoc}
|
Chris@0
|
196 */
|
Chris@0
|
197 public function count() {
|
Chris@0
|
198 $count = 0;
|
Chris@0
|
199 foreach ($this as $item) {
|
Chris@0
|
200 $count++;
|
Chris@0
|
201 }
|
Chris@0
|
202 return $count;
|
Chris@0
|
203 }
|
Chris@0
|
204
|
Chris@0
|
205 /**
|
Chris@0
|
206 * Return the selectors used to populate each configured field.
|
Chris@0
|
207 *
|
Chris@0
|
208 * @return string[]
|
Chris@0
|
209 * Array of selectors, keyed by field name.
|
Chris@0
|
210 */
|
Chris@0
|
211 protected function fieldSelectors() {
|
Chris@0
|
212 $fields = [];
|
Chris@0
|
213 foreach ($this->configuration['fields'] as $field_info) {
|
Chris@0
|
214 if (isset($field_info['selector'])) {
|
Chris@0
|
215 $fields[$field_info['name']] = $field_info['selector'];
|
Chris@0
|
216 }
|
Chris@0
|
217 }
|
Chris@0
|
218 return $fields;
|
Chris@0
|
219 }
|
Chris@0
|
220
|
Chris@0
|
221 }
|