Mercurial > hg > isophonics-drupal-site
comparison modules/contrib/migrate_plus/src/Plugin/migrate/source/Url.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\migrate_plus\Plugin\migrate\source; | |
4 | |
5 use Drupal\migrate\Plugin\MigrationInterface; | |
6 use Drupal\migrate_plus\DataParserPluginInterface; | |
7 | |
8 /** | |
9 * Source plugin for retrieving data via URLs. | |
10 * | |
11 * @MigrateSource( | |
12 * id = "url" | |
13 * ) | |
14 */ | |
15 class Url extends SourcePluginExtension { | |
16 | |
17 /** | |
18 * The source URLs to retrieve. | |
19 * | |
20 * @var array | |
21 */ | |
22 protected $sourceUrls = []; | |
23 | |
24 /** | |
25 * The data parser plugin. | |
26 * | |
27 * @var \Drupal\migrate_plus\DataParserPluginInterface | |
28 */ | |
29 protected $dataParserPlugin; | |
30 | |
31 /** | |
32 * {@inheritdoc} | |
33 */ | |
34 public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration) { | |
35 if (!is_array($configuration['urls'])) { | |
36 $configuration['urls'] = [$configuration['urls']]; | |
37 } | |
38 parent::__construct($configuration, $plugin_id, $plugin_definition, $migration); | |
39 | |
40 $this->sourceUrls = $configuration['urls']; | |
41 | |
42 // Set a default Accept header. | |
43 /* $this->headers = array_merge(['Accept' => 'application/json'], | |
44 $configuration['headers'] ?: []);*/ | |
45 | |
46 // See if this is a paged response with next links. If so, add to the source_urls array. | |
47 /* foreach ( (array) $configuration['urls'] as $url) { | |
48 $this->sourceUrls += $this->getNextLinks($url); | |
49 }*/ | |
50 } | |
51 | |
52 /** | |
53 * Return a string representing the source URLs. | |
54 * | |
55 * @return string | |
56 * Comma-separated list of URLs being imported. | |
57 */ | |
58 public function __toString() { | |
59 // This could cause a problem when using a lot of urls, may need to hash. | |
60 $urls = implode(', ', $this->sourceUrls); | |
61 return $urls; | |
62 } | |
63 | |
64 /** | |
65 * Returns the initialized data parser plugin. | |
66 * | |
67 * @return \Drupal\migrate_plus\DataParserPluginInterface | |
68 * The data parser plugin. | |
69 */ | |
70 public function getDataParserPlugin() { | |
71 if (!isset($this->dataParserPlugin)) { | |
72 $this->dataParserPlugin = \Drupal::service('plugin.manager.migrate_plus.data_parser')->createInstance($this->configuration['data_parser_plugin'], $this->configuration); | |
73 } | |
74 return $this->dataParserPlugin; | |
75 } | |
76 | |
77 /** | |
78 * Creates and returns a filtered Iterator over the documents. | |
79 * | |
80 * @return \Iterator | |
81 * An iterator over the documents providing source rows that match the | |
82 * configured item_selector. | |
83 */ | |
84 protected function initializeIterator() { | |
85 return $this->getDataParserPlugin(); | |
86 } | |
87 | |
88 /** | |
89 * Collect an array of next links from a paged response. | |
90 */ | |
91 /* protected function getNextLinks($url) { | |
92 $urls = array(); | |
93 $more = TRUE; | |
94 while ($more == TRUE) { | |
95 $response = $this->dataParserPlugin->getDataFetcher()->getResponse($url); | |
96 if ($url = $this->getNextFromHeaders($response)) { | |
97 $urls[] = $url; | |
98 } | |
99 elseif ($url = $this->getNextFromLinks($response)) { | |
100 $urls[] = $url; | |
101 } | |
102 else { | |
103 $more = FALSE; | |
104 } | |
105 } | |
106 return $urls; | |
107 } | |
108 */ | |
109 /** | |
110 * See if the next link is in a 'links' group in the response. | |
111 * | |
112 * @param \Psr\Http\Message\ResponseInterface $response | |
113 */ | |
114 /* protected function getNextFromLinks(ResponseInterface $response) { | |
115 $body = json_decode($response->getBody(), TRUE); | |
116 if (!empty($body['links']) && array_key_exists('next', $body['links'])) { | |
117 return $body['links']['next']; | |
118 } | |
119 return FALSE; | |
120 } | |
121 */ | |
122 /** | |
123 * See if the next link is in the header. | |
124 * | |
125 * @param \Psr\Http\Message\ResponseInterface $response | |
126 */ | |
127 /* protected function getNextFromHeaders(ResponseInterface $response) { | |
128 $headers = $response->getHeader('Link'); | |
129 foreach ($headers as $header) { | |
130 $matches = array(); | |
131 preg_match('/^<(.*)>; rel="next"$/', $header, $matches); | |
132 if (!empty($matches) && !empty($matches[1])) { | |
133 return $matches[1]; | |
134 } | |
135 } | |
136 return FALSE; | |
137 } | |
138 */ | |
139 } |