|
- <?php
- namespace OCA\Memento\Controller;
-
- require_once __DIR__ . '/findMementos.php';
- require_once __DIR__ . '/datetimeConversion.php';
- require_once __DIR__ . '/getUrlParameter.php';
-
- use OCP\IRequest;
- use OCP\IServerContainer;
- use OCP\IURLGenerator;
- use OCP\AppFramework\Controller;
- use OCP\AppFramework\Http\DataDisplayResponse;
-
- 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;
- }
-
- /**
- * @NoAdminRequired
- * @NoCSRFRequired
- */
- public function timeMap($url) {
- $url = getUrlParameter('timemap'); // XXX workaround, as nextcloud corrupts the $url parameter.
-
- $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 = datetimeTimestampToString($matchingMementos[0]['datetime']);
- $lastMemento = $matchingMementos[count($matchingMementos)-1];
- $lastDatetime = datetimeTimestampToString($lastMemento['datetime']);
- }
- $links = [
- "<$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 = datetimeTimestampToString($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;
- }
- }
|