@museumwnf/viewer-core 2.21.0 → 2.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -157,6 +157,8 @@ What a website's `main.js` still writes, and why it cannot move here:
157
157
  | `media` | `{ legacyHost }` | no | the host of the legacy media server, for [`mediaUrl()`](#the-declaration-outside-a-component) |
158
158
  | `links` | object | no | the addresses a website links out to: `portal`, `galleries`, `myCollection`, `about`, `contact`, `legalNotice`, `credits`, `cookies` |
159
159
  | `site.origin` | string | no | the absolute origin this build is deployed at, base path included when there is one (`https://metanull.github.io/islamicart`, no trailing slash) — declared once here rather than baked into the package, since the package cannot know where it will be served; read by [`sourceUrl()`](#the-declaration-outside-a-component) |
160
+ | `site.slug` | string | no | the site's slug, as in the exporters' instance files; defaults to the last segment of `site.origin`. The global search's link sends it as `from` |
161
+ | `globalSearch` | `true` or `{ url }` | no | **off by default.** Turns on the link to the global search (inventory-app M15, ADR 005): `true` for its default address (`GLOBAL_SEARCH_URL`), `{ url }` for another https address. viewer-layout's header then shows its search box. The parity check turns it on for the sites whose legacy had a similar feature |
160
162
 
161
163
  `dataset.config.js` is the whole declaration of a website. Before it mounts,
162
164
  a website reads nothing from its data package but `manifest.json`; it reads
@@ -579,6 +581,9 @@ below for whichever future site needs its own).
579
581
  | `mediaUrl(path, size = 'hi_res')` | `${config.media.legacyHost}/${size}/${path}` for a legacy media path the package carries; an address is returned as it is |
580
582
  | `useSiteRights()` | `{ holder, termsUrl, attribution }` from the data package's `manifest.rights`; a package with no such block offers no holder or attribution, and `termsUrl` falls back to `mwnfLinks.legalNotice` |
581
583
  | `sourceUrl(route)` | `${config.site.origin}/#${path}` for `route` — a route already resolved (its `fullPath` read as it is) or a raw location the installed router resolves; null when the website declares no `site.origin` |
584
+ | `globalSearchUrl({ q, lang })` | the global search's address for `q` in `lang`, with this site's slug as `from`; null unless the site sets `globalSearch`. It never takes an address from content: the search site accepts only slugs from its own list of sites |
585
+ | `useGlobalSearch()` | `{ enabled, url(q, lang) }` for a component, such as viewer-layout's search box |
586
+ | `siteSlug()` | this site's slug: `site.slug`, else the last segment of `site.origin`; null when neither is a valid slug |
582
587
 
583
588
  ### `useDataPackage()` → data access (the only allowed way to read the data package)
584
589
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@museumwnf/viewer-core",
3
- "version": "2.21.0",
3
+ "version": "2.23.0",
4
4
  "description": "Application engine for MWNF websites: turns a dataset.config.js into a mounted Vue application.",
5
5
  "type": "module",
6
6
  "license": "SEE LICENSE IN LICENSE.md",
@@ -0,0 +1,74 @@
1
+ import { useSiteConfig } from './siteConfig.js'
2
+
3
+ // The global search (inventory-app M15, ADR 005): one search over every site's
4
+ // items, on a site of its own. A site opts in with `globalSearch` in its
5
+ // dataset.config.js, off by default:
6
+ //
7
+ // globalSearch: true // the default address
8
+ // globalSearch: { url: 'https://example.org/search/' } // another address
9
+ //
10
+ // The link carries the text, the language and this site's own slug as `from`,
11
+ // and nothing else: the search site accepts a slug only if it is in its list
12
+ // of sites, never an address, so no parameter can send a visitor elsewhere.
13
+
14
+ /** Where the search site is served, unless a site's config names another. */
15
+ export const GLOBAL_SEARCH_URL = 'https://museumwithnofrontiers.github.io/search/'
16
+
17
+ /** A site's slug: lowercase words joined by single hyphens, as the exporters' instance files. */
18
+ const SLUG = /^[a-z0-9]+(-[a-z0-9]+)*$/
19
+ const LANGUAGE = /^[a-z]{2}$/
20
+
21
+ function searchBase(setting) {
22
+ if (setting === true) return GLOBAL_SEARCH_URL
23
+ if (setting && typeof setting === 'object' && typeof setting.url === 'string') {
24
+ let url
25
+ try {
26
+ url = new URL(setting.url)
27
+ } catch {
28
+ return null
29
+ }
30
+ if (url.protocol !== 'https:' || url.search || url.hash || url.username || url.password) return null
31
+ return url.href.endsWith('/') ? url.href : `${url.href}/`
32
+ }
33
+ return null
34
+ }
35
+
36
+ /**
37
+ * This site's slug: `site.slug` when declared, else the last segment of
38
+ * `site.origin` (every site is served one path segment under a common
39
+ * parent). Null when neither gives a valid slug.
40
+ */
41
+ export function siteSlug(config = useSiteConfig()) {
42
+ const declared = config?.site?.slug
43
+ if (typeof declared === 'string') return SLUG.test(declared) ? declared : null
44
+ const origin = String(config?.site?.origin ?? '').replace(/\/+$/, '')
45
+ const last = origin.slice(origin.lastIndexOf('/') + 1)
46
+ return SLUG.test(last) ? last : null
47
+ }
48
+
49
+ /**
50
+ * The address of the global search for `q` in `lang`, or null when the site
51
+ * has not opted in. Built only from the configured address and the escaped
52
+ * parameters.
53
+ */
54
+ export function globalSearchUrl({ q = '', lang = '' } = {}, config = useSiteConfig()) {
55
+ const base = searchBase(config?.globalSearch)
56
+ if (!base) return null
57
+ const params = new URLSearchParams()
58
+ const text = String(q ?? '').trim()
59
+ if (text) params.set('q', text)
60
+ if (LANGUAGE.test(String(lang))) params.set('lang', String(lang))
61
+ const from = siteSlug(config)
62
+ if (from) params.set('from', from)
63
+ const query = params.toString()
64
+ return `${base}#/${query ? `?${query}` : ''}`
65
+ }
66
+
67
+ /** For a component: whether the site has opted in, and the link builder. */
68
+ export function useGlobalSearch() {
69
+ const config = useSiteConfig()
70
+ return {
71
+ enabled: searchBase(config?.globalSearch) !== null,
72
+ url: (q, lang) => globalSearchUrl({ q, lang }, config),
73
+ }
74
+ }
package/src/index.js CHANGED
@@ -21,6 +21,8 @@ export { mediaUrl, useSiteConfig } from './siteConfig.js'
21
21
  export { bindDisplayLanguage, displayLanguage } from './displayLanguage.js'
22
22
  export { siblingSiteUrl, sourceUrl, useSiteRights } from './rights.js'
23
23
  export { LEGACY_SITES, toNewSite, watchNewSiteLinks, withScheme } from './legacySites.js'
24
+ export { GLOBAL_SEARCH_URL, globalSearchUrl, siteSlug, useGlobalSearch } from './globalSearch.js'
25
+ export { legacyRecordId } from './legacyIds.js'
24
26
 
25
27
  // The list pages: query state, pagination, facets, dates, keyword search.
26
28
  export { useListQuery } from './catalogue/listQuery.js'
@@ -0,0 +1,75 @@
1
+ // The inventory id of a legacy record, from its legacy key (inventory-app#2544).
2
+ // The importer gives every legacy record the same id on every import: a
3
+ // UUIDv5 of `<entity>:<backward_compatibility, lowercased>` in one frozen
4
+ // namespace (inventory-app scripts/importer/src/utils/deterministic-uuid.ts,
5
+ // whose header makes the namespace and the name's shape a contract). A link
6
+ // to a legacy record's page can therefore name the new page's record without
7
+ // reading the other site's package. Synchronous on purpose: a link is
8
+ // rewritten as it is drawn.
9
+
10
+ /** The importer's namespace (`IMPORTER_UUID_NAMESPACE`), frozen there. */
11
+ export const IMPORTER_UUID_NAMESPACE = 'bc112041-0784-4475-80b2-0c96425ac5ea'
12
+
13
+ function utf8(text) {
14
+ return new TextEncoder().encode(text)
15
+ }
16
+
17
+ // SHA-1 (FIPS 180-4) of `bytes`, as 20 bytes. Only UUIDv5 needs it here.
18
+ function sha1(bytes) {
19
+ const length = bytes.length
20
+ const blocks = ((length + 8) >> 6) + 1
21
+ const words = new Uint32Array(blocks * 16)
22
+ for (let i = 0; i < length; i++) words[i >> 2] |= bytes[i] << (24 - (i % 4) * 8)
23
+ words[length >> 2] |= 0x80 << (24 - (length % 4) * 8)
24
+ words[blocks * 16 - 1] = length * 8
25
+ words[blocks * 16 - 2] = Math.floor((length * 8) / 0x100000000)
26
+ let [a, b, c, d, e] = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0]
27
+ const w = new Uint32Array(80)
28
+ const rotl = (x, n) => (x << n) | (x >>> (32 - n))
29
+ for (let block = 0; block < blocks; block++) {
30
+ for (let t = 0; t < 16; t++) w[t] = words[block * 16 + t]
31
+ for (let t = 16; t < 80; t++) w[t] = rotl(w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16], 1)
32
+ let [h0, h1, h2, h3, h4] = [a, b, c, d, e]
33
+ for (let t = 0; t < 80; t++) {
34
+ let f, k
35
+ if (t < 20) [f, k] = [(h1 & h2) | (~h1 & h3), 0x5a827999]
36
+ else if (t < 40) [f, k] = [h1 ^ h2 ^ h3, 0x6ed9eba1]
37
+ else if (t < 60) [f, k] = [(h1 & h2) | (h1 & h3) | (h2 & h3), 0x8f1bbcdc]
38
+ else [f, k] = [h1 ^ h2 ^ h3, 0xca62c1d6]
39
+ const next = (rotl(h0, 5) + f + h4 + k + w[t]) >>> 0
40
+ ;[h4, h3, h2, h1, h0] = [h3, h2, rotl(h1, 30) >>> 0, h0, next]
41
+ }
42
+ a = (a + h0) >>> 0
43
+ b = (b + h1) >>> 0
44
+ c = (c + h2) >>> 0
45
+ d = (d + h3) >>> 0
46
+ e = (e + h4) >>> 0
47
+ }
48
+ const out = new Uint8Array(20)
49
+ ;[a, b, c, d, e].forEach((word, i) => {
50
+ for (let j = 0; j < 4; j++) out[i * 4 + j] = (word >>> (24 - j * 8)) & 0xff
51
+ })
52
+ return out
53
+ }
54
+
55
+ /** RFC 4122 UUIDv5 of `name` in `namespace`. */
56
+ export function uuidV5(name, namespace = IMPORTER_UUID_NAMESPACE) {
57
+ const ns = namespace.replace(/-/g, '').match(/../g).map((hex) => parseInt(hex, 16))
58
+ const nameBytes = utf8(name)
59
+ const input = new Uint8Array(16 + nameBytes.length)
60
+ input.set(ns, 0)
61
+ input.set(nameBytes, 16)
62
+ const hash = sha1(input).slice(0, 16)
63
+ hash[6] = (hash[6] & 0x0f) | 0x50
64
+ hash[8] = (hash[8] & 0x3f) | 0x80
65
+ const hex = Array.from(hash, (byte) => byte.toString(16).padStart(2, '0')).join('')
66
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`
67
+ }
68
+
69
+ /**
70
+ * The inventory id the importer gives the legacy record `key` (its
71
+ * `backward_compatibility`) of `entity` (`publication`, `collection`…).
72
+ */
73
+ export function legacyRecordId(entity, key) {
74
+ return uuidV5(`${entity}:${String(key).toLowerCase()}`)
75
+ }
@@ -1,3 +1,4 @@
1
+ import { legacyRecordId } from './legacyIds.js'
1
2
  import { siblingSiteUrl } from './rights.js'
2
3
 
3
4
  // Links to legacy MWNF sites that have a new one (inventory-app#2482). The
@@ -9,7 +10,8 @@ import { siblingSiteUrl } from './rights.js'
9
10
  //
10
11
  // The estate's legacy addresses, one row a site: a gallery, Explore, the
11
12
  // galleries hub and the three products by host; an exhibition by host and
12
- // its first path segment (they share a host); a mini-site by its own page
13
+ // its first path segment (they share a host); Publications (legacy books)
14
+ // and Travels (legacy's Exhibition Trails) by host; a mini-site by its own page
13
15
  // (a Sharing History one by its `eId`). Built from the parity tool's sitemap
14
16
  // (the legacy dumps) and the exporters' instance files; the parity `links`
15
17
  // pass fails on a link this table misses.
@@ -18,6 +20,7 @@ export const LEGACY_SITES = [
18
20
  { host: 'archaeology.museumwnf.org', slug: 'archaeology', family: 'gallery' },
19
21
  { host: 'architectural-elements.museumwnf.org', slug: 'architectural-elements', family: 'gallery' },
20
22
  { host: 'arms-armoury.museumwnf.org', slug: 'arms-armoury', family: 'gallery' },
23
+ { host: 'books.museumwnf.org', slug: 'publications', family: 'publications' },
21
24
  { host: 'exhibitions.museumwnf.org', path: '/arts_in_dialogue/', slug: 'arts-in-dialogue', family: 'exhibition' },
22
25
  { host: 'baroqueart.museumwnf.org', slug: 'baroqueart', family: 'baroqueart' },
23
26
  { host: 'baroqueart.museumwnf.org', path: '/exhibitions/bar/absolutism/', slug: 'baroqueart-absolutism', family: 'minisite' },
@@ -94,6 +97,7 @@ export const LEGACY_SITES = [
94
97
  { host: 'exhibitions.museumwnf.org', path: '/the_use_of_colours_in_art/', slug: 'the-use-of-colours-in-art', family: 'exhibition' },
95
98
  { host: 'theatre.museumwnf.org', slug: 'theatre', family: 'gallery' },
96
99
  { host: 'toys-games.museumwnf.org', slug: 'toys-games', family: 'gallery' },
100
+ { host: 'travels.museumwnf.org', slug: 'travels', family: 'travels' },
97
101
  { host: 'wallpaintings.museumwnf.org', slug: 'wallpaintings', family: 'gallery' },
98
102
  { host: 'exhibitions.museumwnf.org', path: '/water_in_islam/', slug: 'water-in-islam', family: 'exhibition' },
99
103
  { host: 'weights-measures.museumwnf.org', slug: 'weights-measures', family: 'gallery' },
@@ -138,6 +142,73 @@ const PAGES = {
138
142
  // The query keys a page's new route reads as legacy's did.
139
143
  const KEPT_QUERY = { '/partners/results': ['type'] }
140
144
 
145
+ // Legacy books.museumwnf.org (book-client's routes): a book by its legacy id
146
+ // (`mwnf3:bookbase:<id>`) in the version's language, a category's page, the
147
+ // search (its free-text fields and the language, whose values the new search
148
+ // reads as legacy's did). Its sign-up page has no counterpart.
149
+ const BOOK_SEARCH_KEYS = ['title', 'author', 'isbn', 'language']
150
+ function publicationsRoute(path, url) {
151
+ if (path === '/') return '/'
152
+ let match = /^\/book\/(\d+)(?:\/([a-z]{2}))?$/i.exec(path)
153
+ if (match) {
154
+ const id = legacyRecordId('publication', `mwnf3:bookbase:${match[1]}`)
155
+ return `/publication/${id}${match[2] ? `/${match[2].toLowerCase()}` : ''}`
156
+ }
157
+ match = /^\/category\/([a-z0-9-]+)(?:\/(\d+))?$/i.exec(path)
158
+ if (match) {
159
+ const page = match[2] && match[2] !== '1' ? `?page=${match[2]}` : ''
160
+ return `/category/${match[1].toLowerCase()}${page}`
161
+ }
162
+ if (path === '/results') {
163
+ const kept = BOOK_SEARCH_KEYS.filter((key) => url.searchParams.get(key))
164
+ return kept.length ? `/results?${kept.map((key) => `${key}=${encodeURIComponent(url.searchParams.get(key))}`).join('&')}` : '/results'
165
+ }
166
+ return null
167
+ }
168
+
169
+ // Legacy travels.museumwnf.org's Exhibition Trails: a country's or a theme's
170
+ // trails, a trail (`?id=<project>;<country>;<number>;<language>`) and an
171
+ // itinerary (`?id=<project>;<country>;<roman number>;<language>;<trail>`),
172
+ // keyed as the importer keys them (`mwnf3_travels:trail:…`,
173
+ // `mwnf3_travels:itinerary:…`), each tab (`fl`) the new page has. The rest of
174
+ // the host (the tours, the booking form) has no new site and stays there.
175
+ // The trails' countries: legacy's code → the inventory id travels-data's
176
+ // countries.json gives it.
177
+ const TRAVELS_COUNTRIES = {
178
+ dz: 'dza', eg: 'egy', es: 'esp', it: 'ita', jo: 'jor', ma: 'mar', pa: 'pse', pt: 'prt', sy: 'syr', tn: 'tun', tr: 'tur',
179
+ }
180
+ const TRAIL_TABS = ['desc', 'its', 'cur']
181
+ const ITINERARY_TABS = ['des', 'vl']
182
+ function travelsQuery(url, tabs, language) {
183
+ const tab = (url.searchParams.get('fl') ?? '').toLowerCase()
184
+ const query = [tabs.includes(tab) ? `tab=${tab}` : null, /^[a-z]{2}$/i.test(language ?? '') ? `lang=${language.toLowerCase()}` : null]
185
+ const kept = query.filter(Boolean)
186
+ return kept.length ? `?${kept.join('&')}` : ''
187
+ }
188
+ function travelsRoute(path, url) {
189
+ const script = path.toLowerCase()
190
+ if (script === '/travel_et_home.php') return '/'
191
+ if (script === '/travel_et_trail.php') {
192
+ const country = TRAVELS_COUNTRIES[(url.searchParams.get('cid') ?? '').toLowerCase()]
193
+ if (country) return `/trails?country=${country}`
194
+ const project = url.searchParams.get('tid')
195
+ return /^[a-z0-9]+$/i.test(project ?? '') ? `/trails?project=${project.toUpperCase()}` : null
196
+ }
197
+ const id = (url.searchParams.get('id') ?? '').split(';').map((part) => part.trim())
198
+ if (script === '/travel_et_traildetail.php' && id.length >= 3 && id.slice(0, 3).every(Boolean)) {
199
+ const [project, country, number, language] = id
200
+ return `/trail/${legacyRecordId('collection', `mwnf3_travels:trail:${project}:${country}:${number}`)}${travelsQuery(url, TRAIL_TABS, language)}`
201
+ }
202
+ if (script === '/travel_et_itenary.php' && id.length >= 5 && [0, 1, 2, 4].every((i) => id[i])) {
203
+ const [project, country, number, language, trail] = id
204
+ return `/itinerary/${legacyRecordId('collection', `mwnf3_travels:itinerary:${project}:${country}:${trail}:${number}`)}${travelsQuery(url, ITINERARY_TABS, language)}`
205
+ }
206
+ return null
207
+ }
208
+
209
+ // The families whose pages name a record or a page in their address.
210
+ const RECORD_PAGES = { publications: publicationsRoute, travels: travelsRoute }
211
+
141
212
  const bareHost = (host) => host.toLowerCase().replace(/^www\./, '')
142
213
 
143
214
  function rowFor(url) {
@@ -164,6 +235,7 @@ function routeFor(row, url) {
164
235
  const path = pathname.replace(/\/+$/, '') || '/'
165
236
  if (row.family === 'gallery' || row.family === 'explore' || row.family === 'galleries') return `${path}${url.search}`
166
237
  if (row.family === 'minisite') return '/'
238
+ if (RECORD_PAGES[row.family]) return RECORD_PAGES[row.family](path, url)
167
239
  if (row.family === 'exhibition') {
168
240
  const rest = path.split('/').filter(Boolean).slice(1)
169
241
  const lang = /^[a-z]{2}$/.test(rest[0] ?? '') ? rest.shift() : null