Skip to content

Commit

Permalink
Support linking by query URI
Browse files Browse the repository at this point in the history
  • Loading branch information
nichtich committed Feb 22, 2022
1 parent aa21982 commit 75b12a9
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 8 deletions.
6 changes: 6 additions & 0 deletions routes/redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ router.get("/([a-z][a-z])/:page([a-z-]+)", (req, res) => {
// Redirec ILC1 URL to its URI
router.get("/ILC/1", (req, res) => res.redirect("/en/node/472"))

// Redirect local vocabulary URIs to URI search
router.get("/en/Format/:id([a-zA-Z0-9_-]+)", (req, res) =>
res.redirect(`/?uri=http://bartoc.org/en/Format/${req.params.id}`))
router.get("/language/:id([a-z]{2,3})", (req, res) =>
res.redirect(`/?uri=http://bartoc.org/language/${req.params.id}`))

// redirect old subject URLs to vocabulary search

for (const [url, uri] of readCsv("./data/eurovoc-ids.csv")) {
Expand Down
70 changes: 70 additions & 0 deletions routes/uri.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import express from "express"
const router = express.Router()

import config from "../config/index.js"

const suffix = (s, prefix) => s.startsWith(prefix) ? s.slice(prefix.length) : null

const conceptView = (id, uri) => `/en/node/${id}?uri=${encodeURIComponent(uri)}#content`

// query uri at base
router.get("/", async (req, res, next) => {
const { uri } = req.query
if (!uri) {
next()
} else {

// BARTOC URI, URL or lookalike
const bartocUri = uri.match('^https?://(www\.)?bartoc.org/(.*)')
if (bartocUri) {
const localPart = bartocUri[2]

// known vocabularies with namespace at bartoc.org
if (localPart.startsWith("en/Format/")) {
res.redirect(conceptView("20000", `http://bartoc.org/${localPart}`))
} else if (localPart.startsWith("language/")) {
res.redirect(conceptView("20287", `http://bartoc.org/${localPart}`))
} else {
// TODO: add ILC as well
// any other page
res.redirect(`/${localPart}`)
}

return
}

// Vocabulary or concept stored in the backend
const dataEndpoint = config.registry._api.api + "data"
const jskos = await config.registry.axios.get(dataEndpoint, { params: { uri } })
.then(res => res[0])
.catch(() => null)
if (jskos) {
const type = jskos.type[0]
if (type === "http://www.w3.org/2004/02/skos/core#ConceptScheme") {
// known vocabulary
const id = suffix(jskos.uri, "http://bartoc.org/en/node/")
if (id) {
res.redirect(`/en/node/${id}`)
return
}
} else if (type === "http://www.w3.org/2004/02/skos/core#Concept") {
// known concept
console.log(jskos.inScheme)
const schemes = jskos.inScheme
.map(s => suffix(s.uri, "http://bartoc.org/en/node/"))
.filter(Boolean)
if (schemes.length) {
res.redirect(conceptView(schemes[0], concept))
return
}
}
}

// TODO: try to match to a known vocabulary namespace
// e.g. http://bartoc.org/api-type/jskos

next()
}
})

export default router
10 changes: 3 additions & 7 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ app.use("/dist/", express.static("dist"))

import redirectsRoute from "./routes/redirects.js"
import apiRoute from "./routes/api.js"
import uriRoute from "./routes/uri.js"
import pageRoute from "./routes/page.js"

// Routes without server-side backend access
app.use(redirectsRoute)
app.use("/api", apiRoute)
app.use(uriRoute)
app.use(pageRoute)

// render HTML page with EJS
Expand Down Expand Up @@ -161,12 +162,7 @@ function conceptPageHandler(prefix) {
}
}


app.get("/en/Format/:id", conceptPageHandler("http://bartoc.org/en/Format/") )

app.get("/language/:id([a-z]{2,3})", conceptPageHandler("https://bartoc.org/language/") )

// FIXME: ILC is not in the BARTOC registry yet
// TODO: ILC is not in the BARTOC registry yet
app.get("/ILC/1/:id([a-z0-9-]+)", conceptPageHandler("https://bartoc.org/ILC/1/") )

// list of terminology registries
Expand Down
13 changes: 12 additions & 1 deletion views/404.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<%- include('header') %>

<p>Sorry, the resource could not be found!</p>
<p>
Sorry, the resource could not be found!
Are you <a href="/vocabularies">looking for vocabularies</a>?
</p>
<p>
<p>
Search by URI:
<form action="/" method="get">
<input type="text" name="uri" />
<input type="submit" />
</form>
</p>

<%- include('footer') %>

0 comments on commit 75b12a9

Please sign in to comment.