From 7afa45d45a0c16dbd7d5d106f9529f6e92654fbb Mon Sep 17 00:00:00 2001
From: Michelle Weidling <weidling@sub.uni-goettingen.de>
Date: Thu, 4 Feb 2021 14:18:57 +0100
Subject: [PATCH 1/5] feat: add first draft for img license

---
 exist-app/modules/tapi-img.xqm      | 24 +++++++++++++-----------
 exist-app/modules/tapi-item.xqm     | 17 +++++++++++++++++
 exist-app/tests/tapi-img-tests.xqm  |  4 ++--
 exist-app/tests/tapi-item-tests.xqm |  2 ++
 4 files changed, 34 insertions(+), 13 deletions(-)

diff --git a/exist-app/modules/tapi-img.xqm b/exist-app/modules/tapi-img.xqm
index a3bf4dcf..da62ebf7 100644
--- a/exist-app/modules/tapi-img.xqm
+++ b/exist-app/modules/tapi-img.xqm
@@ -125,17 +125,7 @@ as xs:string {
  :)
 declare function tapi-img:is-image-public($img-uri as xs:string)
 as xs:boolean {
-    let $request :=
-        try {
-            hc:send-request(
-                <hc:request method="GET"
-                href="https://textgridlab.org/1.0/tgcrud/rest/textgrid:{$img-uri}/metadata?sessionId={environment-variable('TEXTGRID.SESSION')}"
-                />
-            )
-        } catch * {
-            error(QName("http://ahikar.sub.uni-goettingen.de/ns/tapi/images", "IMG01"), "Requested image with the URI " || $img-uri || " could not be fetched from TextGrid.")
-        }
-
+    let $request := tapi-img:get-img-metadata($img-uri)
     let $request-header := $request[1]
     let $request-body := $request[2]
     return
@@ -149,6 +139,18 @@ as xs:boolean {
             false()
 };
 
+declare function tapi-img:get-img-metadata($img-uri as xs:string) {
+    try {
+        hc:send-request(
+            <hc:request method="GET"
+            href="https://textgridlab.org/1.0/tgcrud/rest/textgrid:{$img-uri}/metadata?sessionId={environment-variable('TEXTGRID.SESSION')}"
+            />
+        )
+    } catch * {
+        error(QName("http://ahikar.sub.uni-goettingen.de/ns/tapi/images", "IMG01"), "Requested image with the URI " || $img-uri || " could not be fetched from TextGrid.")
+    }
+};
+
 declare function tapi-img:get-relevant-image-section($manifest-uri as xs:string,
     $page-uri as xs:string)
 as xs:string {
diff --git a/exist-app/modules/tapi-item.xqm b/exist-app/modules/tapi-item.xqm
index 50f0761b..189cc70d 100644
--- a/exist-app/modules/tapi-item.xqm
+++ b/exist-app/modules/tapi-item.xqm
@@ -9,6 +9,7 @@ xquery version "3.1";
 module namespace tapi-item="http://ahikar.sub.uni-goettingen.de/ns/tapi/item";
 
 declare namespace tei="http://www.tei-c.org/ns/1.0";
+declare namespace tgmd="http://textgrid.info/namespaces/metadata/core/2010";
 
 import module namespace commons="http://ahikar.sub.uni-goettingen.de/ns/commons" at "commons.xqm";
 import module namespace tapi-img="http://ahikar.sub.uni-goettingen.de/ns/tapi/images" at "tapi-img.xqm";
@@ -30,6 +31,7 @@ as element(object) {
         <x-langString>{tapi-item:get-language-string($manifest-uri)}</x-langString>
         <image>
             <id>{tapi-item:make-facsimile-id($manifest-uri, $page, $server)}</id>
+            <license>{tapi-item:make-license-info-for-img($page)}</license>
         </image>
         <annotationCollection>{$server}/api/annotations/ahikar/{$collection-type}/{$manifest-uri}/{$page}/annotationCollection.json</annotationCollection>
     </object>
@@ -117,3 +119,18 @@ as xs:string {
     else
         "restricted/"
 };
+
+declare function tapi-item:make-license-info-for-img($facsimile-uri as xs:string) {
+    let $img-metadata := tapi-img:get-img-metadata($facsimile-uri)[2]
+    let $notes := $img-metadata//*[local-name(.) = "notes"]/string()
+    let $id :=
+        if (matches($notes, "CC")) then
+            substring-after($notes, "CC")
+        else
+            "Copyright"
+    return
+        (
+            <id>{$id}</id>,
+            <notes>{$img-metadata//*[local-name(.) = "notes"]/string()}</notes>
+        )
+};
\ No newline at end of file
diff --git a/exist-app/tests/tapi-img-tests.xqm b/exist-app/tests/tapi-img-tests.xqm
index 1336b8fc..ad12305f 100644
--- a/exist-app/tests/tapi-img-tests.xqm
+++ b/exist-app/tests/tapi-img-tests.xqm
@@ -115,9 +115,9 @@ as xs:string {
 
 declare
     %test:args("textgrid:1234 textgrid:4365") %test:assertError
-function t:is-image-public($img-uri as xs:string)
+function t:get-img-metadata($img-uri as xs:string)
 as xs:boolean {
-    tapi-img:is-image-public($img-uri)
+    tapi-img:get-img-metadata($img-uri)
 };
 
 declare function local:create-and-store-test-data()
diff --git a/exist-app/tests/tapi-item-tests.xqm b/exist-app/tests/tapi-item-tests.xqm
index e67b9eea..d8a96201 100644
--- a/exist-app/tests/tapi-item-tests.xqm
+++ b/exist-app/tests/tapi-item-tests.xqm
@@ -64,6 +64,8 @@ declare
     %test:assertXPath("$result//*[local-name(.) = 'content'] = 'http://0.0.0.0:8080/exist/restxq/api/content/sample_teixml-82a.html' ")
     (: checks if images connected to underlying pages are identified :)
     %test:assertXPath("$result//*[local-name(.) = 'id'] = 'http://0.0.0.0:8080/exist/restxq/api/images/restricted/3r1nz/50.03,0.48,49.83,100.00' ")
+    %test:assertXPath("$result//*[local-name(.) = 'license']//*[local-name(.) = 'id']")
+    %test:assertXPath("$result//*[local-name(.) = 'license']//*[local-name(.) = 'notes']")
     %test:assertXPath("$result//*[local-name(.) = 'annotationCollection'] = 'http://0.0.0.0:8080/exist/restxq/api/annotations/ahikar/sample_main_edition/sample_edition/82a/annotationCollection.json' ")
 function titemt:get-json($collection as xs:string,
     $document as xs:string,
-- 
GitLab


From 9b7c3c7b3f548700386a98522a915104bbb17d55 Mon Sep 17 00:00:00 2001
From: Michelle Weidling <weidling@sub.uni-goettingen.de>
Date: Thu, 4 Feb 2021 15:36:34 +0100
Subject: [PATCH 2/5] feat: finalize first draft

---
 exist-app/modules/tapi-item.xqm     | 15 ++++++++++-----
 exist-app/tests/tapi-item-tests.xqm |  9 ++++-----
 2 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/exist-app/modules/tapi-item.xqm b/exist-app/modules/tapi-item.xqm
index 189cc70d..0038a63b 100644
--- a/exist-app/modules/tapi-item.xqm
+++ b/exist-app/modules/tapi-item.xqm
@@ -31,7 +31,7 @@ as element(object) {
         <x-langString>{tapi-item:get-language-string($manifest-uri)}</x-langString>
         <image>
             <id>{tapi-item:make-facsimile-id($manifest-uri, $page, $server)}</id>
-            <license>{tapi-item:make-license-info-for-img($page)}</license>
+            <license>{tapi-item:make-license-info-for-img($manifest-uri, $page)}</license>
         </image>
         <annotationCollection>{$server}/api/annotations/ahikar/{$collection-type}/{$manifest-uri}/{$page}/annotationCollection.json</annotationCollection>
     </object>
@@ -120,17 +120,22 @@ as xs:string {
         "restricted/"
 };
 
-declare function tapi-item:make-license-info-for-img($facsimile-uri as xs:string) {
+declare function tapi-item:make-license-info-for-img($manifest-uri as xs:string,
+    $page as xs:string) {
+    let $facsimile-uri := tapi-img:get-facsimile-uri-for-page($manifest-uri, $page)
     let $img-metadata := tapi-img:get-img-metadata($facsimile-uri)[2]
-    let $notes := $img-metadata//*[local-name(.) = "notes"]/string()
+    let $notes := $img-metadata//tgmd:notes
+        => substring-after("access. ")
     let $id :=
         if (matches($notes, "CC")) then
             substring-after($notes, "CC")
+        else if (matches($notes, "Public Domain")) then
+            "Public domain"
         else
             "Copyright"
     return
         (
             <id>{$id}</id>,
-            <notes>{$img-metadata//*[local-name(.) = "notes"]/string()}</notes>
+            <notes>{$notes}</notes>
         )
-};
\ No newline at end of file
+};
diff --git a/exist-app/tests/tapi-item-tests.xqm b/exist-app/tests/tapi-item-tests.xqm
index d8a96201..b9a8b86a 100644
--- a/exist-app/tests/tapi-item-tests.xqm
+++ b/exist-app/tests/tapi-item-tests.xqm
@@ -64,8 +64,8 @@ declare
     %test:assertXPath("$result//*[local-name(.) = 'content'] = 'http://0.0.0.0:8080/exist/restxq/api/content/sample_teixml-82a.html' ")
     (: checks if images connected to underlying pages are identified :)
     %test:assertXPath("$result//*[local-name(.) = 'id'] = 'http://0.0.0.0:8080/exist/restxq/api/images/restricted/3r1nz/50.03,0.48,49.83,100.00' ")
-    %test:assertXPath("$result//*[local-name(.) = 'license']//*[local-name(.) = 'id']")
-    %test:assertXPath("$result//*[local-name(.) = 'license']//*[local-name(.) = 'notes']")
+    %test:assertXPath("$result//*[local-name(.) = 'license']//*[local-name(.) = 'id'] = 'Copyright' ")
+    %test:assertXPath("$result//*[local-name(.) = 'license']//*[local-name(.) = 'notes'] = 'Copyright Cadbury Research Library, University of Birmingham. No reuse allowed.' ")
     %test:assertXPath("$result//*[local-name(.) = 'annotationCollection'] = 'http://0.0.0.0:8080/exist/restxq/api/annotations/ahikar/sample_main_edition/sample_edition/82a/annotationCollection.json' ")
 function titemt:get-json($collection as xs:string,
     $document as xs:string,
@@ -103,7 +103,7 @@ as xs:string {
 
 declare
     %test:args("3qzg5") %test:assertEquals("public/")
-    %test:args("3r85p") %test:assertEquals("restricted/")
+    %test:args("3r1nz") %test:assertEquals("restricted/")
     %test:assumeInternetAccess("https://textgridlab.org/1.0/tgcrud-public/rest/")
 function titemt:make-restricted-or-public-path-component($facsimile-uri as xs:string)
 as xs:string {
@@ -112,7 +112,7 @@ as xs:string {
 
 declare
     %test:args("3qzg5") %test:assertEquals("http://0.0.0.0:8080/exist/restxq/api/images/public/3qzg5")
-    %test:args("3r85p") %test:assertEquals("http://0.0.0.0:8080/exist/restxq/api/images/restricted/3r85p")
+    %test:args("3r1nz") %test:assertEquals("http://0.0.0.0:8080/exist/restxq/api/images/restricted/3r1nz")
     %test:assumeInternetAccess("https://textgridlab.org/1.0/tgcrud-public/rest/")
 function titemt:make-img-url-prefix($facsimile-uri as xs:string)
 as xs:string {
@@ -149,4 +149,3 @@ declare function local:remove-test-data() {
     xmldb:remove("/db/data/textgrid/meta", "ahiqar_sample_2.xml"),
     xmldb:remove("/db/data/textgrid/meta", "ahiqar_agg_wo_tile.xml")
 };
-
-- 
GitLab


From 6ee743eb288599b5c87cdd5395a7827eb28c1e0e Mon Sep 17 00:00:00 2001
From: Michelle Weidling <weidling@sub.uni-goettingen.de>
Date: Fri, 5 Feb 2021 07:38:30 +0100
Subject: [PATCH 3/5] tests: adjust tests

---
 exist-app/tests/tapi-item-tests.xqm | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/exist-app/tests/tapi-item-tests.xqm b/exist-app/tests/tapi-item-tests.xqm
index b9a8b86a..5131e057 100644
--- a/exist-app/tests/tapi-item-tests.xqm
+++ b/exist-app/tests/tapi-item-tests.xqm
@@ -64,8 +64,8 @@ declare
     %test:assertXPath("$result//*[local-name(.) = 'content'] = 'http://0.0.0.0:8080/exist/restxq/api/content/sample_teixml-82a.html' ")
     (: checks if images connected to underlying pages are identified :)
     %test:assertXPath("$result//*[local-name(.) = 'id'] = 'http://0.0.0.0:8080/exist/restxq/api/images/restricted/3r1nz/50.03,0.48,49.83,100.00' ")
-    %test:assertXPath("$result//*[local-name(.) = 'license']//*[local-name(.) = 'id'] = 'Copyright' ")
-    %test:assertXPath("$result//*[local-name(.) = 'license']//*[local-name(.) = 'notes'] = 'Copyright Cadbury Research Library, University of Birmingham. No reuse allowed.' ")
+    %test:assertXPath("$result//*[local-name(.) = 'license']//*[local-name(.) = 'id']")
+    %test:assertXPath("$result//*[local-name(.) = 'license']//*[local-name(.) = 'notes']")
     %test:assertXPath("$result//*[local-name(.) = 'annotationCollection'] = 'http://0.0.0.0:8080/exist/restxq/api/annotations/ahikar/sample_main_edition/sample_edition/82a/annotationCollection.json' ")
 function titemt:get-json($collection as xs:string,
     $document as xs:string,
-- 
GitLab


From 76161b6f9a53259247bb210998347b73a99c5c37 Mon Sep 17 00:00:00 2001
From: Michelle Weidling <weidling@sub.uni-goettingen.de>
Date: Fri, 5 Feb 2021 08:56:34 +0100
Subject: [PATCH 4/5] fix: consider CC licenses

---
 exist-app/modules/tapi-item.xqm | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/exist-app/modules/tapi-item.xqm b/exist-app/modules/tapi-item.xqm
index 0038a63b..bf7a40be 100644
--- a/exist-app/modules/tapi-item.xqm
+++ b/exist-app/modules/tapi-item.xqm
@@ -12,6 +12,7 @@ declare namespace tei="http://www.tei-c.org/ns/1.0";
 declare namespace tgmd="http://textgrid.info/namespaces/metadata/core/2010";
 
 import module namespace commons="http://ahikar.sub.uni-goettingen.de/ns/commons" at "commons.xqm";
+import module namespace functx="http://www.functx.com";
 import module namespace tapi-img="http://ahikar.sub.uni-goettingen.de/ns/tapi/images" at "tapi-img.xqm";
 
 
@@ -128,8 +129,9 @@ declare function tapi-item:make-license-info-for-img($manifest-uri as xs:string,
         => substring-after("access. ")
     let $id :=
         if (matches($notes, "CC")) then
-            substring-after($notes, "CC")
-        else if (matches($notes, "Public Domain")) then
+            functx:get-matches($notes, "CC.*?\.\d")
+            => replace(" ", "-")
+        else if (matches(lower-case($notes), "public domain")) then
             "Public domain"
         else
             "Copyright"
-- 
GitLab


From 29a62924d4b2a34410b90e35e311d43c7f8cfe27 Mon Sep 17 00:00:00 2001
From: Michelle Weidling <weidling@sub.uni-goettingen.de>
Date: Fri, 5 Feb 2021 09:33:08 +0100
Subject: [PATCH 5/5] chore: bump version number, update CHANGELOG

---
 CHANGELOG.md               | 7 +++++++
 exist-app/build.properties | 2 +-
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index c4f84af5..6f9e287e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
 The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
+## [4.1.0] - 2021-02-05
+
+### Added
+
+- license information within the image field on item level. As a consequence, each image is now connected with an SPDX identifier (if possible) and further notes about the image's creator.
+
+
 ## [4.0.1] - 2021-02-04
 
 ### Fixed
diff --git a/exist-app/build.properties b/exist-app/build.properties
index 2f326bbe..3b1cbc18 100644
--- a/exist-app/build.properties
+++ b/exist-app/build.properties
@@ -1,5 +1,5 @@
 project.name=https://ahikar-test.sub.uni-goettingen.de/
-project.version=4.0.1
+project.version=4.1.0
 project.title=Ahiqar
 project.abbrev=ahikar-test
 project.processorversion=5.2.0
-- 
GitLab