ochre-sdk 1.0.72 → 1.0.74

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.
@@ -1,5 +1,5 @@
1
1
  import { XML_PARSER_OPTIONS } from "../constants.mjs";
2
- import { createSchemaValidationError, getErrorOutput, stringLiteral } from "../utilities.mjs";
2
+ import { SUPPLEMENTAL_XQUERY_PROLOG, createSchemaValidationError, getErrorOutput, omitSupplemental, stringLiteral } from "../utilities.mjs";
3
3
  import { iso639_3Schema } from "../schemas.mjs";
4
4
  import { restoreXMLMetadata } from "../xml/metadata.mjs";
5
5
  import { parseStringLike } from "../parsers/helpers.mjs";
@@ -41,6 +41,8 @@ function parseWebsiteMetadata(data, options) {
41
41
  function buildXQuery(parameters) {
42
42
  return String.raw`xquery version "1.0-ml";
43
43
 
44
+ ${SUPPLEMENTAL_XQUERY_PROLOG}
45
+
44
46
  declare function local:resource-items($resources) {
45
47
  for $resource in $resources
46
48
  return
@@ -131,8 +133,10 @@ return
131
133
  $website/@belongsTo,
132
134
  $website/@publicationDateTime,
133
135
  $website/@languages,
134
- $website/metadata,
135
- local:metadata-tree($website/tree[1], $target-slug, "")
136
+ ${omitSupplemental(`(
137
+ $website/metadata,
138
+ local:metadata-tree($website/tree[1], $target-slug, "")
139
+ )`)}
136
140
  }</ochre>`;
137
141
  }
138
142
  async function fetchWebsiteMetadata(abbreviation, options) {
@@ -1,12 +1,12 @@
1
1
  import { XML_PARSER_OPTIONS } from "../constants.mjs";
2
- import { createSchemaValidationError, getErrorOutput } from "../utilities.mjs";
2
+ import { SUPPLEMENTAL_XQUERY_PROLOG, createSchemaValidationError, getErrorOutput, omitSupplemental, stringLiteral } from "../utilities.mjs";
3
3
  import { restoreXMLMetadata } from "../xml/metadata.mjs";
4
4
  import { XMLWebsiteData } from "../xml/schemas.mjs";
5
5
  import { parseWebsite } from "../parsers/website/index.mjs";
6
6
  import * as v from "valibot";
7
7
  import { XMLParser } from "fast-xml-parser";
8
8
  //#region src/fetchers/website.ts
9
- async function validateWebsiteCredentials(uuid, credentials, fetcher) {
9
+ async function areWebsiteCredentialsValid(uuid, credentials, fetcher) {
10
10
  const security = typeof credentials === "string" ? { validate: credentials } : {
11
11
  validate: credentials.password,
12
12
  userOCHRE: credentials.username
@@ -21,6 +21,20 @@ async function validateWebsiteCredentials(uuid, credentials, fetcher) {
21
21
  })).ok;
22
22
  }
23
23
  /**
24
+ * Build an XQuery string to fetch a website tree document by abbreviation.
25
+ *
26
+ * @param abbreviation - The lowercased website abbreviation to match
27
+ * @returns An XQuery string
28
+ */
29
+ function buildXQuery(abbreviation) {
30
+ return `xquery version "1.0-ml";
31
+
32
+ ${SUPPLEMENTAL_XQUERY_PROLOG}
33
+
34
+ for $ochre in collection("ochre/tree")/ochre[tree/identification/abbreviation/content/string = ${stringLiteral(abbreviation)}]
35
+ return element ochre { $ochre/@*, ${omitSupplemental("$ochre/node()")} }`;
36
+ }
37
+ /**
24
38
  * Fetches and parses a website configuration from the OCHRE API.
25
39
  *
26
40
  * For password-protected or OCHRE-credential-protected websites, if no credentials
@@ -32,8 +46,11 @@ async function validateWebsiteCredentials(uuid, credentials, fetcher) {
32
46
  async function fetchWebsite(abbreviation, options) {
33
47
  try {
34
48
  const fetcher = options?.fetch ?? fetch;
35
- const cleanAbbreviation = abbreviation.trim().toLocaleLowerCase("en-US");
36
- const response = await fetcher(`https://ochre.lib.uchicago.edu/ochre/v2/ochre.php?xquery=${encodeURIComponent(`collection('ochre/tree')/ochre[tree/identification/abbreviation/content/string='${cleanAbbreviation}']`)}&xsl=none&lang="*"`);
49
+ const response = await fetcher("https://ochre.lib.uchicago.edu/ochre/v2/ochre.php?xquery&xsl=none&lang=\"*\"", {
50
+ method: "POST",
51
+ body: buildXQuery(abbreviation.trim().toLocaleLowerCase("en-US")),
52
+ headers: { "Content-Type": "application/xquery" }
53
+ });
37
54
  if (!response.ok) throw new Error("Failed to fetch website", { cause: response.statusText });
38
55
  const dataRaw = await response.text();
39
56
  const data = new XMLParser(XML_PARSER_OPTIONS).parse(dataRaw);
@@ -52,7 +69,7 @@ async function fetchWebsite(abbreviation, options) {
52
69
  error: null,
53
70
  detailedError: null
54
71
  };
55
- if (!await validateWebsiteCredentials(website.uuid, options.credentials, fetcher)) throw new Error("Invalid credentials for protected website");
72
+ if (!await areWebsiteCredentialsValid(website.uuid, options.credentials, fetcher)) throw new Error("Invalid credentials for protected website");
56
73
  }
57
74
  return {
58
75
  website,
@@ -4,9 +4,13 @@ import { LanguageCodes, Property, PropertyLike, PropertyValueContent, SetItemPro
4
4
  * Options for property search operations.
5
5
  */
6
6
  type PropertyOptions = {
7
- /** Whether to recursively search through nested properties. */
7
+ /**
8
+ Whether to recursively search through nested properties.
9
+ */
8
10
  includeNestedProperties?: boolean;
9
- /** Whether to limit property values to leaf values. */
11
+ /**
12
+ Whether to limit property values to leaf values.
13
+ */
10
14
  limitToLeafPropertyValues?: boolean;
11
15
  };
12
16
  type PropertyContent<T extends LanguageCodes> = PropertyValueContent<T>["content"];
@@ -7,10 +7,6 @@ type FlattenedItem<U, T extends LanguageCodes> = Omit<U, "properties"> & {
7
7
  * The default page size to use for fetching paginated items
8
8
  */
9
9
  declare const DEFAULT_PAGE_SIZE = 48;
10
- /**
11
- * The default cap on OCR matches returned per requested item
12
- */
13
- declare const DEFAULT_MAX_OCR_MATCHES_PER_ITEM = 50;
14
10
  /**
15
11
  * Flatten the properties of an item
16
12
  * @param item - The item whose properties to flatten
@@ -18,4 +14,4 @@ declare const DEFAULT_MAX_OCR_MATCHES_PER_ITEM = 50;
18
14
  */
19
15
  declare function flattenItemProperties<U extends ItemCategory = ItemCategory, V extends ContainedItemCategory<U> = ContainedItemCategory<U>, T extends LanguageCodes = LanguageCodes, W extends ItemPayloadKind = "topLevel">(item: Item<U, V, T, W>): FlattenedItem<Item<U, V, T, W>, T>;
20
16
  //#endregion
21
- export { DEFAULT_MAX_OCR_MATCHES_PER_ITEM, DEFAULT_PAGE_SIZE, flattenItemProperties };
17
+ export { DEFAULT_PAGE_SIZE, flattenItemProperties };
package/dist/helpers.mjs CHANGED
@@ -5,10 +5,6 @@ import { flattenProperties } from "./utilities.mjs";
5
5
  */
6
6
  const DEFAULT_PAGE_SIZE = 48;
7
7
  /**
8
- * The default cap on OCR matches returned per requested item
9
- */
10
- const DEFAULT_MAX_OCR_MATCHES_PER_ITEM = 50;
11
- /**
12
8
  * Flatten the properties of an item
13
9
  * @param item - The item whose properties to flatten
14
10
  * @returns The item with the properties flattened
@@ -34,4 +30,4 @@ function flattenItemProperties(item) {
34
30
  };
35
31
  }
36
32
  //#endregion
37
- export { DEFAULT_MAX_OCR_MATCHES_PER_ITEM, DEFAULT_PAGE_SIZE, flattenItemProperties };
33
+ export { DEFAULT_PAGE_SIZE, flattenItemProperties };
package/dist/index.d.mts CHANGED
@@ -1,15 +1,15 @@
1
1
  import { MultilingualOptions, MultilingualString, MultilingualStringEntries, MultilingualStringEntry, MultilingualStringInput, MultilingualStringJSON, MultilingualStringObject, MultilingualStringText } from "./parsers/multilingual.mjs";
2
2
  import { AccordionWebBlock, ContextTree, ContextTreeFilterLevel, ContextTreeLevel, ContextTreeLevelItem, ProtectedWebsite, Scope, Style, StylesheetCategory, StylesheetItem, WebAccordionItem, WebBlock, WebBlockByLayout, WebBlockItem, WebBlockLayout, WebElement, WebElementComponent, WebElementComponentName, WebElementComponentOf, WebElementOf, WebImage, WebSidebar, WebTitle, Webpage, Website, WebsiteMetadata, WebsitePropertyQuery, WebsitePropertyQueryNode, WebsiteSegment, WebsiteType } from "./types/website.mjs";
3
- import { AnyBibliography, AnyConcept, AnyItem, AnyPeriod, AnyPerson, AnyPropertyValue, AnyPropertyVariable, AnyResource, AnySet, AnySpatialUnit, AnyText, AnyTree, BaseItem, BaseItemLink, BelongsTo, Bibliography, BibliographyEntryInfo, BibliographyItemLink, BibliographySourceDocument, Concept, ConceptItemLink, ContainedItemCategory, ContainedItemCategoryFromOption, ContainedItemCategoryOption, Context, ContextItem, ContextItemCategory, ContextNode, Coordinates, CoordinatesSource, DictionaryUnitItemLink, EmbeddedBibliography, EmbeddedConcept, EmbeddedItem, EmbeddedPeriod, EmbeddedPerson, EmbeddedPropertyValue, EmbeddedPropertyVariable, EmbeddedResource, EmbeddedSet, EmbeddedSpatialUnit, EmbeddedText, EmbeddedTree, Event, Gallery, Heading, HeadingItemCategory, Identification, Image, ImageMap, ImageMapArea, Interpretation, Item, ItemCategory, ItemCategoryFromOption, ItemCategoryOption, ItemCategoryWithEmbeddedItems, ItemContainerCategory, ItemLink, ItemLinkCategory, ItemLinks, ItemPayloadKind, ItemProperty, ItemWithoutEmbeddedItems, LanguageCodes, License, Metadata, Note, Observation, OcrMatch, OcrPage, OcrPoint, OcrTextBlock, OcrTextLine, OcrWord, Period, PeriodItemLink, Person, PersonItemLink, Prettify, Property, PropertyLike, PropertyRelation, PropertyValue, PropertyValueContent, PropertyValueDataType, PropertyValueItemLink, PropertyValueQueryItem, PropertyVariable, PropertyVariableItemLink, Query, QueryGroup, QueryLeaf, QueryablePropertyValueDataType, RecursiveItemCategory, Resource, ResourceItemLink, Section, Set, SetAttributeValueQueryItem, SetBibliography, SetConcept, SetItem, SetItemCategory, SetItemLink, SetItemProperty, SetItemSimplifiedProperty, SetItemsSort, SetItemsSortDirection, SetPeriod, SetResource, SetSpatialUnit, SetTree, SimplifiedProperty, SpatialUnit, SpatialUnitItemLink, Text, TextItemLink, TopLevelItem, Tree, TreeItemCategory, TreeItemLink } from "./types/index.mjs";
3
+ import { AnyBibliography, AnyConcept, AnyItem, AnyPeriod, AnyPerson, AnyPropertyValue, AnyPropertyVariable, AnyResource, AnySet, AnySpatialUnit, AnyText, AnyTree, BaseItem, BaseItemLink, BelongsTo, Bibliography, BibliographyEntryInfo, BibliographyItemLink, BibliographySourceDocument, Concept, ConceptItemLink, ContainedItemCategory, ContainedItemCategoryFromOption, ContainedItemCategoryOption, Context, ContextItem, ContextItemCategory, ContextNode, Coordinates, CoordinatesSource, DictionaryUnitItemLink, EmbeddedBibliography, EmbeddedConcept, EmbeddedItem, EmbeddedPeriod, EmbeddedPerson, EmbeddedPropertyValue, EmbeddedPropertyVariable, EmbeddedResource, EmbeddedSet, EmbeddedSpatialUnit, EmbeddedText, EmbeddedTree, Event, Gallery, Heading, HeadingItemCategory, Identification, Image, ImageMap, ImageMapArea, Interpretation, Item, ItemCategory, ItemCategoryFromOption, ItemCategoryOption, ItemCategoryWithEmbeddedItems, ItemContainerCategory, ItemLink, ItemLinkCategory, ItemLinks, ItemPayloadKind, ItemProperty, ItemWithoutEmbeddedItems, LanguageCodes, License, Metadata, Note, Observation, OcrString, Period, PeriodItemLink, Person, PersonItemLink, Prettify, Property, PropertyLike, PropertyRelation, PropertyValue, PropertyValueContent, PropertyValueDataType, PropertyValueItemLink, PropertyValueQueryItem, PropertyVariable, PropertyVariableItemLink, Query, QueryGroup, QueryLeaf, QueryablePropertyValueDataType, RecursiveItemCategory, Resource, ResourceItemLink, Section, Set, SetAttributeValueQueryItem, SetBibliography, SetConcept, SetItem, SetItemCategory, SetItemLink, SetItemProperty, SetItemSimplifiedProperty, SetItemsSort, SetItemsSortDirection, SetPeriod, SetResource, SetSpatialUnit, SetTree, SimplifiedProperty, SpatialUnit, SpatialUnitItemLink, Text, TextItemLink, TopLevelItem, Tree, TreeItemCategory, TreeItemLink } from "./types/index.mjs";
4
4
  import { fetchGallery } from "./fetchers/gallery.mjs";
5
5
  import { fetchItemChildren } from "./fetchers/item-children.mjs";
6
6
  import { fetchItemLinks } from "./fetchers/item-links.mjs";
7
+ import { fetchItemOcrData } from "./fetchers/item-ocr-data.mjs";
7
8
  import { defineLanguages, fetchItem, withLanguages } from "./fetchers/item.mjs";
8
- import { fetchOcrMatches } from "./fetchers/ocr-matches.mjs";
9
9
  import { fetchSetItems } from "./fetchers/set/items.mjs";
10
10
  import { fetchSetPropertyValues } from "./fetchers/set/property-values.mjs";
11
11
  import { fetchWebsiteMetadata } from "./fetchers/website-metadata.mjs";
12
12
  import { fetchWebsite } from "./fetchers/website.mjs";
13
13
  import { PropertyOptions, filterProperties, getLeafPropertyValues, getPropertyByVariableLabel, getPropertyByVariableLabelAndValue, getPropertyByVariableLabelAndValueContent, getPropertyByVariableLabelAndValueContents, getPropertyByVariableLabelAndValues, getPropertyByVariableUuid, getPropertyValueByVariableLabel, getPropertyValueByVariableUuid, getPropertyValueContentByVariableLabel, getPropertyValueContentByVariableUuid, getPropertyValueContentsByVariableUuid, getPropertyValuesByVariableLabel, getPropertyValuesByVariableUuid, getUniqueProperties, getUniquePropertyVariableLabels } from "./getters.mjs";
14
- import { DEFAULT_MAX_OCR_MATCHES_PER_ITEM, DEFAULT_PAGE_SIZE, flattenItemProperties } from "./helpers.mjs";
15
- export { type AccordionWebBlock, type AnyBibliography, type AnyConcept, type AnyItem, type AnyPeriod, type AnyPerson, type AnyPropertyValue, type AnyPropertyVariable, type AnyResource, type AnySet, type AnySpatialUnit, type AnyText, type AnyTree, type BaseItem, type BaseItemLink, type BelongsTo, type Bibliography, type BibliographyEntryInfo, type BibliographyItemLink, type BibliographySourceDocument, type Concept, type ConceptItemLink, type ContainedItemCategory, type ContainedItemCategoryFromOption, type ContainedItemCategoryOption, type Context, type ContextItem, type ContextItemCategory, type ContextNode, type ContextTree, type ContextTreeFilterLevel, type ContextTreeLevel, type ContextTreeLevelItem, type Coordinates, type CoordinatesSource, DEFAULT_MAX_OCR_MATCHES_PER_ITEM, DEFAULT_PAGE_SIZE, type DictionaryUnitItemLink, type EmbeddedBibliography, type EmbeddedConcept, type EmbeddedItem, type EmbeddedPeriod, type EmbeddedPerson, type EmbeddedPropertyValue, type EmbeddedPropertyVariable, type EmbeddedResource, type EmbeddedSet, type EmbeddedSpatialUnit, type EmbeddedText, type EmbeddedTree, type Event, type Gallery, type Heading, type HeadingItemCategory, type Identification, type Image, type ImageMap, type ImageMapArea, type Interpretation, type Item, type ItemCategory, type ItemCategoryFromOption, type ItemCategoryOption, type ItemCategoryWithEmbeddedItems, type ItemContainerCategory, type ItemLink, type ItemLinkCategory, type ItemLinks, type ItemPayloadKind, type ItemProperty, type ItemWithoutEmbeddedItems, type LanguageCodes, type License, type Metadata, type MultilingualOptions, MultilingualString, type MultilingualStringEntries, type MultilingualStringEntry, type MultilingualStringInput, type MultilingualStringJSON, type MultilingualStringObject, type MultilingualStringText, type Note, type Observation, type OcrMatch, type OcrPage, type OcrPoint, type OcrTextBlock, type OcrTextLine, type OcrWord, type Period, type PeriodItemLink, type Person, type PersonItemLink, type Prettify, type Property, type PropertyLike, PropertyOptions, type PropertyRelation, type PropertyValue, type PropertyValueContent, type PropertyValueDataType, type PropertyValueItemLink, type PropertyValueQueryItem, type PropertyVariable, type PropertyVariableItemLink, type ProtectedWebsite, type Query, type QueryGroup, type QueryLeaf, type QueryablePropertyValueDataType, type RecursiveItemCategory, type Resource, type ResourceItemLink, type Scope, type Section, type Set, type SetAttributeValueQueryItem, type SetBibliography, type SetConcept, type SetItem, type SetItemCategory, type SetItemLink, type SetItemProperty, type SetItemSimplifiedProperty, type SetItemsSort, type SetItemsSortDirection, type SetPeriod, type SetResource, type SetSpatialUnit, type SetTree, type SimplifiedProperty, type SpatialUnit, type SpatialUnitItemLink, type Style, type StylesheetCategory, type StylesheetItem, type Text, type TextItemLink, type TopLevelItem, type Tree, type TreeItemCategory, type TreeItemLink, type WebAccordionItem, type WebBlock, type WebBlockByLayout, type WebBlockItem, type WebBlockLayout, type WebElement, type WebElementComponent, type WebElementComponentName, type WebElementComponentOf, type WebElementOf, type WebImage, type WebSidebar, type WebTitle, type Webpage, type Website, type WebsiteMetadata, type WebsitePropertyQuery, type WebsitePropertyQueryNode, type WebsiteSegment, type WebsiteType, defineLanguages, fetchGallery, fetchItem, fetchItemChildren, fetchItemLinks, fetchOcrMatches, fetchSetItems, fetchSetPropertyValues, fetchWebsite, fetchWebsiteMetadata, filterProperties, flattenItemProperties, getLeafPropertyValues, getPropertyByVariableLabel, getPropertyByVariableLabelAndValue, getPropertyByVariableLabelAndValueContent, getPropertyByVariableLabelAndValueContents, getPropertyByVariableLabelAndValues, getPropertyByVariableUuid, getPropertyValueByVariableLabel, getPropertyValueByVariableUuid, getPropertyValueContentByVariableLabel, getPropertyValueContentByVariableUuid, getPropertyValueContentsByVariableUuid, getPropertyValuesByVariableLabel, getPropertyValuesByVariableUuid, getUniqueProperties, getUniquePropertyVariableLabels, withLanguages };
14
+ import { DEFAULT_PAGE_SIZE, flattenItemProperties } from "./helpers.mjs";
15
+ export { type AccordionWebBlock, type AnyBibliography, type AnyConcept, type AnyItem, type AnyPeriod, type AnyPerson, type AnyPropertyValue, type AnyPropertyVariable, type AnyResource, type AnySet, type AnySpatialUnit, type AnyText, type AnyTree, type BaseItem, type BaseItemLink, type BelongsTo, type Bibliography, type BibliographyEntryInfo, type BibliographyItemLink, type BibliographySourceDocument, type Concept, type ConceptItemLink, type ContainedItemCategory, type ContainedItemCategoryFromOption, type ContainedItemCategoryOption, type Context, type ContextItem, type ContextItemCategory, type ContextNode, type ContextTree, type ContextTreeFilterLevel, type ContextTreeLevel, type ContextTreeLevelItem, type Coordinates, type CoordinatesSource, DEFAULT_PAGE_SIZE, type DictionaryUnitItemLink, type EmbeddedBibliography, type EmbeddedConcept, type EmbeddedItem, type EmbeddedPeriod, type EmbeddedPerson, type EmbeddedPropertyValue, type EmbeddedPropertyVariable, type EmbeddedResource, type EmbeddedSet, type EmbeddedSpatialUnit, type EmbeddedText, type EmbeddedTree, type Event, type Gallery, type Heading, type HeadingItemCategory, type Identification, type Image, type ImageMap, type ImageMapArea, type Interpretation, type Item, type ItemCategory, type ItemCategoryFromOption, type ItemCategoryOption, type ItemCategoryWithEmbeddedItems, type ItemContainerCategory, type ItemLink, type ItemLinkCategory, type ItemLinks, type ItemPayloadKind, type ItemProperty, type ItemWithoutEmbeddedItems, type LanguageCodes, type License, type Metadata, type MultilingualOptions, MultilingualString, type MultilingualStringEntries, type MultilingualStringEntry, type MultilingualStringInput, type MultilingualStringJSON, type MultilingualStringObject, type MultilingualStringText, type Note, type Observation, type OcrString, type Period, type PeriodItemLink, type Person, type PersonItemLink, type Prettify, type Property, type PropertyLike, PropertyOptions, type PropertyRelation, type PropertyValue, type PropertyValueContent, type PropertyValueDataType, type PropertyValueItemLink, type PropertyValueQueryItem, type PropertyVariable, type PropertyVariableItemLink, type ProtectedWebsite, type Query, type QueryGroup, type QueryLeaf, type QueryablePropertyValueDataType, type RecursiveItemCategory, type Resource, type ResourceItemLink, type Scope, type Section, type Set, type SetAttributeValueQueryItem, type SetBibliography, type SetConcept, type SetItem, type SetItemCategory, type SetItemLink, type SetItemProperty, type SetItemSimplifiedProperty, type SetItemsSort, type SetItemsSortDirection, type SetPeriod, type SetResource, type SetSpatialUnit, type SetTree, type SimplifiedProperty, type SpatialUnit, type SpatialUnitItemLink, type Style, type StylesheetCategory, type StylesheetItem, type Text, type TextItemLink, type TopLevelItem, type Tree, type TreeItemCategory, type TreeItemLink, type WebAccordionItem, type WebBlock, type WebBlockByLayout, type WebBlockItem, type WebBlockLayout, type WebElement, type WebElementComponent, type WebElementComponentName, type WebElementComponentOf, type WebElementOf, type WebImage, type WebSidebar, type WebTitle, type Webpage, type Website, type WebsiteMetadata, type WebsitePropertyQuery, type WebsitePropertyQueryNode, type WebsiteSegment, type WebsiteType, defineLanguages, fetchGallery, fetchItem, fetchItemChildren, fetchItemLinks, fetchItemOcrData, fetchSetItems, fetchSetPropertyValues, fetchWebsite, fetchWebsiteMetadata, filterProperties, flattenItemProperties, getLeafPropertyValues, getPropertyByVariableLabel, getPropertyByVariableLabelAndValue, getPropertyByVariableLabelAndValueContent, getPropertyByVariableLabelAndValueContents, getPropertyByVariableLabelAndValues, getPropertyByVariableUuid, getPropertyValueByVariableLabel, getPropertyValueByVariableUuid, getPropertyValueContentByVariableLabel, getPropertyValueContentByVariableUuid, getPropertyValueContentsByVariableUuid, getPropertyValuesByVariableLabel, getPropertyValuesByVariableUuid, getUniqueProperties, getUniquePropertyVariableLabels, withLanguages };
package/dist/index.mjs CHANGED
@@ -1,13 +1,13 @@
1
1
  import { filterProperties, getLeafPropertyValues, getPropertyByVariableLabel, getPropertyByVariableLabelAndValue, getPropertyByVariableLabelAndValueContent, getPropertyByVariableLabelAndValueContents, getPropertyByVariableLabelAndValues, getPropertyByVariableUuid, getPropertyValueByVariableLabel, getPropertyValueByVariableUuid, getPropertyValueContentByVariableLabel, getPropertyValueContentByVariableUuid, getPropertyValueContentsByVariableUuid, getPropertyValuesByVariableLabel, getPropertyValuesByVariableUuid, getUniqueProperties, getUniquePropertyVariableLabels } from "./getters.mjs";
2
- import { DEFAULT_MAX_OCR_MATCHES_PER_ITEM, DEFAULT_PAGE_SIZE, flattenItemProperties } from "./helpers.mjs";
2
+ import { DEFAULT_PAGE_SIZE, flattenItemProperties } from "./helpers.mjs";
3
3
  import { MultilingualString } from "./parsers/multilingual.mjs";
4
4
  import { fetchGallery } from "./fetchers/gallery.mjs";
5
5
  import { fetchItemChildren } from "./fetchers/item-children.mjs";
6
6
  import { fetchItemLinks } from "./fetchers/item-links.mjs";
7
+ import { fetchItemOcrData } from "./fetchers/item-ocr-data.mjs";
7
8
  import { defineLanguages, fetchItem, withLanguages } from "./fetchers/item.mjs";
8
- import { fetchOcrMatches } from "./fetchers/ocr-matches.mjs";
9
9
  import { fetchSetItems } from "./fetchers/set/items.mjs";
10
10
  import { fetchSetPropertyValues } from "./fetchers/set/property-values.mjs";
11
11
  import { fetchWebsiteMetadata } from "./fetchers/website-metadata.mjs";
12
12
  import { fetchWebsite } from "./fetchers/website.mjs";
13
- export { DEFAULT_MAX_OCR_MATCHES_PER_ITEM, DEFAULT_PAGE_SIZE, MultilingualString, defineLanguages, fetchGallery, fetchItem, fetchItemChildren, fetchItemLinks, fetchOcrMatches, fetchSetItems, fetchSetPropertyValues, fetchWebsite, fetchWebsiteMetadata, filterProperties, flattenItemProperties, getLeafPropertyValues, getPropertyByVariableLabel, getPropertyByVariableLabelAndValue, getPropertyByVariableLabelAndValueContent, getPropertyByVariableLabelAndValueContents, getPropertyByVariableLabelAndValues, getPropertyByVariableUuid, getPropertyValueByVariableLabel, getPropertyValueByVariableUuid, getPropertyValueContentByVariableLabel, getPropertyValueContentByVariableUuid, getPropertyValueContentsByVariableUuid, getPropertyValuesByVariableLabel, getPropertyValuesByVariableUuid, getUniqueProperties, getUniquePropertyVariableLabels, withLanguages };
13
+ export { DEFAULT_PAGE_SIZE, MultilingualString, defineLanguages, fetchGallery, fetchItem, fetchItemChildren, fetchItemLinks, fetchItemOcrData, fetchSetItems, fetchSetPropertyValues, fetchWebsite, fetchWebsiteMetadata, filterProperties, flattenItemProperties, getLeafPropertyValues, getPropertyByVariableLabel, getPropertyByVariableLabelAndValue, getPropertyByVariableLabelAndValueContent, getPropertyByVariableLabelAndValueContents, getPropertyByVariableLabelAndValues, getPropertyByVariableUuid, getPropertyValueByVariableLabel, getPropertyValueByVariableUuid, getPropertyValueContentByVariableLabel, getPropertyValueContentByVariableUuid, getPropertyValueContentsByVariableUuid, getPropertyValuesByVariableLabel, getPropertyValuesByVariableUuid, getUniqueProperties, getUniquePropertyVariableLabels, withLanguages };
@@ -1,19 +1,12 @@
1
1
  import { Webpage } from "../types/website.mjs";
2
- import { Bibliography, ContainedItemCategoryFromOption, ContainedItemCategoryOption, Gallery, Identification, Item, ItemCategory, ItemCategoryFromOption, ItemCategoryOption, ItemContainerCategory, ItemLinks, Metadata, Note, OcrMatch, Person, Property, Resource, SetItem, SetItemCategory, SimplifiedProperty } from "../types/index.mjs";
3
- import { XMLBibliography, XMLData, XMLDataItem, XMLGalleryData, XMLIdentification, XMLItemLinks, XMLLink, XMLMetadata, XMLNote, XMLOcrMatchItem, XMLPerson, XMLProperty, XMLResource, XMLSetItems, XMLSimplifiedProperty } from "../xml/types.mjs";
2
+ import { Bibliography, ContainedItemCategoryFromOption, ContainedItemCategoryOption, Gallery, Identification, Item, ItemCategory, ItemCategoryFromOption, ItemCategoryOption, ItemContainerCategory, ItemLinks, Metadata, Note, Person, Property, Resource, SetItem, SetItemCategory, SimplifiedProperty } from "../types/index.mjs";
3
+ import { XMLBibliography, XMLData, XMLDataItem, XMLGalleryData, XMLIdentification, XMLItemLinks, XMLLink, XMLMetadata, XMLNote, XMLPerson, XMLProperty, XMLResource, XMLSetItems, XMLSimplifiedProperty } from "../xml/types.mjs";
4
4
  import { ParserOptions, getParserOptions, parseStringLike } from "./helpers.mjs";
5
5
  //#region src/parsers/index.d.ts
6
6
  type RawOchre = XMLData["result"]["ochre"];
7
7
  type ResourceViewParser<T extends ReadonlyArray<string>> = (view: XMLResource["view"], context: Pick<Resource<T, "topLevel">, "belongsTo" | "metadata">) => Webpage<T> | null;
8
8
  type SetItemCategoryFromCategories<T extends ReadonlyArray<SetItemCategory> | undefined> = T extends ReadonlyArray<infer U> ? Extract<U, SetItemCategory> : SetItemCategory;
9
9
  declare function parseIdentification<T extends ReadonlyArray<string>>(rawIdentification: XMLIdentification, options: ParserOptions<T>): Identification<T>;
10
- /**
11
- * Parse OCR matches returned by the OCHRE API
12
- * @param rawOcrItems - The raw OCR match items
13
- * @returns The parsed OCR matches, in request order
14
- * @internal
15
- */
16
- declare function parseOcrMatches(rawOcrItems: Array<XMLOcrMatchItem>): Array<OcrMatch>;
17
10
  declare function parseNotes<T extends ReadonlyArray<string>>(rawNotes: {
18
11
  note: Array<XMLNote>;
19
12
  } | undefined, options: ParserOptions<T>): Array<Note<T>>;
@@ -68,4 +61,4 @@ declare function parseItem(rawData: XMLData, options: {
68
61
  parseResourceView?: ResourceViewParser<ReadonlyArray<string>>;
69
62
  }): Item<ItemCategory, SetItemCategory, ReadonlyArray<string>>;
70
63
  //#endregion
71
- export { type ParserOptions, RawOchre, getParserOptions, parseBibliographyList, parseGallery, parseIdentification, parseItem, parseLinkedItems, parseLinks, parseMetadata, parseMetadataLanguages, parseNotes, parseOcrMatches, parsePersonList, parseProperties, parseSetItems, parseSimplifiedProperties, parseStringLike, resolveDefaultLanguage, resolveLanguages };
64
+ export { type ParserOptions, RawOchre, getParserOptions, parseBibliographyList, parseGallery, parseIdentification, parseItem, parseLinkedItems, parseLinks, parseMetadata, parseMetadataLanguages, parseNotes, parsePersonList, parseProperties, parseSetItems, parseSimplifiedProperties, parseStringLike, resolveDefaultLanguage, resolveLanguages };
@@ -430,79 +430,6 @@ function parseImageMap(rawImageMap) {
430
430
  height: rawImageMap.height
431
431
  };
432
432
  }
433
- function parseOcrVertices(rawVertices) {
434
- const vertices = [];
435
- if (rawVertices == null) return vertices;
436
- for (const match of rawVertices.matchAll(/\(\s*(-?[\d.]+)\s*,\s*(-?[\d.]+)\s*\)/g)) {
437
- const x = Number(match[1]);
438
- const y = Number(match[2]);
439
- vertices.push({
440
- x: Number.isNaN(x) ? 0 : x,
441
- y: Number.isNaN(y) ? 0 : y
442
- });
443
- }
444
- return vertices;
445
- }
446
- function parseOcrWords(rawWords) {
447
- return Array.from(rawWords ?? [], (rawWord) => ({
448
- content: rawWord.CONTENT,
449
- x: rawWord.HPOS,
450
- y: rawWord.VPOS,
451
- width: rawWord.WIDTH,
452
- height: rawWord.HEIGHT,
453
- vertices: parseOcrVertices(rawWord.VERTICES)
454
- }));
455
- }
456
- function joinOcrWordContents(words) {
457
- return Array.from(words, (word) => word.content).join(" ");
458
- }
459
- function parseOcrPage(rawPage) {
460
- const blocks = Array.from(rawPage.TextBlock ?? [], (rawBlock) => ({ lines: Array.from(rawBlock.TextLine ?? [], (rawLine) => {
461
- const words = parseOcrWords(rawLine.string);
462
- return {
463
- content: joinOcrWordContents(words),
464
- words
465
- };
466
- }) }));
467
- return {
468
- number: rawPage.n ?? null,
469
- fileName: rawPage.fileName ?? null,
470
- width: rawPage.WIDTH ?? null,
471
- height: rawPage.HEIGHT ?? null,
472
- blocks
473
- };
474
- }
475
- function parseOcr(rawOcr) {
476
- return Array.from(rawOcr?.Page ?? [], (rawPage) => parseOcrPage(rawPage));
477
- }
478
- /**
479
- * Parse OCR matches returned by the OCHRE API
480
- * @param rawOcrItems - The raw OCR match items
481
- * @returns The parsed OCR matches, in request order
482
- * @internal
483
- */
484
- function parseOcrMatches(rawOcrItems) {
485
- const matches = [];
486
- for (const rawOcrItem of rawOcrItems) {
487
- const rawMatches = rawOcrItem.ocrMatch ?? [];
488
- for (const rawMatch of rawMatches) {
489
- const words = parseOcrWords(rawMatch.string);
490
- matches.push({
491
- uuid: rawOcrItem.uuid,
492
- resourceUuid: rawMatch.resourceUuid ?? null,
493
- page: {
494
- number: rawMatch.n ?? null,
495
- fileName: rawMatch.fileName ?? null,
496
- width: rawMatch.WIDTH ?? null,
497
- height: rawMatch.HEIGHT ?? null
498
- },
499
- content: joinOcrWordContents(words),
500
- words
501
- });
502
- }
503
- }
504
- return matches;
505
- }
506
433
  function parseNote(rawNote, options) {
507
434
  const authors = Array.from(rawNote.authors?.author ?? [], (author) => parsePerson(author, options));
508
435
  const content = rawNote.content == null ? multilingualFromText(parseXMLString(rawNote), options) : parseRequiredContentLike(rawNote, options);
@@ -1196,7 +1123,6 @@ function parseResource(rawResource, options) {
1196
1123
  image: parseImage(rawResource.image, options),
1197
1124
  document: parseContentLike(rawResource.document, options),
1198
1125
  imageMap: parseImageMap(rawResource.imagemap),
1199
- ocr: parseOcr(rawResource.ocr),
1200
1126
  coordinates: parseCoordinates(rawResource.coordinates, options),
1201
1127
  periods: parsePeriodList(rawResource.periods, options),
1202
1128
  links: parseLinks(rawResource.links, options),
@@ -1399,4 +1325,4 @@ function parseItem(rawData, options) {
1399
1325
  };
1400
1326
  }
1401
1327
  //#endregion
1402
- export { getParserOptions, parseBibliographyList, parseGallery, parseIdentification, parseItem, parseLinkedItems, parseLinks, parseMetadata, parseMetadataLanguages, parseNotes, parseOcrMatches, parsePersonList, parseProperties, parseSetItems, parseSimplifiedProperties, parseStringLike, resolveDefaultLanguage, resolveLanguages };
1328
+ export { getParserOptions, parseBibliographyList, parseGallery, parseIdentification, parseItem, parseLinkedItems, parseLinks, parseMetadata, parseMetadataLanguages, parseNotes, parsePersonList, parseProperties, parseSetItems, parseSimplifiedProperties, parseStringLike, resolveDefaultLanguage, resolveLanguages };
@@ -26,11 +26,17 @@ type MultilingualStringEntries<T extends ReadonlyArray<string> = ReadonlyArray<s
26
26
  * Options for creating and working with multilingual strings
27
27
  */
28
28
  type MultilingualOptions = {
29
- /** Default language to use for fallbacks */
29
+ /**
30
+ Default language to use for fallbacks
31
+ */
30
32
  defaultLanguage?: string;
31
- /** Available languages for this string */
33
+ /**
34
+ Available languages for this string
35
+ */
32
36
  availableLanguages?: ReadonlyArray<string>;
33
- /** Alias values carried by OCHRE as zxx content */
37
+ /**
38
+ Alias values carried by OCHRE as zxx content
39
+ */
34
40
  aliases?: ReadonlyArray<string>;
35
41
  };
36
42
  type MultilingualContent<T extends ReadonlyArray<string>> = Partial<Record<T[number], ReadonlyArray<MultilingualStringEntry>>>;
@@ -78,7 +84,9 @@ declare class MultilingualString<T extends ReadonlyArray<string> = ReadonlyArray
78
84
  /**
79
85
  * Create a new multilingual string from an object of language codes to text.
80
86
  */
81
- /** @internal */
87
+ /**
88
+ @internal
89
+ */
82
90
  constructor(init: MultilingualStringInternalInit<T>);
83
91
  constructor(content: MultilingualStringObject<T>, languages: T, options?: MultilingualOptions);
84
92
  constructor(content?: Partial<Record<string, MultilingualStringInput>>, languages?: undefined, options?: MultilingualOptions);
@@ -907,17 +907,18 @@ function parseWebpage(webpageResource, options, context, slugPrefix) {
907
907
  returnWebpage.properties.isNavbarSearchBarDisplayed = pageReader.valueOr("navbar-search-bar-displayed", true);
908
908
  const redirectValue = pageReader.valueNode("redirect-to");
909
909
  const redirectTarget = parseWebsiteLinkTarget(redirectValue, context);
910
- if (redirectTarget != null) if (redirectValue?.href == null && redirectValue?.uuid != null) returnWebpage.properties.redirect = {
911
- type: "page",
912
- slug: redirectTarget,
913
- uuid: redirectValue.uuid
914
- };
915
- else returnWebpage.properties.redirect = {
916
- type: "url",
917
- href: redirectTarget,
918
- isExternal: redirectTarget.startsWith("http")
919
- };
920
- else if (redirectValue?.uuid != null) returnWebpage.properties.redirect = {
910
+ if (redirectTarget != null) {
911
+ if (redirectValue?.href == null && redirectValue?.uuid != null) returnWebpage.properties.redirect = {
912
+ type: "page",
913
+ slug: redirectTarget,
914
+ uuid: redirectValue.uuid
915
+ };
916
+ else returnWebpage.properties.redirect = {
917
+ type: "url",
918
+ href: redirectTarget,
919
+ isExternal: redirectTarget.startsWith("http")
920
+ };
921
+ } else if (redirectValue?.uuid != null) returnWebpage.properties.redirect = {
921
922
  type: "item",
922
923
  uuid: redirectValue.uuid,
923
924
  pageType: "item"
@@ -1015,7 +1016,7 @@ function parseSidebar(resources, options, context) {
1015
1016
  }
1016
1017
  }
1017
1018
  }
1018
- if (items.length > 0 && title != null) returnSidebar = {
1019
+ if (title != null && items.length > 0) returnSidebar = {
1019
1020
  isDisplayed: true,
1020
1021
  items,
1021
1022
  title,
package/dist/query.d.mts CHANGED
@@ -1,40 +1,32 @@
1
1
  import { Query } from "./types/index.mjs";
2
2
  //#region src/query.d.ts
3
+ declare function buildBelongsToCollectionQueryExpression(belongsToCollectionScopeUuids: Array<string>, belongsToCollectionPropertyVariableUuid: string): string | null;
3
4
  /**
4
- * Error message for OCR queries nested inside an OR group
5
- * @internal
6
- */
7
- declare const OCR_DISJUNCTION_ERROR_MESSAGE = "OCR queries cannot be nested inside an OR group because they are resolved by a document join instead of a CTS query";
8
- type OcrUuidBinding = {
9
- name: string;
10
- expression: string;
11
- };
12
- declare function buildAndCtsQueryExpression(queryExpressions: Array<string>): string | null;
13
- /**
14
- * Compile one CTS query expression per OCR search term, in word order
5
+ * Compile a query tree into the XQuery `let` clauses that bind `$items` to the
6
+ * matching Set items
15
7
  *
16
- * Highlighting matches each OCR word against these same per-term expressions,
17
- * so hit locations always agree with what the filter matched.
18
- * @internal
19
- */
20
- declare function buildOcrTermQueryExpressions(parameters: {
21
- value: string;
22
- matchMode: "includes" | "exact";
23
- isCaseSensitive: boolean;
24
- }): Array<string>;
25
- /**
26
- * Whether a query tree nests an OCR leaf inside an OR group
27
- * @internal
8
+ * Most queries compile to a single `cts:search` over the Set item projections.
9
+ * An `ocr` leaf cannot: the projections drop the `<ocr>` layer, so it resolves
10
+ * to a search over the Resource documents whose matching UUIDs are joined back
11
+ * in as an item path predicate. Path predicates only ever AND, so an `ocr` leaf
12
+ * that sits under an `or` becomes its own arm of a node union instead, and one
13
+ * that sits under an `and` alongside a union becomes an intersection.
14
+ *
15
+ * The searchable path has to stay inline in `cts:search`: binding it to a
16
+ * variable first makes every query XDMP-UNSEARCHABLE.
17
+ * @param parameters - The parameters for the compilation
18
+ * @param parameters.queries - Recursive query tree to compile, if any
19
+ * @param parameters.baseItemsExpression - The inline XQuery path selecting the items to search
20
+ * @param parameters.scopeQueryExpression - An optional CTS query ANDed into every compiled search
21
+ * @returns The prolog declaring the query helpers, and the `let` clauses binding `$items`
28
22
  */
29
- declare function hasOcrQueryInDisjunction(query: Query, isInDisjunction?: boolean): boolean;
30
- declare function buildBelongsToCollectionQueryExpression(belongsToCollectionScopeUuids: Array<string>, belongsToCollectionPropertyVariableUuid: string): string | null;
31
23
  declare function buildQueryPlan(parameters: {
32
24
  queries: Query | null;
25
+ baseItemsExpression: string;
26
+ scopeQueryExpression?: string | null;
33
27
  }): {
34
28
  prolog: string;
35
- queryExpression: string | null;
36
- ocrBindings: Array<OcrUuidBinding>;
37
- itemPredicates: string;
29
+ itemsClause: string;
38
30
  };
39
31
  //#endregion
40
- export { OCR_DISJUNCTION_ERROR_MESSAGE, buildAndCtsQueryExpression, buildBelongsToCollectionQueryExpression, buildOcrTermQueryExpressions, buildQueryPlan, hasOcrQueryInDisjunction };
32
+ export { buildBelongsToCollectionQueryExpression, buildQueryPlan };