|
@@ -0,0 +1,82 @@ |
|
|
|
|
|
<?php |
|
|
|
|
|
namespace OCA\Memento\Controller; |
|
|
|
|
|
|
|
|
|
|
|
require_once __DIR__ . '/findMementos.php'; |
|
|
|
|
|
|
|
|
|
|
|
use DateTime; |
|
|
|
|
|
|
|
|
|
|
|
use OCP\IRequest; |
|
|
|
|
|
use OCP\IServerContainer; |
|
|
|
|
|
use OCP\IURLGenerator; |
|
|
|
|
|
use OCP\AppFramework\Controller; |
|
|
|
|
|
use OCP\AppFramework\Http\DataDisplayResponse; |
|
|
|
|
|
|
|
|
|
|
|
use findMementos; |
|
|
|
|
|
|
|
|
|
|
|
class TimeMapController extends Controller { |
|
|
|
|
|
private $userFolder; |
|
|
|
|
|
private $URLGenerator; |
|
|
|
|
|
|
|
|
|
|
|
public function __construct( |
|
|
|
|
|
$AppName, |
|
|
|
|
|
IRequest $request, |
|
|
|
|
|
$UserId, |
|
|
|
|
|
IServerContainer $serverContainer, |
|
|
|
|
|
IURLGenerator $URLGenerator |
|
|
|
|
|
) { |
|
|
|
|
|
parent::__construct($AppName, $request); |
|
|
|
|
|
$this->userFolder = $serverContainer->getUserFolder($UserId); |
|
|
|
|
|
$this->URLGenerator = $URLGenerator; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @PublicPage |
|
|
|
|
|
* @NoAdminRequired |
|
|
|
|
|
* @NoCSRFRequired |
|
|
|
|
|
*/ |
|
|
|
|
|
public function timeMap($url) { |
|
|
|
|
|
$matchingMementos = findMementos($this->userFolder, $url); |
|
|
|
|
|
|
|
|
|
|
|
// Build the list of links. |
|
|
|
|
|
// $timeMapUrl = $this->URLGenerator->linkToRouteAbsolute('timeMap#timeMap', [ 'url' => $url ]); |
|
|
|
|
|
// $timeGateUrl = $this->URLGenerator->linkToRouteAbsolute('timeGate#timeGate', [ 'url' => $url ]); |
|
|
|
|
|
// FIXME ...is linkToRouteAbsolute broken? Hardcoding the path then.. |
|
|
|
|
|
$timeMapUrl = $this->URLGenerator->getAbsoluteUrl("/apps/memento/timemap/$url"); |
|
|
|
|
|
$timeGateUrl = $this->URLGenerator->getAbsoluteUrl("/apps/memento/timegate/$url"); |
|
|
|
|
|
if (count($matchingMementos) > 0) { |
|
|
|
|
|
$firstDatetime = formatDatetime($matchingMementos[0]['datetime']); |
|
|
|
|
|
$lastDatetime = formatDatetime($matchingMementos[count($matchingMementos)-1]['datetime']); |
|
|
|
|
|
} |
|
|
|
|
|
$links = [ |
|
|
|
|
|
// FIXME Our $url param has its duplicate slashes removed, so it looks like 'http:/abc'. |
|
|
|
|
|
"<$url>;rel=\"original\"", |
|
|
|
|
|
"<$timeGateUrl>;rel=\"timegate\"", |
|
|
|
|
|
"<$timeMapUrl>;rel=\"self\";type=\"application/link-format\"" . |
|
|
|
|
|
($firstDatetime && $lastDatetime ? ";from=\"$firstDatetime\";until=\"$lastDatetime\"" : "") |
|
|
|
|
|
]; |
|
|
|
|
|
foreach ($matchingMementos as $index => $memento) { |
|
|
|
|
|
$datetime = formatDatetime($memento['datetime']); |
|
|
|
|
|
$maybeFirst = $index === 0 ? 'first ' : ''; |
|
|
|
|
|
$maybeLast = $index === count($matchingMementos)-1 ? 'last ' : ''; |
|
|
|
|
|
// Make absolute, as the spec says URLs are to be interpreted relative to the *original* url! |
|
|
|
|
|
$absoluteMementoUrl = $this->URLGenerator->getAbsoluteURL($memento['mementoUrl']); |
|
|
|
|
|
$links[] = "<$absoluteMementoUrl>" |
|
|
|
|
|
. ";rel=\"{$maybeFirst}{$maybeLast}memento\"" |
|
|
|
|
|
. ";datetime=\"$datetime\""; |
|
|
|
|
|
} |
|
|
|
|
|
$linksString = implode(",\n", $links); |
|
|
|
|
|
|
|
|
|
|
|
$headers = [ |
|
|
|
|
|
"Content-Type" => "application/link-format" |
|
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
$response = new DataDisplayResponse($linksString, 200, $headers); |
|
|
|
|
|
return $response; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function formatDatetime($datetime) { |
|
|
|
|
|
$datetime = new DateTime("@$datetime"); |
|
|
|
|
|
$s = $datetime->format("D, d M Y H:i:s") . " GMT"; |
|
|
|
|
|
return $s; |
|
|
|
|
|
} |