Browse Source

Implement TimeMap

tags/v0.1.0
Gerben 6 years ago
parent
commit
ccaaae17ca
3 changed files with 89 additions and 1 deletions
  1. +2
    -0
      appinfo/routes.php
  2. +82
    -0
      lib/Controller/TimeMapController.php
  3. +5
    -1
      lib/Controller/findMementos.php

+ 2
- 0
appinfo/routes.php View File

@@ -6,5 +6,7 @@ return [
'requirements' => array('url' => '.+')], 'requirements' => array('url' => '.+')],
['name' => 'timeGate#timeGate', 'url' => '/timegate/{url}', 'verb' => 'GET', ['name' => 'timeGate#timeGate', 'url' => '/timegate/{url}', 'verb' => 'GET',
'requirements' => array('url' => '.+')], 'requirements' => array('url' => '.+')],
['name' => 'timeMap#timeMap', 'url' => '/timemap/{url}', 'verb' => 'GET',
'requirements' => array('url' => '.+')],
] ]
]; ];

+ 82
- 0
lib/Controller/TimeMapController.php View File

@@ -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;
}

+ 5
- 1
lib/Controller/findMementos.php View File

@@ -5,7 +5,7 @@ use DOMDocument;
use DateTime; use DateTime;


// Finds HTML files that claim to be a snapshot of the given URL; // Finds HTML files that claim to be a snapshot of the given URL;
// Returns an array, each item being a file's url + original url + snapshot datetime.
// Returns an array of each file's url + original url + snapshot datetime, sorted by datetime.
function findMementos($folder, $url) { function findMementos($folder, $url) {
// Get all HTML files the user owns. // Get all HTML files the user owns.
$files = $folder->searchByMime('text/html'); $files = $folder->searchByMime('text/html');
@@ -40,6 +40,10 @@ function findMementos($folder, $url) {
continue; continue;
} }
} }

// Sort mementos by their datetime. Oldest first.
usort($matchingMementos, function ($m1, $m2) { return $m1['datetime'] <=> $m2['datetime']; });

return $matchingMementos; return $matchingMementos;
} }




Loading…
Cancel
Save