X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Flink%2Fsrc%2FPlugin%2Fmigrate%2Fprocess%2FFieldLink.php;fp=web%2Fcore%2Fmodules%2Flink%2Fsrc%2FPlugin%2Fmigrate%2Fprocess%2FFieldLink.php;h=4d3d7eec674523cd5292f4c93ddf8358b57c4d62;hp=0000000000000000000000000000000000000000;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hpb=aea91e65e895364e460983b890e295aa5d5540a5 diff --git a/web/core/modules/link/src/Plugin/migrate/process/FieldLink.php b/web/core/modules/link/src/Plugin/migrate/process/FieldLink.php new file mode 100644 index 000000000..4d3d7eec6 --- /dev/null +++ b/web/core/modules/link/src/Plugin/migrate/process/FieldLink.php @@ -0,0 +1,133 @@ + 'http://']; + parent::__construct($configuration, $plugin_id, $plugin_definition); + } + + /** + * Turn a Drupal 6/7 URI into a Drupal 8-compatible format. + * + * @param string $uri + * The 'url' value from Drupal 6/7. + * + * @return string + * The Drupal 8-compatible URI. + * + * @see \Drupal\link\Plugin\Field\FieldWidget\LinkWidget::getUserEnteredStringAsUri() + */ + protected function canonicalizeUri($uri) { + // If we already have a scheme, we're fine. + if (empty($uri) || parse_url($uri, PHP_URL_SCHEME)) { + return $uri; + } + + // Remove the component of the URL. + if (strpos($uri, '') === 0) { + $uri = substr($uri, strlen('')); + } + else { + // List of unicode-encoded characters that were allowed in URLs, + // according to link module in Drupal 7. Every character between ¿ + // and ÿ (except × × and ÷ ÷) with the addition of + // Œ, œ and Ÿ. + // @see http://cgit.drupalcode.org/link/tree/link.module?h=7.x-1.5-beta2#n1382 + $link_ichars = '¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿŒœŸ'; + + // Pattern specific to internal links. + $internal_pattern = "/^(?:[a-z0-9" . $link_ichars . "_\-+\[\] ]+)"; + + $directories = "(?:\/[a-z0-9" . $link_ichars . "_\-\.~+%=&,$'#!():;*@\[\]]*)*"; + // Yes, four backslashes == a single backslash. + $query = "(?:\/?\?([?a-z0-9" . $link_ichars . "+_|\-\.~\/\\\\%=&,$'():;*@\[\]{} ]*))"; + $anchor = "(?:#[a-z0-9" . $link_ichars . "_\-\.~+%=&,$'():;*@\[\]\/\?]*)"; + + // The rest of the path for a standard URL. + $end = $directories . '?' . $query . '?' . $anchor . '?' . '$/i'; + + if (!preg_match($internal_pattern . $end, $uri)) { + $link_domains = '[a-z][a-z0-9-]{1,62}'; + + // Starting a parenthesis group with (?: means that it is grouped, but is not captured + $authentication = "(?:(?:(?:[\w\.\-\+!$&'\(\)*\+,;=" . $link_ichars . "]|%[0-9a-f]{2})+(?::(?:[\w" . $link_ichars . "\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})*)?)?@)"; + $domain = '(?:(?:[a-z0-9' . $link_ichars . ']([a-z0-9' . $link_ichars . '\-_\[\]])*)(\.(([a-z0-9' . $link_ichars . '\-_\[\]])+\.)*(' . $link_domains . '|[a-z]{2}))?)'; + $ipv4 = '(?:[0-9]{1,3}(\.[0-9]{1,3}){3})'; + $ipv6 = '(?:[0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7})'; + $port = '(?::([0-9]{1,5}))'; + + // Pattern specific to external links. + $external_pattern = '/^' . $authentication . '?(' . $domain . '|' . $ipv4 . '|' . $ipv6 . ' |localhost)' . $port . '?'; + if (preg_match($external_pattern . $end, $uri)) { + return $this->configuration['uri_scheme'] . $uri; + } + } + } + + // Add the internal: scheme and ensure a leading slash. + return 'internal:/' . ltrim($uri, '/'); + } + + /** + * {@inheritdoc} + */ + public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { + $attributes = unserialize($value['attributes']); + // Drupal 6/7 link attributes might be double serialized. + if (!is_array($attributes)) { + $attributes = unserialize($attributes); + } + + if (!$attributes) { + $attributes = []; + } + + // Massage the values into the correct form for the link. + $route['uri'] = $this->canonicalizeUri($value['url']); + $route['options']['attributes'] = $attributes; + $route['title'] = $value['title']; + return $route; + } + +}