Browse Source

Deduplicate results

tags/v0.1.0
Gerben 6 years ago
parent
commit
a381b3dd32
1 changed files with 27 additions and 15 deletions
  1. +27
    -15
      lib/Controller/findMementos.php

+ 27
- 15
lib/Controller/findMementos.php View File

@@ -25,13 +25,10 @@ trait MementoFinder {
if ($this->loggedInUserId === $userId) {
$userFolder = $this->serverContainer->getUserFolder($this->loggedInUserId);
$moreMementos = findPrivateMementos($userFolder);
$foundMementos = mergeMementos($foundMementos, $moreMementos);
$foundMementos = array_merge($foundMementos, $moreMementos);
}

// Filter those that match the requested URL, and sort them.
$matchingMementos = filterMementosByUrl($foundMementos, $url);
sortMementos($matchingMementos);
return $matchingMementos;
return listMementosMatchingUrl($foundMementos, $url);
}

function findAllUsersMementosForUrl($url) {
@@ -45,20 +42,17 @@ trait MementoFinder {
$shareManager = $this->serverContainer->getShareManager();
foreach ($allUserIds as $userId) {
$moreMementos = findPublicMementos($shareManager, $userId);
$foundMementos = mergeMementos($foundMementos, $moreMementos);
$foundMementos = array_merge($foundMementos, $moreMementos);
}

// If logged in, get current user's private mementos too.
if ($this->loggedInUserId) {
$userFolder = $this->serverContainer->getUserFolder($this->loggedInUserId);
$moreMementos = findPrivateMementos($userFolder);
$foundMementos = mergeMementos($foundMementos, $moreMementos);
$foundMementos = array_merge($foundMementos, $moreMementos);
}

// Filter those that match the requested URL, and sort them.
$matchingMementos = filterMementosByUrl($foundMementos, $url);
$matchingMementos = sortMementos($matchingMementos);
return $matchingMementos;
return listMementosMatchingUrl($foundMementos, $url);
}
}

@@ -127,9 +121,14 @@ function findPublicMementos($shareManager, $userId) {
return $foundMementos;
}

function mergeMementos($mementos1, $mementos2) {
// TODO deduplicate (we'll get public & private URLs for the same files)
return array_merge($mementos1, $mementos2);
function listMementosMatchingUrl($foundMementos, $url) {
// Filter those that match the requested URL
$matchingMementos = filterMementosByUrl($foundMementos, $url);
// Deduplicate (as a file may be accessible both through a public and a private URL)
$matchingMementos = deduplicateMementos($matchingMementos);
// Sort them by date.
$matchingMementos = sortMementos($matchingMementos);
return $matchingMementos;
}

function filterMementosByUrl($mementos, $url) {
@@ -155,6 +154,18 @@ function normaliseUrl($url) {
return $url;
}

function deduplicateMementos($mementos) {
$deduped = [];
$seenIds = [];
foreach ($mementos as $memento) {
if (!array_key_exists($memento['id'], $seenIds)) {
$deduped[] = $memento;
$seenIds[$memento['id']] = null;
}
}
return $deduped;
}

// Sort an array of mementos by their datetime. Oldest first.
function sortMementos($mementos) {
usort($mementos, function ($m1, $m2) { return $m1['datetime'] <=> $m2['datetime']; });
@@ -177,7 +188,8 @@ function extractMementoInfo($file) {
$datetime = getDatetime($headElement);
return [
'originalUrls' => $originalUrls,
'datetime' => $datetime
'datetime' => $datetime,
'id' => $file->getFileInfo()->getId() // for deduplication
];
}



Loading…
Cancel
Save