Issue #2897254 by Jo Fitzgerald, heddn, rakesh.gectcr, maxocub, masipila, phenaproxima, larowlan: URLs without http:// are broken after migration from d6 or d7

8.5.x
Nathaniel Catchpole 2017-10-23 13:48:49 +01:00
parent df974e5b19
commit df1dd607ad
7 changed files with 239 additions and 82 deletions

View File

@ -39,7 +39,7 @@ class LinkField extends FieldPluginBase {
*/
public function processFieldValues(MigrationInterface $migration, $field_name, $data) {
$process = [
'plugin' => 'd6_field_link',
'plugin' => 'field_link',
'source' => $field_name,
];
$migration->mergeProcessOfProperty($field_name, $process);

View File

@ -0,0 +1,133 @@
<?php
namespace Drupal\link\Plugin\migrate\process;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
/**
* Transform a pre-Drupal 8 formatted link for use in Drupal 8.
*
* Previous to Drupal 8, URLs didn't need to have a URI scheme assigned. The
* contrib link module would auto-prefix the URL with a URI scheme. A link in
* Drupal 8 has more validation and external links must include the URI scheme.
* All external URIs need to be converted to use a URI scheme.
*
* Available configuration keys
* - uri_scheme: (optional) The URI scheme prefix to use for URLs without a
* scheme. Defaults to 'http://', which was the default in Drupal 6 and
* Drupal 7.
*
* Examples:
*
* Consider a link field migration, where you want to use https:// as the
* prefix:
*
* @code
* process:
* field_link:
* plugin: field_link
* uri_scheme: 'https://'
* source: field_link
* @endcode
*
* @MigrateProcessPlugin(
* id = "field_link"
* )
*/
class FieldLink extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration) {
$configuration += ['uri_scheme' => '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 <front> component of the URL.
if (strpos($uri, '<front>') === 0) {
$uri = substr($uri, strlen('<front>'));
}
else {
// List of unicode-encoded characters that were allowed in URLs,
// according to link module in Drupal 7. Every character between &#x00BF;
// and &#x00FF; (except × &#x00D7; and ÷ &#x00F7;) with the addition of
// &#x0152;, &#x0153; and &#x0178;.
// @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;
}
}

View File

@ -2,9 +2,9 @@
namespace Drupal\link\Plugin\migrate\process\d6;
@trigger_error('CckLink is deprecated in Drupal 8.3.x and will be removed before
Drupal 9.0.x. Use \Drupal\link\Plugin\migrate\process\d6\FieldLink instead.',
E_USER_DEPRECATED);
use Drupal\link\Plugin\migrate\process\FieldLink;
@trigger_error('CckLink is deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.x. Use \Drupal\link\Plugin\migrate\process\FieldLink instead.', E_USER_DEPRECATED);
/**
* @MigrateProcessPlugin(
@ -12,6 +12,6 @@ E_USER_DEPRECATED);
* )
*
* @deprecated in Drupal 8.3.x, to be removed before Drupal 9.0.x. Use
* \Drupal\link\Plugin\migrate\process\d6\FieldLink instead.
* \Drupal\link\Plugin\migrate\process\FieldLink instead.
*/
class CckLink extends FieldLink {}

View File

@ -2,85 +2,16 @@
namespace Drupal\link\Plugin\migrate\process\d6;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\link\Plugin\migrate\process\FieldLink as GeneralPurposeFieldLink;
@trigger_error('FieldLink is deprecated in Drupal 8.4.x and will be removed before Drupal 9.0.x. Use \Drupal\link\Plugin\migrate\process\FieldLink instead.', E_USER_DEPRECATED);
/**
* @MigrateProcessPlugin(
* id = "d6_field_link"
* )
*
* @deprecated in Drupal 8.4.x, to be removed before Drupal 9.0.x. Use
* \Drupal\link\Plugin\migrate\process\FieldLink instead.
*/
class FieldLink extends ProcessPluginBase implements ContainerFactoryPluginInterface {
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->migration = $migration;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$migration
);
}
/**
* Turn a Drupal 6 URI into a Drupal 8-compatible format.
*
* @param string $uri
* The 'url' value from Drupal 6.
*
* @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) || !is_null(parse_url($uri, PHP_URL_SCHEME))) {
return $uri;
}
// Remove the <front> component of the URL.
if (strpos($uri, '<front>') === 0) {
$uri = substr($uri, strlen('<front>'));
}
// 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 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;
}
}
class FieldLink extends GeneralPurposeFieldLink {}

View File

@ -50,7 +50,7 @@ class LinkFieldTest extends UnitTestCase {
$this->plugin->processFieldValues($this->migration, 'somefieldname', []);
$expected = [
'plugin' => 'd6_field_link',
'plugin' => 'field_link',
'source' => 'somefieldname',
];
$this->assertSame($expected, $this->migration->getProcess());

View File

@ -0,0 +1,92 @@
<?php
namespace Drupal\Tests\link\Unit\Plugin\migrate\process;
use Drupal\link\Plugin\migrate\process\FieldLink;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Drupal\Tests\UnitTestCase;
/**
* @group Link
*/
class FieldLinkTest extends UnitTestCase {
/**
* Test the url transformations in the FieldLink process plugin.
*
* @dataProvider canonicalizeUriDataProvider
*/
public function testCanonicalizeUri($url, $expected, $configuration = []) {
$link_plugin = new FieldLink($configuration, '', [], $this->getMock(MigrationInterface::class));
$transformed = $link_plugin->transform([
'url' => $url,
'title' => '',
'attributes' => serialize([]),
], $this->getMock(MigrateExecutableInterface::class), $this->getMockBuilder(Row::class)->disableOriginalConstructor()->getMock(), NULL);
$this->assertEquals($expected, $transformed['uri']);
}
/**
* Data provider for testCanonicalizeUri.
*/
public function canonicalizeUriDataProvider() {
return [
'Simple front-page' => [
'<front>',
'internal:/',
],
'Front page with query' => [
'<front>?query=1',
'internal:/?query=1',
],
'No leading forward slash' => [
'node/10',
'internal:/node/10',
],
'Leading forward slash' => [
'/node/10',
'internal:/node/10',
],
'Existing scheme' => [
'scheme:test',
'scheme:test',
],
'Absolute URL with protocol prefix' => [
'http://www.google.com',
'http://www.google.com',
],
'Absolute URL without protocol prefix' => [
'www.yahoo.com',
'http://www.yahoo.com',
],
'Absolute URL without protocol prefix nor www' => [
'yahoo.com',
'https://yahoo.com',
['uri_scheme' => 'https://'],
],
'Absolute URL with non-standard characters' => [
'http://www.ßÀÑÐ¥ƒå¢ë.com',
'http://www.ßÀÑÐ¥ƒå¢ë.com',
],
'Absolute URL with non-standard characters, without protocol prefix' => [
'www.ÐØÑ¢åþë.com',
'http://www.ÐØÑ¢åþë.com',
],
'Absolute URL with non-standard top level domain' => [
'http://www.example.xxx',
'http://www.example.xxx',
],
'Internal link with fragment' => [
'/node/10#top',
'internal:/node/10#top',
],
'External link with fragment' => [
'http://www.example.com/page#links',
'http://www.example.com/page#links',
],
];
}
}

View File

@ -7,6 +7,7 @@ use Drupal\Tests\UnitTestCase;
/**
* @group Link
* @group legacy
*/
class FieldLinkTest extends UnitTestCase {