diff --git a/Gemini/cfg/config.example.yaml b/Gemini/cfg/config.example.yaml index c08c6318..732cda88 100644 --- a/Gemini/cfg/config.example.yaml +++ b/Gemini/cfg/config.example.yaml @@ -2,7 +2,11 @@ debug: false -fedora_base_url: http://localhost:8080/fcrepo/rest +# Leave these blank unless you know what you're doing. If you're moving +# data from one server to another and the domain changes, set these +# to your new domain. +fedora_domain: +drupal_domain: db.options: driver: pdo_mysql diff --git a/Gemini/src/Controller/GeminiController.php b/Gemini/src/Controller/GeminiController.php index 3c878a36..7f9f32a0 100644 --- a/Gemini/src/Controller/GeminiController.php +++ b/Gemini/src/Controller/GeminiController.php @@ -33,9 +33,9 @@ class GeminiController /** * GeminiController constructor. - * @param \Islandora\Gemini\UrlMapper\UrlMapperInterface - * @param \Islandora\Gemini\UrlMinter\UrlMinterInterface - * @param \Symfony\Component\Routing\Generator\UrlGenerator + * @param \Islandora\Gemini\UrlMapper\UrlMapperInterface $urlMapper + * @param \Islandora\Gemini\UrlMinter\UrlMinterInterface $urlMinter + * @param \Symfony\Component\Routing\Generator\UrlGenerator $urlGenerator */ public function __construct( UrlMapperInterface $urlMapper, diff --git a/Gemini/src/UrlMapper/UrlMapper.php b/Gemini/src/UrlMapper/UrlMapper.php index 8e13532f..c71614a9 100644 --- a/Gemini/src/UrlMapper/UrlMapper.php +++ b/Gemini/src/UrlMapper/UrlMapper.php @@ -17,13 +17,30 @@ class UrlMapper implements UrlMapperInterface */ protected $connection; + /** + * @var string + */ + protected $drupalDomain; + + /** + * @var string + */ + protected $fedoraDomain; + /** * UrlMapper constructor. * @param \Doctrine\DBAL\Connection $connection + * @param string $drupalDomain + * @param string $fedoraDomain */ - public function __construct(Connection $connection) - { + public function __construct( + Connection $connection, + $drupalDomain = "", + $fedoraDomain = "" + ) { $this->connection = $connection; + $this->drupalDomain = $drupalDomain; + $this->fedoraDomain = $fedoraDomain; } /** @@ -32,10 +49,26 @@ public function __construct(Connection $connection) public function getUrls($uuid) { $sql = 'SELECT drupal_uri as drupal, fedora_uri as fedora FROM Gemini WHERE uuid = :uuid'; - return $this->connection->fetchAssoc( + $result = $this->connection->fetchAssoc( $sql, ['uuid' => $uuid] ); + + if (!empty($this->drupalDomain) && isset($result['drupal'])) { + $result['drupal'] = $this->replaceDomain($result['drupal'], $this->drupalDomain); + } + + if (!empty($this->fedoraDomain) && isset($result['fedora'])) { + $result['fedora'] = $this->replaceDomain($result['fedora'], $this->fedoraDomain); + } + + return $result; + } + + protected function replaceDomain($url, $domain) + { + $parts = parse_url($url); + return "$parts[scheme]://$domain$parts[path]"; } /** @@ -108,12 +141,34 @@ public function deleteUrls($uuid) */ public function findUrls($uri) { + $parts = parse_url($uri); + $path = $parts['path']; + $query = - 'SELECT fedora_uri as uri FROM Gemini WHERE drupal_uri = :uri union - SELECT drupal_uri as uri FROM Gemini WHERE fedora_uri = :uri'; - return $this->connection->fetchAssoc( + 'SELECT fedora_uri FROM Gemini WHERE drupal_uri LIKE :path union + SELECT drupal_uri FROM Gemini WHERE fedora_uri LIKE :path'; + + $result = $this->connection->fetchAssoc( $query, - ['uri' => $uri] + ['path' => "%$path"] ); + + if (isset($result['fedora_uri'])) { + if (!empty($this->fedoraDomain)) { + $result['fedora_uri'] = $this->replaceDomain($result['fedora_uri'], $this->fedoraDomain); + } + $result['uri'] = $result['fedora_uri']; + unset($result['fedora_uri']); + } + + if (isset($result['drupal_uri'])) { + if (!empty($this->drupalDomain)) { + $result['drupal_uri'] = $this->replaceDomain($result['drupal_uri'], $this->drupalDomain); + } + $result['uri'] = $result['drupal_uri']; + unset($result['drupal_uri']); + } + + return $result; } } diff --git a/Gemini/src/app.php b/Gemini/src/app.php index 97ee9940..c05a316b 100644 --- a/Gemini/src/app.php +++ b/Gemini/src/app.php @@ -15,7 +15,11 @@ $app->register(new YamlConfigServiceProvider(__DIR__ . '/../cfg/config.yaml')); $app['debug'] = $app['crayfish.debug']; $app['gemini.mapper'] = function ($app) { - return new UrlMapper($app['db']); + return new UrlMapper( + $app['db'], + $app['crayfish.drupal_domain'], + $app['crayfish.fedora_domain'] + ); }; $app['gemini.minter'] = function ($app) { return new UrlMinter(); diff --git a/Gemini/tests/Islandora/Gemini/Tests/UrlMapperTest.php b/Gemini/tests/Islandora/Gemini/Tests/UrlMapperTest.php index 03a3683d..202c880e 100644 --- a/Gemini/tests/Islandora/Gemini/Tests/UrlMapperTest.php +++ b/Gemini/tests/Islandora/Gemini/Tests/UrlMapperTest.php @@ -19,7 +19,7 @@ class UrlMapperTest extends \PHPUnit_Framework_TestCase * @covers ::__construct * @covers ::getUrls */ - public function testGetUrlsReturnsUnmodifiedResults() + public function testGetUrlsReturnsUnmodifiedResultsIfNotConfigured() { // Simulate a record being returned. $connection = $this->prophesize(Connection::class); @@ -56,6 +56,49 @@ public function testGetUrlsReturnsUnmodifiedResults() ); } + /** + * @covers ::__construct + * @covers ::getUrls + */ + public function testGetUrlsReturnsModifiedResultsIfConfigured() + { + // Simulate a record being returned. + $connection = $this->prophesize(Connection::class); + $connection->fetchAssoc(Argument::any(), Argument::any()) + ->willReturn(['fedora' => 'http://exapmle.org/foo', 'drupal' => 'http://example.org/bar']); + $connection = $connection->reveal(); + + $mapper = new UrlMapper($connection, 'drupal.example.org', 'fcrepo.example.org'); + + $results = $mapper->getUrls("abc"); + + $this->assertTrue( + $results['fedora'] == 'http://fcrepo.example.org/foo', + "getUrls() disobeyed configuration. Actual: ${results['fedora']}. Expected: " . + "http://fcrepo.example.org/foo" + ); + $this->assertTrue( + $results['drupal'] == 'http://drupal.example.org/bar', + "getUrls() modified connection results. Actual: ${results['drupal']}. Expected: " . + "http://drupal.example.org/bar" + ); + + // Simulate when no record is found. + $connection = $this->prophesize(Connection::class); + $connection->fetchAssoc(Argument::any(), Argument::any()) + ->willReturn([]); + $connection = $connection->reveal(); + + $mapper = new UrlMapper($connection); + + $results = $mapper->getUrls("abc"); + + $this->assertTrue( + empty($results), + "getUrls() modified connection results. Expected empty array, received " . json_encode($results) + ); + } + /** * @covers ::__construct * @covers ::saveUrls @@ -187,7 +230,7 @@ public function testDeleteUrlsRollsBackOnException() /** * @covers ::findUrls */ - public function testFindUrls() + public function testFindUrlsReturnsUnmodifiedIfNotConfigured() { // Simulate a record being returned. $connection = $this->prophesize(Connection::class); @@ -212,4 +255,48 @@ public function testFindUrls() "getUrls() modified connection results. Expected empty array, received " . json_encode($results) ); } + + /** + * @covers ::findUrls + */ + public function testFindUrlsReturnsModifiedIfConfigured() + { + // Simulate a record being returned for Fedora. + $connection = $this->prophesize(Connection::class); + $connection->fetchAssoc(Argument::any(), Argument::any()) + ->willReturn(['fedora_uri' => 'http://example.org/foo']); + $connection = $connection->reveal(); + $mapper = new UrlMapper($connection, 'drupal.example.org', 'fcrepo.example.org'); + $results = $mapper->findUrls("abc"); + $this->assertTrue( + $results['uri'] == 'http://fcrepo.example.org/foo', + "getUrls() did not modify connection results. Actual: ${results['uri']}. Expected: " . + "http://fcrepo.example.org/foo" + ); + + // Simulate a record being returned for Drupal. + $connection = $this->prophesize(Connection::class); + $connection->fetchAssoc(Argument::any(), Argument::any()) + ->willReturn(['drupal_uri' => 'http://example.org/bar']); + $connection = $connection->reveal(); + $mapper = new UrlMapper($connection, 'drupal.example.org', 'fcrepo.example.org'); + $results = $mapper->findUrls("abc"); + $this->assertTrue( + $results['uri'] == 'http://drupal.example.org/bar', + "getUrls() did not modify connection results. Actual: ${results['uri']}. Expected: " . + "http://drupal.example.org/bar" + ); + + // Simulate when no record is found. + $connection = $this->prophesize(Connection::class); + $connection->fetchAssoc(Argument::any(), Argument::any()) + ->willReturn([]); + $connection = $connection->reveal(); + $mapper = new UrlMapper($connection, 'drupal.example.org', 'fcrepo.example.org'); + $results = $mapper->findUrls("abc"); + $this->assertTrue( + empty($results), + "getUrls() modified connection results. Expected empty array, received " . json_encode($results) + ); + } }