Skip to content

Instantly share code, notes, and snippets.

@aschweer
Created August 30, 2012 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aschweer/3541306 to your computer and use it in GitHub Desktop.
Save aschweer/3541306 to your computer and use it in GitHub Desktop.
PHP script to redirect item/bitstream/newsfeed links from an EPrints instance (eprints.example.net) to a DSpace instance (dspace.example.net; handle prefix 123456789)
<?php
// Example redirect script for EPrints to DSpace
// Written by Andrea Schweer <schweer@waikato.ac.nz>
//
// Configure Apache on eprints.example.net server to redirect all requests to this script:
// RewriteEngine On
//
// RewriteCond ${REQUEST_URI} !="/index.php"
// RewriteRule ^(.*)$ /index.php/$1 [L]
//
function translate_feed_url() {
// figure out the format from the query string
$format_string = $_SERVER['QUERY_STRING'];
$result = 'feed/atom_1.0/123456789/113';
if ($format_string === 'output=RSS') {
$result = 'feed/rss_1.0/123456789/113';
} else if ($format_string === 'output=RSS2') {
$result = 'feed/rss_2.0/123456789/113';
} else if ($format_string === 'output=Atom') {
$result = 'feed/atom_1.0/123456789/113';
}
return $result;
}
function lookup_dspace_id($eprints_item_id) {
return 5;
// This is just an example -- you need to have something here that looks up the item ID mapping, eg
// $db = new SQLiteDatabase('eprints_to_dspace');
// $q = @$db->query("SELECT dspace_id FROM mapping WHERE eprints_id = " . $eprints_item_id);
// $row = $q->fetch();
// return $row['dspace_id'];
}
$eprints_url = $_SERVER['REQUEST_URI'];
$found_match = false;
if (substr($eprints_url, 0, 16) === '/cgi/latest_tool') {
$dspace_url = translate_feed_url();
$found_match = true;
} else if (preg_match("/^\/(\d+)\/?$/", $eprints_url, $matches)) {
$dspace_id = lookup_dspace_id($matches[1]);
$dspace_url = '/handle/123456789/' . $dspace_id;
$found_match = true;
} else if (preg_match("/^\/(\d+)\/(\d+)\/(.+)$/", $eprints_url, $matches)) {
$dspace_id = lookup_dspace_id($matches[1]);
$dspace_url = '/bitstream/123456789/' . $dspace_id . '/' . $matches[3];
$found_match = true;
}
if ($found_match) {
header('HTTP/1.1 301 Moved Permanently');
// the previous line may have to be
// header('Status: 301 Moved Permanently');
// instead if you're using FastCGI
header('Location: http://dspace.example.net' . $dspace_url);
} else {
// it's some other type of url -> just redirect to start page
header('HTTP/1.1 410 Gone');
// the previous line may have to be
// header('Status: 410 Gone');
// instead if you're using FastCGI
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="refresh" content="20; URL=http://dspace.example.net/">
<title>This page has moved</title>
</head>
<body>
<h1>This page has moved</h1>
<p>This EPrints instance is now part of the central university <a href="http://dspace.example.net/">DSpace instance</a>. The page you were looking for no longer exists.</p>
<p>You will be redirected to DSpace in a few seconds. If not, please follow this link to DSpace: <a href="http://dspace.example.net/">dspace.example.net</a>.</p>
</body>
</html>
<?
}
flush();
exit();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment