-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script for linking CC licenses given in
uri
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
whelktool/scripts/cleanups/2023/03/link-cc-licenses.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
select distinct ?uri (count(?g) as ?count) (sample(?g) as ?sample) { | ||
graph ?g {?s kbv:uri ?uri . filter(REGEX(?uri, "https?://creativecommons.org/")) } | ||
} order by desc(?count) | ||
*/ | ||
import groovy.transform.Memoized | ||
|
||
import whelk.Document | ||
import whelk.util.DocumentUtil | ||
|
||
URI_KEY = 'uri' | ||
|
||
selectBySqlWhere("collection = 'bib' and data::text LIKE '%://creativecommons.org/%'") { bib -> | ||
var data = bib.doc.data | ||
DocumentUtil.traverse(data, { value, path -> | ||
if (!path || path[-1] != URI_KEY) { | ||
return | ||
} | ||
def uri = value instanceof List && value.size() > 0 ? value[0] : value | ||
if (uri instanceof String && uri.indexOf('://creativecommons.org/') > -1) { | ||
var licenseId = getLicenseId(uri) | ||
if (licenseId) { | ||
def owner = DocumentUtil.getAtPath(data, path[0..-2]) | ||
assert owner instanceof Map && owner[URI_KEY].is(value) | ||
owner.clear() | ||
owner[ID] = licenseId | ||
bib.scheduleSave() | ||
} | ||
} | ||
}) | ||
} | ||
|
||
@Memoized | ||
String getLicenseId(String id) { | ||
id = id.replace(/\/licenses\/([123]\.[0-9])(\/.*)?/, '/licenses/4.0/') | ||
if (!id.endsWith('/')) { | ||
id += '/' | ||
} | ||
return findCanonicalId(id) | ||
} |