File size: 22,436 Bytes
0b82dd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
"""
re3data.py β€” supplementary repository discovery from the re3data.org registry.

WHY THIS EXISTS
---------------
The curated KU Library registry (repositories.py) stays the primary source:
the model selects from it by id and every displayed fact comes from a
reviewed record. That registry is intentionally small, so a dataset in a
narrower field may only receive generalist suggestions.

This module adds a clearly separated "more options" list from re3data.org,
the Registry of Research Data Repositories (DataCite). It never changes the
curated recommendations, the deposit strategy, or Khazna's position.

EVIDENCE RULES
--------------
* Facts shown for these entries come only from the re3data record, labelled
  as such, with the record's own last-update date. They are not reviewed by
  KU Library and are never merged into curated entries.
* Repositories already in the curated registry, repositories with an end
  date (closed), repositories whose data upload is closed, and purely
  institutional repositories (usually restricted to their own staff) are
  excluded.
* Only the researcher-entered *discipline* is sent to re3data, never the
  dataset title, description or notes, which may be unpublished.

FAILURE MODEL
-------------
Every network, parsing or validation failure returns an "unavailable"
status and an empty list. Nothing here may raise into the route.
"""
from __future__ import annotations

import asyncio
import logging
import os
import re
import time
from collections import OrderedDict
from datetime import datetime, timezone
from typing import Any, Dict, Iterable, List, Optional, Tuple
from urllib.parse import urlparse
import xml.etree.ElementTree as ET

import httpx

logger = logging.getLogger(__name__)

API_HOST = "www.re3data.org"
SEARCH_URL = f"https://{API_HOST}/api/beta/repositories"
DETAIL_URL = f"https://{API_HOST}/api/v1/repository/{{rid}}"
RECORD_URL = f"https://{API_HOST}/repository/{{rid}}"
UA = "ResearchBee/1.8.3 (mailto:library@ku.ac.ae)"

ID_RE = re.compile(r"^r3d\d{9}$")
MAX_RESPONSE_BYTES = 2_000_000
MAX_DETAIL_FETCHES = 20
DETAIL_CONCURRENCY = 6
SEARCH_TTL = 24 * 3600
FAILURE_TTL = 10 * 60
DETAIL_TTL = 7 * 24 * 3600
CACHE_MAX = 256
DETAIL_CACHE_MAX = 1024

_STOPWORDS = {
    "and", "of", "the", "in", "for", "or", "a", "an", "to", "with", "on", "data",
    "dataset", "datasets", "research", "science", "sciences", "studies", "study",
    "general", "applied", "other", "misc", "miscellaneous",
}

_http_client: Optional[httpx.AsyncClient] = None
_search_cache: "OrderedDict[str, Tuple[float, Any]]" = OrderedDict()
_detail_cache: "OrderedDict[str, Tuple[float, Optional[dict]]]" = OrderedDict()
_inflight: Dict[str, asyncio.Future] = {}
_last_success: Optional[str] = None
_last_error: Optional[dict] = None


def enabled() -> bool:
    return (os.getenv("RE3DATA_ENABLED", "true").strip().lower() not in {"0", "false", "no", "off"})


def _now_iso() -> str:
    return datetime.now(timezone.utc).isoformat(timespec="seconds")


def status() -> dict:
    return {
        "enabled": enabled(),
        "last_success": _last_success,
        "last_error": _last_error,
        "availability": (
            "disabled" if not enabled()
            else "not_checked" if _last_success is None and _last_error is None
            else "degraded" if _last_error else "available"
        ),
    }


# ── HTTP client lifecycle ─────────────────────────────────────────────────

async def _enforce_origin(request: httpx.Request) -> None:
    url = request.url
    if url.scheme != "https" or url.host != API_HOST or url.port not in (None, 443):
        raise ValueError("re3data client refuses non-provider origins")


def init_client() -> None:
    global _http_client
    if _http_client is None:
        _http_client = httpx.AsyncClient(
            timeout=httpx.Timeout(6.0, connect=4.0),
            headers={"User-Agent": UA, "Accept": "application/xml, text/xml"},
            follow_redirects=False,
            event_hooks={"request": [_enforce_origin]},
        )


async def close_client() -> None:
    global _http_client
    if _http_client is not None:
        await _http_client.aclose()
        _http_client = None


def _client() -> httpx.AsyncClient:
    if _http_client is None:
        init_client()
    return _http_client  # type: ignore[return-value]


# ── Bounded caches ────────────────────────────────────────────────────────

def _cache_get(cache: OrderedDict, key: str):
    item = cache.get(key)
    if not item:
        return None
    expires, value = item
    if expires < time.monotonic():
        cache.pop(key, None)
        return None
    cache.move_to_end(key)
    return item


def _cache_put(cache: OrderedDict, key: str, value: Any, ttl: float, limit: int) -> None:
    cache[key] = (time.monotonic() + ttl, value)
    cache.move_to_end(key)
    while len(cache) > limit:
        cache.popitem(last=False)


def clear_caches() -> None:
    _search_cache.clear()
    _detail_cache.clear()
    _inflight.clear()


# ── XML helpers (namespace-agnostic; the schema namespace has changed) ────

def _local(tag: Any) -> str:
    return tag.rsplit("}", 1)[-1] if isinstance(tag, str) else ""


def _safe_parse(content: bytes) -> Optional[ET.Element]:
    if not content or len(content) > MAX_RESPONSE_BYTES:
        return None
    head = content[:4096].upper()
    # re3data never needs a DTD; refusing one blocks entity-expansion tricks.
    if b"<!DOCTYPE" in head or b"<!ENTITY" in content.upper():
        return None
    try:
        return ET.fromstring(content)
    except ET.ParseError:
        return None


def _children(node: ET.Element, name: str) -> List[ET.Element]:
    return [child for child in list(node) if _local(child.tag) == name]


def _text(node: Optional[ET.Element]) -> str:
    if node is None:
        return ""
    return " ".join("".join(node.itertext()).split())


def _first_text(node: ET.Element, name: str) -> str:
    for child in _children(node, name):
        value = _text(child)
        if value:
            return value
    return ""


def _all_text(node: ET.Element, name: str, limit: int = 20, width: int = 200) -> List[str]:
    out: List[str] = []
    for child in node.iter():
        if _local(child.tag) == name:
            value = _text(child)[:width]
            if value and value not in out:
                out.append(value)
            if len(out) >= limit:
                break
    return out


def _https_url(value: str) -> str:
    value = (value or "").strip()
    try:
        parsed = urlparse(value)
    except ValueError:
        return ""
    if parsed.scheme != "https" or not parsed.hostname or "." not in parsed.hostname:
        return ""
    if parsed.username or parsed.password or (parsed.port not in (None, 443)):
        return ""
    return value[:500]


def _host(value: str) -> str:
    try:
        host = (urlparse(value).hostname or "").lower()
    except ValueError:
        return ""
    return host[4:] if host.startswith("www.") else host


def parse_search(content: bytes) -> List[dict]:
    """Parse a search/list response into [{"id", "name", "doi"}]."""
    root = _safe_parse(content)
    if root is None:
        return []
    out: List[dict] = []
    seen = set()
    for node in root.iter():
        if _local(node.tag) != "repository":
            continue
        rid = _first_text(node, "id")
        if not ID_RE.match(rid) or rid in seen:
            continue
        seen.add(rid)
        out.append({
            "id": rid,
            "name": _first_text(node, "name")[:300],
            "doi": _first_text(node, "doi")[:200],
        })
    return out


def _yes_no_unknown(value: str) -> str:
    value = (value or "").strip().lower()
    if value == "yes":
        return "Yes"
    if value == "no":
        return "No"
    return "Unknown"


_SUBJECT_CODE = re.compile(r"^\s*\d+\s+")


def parse_detail(content: bytes) -> Optional[dict]:
    """Parse one re3data repository record. Returns None if unusable."""
    root = _safe_parse(content)
    if root is None:
        return None
    repo = root if _local(root.tag) == "repository" else next(
        (node for node in root.iter() if _local(node.tag) == "repository"), None
    )
    if repo is None:
        return None
    rid = _first_text(repo, "re3data.orgIdentifier")
    name = _first_text(repo, "repositoryName")
    if not ID_RE.match(rid) or not name:
        return None

    subjects: List[str] = []
    for value in _all_text(repo, "subject", limit=30):
        clean = _SUBJECT_CODE.sub("", value).strip()
        if clean and clean not in subjects:
            subjects.append(clean)

    pid_systems = [p for p in _all_text(repo, "pidSystem", limit=8, width=40) if p.lower() != "none"]
    upload_types = [v.lower() for v in _all_text(repo, "dataUploadType", limit=5, width=30)]
    access_types = [v.lower() for v in _all_text(repo, "dataAccessType", limit=5, width=30)]
    description = _first_text(repo, "description")
    if len(description) > 600:
        description = description[:597].rsplit(" ", 1)[0] + "…"

    return {
        "id": rid,
        "name": name[:300],
        "url": _https_url(_first_text(repo, "repositoryURL")),
        "description": description,
        "types": [t.lower() for t in _all_text(repo, "type", limit=5, width=40)],
        "subjects": subjects[:10],
        "keywords": _all_text(repo, "keyword", limit=15, width=80),
        "content_types": [_SUBJECT_CODE.sub("", c) for c in _all_text(repo, "contentType", limit=10, width=120)],
        "pid_systems": pid_systems,
        "versioning": _yes_no_unknown(_first_text(repo, "versioning")),
        "data_upload": upload_types,
        "data_access": access_types,
        "certificates": _all_text(repo, "certificate", limit=5, width=80),
        "countries": _all_text(repo, "institutionCountry", limit=5, width=10),
        "end_date": _first_text(repo, "endDate")[:30],
        "last_update": _first_text(repo, "lastUpdate")[:30],
    }


# ── Network operations ────────────────────────────────────────────────────

class Re3dataUnavailable(RuntimeError):
    pass


async def _get(url: str, params: Optional[dict] = None) -> bytes:
    global _last_success, _last_error
    try:
        response = await _client().get(url, params=params)
    except (httpx.HTTPError, ValueError) as exc:
        _last_error = {"type": type(exc).__name__, "at": _now_iso()}
        raise Re3dataUnavailable() from None
    if response.status_code != 200:
        _last_error = {"status": response.status_code, "at": _now_iso()}
        raise Re3dataUnavailable()
    content = response.content
    if len(content) > MAX_RESPONSE_BYTES:
        _last_error = {"type": "ResponseTooLarge", "at": _now_iso()}
        raise Re3dataUnavailable()
    _last_success = _now_iso()
    _last_error = None
    return content


async def search(query: str) -> List[dict]:
    """Search re3data; cached. Raises Re3dataUnavailable on failure."""
    key = " ".join(query.lower().split())[:120]
    cached = _cache_get(_search_cache, key)
    if cached is not None:
        value = cached[1]
        if isinstance(value, Exception):
            raise Re3dataUnavailable()
        return value
    pending = _inflight.get("s:" + key)
    if pending is not None:
        return await asyncio.shield(pending)

    loop = asyncio.get_running_loop()
    future: asyncio.Future = loop.create_future()
    _inflight["s:" + key] = future
    try:
        content = await _get(SEARCH_URL, params={"query": key})
        hits = parse_search(content)
        _cache_put(_search_cache, key, hits, SEARCH_TTL, CACHE_MAX)
        future.set_result(hits)
        return hits
    except Exception as exc:
        _cache_put(_search_cache, key, Re3dataUnavailable(), FAILURE_TTL, CACHE_MAX)
        future.set_exception(Re3dataUnavailable())
        future.exception()  # mark retrieved; concurrent waiters still see it
        raise Re3dataUnavailable() from None
    finally:
        _inflight.pop("s:" + key, None)


async def detail(rid: str) -> Optional[dict]:
    """Fetch one record; cached. Returns None when unavailable."""
    if not ID_RE.match(rid or ""):
        return None
    cached = _cache_get(_detail_cache, rid)
    if cached is not None:
        return cached[1]
    try:
        content = await _get(DETAIL_URL.format(rid=rid))
    except Re3dataUnavailable:
        _cache_put(_detail_cache, rid, None, FAILURE_TTL, DETAIL_CACHE_MAX)
        return None
    record = parse_detail(content)
    _cache_put(_detail_cache, rid, record, DETAIL_TTL if record else FAILURE_TTL, DETAIL_CACHE_MAX)
    return record


# ── Selection ─────────────────────────────────────────────────────────────

def _tokens(text: str) -> List[str]:
    out: List[str] = []
    for token in re.findall(r"[a-z0-9]+", (text or "").lower()):
        if len(token) > 2 and token not in _STOPWORDS and token not in out:
            out.append(token)
    return out


def _stem(token: str) -> str:
    for suffix in ("ies", "es", "s"):
        if token.endswith(suffix) and len(token) - len(suffix) >= 4:
            return token[: -len(suffix)] + ("y" if suffix == "ies" else "")
    return token


def _matches(tokens: Iterable[str], text: str) -> List[str]:
    words = {_stem(w) for w in re.findall(r"[a-z0-9]+", (text or "").lower())}
    return [t for t in tokens if _stem(t) in words]


def build_query(discipline: str) -> str:
    """Only the user's discipline field is sent; bounded and normalised."""
    text = " ".join(str(discipline or "").split())
    text = re.sub(r"[^\w\s\-&,/()]", " ", text, flags=re.UNICODE)
    return " ".join(text.split())[:100]


def _is_sensitive(constraints: dict) -> bool:
    sensitivity = str(constraints.get("sensitivity") or "").strip().lower()
    personal = str(constraints.get("contains_personal_data") or "").strip().lower()
    return (bool(sensitivity) and sensitivity not in {"open", "none", "non-sensitive", "not sensitive"}) or (
        bool(personal) and personal != "no"
    )


def _excluded_by_registry(record: dict, curated: Iterable[dict]) -> bool:
    name = " ".join(record.get("name", "").lower().split())
    host = _host(record.get("url", ""))
    for entry in curated:
        entry_name = " ".join(str(entry.get("name", "")).lower().split())
        if name and (name == entry_name or (len(entry_name) > 4 and entry_name in name)):
            return True
        entry_host = _host(str(entry.get("url", "")))
        if host and entry_host and (host == entry_host or host.endswith("." + entry_host)):
            return True
    return False


def score_record(record: dict, tokens: List[str]) -> Tuple[int, List[str]]:
    """Relevance from subject/keyword/name evidence plus deposit-readiness."""
    subject_text = " ".join(record.get("subjects", []) + record.get("keywords", []) + [record.get("name", "")])
    strong = _matches(tokens, subject_text)
    weak = [t for t in _matches(tokens, record.get("description", "")) if t not in strong]
    if not strong:
        return 0, []
    score = 10 * len(strong) + 3 * len(weak)
    pids = {p.lower() for p in record.get("pid_systems", [])}
    if "doi" in pids:
        score += 4
    if record.get("certificates"):
        score += 3
    if "open" in record.get("data_upload", []):
        score += 2
    if record.get("versioning") == "Yes":
        score += 1
    return score, strong + weak


def _public_entry(record: dict, matched: List[str], sensitive: bool) -> dict:
    pids = record.get("pid_systems", [])
    upload = record.get("data_upload", [])
    access = record.get("data_access", [])
    notes = [
        "Listed in re3data.org; not reviewed by KU Library. Confirm deposit eligibility, costs, "
        "licences and file limits on the repository's own site before uploading."
    ]
    if upload and "open" not in upload:
        notes.append("re3data lists data upload as restricted; depositor eligibility must be confirmed.")
    if sensitive:
        notes.append(
            "Your dataset is marked sensitive or personal: KU Library and the responsible ethics/data-protection "
            "team must approve any repository and access conditions before upload."
        )
    last_update = record.get("last_update", "")
    return {
        "id": record["id"],
        "name": record["name"],
        "url": record.get("url", ""),
        "re3data_url": RECORD_URL.format(rid=record["id"]),
        "description": record.get("description", ""),
        "repository_types": record.get("types", []),
        "subjects": record.get("subjects", [])[:6],
        "content_types": record.get("content_types", [])[:6],
        "persistent_identifier": ", ".join(pids) if pids else "Not listed",
        "doi_assignment": "Yes" if any(p.lower() == "doi" for p in pids) else "Not listed",
        "versioning": record.get("versioning", "Unknown"),
        "data_upload": ", ".join(u.capitalize() for u in upload) if upload else "Not listed",
        "data_access": ", ".join(a.capitalize() for a in access) if access else "Not listed",
        "certificates": record.get("certificates", []),
        "countries": record.get("countries", []),
        "last_update": last_update,
        "matched_terms": matched[:6],
        "source": "re3data",
        "source_note": (
            "Details as recorded by re3data.org"
            + (f" (record last updated {last_update[:10]})" if last_update else "")
            + "."
        ),
        "review_notes": notes,
        "file_deposit_recommended": False,
    }


async def find_supplementary(
    discipline: str,
    constraints: Optional[dict] = None,
    curated: Iterable[dict] = (),
    limit: int = 5,
) -> dict:
    """Return {"status", "query", "repositories", "message", "retrieved_at"}.

    Never raises. ``curated`` is the KU registry, used only for de-duplication.
    """
    constraints = constraints if isinstance(constraints, dict) else {}
    query = build_query(discipline)
    base = {"status": "skipped", "query": query, "repositories": [], "message": "",
            "source": "re3data.org", "source_url": "https://www.re3data.org",
            "retrieved_at": _now_iso()}
    if not enabled():
        return {**base, "status": "disabled",
                "message": "re3data search is switched off for this service."}
    tokens = _tokens(query)
    if not tokens:
        return {**base, "message": "Enter a discipline to search re3data for additional repositories."}

    curated = list(curated)
    try:
        hits = await search(query)
        if len(hits) < 3 and len(tokens) > 1:
            # A multi-word discipline can be too narrow for the registry's
            # search; retry once with the most distinctive (longest) word.
            extra = await search(max(tokens, key=len))
            seen = {h["id"] for h in hits}
            hits += [h for h in extra if h["id"] not in seen]
    except Re3dataUnavailable:
        return {**base, "status": "unavailable",
                "message": "re3data.org could not be reached. The curated KU Library recommendations are unaffected."}
    except Exception as exc:  # defensive: never break the repository finder
        logger.warning("[re3data] search failed (%s)", type(exc).__name__)
        return {**base, "status": "unavailable",
                "message": "re3data.org could not be searched. The curated KU Library recommendations are unaffected."}

    if not hits:
        return {**base, "status": "no_matches",
                "message": f'No additional re3data repositories were found for "{query}".'}

    # Fetch the hits most likely to be relevant first: name matches lead.
    ordered = sorted(enumerate(hits), key=lambda item: (-len(_matches(tokens, item[1].get("name", ""))), item[0]))
    candidates = [hit for _i, hit in ordered[:MAX_DETAIL_FETCHES]]
    semaphore = asyncio.Semaphore(DETAIL_CONCURRENCY)

    async def fetch(rid: str) -> Optional[dict]:
        async with semaphore:
            try:
                return await detail(rid)
            except Exception as exc:  # defensive
                logger.warning("[re3data] detail failed (%s)", type(exc).__name__)
                return None

    records = await asyncio.gather(*(fetch(hit["id"]) for hit in candidates))
    sensitive = _is_sensitive(constraints)
    scored: List[Tuple[int, str, dict, List[str]]] = []
    for record in records:
        if not record:
            continue
        if record.get("end_date"):
            continue  # closed repository
        upload = record.get("data_upload", [])
        if upload and all(u == "closed" for u in upload):
            continue  # does not accept deposits
        types = set(record.get("types", []))
        if types and types <= {"institutional"}:
            continue  # usually limited to that institution's own members
        if _excluded_by_registry(record, curated):
            continue
        if sensitive:
            access = set(record.get("data_access", []))
            if not access & {"restricted", "embargoed", "closed"}:
                continue  # no controlled-access route recorded
        score, matched = score_record(record, tokens)
        if score <= 0:
            continue
        scored.append((score, record["name"].lower(), record, matched))

    scored.sort(key=lambda item: (-item[0], item[1]))
    picked = [_public_entry(record, matched, sensitive) for _s, _n, record, matched in scored[: max(0, limit)]]
    if not picked:
        return {**base, "status": "no_matches",
                "message": f're3data returned results for "{query}", but none passed the subject and deposit checks.'}
    return {**base, "status": "ok", "repositories": picked,
            "message": (
                f'{len(picked)} additional repositor{"y" if len(picked) == 1 else "ies"} from re3data.org matched "{query}". '
                "These are registry listings, not KU Library recommendations."
            )}