Skip to content

Commit

Permalink
start integrating object linking
Browse files Browse the repository at this point in the history
  • Loading branch information
pjc09h committed Jun 8, 2024
1 parent 50fdf31 commit edca738
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 1,300 deletions.
4 changes: 2 additions & 2 deletions app/Api/Internal.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public static function autocomplete(): void

$get = \Gazelle\Http::get();
$get["query"] ??= null;
$get["searchContext"] ??= null;
$get["context"] ??= null;

try {
$autocomplete = new \Gazelle\Autocomplete();
$response = $autocomplete->fetch($get["query"], $get["searchContext"]);
$response = $autocomplete->fetch($get["query"], $get["context"]);

self::success(200, $response);
} catch (\Throwable $e) {
Expand Down
46 changes: 44 additions & 2 deletions app/Autocomplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Autocomplete

# cache settings
private string $cachePrefix = "autocomplete:";
private string $cacheDuration = "1 hour";
private string $cacheDuration = "5 minutes";
private string $cacheAlgorithm = "sha3-512";


Expand Down Expand Up @@ -89,7 +89,28 @@ private function formatOpenAlexResponse(array $response): array
*/
public function fetch(string $query, string $context, bool $fetchRemote = true): array
{
return $this->$context($query, $fetchRemote);
$app = App::go();

$cacheKey = hash($this->cacheAlgorithm, $this->cachePrefix . __FUNCTION__ . json_encode(func_get_args()));
$cacheHit = $app->cache->get($cacheKey);

if ($cacheHit) {
return $cacheHit;
}

$data = match ($context) {
"collages" => $this->collages($query, $fetchRemote),
"creators" => $this->creators($query, $fetchRemote),
"literature" => $this->literature($query, $fetchRemote),
"organizations" => $this->organizations($query, $fetchRemote),
"publications" => $this->publications($query, $fetchRemote),
"requests" => $this->requests($query, $fetchRemote),
"torrentGroups" => $this->torrentGroups($query, $fetchRemote),
default => [],
};

$app->cache->set($cacheKey, $data, $this->cacheDuration);
return $data;
}


Expand Down Expand Up @@ -169,6 +190,27 @@ public function organizations(string $query, bool $fetchRemote = true): array
}


/**
* publications
*
* @param string $query
* @param bool $fetchRemote
* @return array
*/
public function publications(string $query, bool $fetchRemote = true): array
{
$localResults = $this->callManticore("publications", $query);
if (!$fetchRemote) {
return $localResults;
}

$remoteResults = $this->openAlex->search("sources", $query);
$formattedResults = $this->formatOpenAlexResponse($remoteResults);

return array_merge($localResults, $formattedResults);
}


/**
* requests
*
Expand Down
52 changes: 35 additions & 17 deletions app/Manticore.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ class Manticore
"creators" => ["creators_main", "creators_delta"],
"literature" => ["literature_main", "literature_delta"],
"organizations" => ["organizations_main", "organizations_delta"],
"publications" => ["publications_main", "publications_delta"],
"requests" => ["requests_main", "requests_delta"],
"torrentGroups" => ["torrents_group_main", "torrents_group_delta"],
];


# map of search form fields => index fields
private array $fieldMaps = [
# collages
Expand All @@ -66,8 +66,8 @@ class Manticore

# creators
"creators" => [
"simpleSearch" => ["creators_orcid", "creators_semanticScholarId", "creators_name", "creators_slug", "creators_description", "creators_aliases", "creators_affiliations", "creators_homepage", "creators_picture", "creators_hIndex", "creators_paperCount", "creators_citationCount", "creators_failCount", "creators_degreesOfSeparation", "creators_createdAt", "creators_updatedAt", "creators_deletedAt"],
"complexSearch" => ["creators_orcid", "creators_semanticScholarId", "creators_name", "creators_slug", "creators_aliases"],
"simpleSearch" => ["creators_userId", "creators_openAlexId", "creators_orcid", "creators_scopusId", "creators_semanticScholarId", "creators_name", "creators_slug", "creators_biography", "creators_aliases", "creators_affiliations", "creators_homepage", "creators_picture", "creators_wikipedia", "creators_hIndex", "creators_paperCount", "creators_citationCount", "creators_summaryStats", "creators_affiliationsOverTime", "creators_topics", "creators_concepts", "creators_countsByYear", "creators_degreesOfSeparation", "creators_failCount", "creators_updatedById", "creators_createdAt", "creators_updatedAt", "creators_deletedAt"],
"complexSearch" => ["creators_openAlexId", "creators_orcid", "creators_scopusId", "creators_semanticScholarId", "creators_name", "creators_slug", "creators_aliases"],
"orderBy" => [
"random" => "rand()",
"createdAt" => "id",
Expand All @@ -80,8 +80,8 @@ class Manticore

# literature
"literature" => [
"simpleSearch" => ["literature_userId", "literature_doi", "literature_semanticScholarId", "literature_title", "literature_venue", "literature_journal", "literature_year", "literature_publicationDate", "literature_abstract", "literature_tldr", "literature_bibtex", "literature_influentialCitationCount", "literature_citationCount", "literature_referenceCount", "literature_isOpenAccess", "literature_openAccessPdf", "literature_failCount", "literature_degreesOfSeparation", "literature_createdAt", "literature_updatedAt", "literature_deletedAt"],
"complexSearch" => ["literature_doi", "literature_semanticScholarId", "literature_title", "literature_abstract", "literature_bibtex"],
"simpleSearch" => ["literature_userId", "literature_doi", "literature_openAlexId", "literature_semanticScholarId", "literature_title", "literature_slug", "literature_language", "literature_type", "literature_journal", "literature_bibtex", "literature_publicationDate", "literature_license", "literature_abstract", "literature_tldr", "literature_picture", "literature_primaryTopic", "literature_concepts", "literature_countsByYear", "literature_influentialCitationCount", "literature_citationCount", "literature_referenceCount", "literature_isOpenAccess", "literature_openAccessPdf", "literature_isRetracted", "literature_degreesOfSeparation", "literature_failCount", "literature_updatedById", "literature_createdAt", "literature_updatedAt", "literature_deletedAt"],
"complexSearch" => ["literature_doi", "literature_openAlexId", "literature_semanticScholarId", "literature_title", "literature_slug", "literature_bibtex", "literature_abstract"],
"orderBy" => [
"random" => "rand()",
"createdAt" => "id",
Expand All @@ -95,8 +95,8 @@ class Manticore

# organizations
"organizations" => [
"simpleSearch" => ["organizations_rorId", "organizations_grid", "organizations_userId", "organizations_name", "organizations_acronym", "organizations_established", "organizations_status", "organizations_relationships", "organizations_latitude", "organizations_longitude", "organizations_reverseGeocode", "organizations_country", "organizations_state", "organizations_city", "organizations_postalCode", "organizations_type", "organizations_homepage", "organizations_wikipedia", "organizations_failCount", "organizations_degreesOfSeparation", "organizations_createdAt", "organizations_updatedAt", "organizations_deletedAt"],
"complexSearch" => ["organizations_rorId", "organizations_grid", "organizations_name", "organizations_acronym"],
"simpleSearch" => ["organizations_userId", "organizations_grid", "organizations_openAlexId", "organizations_rorId", "organizations_wikidataId", "organizations_name", "organizations_slug", "organizations_acronym", "organizations_established", "organizations_status", "organizations_relationships", "organizations_repositories", "organizations_latitude", "organizations_longitude", "organizations_reverseGeocode", "organizations_country", "organizations_state", "organizations_city", "organizations_postalCode", "organizations_type", "organizations_homepage", "organizations_picture", "organizations_wikipedia", "organizations_summaryStats", "organizations_countsByYear", "organizations_topics", "organizations_concepts", "organizations_degreesOfSeparation", "organizations_failCount", "organizations_updatedById", "organizations_createdAt", "organizations_updatedAt", "organizations_deletedAt"],
"complexSearch" => ["organizations_grid", "organizations_openAlexId", "organizations_rorId", "organizations_wikidataId", "organizations_name", "organizations_slug", "organizations_acronym", "organizations_reverseGeocode"],
"orderBy" => [
"random" => "rand()",
"createdAt" => "id",
Expand All @@ -107,10 +107,25 @@ class Manticore
],
],

# publications
"publications" => [
"simpleSearch" => ["publications_userId", "publications_issn", "publications_openAlexId", "publications_wikidataId", "publications_title", "publications_slug", "publications_homepage", "publications_picture", "publications_isOpenAccess", "publications_currentDoiCount", "publications_backfileDoiCount", "publications_totalDoiCount", "publications_summaryStats", "publications_topics", "publications_concepts", "publications_coverage", "publications_flags", "publications_countsByYear", "publications_doisIssuedByYear", "publications_degreesOfSeparation", "publications_failCount", "publications_updatedById", "publications_createdAt", "publications_updatedAt", "publications_deletedAt"],
"complexSearch" => ["publications_issn", "publications_openAlexId", "publications_wikidataId", "publications_title", "publications_slug"],
"orderBy" => [
"random" => "rand()",
"createdAt" => "id",
"alphabetical" => "organizations_name",
"established" => "organizations_established",
"status" => "organizations_status",
"type" => "organizations_type",
],
],


# requests
"requests" => [
"simpleSearch" => ["requests_categoryId", "requests_userId", "requests_groupId", "requests_torrentId", "requests_filledById", "requests_filledAt", "requests_lastVote", "requests_identifier", "requests_title", "requests_slug", "requests_subject", "requests_object", "requests_description", "requests_picture", "requests_createdAt", "requests_updatedAt", "requests_deletedAt"],
"complexSearch" => ["requests_identifier", "requests_title", "requests_slug", "requests_subject", "requests_object"],
"complexSearch" => ["requests_identifier", "requests_title", "requests_slug", "requests_subject", "requests_object", "requests_description"],
"orderBy" => [
"random" => "rand()",
"createdAt" => "id",
Expand All @@ -124,8 +139,8 @@ class Manticore

# torrentGroups
"torrentGroups" => [
"simpleSearch" => ["torrentGroups_categoryId", "torrentGroups_revisionId", "torrentGroups_identifier", "torrentGroups_title", "torrentGroups_slug", "torrentGroups_subject", "torrentGroups_object", "torrentGroups_workgroup", "torrentGroups_location", "torrentGroups_year", "torrentGroups_description", "torrentGroups_picture", "torrentGroups_tags", "torrentGroups_createdAt", "torrentGroups_updatedAt", "torrentGroups_deletedAt"],
"complexSearch" => ["torrentGroups_identifier", "torrentGroups_title", "torrentGroups_slug", "torrentGroups_subject", "torrentGroups_object"],
"simpleSearch" => ["torrentGroups_id", "torrentGroups_categoryId", "torrentGroups_revisionId", "torrentGroups_identifier", "torrentGroups_title", "torrentGroups_slug", "torrentGroups_subject", "torrentGroups_object", "torrentGroups_workgroup", "torrentGroups_location", "torrentGroups_year", "torrentGroups_description", "torrentGroups_picture", "torrentGroups_tags", "torrentGroups_createdAt", "torrentGroups_updatedAt", "torrentGroups_deletedAt"],
"complexSearch" => ["torrentGroups_identifier", "torrentGroups_title", "torrentGroups_slug", "torrentGroups_subject", "torrentGroups_object", "torrentGroups_description"],
"orderBy" => [
"random" => "rand()",
"createdAt" => "id",
Expand All @@ -139,14 +154,14 @@ class Manticore

# shared
"shared" => [
"creators" => ["creators_id", "creators_orcid", "creators_semanticScholarId", "creators_name", "creators_slug", "creators_aliases"],
"literature" => ["literature_id", "literature_doi", "literature_semanticscholarid", "literature_title", "literature_journal", "literature_bibtex"],
"creators" => ["creators_openAlexId", "creators_orcid", "creators_scopusId", "creators_semanticScholarId", "creators_name", "creators_slug", "creators_aliases"],
"literature" => ["literature_doi", "literature_openAlexId", "literature_semanticScholarId", "literature_title", "literature_slug", "literature_bibtex", "literature_abstract"],

"workgroups" => ["torrentGroups_workgroup", "creators_affiliations", "organizations_id", "organizations_rorId", "organizations_grid", "organizations_name", "organizations_acronym"],
"workgroups" => ["torrentGroups_workgroup", "creators_affiliations", "creators_affiliationsOverTime", "organizations_grid", "organizations_openAlexId", "organizations_rorId", "organizations_wikidataId", "organizations_name", "organizations_slug", "organizations_acronym", "organizations_reverseGeocode"],
"locations" => ["torrentGroups_location", "organizations_latitude", "organizations_longitude", "organizations_reverseGeocode", "organizations_country", "organizations_state", "organizations_city", "organizations_postalCode"],

"descriptions" => ["torrentGroups_description", "collages_description", "creators_description", "literature_abstract", "requests_description", "torrents_description"],
"files" => ["torrents_id", "torrents_infoHash", "torrents_filePath", "torrents_fileList"],
"files" => ["torrents_infoHash", "torrents_filePath", "torrents_fileList"],
"leechStatus" => ["torrents_upMultiplier", "torrents_downMultiplier", "torrents_freeleechStatus", "torrents_freeleechType"],

"numbers" => ["torrentGroups_identifier", "torrents_version"],
Expand Down Expand Up @@ -238,9 +253,11 @@ public function __construct(string $context = "torrentGroups")
*/
public function setContext(string $context): self
{
/*
if (!in_array($context, array_keys($this->indexMaps))) {
throw new Exception("expected one of " . implode(", ", array_keys($this->indexMaps)) . ", got {$context}");
}
*/

$this->context = $context;
return $this;
Expand Down Expand Up @@ -485,12 +502,13 @@ public function autocomplete(string $query)

# set the properties to match
$properties = match ($this->context) {
"collages" => ["collages_title"],
"collages" => ["collages_title", "collages_deletedAt"],
"creators" => ["creators_name", "creators_openAlexId"],
"literature" => ["literature_title", "literature_openAlexId"],
"organizations" => ["organizations_name", "organizations_openAlexId"],
"requests" => ["requests_title", "requests_identifier"],
"torrentGroups" => ["torrentGroups_title", "torrentGroups_identifier"],
"publications" => ["publications_title", "publications_openAlexId"],
"requests" => ["requests_title", "requests_deletedAt"],
"torrentGroups" => ["torrentGroups_title", "torrentGroups_deletedAt"],
default => throw new Exception("bad context"),
};

Expand Down
4 changes: 2 additions & 2 deletions app/Models/ObjectCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function update(array $data = []): void

# does the object exist?
if (!$this->exists($this->id)) {
throw new Exception("can't update on {$this->type} with the id {$this->id}");
throw new Exception("can't update {$this->type} with the id {$this->id}");
}

# map display => database
Expand Down Expand Up @@ -207,7 +207,7 @@ public function delete(): void

# does the object exist?
if (!$this->exists($this->id)) {
throw new Exception("can't delete from {$this->type} with the id {$this->id}");
throw new Exception("can't delete {$this->type} with the id {$this->id}");
}

# determine the identifier
Expand Down
1 change: 1 addition & 0 deletions app/RecursiveCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function __construct(mixed $input = [])
public function __get(mixed $key): mixed
{
return $this->get($key);
#return $this->get($key)->lazy();
}


Expand Down
1 change: 1 addition & 0 deletions resources/scss/global/forms.scss
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
.tt-open {
border-radius: 4px;
box-shadow: 0.25rem 0.25rem 0.5rem rgba(0, 0, 0, 0.25);
width: 100%;
}

.typeaheadResult {
Expand Down
8 changes: 4 additions & 4 deletions routes/internal.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

# autocomplete
Flight::get("/api/internal/autocomplete", ["Gazelle\Api\Internal", "autocomplete"]);
Flight::route("/api/internal/autocomplete", ["Gazelle\Api\Internal", "autocomplete"]);


# 2fa (totp)
Expand All @@ -17,17 +17,17 @@


# webauthn (fido2)
Flight::get("/api/internal/webAuthn/creationRequest", ["Gazelle\Api\Internal", "webAuthnCreationRequest"]);
Flight::route("/api/internal/webAuthn/creationRequest", ["Gazelle\Api\Internal", "webAuthnCreationRequest"]);
Flight::post("/api/internal/webAuthn/creationResponse", ["Gazelle\Api\Internal", "webAuthnCreationResponse"]);

Flight::get("/api/internal/webAuthn/assertionRequest/@username", ["Gazelle\Api\Internal", "webAuthnAssertionRequest"]);
Flight::route("/api/internal/webAuthn/assertionRequest/@username", ["Gazelle\Api\Internal", "webAuthnAssertionRequest"]);
Flight::post("/api/internal/webAuthn/assertionResponse", ["Gazelle\Api\Internal", "webAuthnAssertionResponse"]);

Flight::post("/api/internal/webAuthn/delete", ["Gazelle\Api\Internal", "deleteWebAuthn"]);


# suggest a passphrase
Flight::get("/api/internal/createPassphrase", ["Gazelle\Api\Internal", "createPassphrase"]);
Flight::route("/api/internal/createPassphrase", ["Gazelle\Api\Internal", "createPassphrase"]);


# manage bookmarks
Expand Down
Loading

0 comments on commit edca738

Please sign in to comment.