Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script for linking CC licenses given in uri #1221

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions whelktool/scripts/cleanups/2023/03/link-cc-licenses.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
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.findKey(data, URI_KEY, { value, path ->
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)
}