nextcloud-memento/lib/Controller/ TimeMapController.php
98 lines
3.2 KiB

  1. <?php
  2. namespace OCA\Memento\Controller;
  3. require_once __DIR__ . '/datetimeConversion.php';
  4. require_once __DIR__ . '/getUrlParameter.php';
  5. use OCP\IRequest;
  6. use OCP\IServerContainer;
  7. use OCP\IURLGenerator;
  8. use OCP\AppFramework\Controller;
  9. use OCP\AppFramework\Http\DataDisplayResponse;
  10. class TimeMapController extends Controller {
  11. use MementoFinder;
  12. private $loggedInUserId;
  13. private $serverContainer;
  14. public function __construct(
  15. $AppName,
  16. IRequest $request,
  17. $UserId,
  18. IServerContainer $serverContainer
  19. ) {
  20. parent::__construct($AppName, $request);
  21. $this->loggedInUserId = $UserId;
  22. $this->serverContainer = $serverContainer;
  23. }
  24. /**
  25. * @NoAdminRequired
  26. * @NoCSRFRequired
  27. * @PublicPage
  28. */
  29. public function singleUserTimeMap($userId, $url) {
  30. // XXX workaround, as nextcloud corrupts the $url parameter.
  31. $routePrefix = "u/$userId/";
  32. $url = getUrlParameter("{$routePrefix}timemap");
  33. $matchingMementos = $this->findSingleUserMementosForUrl($userId, $url);
  34. return $this->makeResponse($url, $matchingMementos, $routePrefix);
  35. }
  36. /**
  37. * @NoAdminRequired
  38. * @NoCSRFRequired
  39. * @PublicPage
  40. */
  41. public function allUsersTimeMap($url) {
  42. $routePrefix = "";
  43. $url = getUrlParameter("{$routePrefix}timemap");
  44. $matchingMementos = $this->findAllUsersMementosForUrl($url);
  45. return $this->makeResponse($url, $matchingMementos, $routePrefix);
  46. }
  47. private function makeResponse($url, $matchingMementos, $routePrefix) {
  48. // Build the list of links.
  49. // $timeMapUrl = $this->URLGenerator->linkToRouteAbsolute('timeMap#timeMap', [ 'url' => $url ]);
  50. // $timeGateUrl = $this->URLGenerator->linkToRouteAbsolute('timeGate#timeGate', [ 'url' => $url ]);
  51. // FIXME ...is linkToRouteAbsolute broken? Hardcoding the path then..
  52. $URLGenerator = $this->serverContainer->getURLGenerator();
  53. $timeMapUrl = $URLGenerator->getAbsoluteUrl("/apps/memento/{$routePrefix}timemap/$url");
  54. $timeGateUrl = $URLGenerator->getAbsoluteUrl("/apps/memento/{$routePrefix}timegate/$url");
  55. if (count($matchingMementos) > 0) {
  56. $firstDatetime = datetimeTimestampToString($matchingMementos[0]['datetime']);
  57. $lastMemento = $matchingMementos[count($matchingMementos)-1];
  58. $lastDatetime = datetimeTimestampToString($lastMemento['datetime']);
  59. }
  60. $links = [
  61. "<$url>;rel=\"original\"",
  62. "<$timeGateUrl>;rel=\"timegate\"",
  63. "<$timeMapUrl>;rel=\"self\";type=\"application/link-format\"" .
  64. ($firstDatetime && $lastDatetime ? ";from=\"$firstDatetime\";until=\"$lastDatetime\"" : "")
  65. ];
  66. foreach ($matchingMementos as $index => $memento) {
  67. $datetime = datetimeTimestampToString($memento['datetime']);
  68. $maybeFirst = $index === 0 ? 'first ' : '';
  69. $maybeLast = $index === count($matchingMementos)-1 ? 'last ' : '';
  70. // Make absolute, as the spec says URLs are to be interpreted relative to the *original* url!
  71. $absoluteMementoUrl = $URLGenerator->getAbsoluteURL($memento['mementoUrl']);
  72. $links[] = "<$absoluteMementoUrl>"
  73. . ";rel=\"{$maybeFirst}{$maybeLast}memento\""
  74. . ";datetime=\"$datetime\"";
  75. }
  76. $linksString = implode(",\n", $links);
  77. $headers = [
  78. "Content-Type" => "application/link-format"
  79. ];
  80. $response = new DataDisplayResponse($linksString, 200, $headers);
  81. return $response;
  82. }
  83. }