geo_combine 0.10.0 → 0.12.0
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.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +35 -12
- data/.rubocop.yml +4 -0
- data/.rubocop_todo.yml +2 -10
- data/Gemfile +3 -0
- data/README.md +203 -137
- data/geo_combine.gemspec +1 -1
- data/lib/geo_combine/exceptions.rb +3 -0
- data/lib/geo_combine/fgdc.rb +7 -0
- data/lib/geo_combine/geoblacklight_aardvark.rb +70 -0
- data/lib/geo_combine/harvester.rb +106 -28
- data/lib/geo_combine/indexer.rb +49 -4
- data/lib/geo_combine/iso19139.rb +7 -0
- data/lib/geo_combine/migrators/v1_aardvark_migrator.rb +1 -2
- data/lib/geo_combine/schema.rb +32 -0
- data/lib/geo_combine/version.rb +1 -1
- data/lib/geo_combine/withdrawals.rb +125 -0
- data/lib/geo_combine.rb +25 -1
- data/lib/tasks/geo_combine.rake +11 -12
- data/lib/xslt/fgdc2Aardvark.xsl +1512 -0
- data/lib/xslt/iso2Aardvark.xsl +1634 -0
- data/spec/fixtures/docs/arizona_reservations_iso.xml +1183 -0
- data/spec/fixtures/docs/arizona_topo_iso.xml +560 -0
- data/spec/fixtures/docs/cornell_agdistricts_fgdc.xml +572 -0
- data/spec/fixtures/docs/duplicate_values_fgdc.xml +116 -0
- data/spec/fixtures/docs/gbl_keywords_fgdc.xml +105 -0
- data/spec/fixtures/docs/harvard_euratlas_fgdc.xml +449 -0
- data/spec/fixtures/docs/harvard_glb_fgdc.xml +455 -0
- data/spec/fixtures/docs/harvard_kng_fgdc.xml +403 -0
- data/spec/fixtures/docs/harvard_mapa_fgdc.xml +356 -0
- data/spec/fixtures/docs/harvard_nhgis_fgdc.xml +1706 -0
- data/spec/fixtures/docs/harvard_usgs_fgdc.xml +393 -0
- data/spec/fixtures/docs/nyu_colleges_iso.xml +616 -0
- data/spec/fixtures/docs/services_fgdc.xml +227 -0
- data/spec/fixtures/docs/services_iso.xml +433 -0
- data/spec/fixtures/indexing/no_schema.json +5 -0
- data/spec/fixtures/indexing/restricted_aardvark.json +34 -0
- data/spec/fixtures/indexing/restricted_geoblacklight.json +42 -0
- data/spec/fixtures/withdrawal/edu.example-a/metadata-aardvark/a-contradicted.json +8 -0
- data/spec/fixtures/withdrawal/edu.example-a/metadata-aardvark/a-live.json +8 -0
- data/spec/fixtures/withdrawal/edu.example-a/withdrawn.json +13 -0
- data/spec/fixtures/withdrawal/edu.example-b/metadata-aardvark/b-live.json +8 -0
- data/spec/fixtures/withdrawal/edu.example-b/withdrawn.json +7 -0
- data/spec/fixtures/withdrawal/edu.example-broken/withdrawn.json +4 -0
- data/spec/fixtures/withdrawal/edu.example-skipped/withdrawn.json +7 -0
- data/spec/fixtures/xml_docs.rb +110 -0
- data/spec/integration/withdrawals_spec.rb +47 -0
- data/spec/lib/geo_combine/ckan_metadata_spec.rb +44 -0
- data/spec/lib/geo_combine/fgdc_spec.rb +520 -0
- data/spec/lib/geo_combine/geoblacklight_aardvark_spec.rb +85 -0
- data/spec/lib/geo_combine/harvester_spec.rb +255 -11
- data/spec/lib/geo_combine/indexer_spec.rb +140 -2
- data/spec/lib/geo_combine/iso19139_spec.rb +540 -1
- data/spec/lib/geo_combine/logger_spec.rb +38 -0
- data/spec/lib/geo_combine/ogp_spec.rb +37 -0
- data/spec/lib/geo_combine/schema_spec.rb +74 -0
- data/spec/lib/geo_combine/withdrawals_spec.rb +294 -0
- data/spec/lib/tasks/geo_combine_spec.rb +51 -0
- data/spec/spec_helper.rb +4 -1
- metadata +72 -12
- data/lib/.DS_Store +0 -0
- data/lib/geo_combine/geo_blacklight_harvester.rb +0 -216
- data/lib/xslt/isoAardvark.xsl +0 -738
- data/spec/lib/geo_combine/geo_blacklight_harvester_spec.rb +0 -195
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'open-uri'
|
|
4
|
+
|
|
5
|
+
module GeoCombine
|
|
6
|
+
class GeoblacklightAardvark
|
|
7
|
+
attr_reader :metadata
|
|
8
|
+
|
|
9
|
+
SCHEMA_VERSION = 'Aardvark'
|
|
10
|
+
SCHEMA_JSON_URL = 'https://raw.githubusercontent.com/OpenGeoMetadata/opengeometadata.github.io/main/docs/schema/geoblacklight-schema-aardvark.json'
|
|
11
|
+
SCHEMA_DRAFT_URL = 'http://json-schema.org/draft-04/schema#'
|
|
12
|
+
|
|
13
|
+
##
|
|
14
|
+
# @param [String] metadata a JSON string document in the Aardvark schema
|
|
15
|
+
# @param [Hash] fields values merged with the parsed document, used for
|
|
16
|
+
# fields Aardvark defines but the source metadata cannot supply
|
|
17
|
+
def initialize(metadata, fields = {})
|
|
18
|
+
@metadata = JSON.parse(metadata).merge(fields)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
##
|
|
22
|
+
# Returns a string of JSON from an Aardvark hash
|
|
23
|
+
# @return [String]
|
|
24
|
+
def to_json(options = {})
|
|
25
|
+
metadata.to_json(options)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
##
|
|
29
|
+
# True if the document is a valid Aardvark record
|
|
30
|
+
# @return [Boolean]
|
|
31
|
+
def valid?
|
|
32
|
+
validate!
|
|
33
|
+
true
|
|
34
|
+
rescue StandardError
|
|
35
|
+
false
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
##
|
|
39
|
+
# Validates an Aardvark json document
|
|
40
|
+
def validate!
|
|
41
|
+
JSON::Validator.validate!(schema, to_json)
|
|
42
|
+
validate_version!
|
|
43
|
+
validate_spatial!
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
##
|
|
47
|
+
# Validate gbl_mdVersion_s
|
|
48
|
+
def validate_version!
|
|
49
|
+
return if metadata['gbl_mdVersion_s'] == SCHEMA_VERSION
|
|
50
|
+
|
|
51
|
+
raise GeoCombine::Exceptions::InvalidSchemaVersion,
|
|
52
|
+
"gbl_mdVersion_s must be #{SCHEMA_VERSION.inspect}, got #{metadata['gbl_mdVersion_s'].inspect}"
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
##
|
|
56
|
+
# Validate locn_geometry
|
|
57
|
+
def validate_spatial!
|
|
58
|
+
geometry = metadata['locn_geometry']
|
|
59
|
+
return unless geometry =~ /ENVELOPE\(/
|
|
60
|
+
|
|
61
|
+
raise GeoCombine::Exceptions::InvalidGeometry unless GeoCombine::BoundingBox.from_envelope(geometry).valid?
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
private
|
|
65
|
+
|
|
66
|
+
def schema
|
|
67
|
+
@schema ||= JSON.parse(URI.open(SCHEMA_JSON_URL).read).merge('$schema' => SCHEMA_DRAFT_URL)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -1,71 +1,83 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'json'
|
|
4
|
+
require 'fileutils'
|
|
4
5
|
require 'find'
|
|
5
6
|
require 'git'
|
|
6
7
|
require 'net/http'
|
|
8
|
+
require 'set'
|
|
7
9
|
require 'geo_combine/logger'
|
|
10
|
+
require 'geo_combine/schema'
|
|
8
11
|
|
|
9
12
|
module GeoCombine
|
|
10
13
|
# Harvests Geoblacklight documents from OpenGeoMetadata for indexing
|
|
11
14
|
class Harvester
|
|
12
|
-
|
|
15
|
+
# Files at the root of a repository that describe the repository, not records
|
|
16
|
+
REPOSITORY_FILES = %w[layers.json withdrawn.json].freeze
|
|
17
|
+
|
|
18
|
+
attr_reader :ogm_path, :schema_version, :present_ids
|
|
13
19
|
|
|
14
20
|
# GitHub API endpoint for OpenGeoMetadata repositories
|
|
21
|
+
# This is the first page of results; 100 is the largest page size GitHub allows
|
|
15
22
|
def self.ogm_api_uri
|
|
16
|
-
URI('https://api.github.com/orgs/opengeometadata/repos?per_page=
|
|
23
|
+
URI('https://api.github.com/orgs/opengeometadata/repos?per_page=100')
|
|
17
24
|
end
|
|
18
25
|
|
|
19
26
|
# Initialize a new harvester
|
|
20
27
|
# @param ogm_path [String] path to the directory where repositories will be cloned
|
|
21
28
|
# @param skip_repos [Array<String>] list of repository names to skip
|
|
29
|
+
# @param skip_restricted [Boolean] whether to skip indexing restricted documents
|
|
22
30
|
# @param schema_version [String] schema version to filter repositories by
|
|
23
31
|
# @param logger [Logger] logger to use for logging messages
|
|
24
32
|
def initialize(
|
|
25
33
|
ogm_path:,
|
|
26
34
|
skip_repos: ENV.fetch('OGM_SKIP_REPOS', '').split(',').map(&:strip),
|
|
27
|
-
|
|
35
|
+
skip_restricted: ENV.fetch('OGM_SKIP_RESTRICTED', 'true') == 'true',
|
|
36
|
+
schema_version: ENV.fetch('SCHEMA_VERSION', GeoCombine::Schema::DEFAULT_VERSION),
|
|
28
37
|
logger: GeoCombine::Logger.logger
|
|
29
38
|
)
|
|
30
39
|
@ogm_path = ogm_path
|
|
31
40
|
@schema_version = schema_version
|
|
32
41
|
@skip_repos = skip_repos
|
|
42
|
+
@skip_restricted = skip_restricted
|
|
33
43
|
@logger = logger
|
|
34
44
|
end
|
|
35
45
|
|
|
36
46
|
# Enumerable of docs to index, for passing to an indexer
|
|
47
|
+
# While enumerating, collects the ids of the records yielded in present_ids
|
|
37
48
|
def docs_to_index
|
|
38
49
|
return to_enum(:docs_to_index) unless block_given?
|
|
39
50
|
|
|
51
|
+
@present_ids = Set.new
|
|
52
|
+
@walk_complete = false
|
|
40
53
|
@logger.info "loading documents from #{ogm_path}"
|
|
41
54
|
Find.find(@ogm_path) do |path|
|
|
42
|
-
# skip
|
|
43
|
-
|
|
55
|
+
# skip hidden directories, like .git
|
|
56
|
+
Find.prune if hidden_directory?(path)
|
|
57
|
+
|
|
58
|
+
# skip non-json files, and files that describe a repository rather than records
|
|
59
|
+
if REPOSITORY_FILES.include?(File.basename(path)) || !File.basename(path).end_with?('.json')
|
|
44
60
|
@logger.debug "skipping #{path}; not a geoblacklight JSON document"
|
|
45
61
|
next
|
|
46
62
|
end
|
|
47
63
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
record_schema = record['gbl_mdVersion_s'] || record['geoblacklight_version']
|
|
51
|
-
record_id = record['layer_slug_s'] || record['dc_identifier_s']
|
|
64
|
+
records_in(path).each do |record|
|
|
65
|
+
next unless record.is_a?(Hash)
|
|
52
66
|
|
|
53
|
-
|
|
54
|
-
unless
|
|
55
|
-
@logger.debug "skipping #{record_id || path}; no schema version declared in record"
|
|
56
|
-
next
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
# skip indexing if this record has a different schema version than what we want
|
|
60
|
-
if record_schema != @schema_version
|
|
61
|
-
@logger.debug "skipping #{record_id}; schema version #{record_schema} doesn't match #{@schema_version}"
|
|
62
|
-
next
|
|
63
|
-
end
|
|
67
|
+
record_id = GeoCombine::Schema.id(record)
|
|
68
|
+
next unless indexable?(record, record_id, path)
|
|
64
69
|
|
|
65
70
|
@logger.debug "found record #{record_id} at #{path}"
|
|
71
|
+
@present_ids << record_id
|
|
66
72
|
yield record, path
|
|
67
73
|
end
|
|
68
74
|
end
|
|
75
|
+
@walk_complete = true
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Whether the run visited every file
|
|
79
|
+
def walk_complete?
|
|
80
|
+
@walk_complete == true
|
|
69
81
|
end
|
|
70
82
|
|
|
71
83
|
# Update a repository via git
|
|
@@ -103,7 +115,8 @@ module GeoCombine
|
|
|
103
115
|
return @logger.warn "repository #{repo_url} clone to #{repo_path}; repository properties don't include schema version #{@schema_version} (found #{repo_schemas.join(', ')})"
|
|
104
116
|
end
|
|
105
117
|
|
|
106
|
-
|
|
118
|
+
FileUtils.mkdir_p(ogm_path)
|
|
119
|
+
Git.clone(repo_url, nil, chdir: ogm_path, depth: 1)
|
|
107
120
|
@logger.info "cloned #{repo_url} to #{repo_path}"
|
|
108
121
|
repo
|
|
109
122
|
end
|
|
@@ -118,19 +131,84 @@ module GeoCombine
|
|
|
118
131
|
|
|
119
132
|
private
|
|
120
133
|
|
|
134
|
+
# Whether to index a record; if not, log why
|
|
135
|
+
def indexable?(record, record_id, path)
|
|
136
|
+
record_schema = GeoCombine::Schema.version(record)
|
|
137
|
+
record_rights = record['dct_accessRights_s'] || record['dc_rights_s']
|
|
138
|
+
|
|
139
|
+
# skip indexing if no identifiable schema version
|
|
140
|
+
unless record_schema
|
|
141
|
+
@logger.debug "skipping #{path}; no schema version declared in record"
|
|
142
|
+
return false
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# skip indexing if this record has a different schema version than what we want
|
|
146
|
+
if record_schema != @schema_version
|
|
147
|
+
@logger.debug "skipping #{record_id}; schema version #{record_schema} doesn't match #{@schema_version}"
|
|
148
|
+
return false
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# skip indexing if this record is restricted and we want to skip restricted records
|
|
152
|
+
if @skip_restricted && record_rights == 'Restricted'
|
|
153
|
+
@logger.debug "skipping #{record_id}; access rights are restricted"
|
|
154
|
+
return false
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
true
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# Whether a path is a hidden directory inside ogm_path
|
|
161
|
+
def hidden_directory?(path)
|
|
162
|
+
path != @ogm_path && File.basename(path).start_with?('.') && File.directory?(path)
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# The records in a JSON file. A file that can't be parsed is logged and skipped,
|
|
166
|
+
# so one bad file can't stop the rest of the walk.
|
|
167
|
+
def records_in(path)
|
|
168
|
+
json = File.read(path, mode: 'r:bom|utf-8')
|
|
169
|
+
raise JSON::ParserError, 'not valid UTF-8' unless json.valid_encoding?
|
|
170
|
+
|
|
171
|
+
[JSON.parse(json)].flatten
|
|
172
|
+
rescue JSON::ParserError => e
|
|
173
|
+
@logger.error "skipping #{path}; could not parse JSON: #{e.message}"
|
|
174
|
+
[]
|
|
175
|
+
end
|
|
176
|
+
|
|
121
177
|
# List of repository names to harvest
|
|
122
178
|
def repositories
|
|
123
|
-
@repositories ||=
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
179
|
+
@repositories ||= organization_repositories
|
|
180
|
+
.filter { |repo| Array(repo.dig('custom_properties', 'supported_schemas')).include? @schema_version }
|
|
181
|
+
.filter { |repo| repo['size'].positive? }
|
|
182
|
+
.reject { |repo| repo['archived'] }
|
|
183
|
+
.reject { |repo| @skip_repos.include?(repo['name']) }
|
|
184
|
+
.map { |repo| repo['name'] }
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# Fetch metadata for every OpenGeoMetadata repository from GitHub API
|
|
188
|
+
# Results are paginated; follow the rel="next" URL in each response's Link header until there isn't one
|
|
189
|
+
def organization_repositories
|
|
190
|
+
repos = []
|
|
191
|
+
uri = self.class.ogm_api_uri
|
|
192
|
+
while uri
|
|
193
|
+
response = Net::HTTP.get_response(uri, github_api_headers)
|
|
194
|
+
repos.concat(JSON.parse(response.body))
|
|
195
|
+
next_url = response['link'].to_s[/<([^>]+)>;\s*rel="next"/, 1]
|
|
196
|
+
uri = next_url && URI(next_url)
|
|
197
|
+
end
|
|
198
|
+
repos
|
|
129
199
|
end
|
|
130
200
|
|
|
131
201
|
# Fetch repository metadata from GitHub API
|
|
132
202
|
def repository_info(repo_name)
|
|
133
|
-
JSON.parse(Net::HTTP.get(URI("https://api.github.com/repos/opengeometadata/#{repo_name}")))
|
|
203
|
+
JSON.parse(Net::HTTP.get(URI("https://api.github.com/repos/opengeometadata/#{repo_name}"), github_api_headers))
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
# Authenticate GitHub API requests if a token is available, which raises the rate limit
|
|
207
|
+
def github_api_headers
|
|
208
|
+
token = ENV.fetch('GITHUB_TOKEN', '')
|
|
209
|
+
return {} if token.empty?
|
|
210
|
+
|
|
211
|
+
{ 'Authorization' => "Bearer #{token}" }
|
|
134
212
|
end
|
|
135
213
|
end
|
|
136
214
|
end
|
data/lib/geo_combine/indexer.rb
CHANGED
|
@@ -1,21 +1,33 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'json'
|
|
3
4
|
require 'rsolr'
|
|
4
5
|
require 'faraday/retry'
|
|
5
6
|
require 'faraday/net_http_persistent'
|
|
6
7
|
require 'geo_combine/logger'
|
|
7
8
|
|
|
8
9
|
module GeoCombine
|
|
9
|
-
# Indexes Geoblacklight documents into Solr
|
|
10
|
+
# Indexes Geoblacklight documents into Solr, and deletes them
|
|
10
11
|
class Indexer
|
|
12
|
+
# faraday-retry middleware normally doesn't retry POSTs, but it's safe
|
|
13
|
+
# because documents are added with overwrite and deleted by id
|
|
14
|
+
RETRY_METHODS = (Faraday::Retry::Middleware::IDEMPOTENT_METHODS + [:post]).freeze
|
|
15
|
+
|
|
11
16
|
attr_reader :solr
|
|
12
17
|
|
|
13
|
-
|
|
18
|
+
# Initialize a new indexer
|
|
19
|
+
# @param solr [RSolr::Client] an existing RSolr client
|
|
20
|
+
# @param solr_url [String] the URL of the Solr instance to connect to
|
|
21
|
+
# @param logger [Logger] a logger instance for logging messages
|
|
22
|
+
def initialize(
|
|
23
|
+
solr: nil,
|
|
24
|
+
solr_url: ENV.fetch('SOLR_URL', nil),
|
|
25
|
+
logger: GeoCombine::Logger.logger
|
|
26
|
+
)
|
|
14
27
|
@logger = logger
|
|
15
28
|
@batch_size = ENV.fetch('SOLR_BATCH_SIZE', 100).to_i
|
|
16
29
|
|
|
17
30
|
# If SOLR_URL is set, use it; if in a Geoblacklight app, use its solr core
|
|
18
|
-
solr_url = ENV.fetch('SOLR_URL', nil)
|
|
19
31
|
solr_url ||= Blacklight.default_index.connection.base_uri.to_s if defined? Blacklight
|
|
20
32
|
|
|
21
33
|
# If neither, warn and try to use local Blacklight default solr core
|
|
@@ -24,6 +36,7 @@ module GeoCombine
|
|
|
24
36
|
solr_url = 'http://localhost:8983/solr/blacklight-core'
|
|
25
37
|
end
|
|
26
38
|
|
|
39
|
+
# If solr connection was provided directly, use it; otherwise connect now
|
|
27
40
|
@solr = solr || RSolr.connect(client, url: solr_url)
|
|
28
41
|
end
|
|
29
42
|
|
|
@@ -48,6 +61,15 @@ module GeoCombine
|
|
|
48
61
|
total_indexed
|
|
49
62
|
end
|
|
50
63
|
|
|
64
|
+
# Delete documents by id, commit, and return successful IDs (regardless of if they existed).
|
|
65
|
+
# NOTE: We don't delete by query because OGM ids can contain characters that mess up the parser.
|
|
66
|
+
def delete(ids)
|
|
67
|
+
@logger.info "deleting #{ids.size} ids from #{solr_url}"
|
|
68
|
+
deleted = ids.each_slice(@batch_size).flat_map { |slice| delete_batch(slice) }
|
|
69
|
+
@solr.commit
|
|
70
|
+
deleted
|
|
71
|
+
end
|
|
72
|
+
|
|
51
73
|
# URL to the solr instance being used
|
|
52
74
|
def solr_url
|
|
53
75
|
@solr.options[:url]
|
|
@@ -55,6 +77,27 @@ module GeoCombine
|
|
|
55
77
|
|
|
56
78
|
private
|
|
57
79
|
|
|
80
|
+
# Delete a batch of ids; if we fail, delete them all individually
|
|
81
|
+
def delete_batch(ids)
|
|
82
|
+
@solr.delete_by_id(ids)
|
|
83
|
+
@logger.debug "deleted batch (#{ids.size} ids)"
|
|
84
|
+
ids
|
|
85
|
+
rescue RSolr::Error::Http => e
|
|
86
|
+
@logger.error "error deleting batch (#{ids.size} ids): #{format_error(e)}"
|
|
87
|
+
@logger.warn 'retrying ids individually'
|
|
88
|
+
ids.filter_map { |id| delete_single(id) }
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Delete a single id; if it fails, log the error and continue
|
|
92
|
+
def delete_single(id)
|
|
93
|
+
@solr.delete_by_id(id)
|
|
94
|
+
@logger.debug "deleted #{id}"
|
|
95
|
+
id
|
|
96
|
+
rescue RSolr::Error::Http => e
|
|
97
|
+
@logger.error "error deleting #{id}: #{format_error(e)}"
|
|
98
|
+
nil
|
|
99
|
+
end
|
|
100
|
+
|
|
58
101
|
# Index a batch of documents; if we fail, index them all individually
|
|
59
102
|
def index_batch(batch)
|
|
60
103
|
docs = batch.map(&:first)
|
|
@@ -84,6 +127,8 @@ module GeoCombine
|
|
|
84
127
|
|
|
85
128
|
# Generate a friendly error message for logging including status code and message
|
|
86
129
|
def format_error(error)
|
|
130
|
+
return [error.class, error.cause&.message].compact.join(' - ') unless error.response
|
|
131
|
+
|
|
87
132
|
code = error.response[:status]
|
|
88
133
|
status_info = "#{code} #{RSolr::Error::Http::STATUS_CODES[code.to_i]}"
|
|
89
134
|
error_info = parse_solr_error(error)
|
|
@@ -107,7 +152,7 @@ module GeoCombine
|
|
|
107
152
|
|
|
108
153
|
def client
|
|
109
154
|
@client ||= Faraday.new do |conn|
|
|
110
|
-
conn.request :retry, max: 3, interval: 1, backoff_factor: 2, exceptions: [
|
|
155
|
+
conn.request :retry, max: 3, interval: 1, backoff_factor: 2, methods: RETRY_METHODS, exceptions: [
|
|
111
156
|
Faraday::TimeoutError,
|
|
112
157
|
Faraday::ConnectionFailed,
|
|
113
158
|
Faraday::TooManyRequestsError
|
data/lib/geo_combine/iso19139.rb
CHANGED
|
@@ -10,6 +10,13 @@ module GeoCombine
|
|
|
10
10
|
Nokogiri::XSLT(File.open(File.join(File.dirname(__FILE__), '../xslt/iso2geoBL.xsl')))
|
|
11
11
|
end
|
|
12
12
|
|
|
13
|
+
##
|
|
14
|
+
# Returns a Nokogiri::XSLT object containing the ISO19139 to Aardvark XSL
|
|
15
|
+
# @return [Nokogiri::XSLT]
|
|
16
|
+
def xsl_aardvark
|
|
17
|
+
Nokogiri::XSLT(File.open(File.join(File.dirname(__FILE__), '../xslt/iso2Aardvark.xsl')))
|
|
18
|
+
end
|
|
19
|
+
|
|
13
20
|
##
|
|
14
21
|
# Returns a Nokogiri::XSLT object containing the ISO19139 to HTML XSL
|
|
15
22
|
# @return [Nokogiri:XSLT]
|
|
@@ -50,7 +50,7 @@ module GeoCombine
|
|
|
50
50
|
end
|
|
51
51
|
|
|
52
52
|
# Convert non-crosswalked fields via lookup tables
|
|
53
|
-
# rubocop:disable Metrics/PerceivedComplexity
|
|
53
|
+
# rubocop:disable-next Metrics/PerceivedComplexity
|
|
54
54
|
def convert_non_crosswalked_fields
|
|
55
55
|
# Keys may or may not include whitespace, so we normalize them.
|
|
56
56
|
# Resource class is required so we default to "Other"; resource type is not required.
|
|
@@ -69,7 +69,6 @@ module GeoCombine
|
|
|
69
69
|
@v2_hash.delete('dct_isPartOf_sm')
|
|
70
70
|
end
|
|
71
71
|
end
|
|
72
|
-
# rubocop:enable Metrics/PerceivedComplexity
|
|
73
72
|
|
|
74
73
|
# Remove fields that are no longer used
|
|
75
74
|
def remove_deprecated_fields
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GeoCombine
|
|
4
|
+
# How each GeoBlacklight schema version declares itself and identifies its records
|
|
5
|
+
module Schema
|
|
6
|
+
DEFAULT_VERSION = 'Aardvark'
|
|
7
|
+
|
|
8
|
+
# The field each schema version uses as its Solr uniqueKey: GeoBlacklight 4+
|
|
9
|
+
# (Aardvark) uses id, GeoBlacklight 2 and 3 (1.0) use layer_slug_s
|
|
10
|
+
ID_FIELDS = { 'Aardvark' => 'id', '1.0' => 'layer_slug_s' }.freeze
|
|
11
|
+
|
|
12
|
+
module_function
|
|
13
|
+
|
|
14
|
+
# The schema version a record declares, if any
|
|
15
|
+
def version(record)
|
|
16
|
+
record['gbl_mdVersion_s'] || record['geoblacklight_version']
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# A record's identifier, sent to Solr as the uniqueKey
|
|
20
|
+
def id(record)
|
|
21
|
+
field = ID_FIELDS[version(record)]
|
|
22
|
+
value = field ? record[field] : record['id'] || record['layer_slug_s']
|
|
23
|
+
value&.to_s
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Whether a string is usable as a delete key. An id with leading or trailing
|
|
27
|
+
# whitespace or with control characters is a bug.
|
|
28
|
+
def valid_id?(id)
|
|
29
|
+
id.is_a?(String) && !id.empty? && id.valid_encoding? && !id.match?(/\A[[:space:]]|[[:space:]]\z|[[:cntrl:]]/)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
data/lib/geo_combine/version.rb
CHANGED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'geo_combine/logger'
|
|
5
|
+
require 'geo_combine/schema'
|
|
6
|
+
|
|
7
|
+
module GeoCombine
|
|
8
|
+
# Reads the withdrawal logs (withdrawn.json) that OpenGeoMetadata repositories publish at
|
|
9
|
+
# their root, and deletes the records they list from an index.
|
|
10
|
+
#
|
|
11
|
+
# A record is deleted only when a log lists it. Removing a record's file is not a
|
|
12
|
+
# withdrawal, and an id still published by any record in the harvest is never deleted.
|
|
13
|
+
class Withdrawals
|
|
14
|
+
FILENAME = 'withdrawn.json'
|
|
15
|
+
|
|
16
|
+
# The reasons the convention defines; consumers treat any other value as 'other'
|
|
17
|
+
REASONS = %w[superseded duplicate upstream-removed rights privacy quality out-of-scope other].freeze
|
|
18
|
+
|
|
19
|
+
# W3C-DTF: a year, year and month, date, or date and time with a time zone
|
|
20
|
+
DATE_FORMAT = /\A\d{4}(-\d{2}(-\d{2}(T\d{2}:\d{2}(:\d{2}(\.\d+)?)?(Z|[+-]\d{2}:\d{2}))?)?)?\z/
|
|
21
|
+
|
|
22
|
+
Entry = Struct.new(:id, :date, :reason, :source_path, keyword_init: true)
|
|
23
|
+
|
|
24
|
+
# Raised internally when a log doesn't match the schema
|
|
25
|
+
class InvalidLog < StandardError; end
|
|
26
|
+
|
|
27
|
+
attr_reader :ogm_path
|
|
28
|
+
|
|
29
|
+
# Initialize a new reader of withdrawal logs
|
|
30
|
+
# @param ogm_path [String] path to the directory where repositories are cloned
|
|
31
|
+
# @param skip_repos [Array<String>] list of repository names whose logs are ignored
|
|
32
|
+
# @param logger [Logger] logger to use for logging messages
|
|
33
|
+
def initialize(
|
|
34
|
+
ogm_path:,
|
|
35
|
+
skip_repos: ENV.fetch('OGM_SKIP_REPOS', '').split(',').map(&:strip),
|
|
36
|
+
logger: GeoCombine::Logger.logger
|
|
37
|
+
)
|
|
38
|
+
@ogm_path = ogm_path
|
|
39
|
+
@skip_repos = skip_repos
|
|
40
|
+
@logger = logger
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Paths of the logs to read: one at the root of each repository, plus one at the root
|
|
44
|
+
# of ogm_path itself, for when it is a single repository. A repository that isn't being
|
|
45
|
+
# harvested doesn't get to delete from the index.
|
|
46
|
+
def paths
|
|
47
|
+
repo_logs = Dir.glob(File.join('*', FILENAME), base: ogm_path).sort
|
|
48
|
+
.reject { |path| @skip_repos.include?(File.dirname(path)) }
|
|
49
|
+
[FILENAME, *repo_logs].map { |path| File.join(ogm_path, path) }.select { |path| File.file?(path) }
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Withdrawn entries keyed by id; when an id is listed more than once, the last entry wins
|
|
53
|
+
def entries
|
|
54
|
+
@entries ||= paths.each_with_object({}) do |path, entries|
|
|
55
|
+
read(path).each { |entry| entries[entry.id] = entry }
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Delete withdrawn records from the index. The harvester must have walked every record
|
|
60
|
+
# first, so it knows which ids are still published: those are never deleted.
|
|
61
|
+
# Every run deletes every withdrawn id again; Solr ignores a delete for an id it doesn't have.
|
|
62
|
+
# Returns the ids deleted.
|
|
63
|
+
def apply(harvester:, indexer:)
|
|
64
|
+
unless harvester.walk_complete?
|
|
65
|
+
@logger.error 'not deleting withdrawn records; the harvest did not walk every record'
|
|
66
|
+
return []
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
present, absent = entries.keys.partition { |id| harvester.present_ids.include?(id) }
|
|
70
|
+
warn_still_published(present)
|
|
71
|
+
deleted = absent.empty? ? [] : indexer.delete(absent)
|
|
72
|
+
|
|
73
|
+
@logger.info "withdrawals: #{entries.size} entries, #{present.size} still published, " \
|
|
74
|
+
"#{deleted.size} deleted or already absent"
|
|
75
|
+
deleted
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
private
|
|
79
|
+
|
|
80
|
+
# A repository that withdraws a record it still publishes contradicts itself; presence wins
|
|
81
|
+
def warn_still_published(ids)
|
|
82
|
+
ids.each do |id|
|
|
83
|
+
@logger.warn "not deleting #{id}; it is withdrawn in #{entries[id].source_path} but still published"
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# The valid entries in a log. A log that can't be read is logged and skipped, so one
|
|
88
|
+
# bad log can't stop the others, or indexing.
|
|
89
|
+
def read(path)
|
|
90
|
+
json = File.read(path, mode: 'r:bom|utf-8')
|
|
91
|
+
raise JSON::ParserError, 'not valid UTF-8' unless json.valid_encoding?
|
|
92
|
+
|
|
93
|
+
log = JSON.parse(json)
|
|
94
|
+
validate(log)
|
|
95
|
+
log['withdrawn'].each_with_index.filter_map { |entry, index| entry_from(entry, path, index) }
|
|
96
|
+
rescue InvalidLog, JSON::ParserError, SystemCallError => e
|
|
97
|
+
@logger.error "skipping withdrawal log #{path}: #{e.message}"
|
|
98
|
+
[]
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def validate(log)
|
|
102
|
+
raise InvalidLog, 'not a JSON object with a "withdrawn" array' unless log.is_a?(Hash) && log['withdrawn'].is_a?(Array)
|
|
103
|
+
|
|
104
|
+
version = log.fetch('ogm_version', '1.0').to_s
|
|
105
|
+
raise InvalidLog, "unsupported ogm_version #{version}" unless version.split('.').first == '1'
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# An entry, or nil if it has no usable id or date
|
|
109
|
+
def entry_from(entry, path, index)
|
|
110
|
+
return invalid(path, index, 'not a JSON object') unless entry.is_a?(Hash)
|
|
111
|
+
|
|
112
|
+
id, date, reason = entry.values_at('id', 'date', 'reason')
|
|
113
|
+
return invalid(path, index, "unusable id #{id.inspect}") unless GeoCombine::Schema.valid_id?(id)
|
|
114
|
+
return invalid(path, index, "invalid date #{date.inspect} for #{id}") unless date.is_a?(String) && date.match?(DATE_FORMAT)
|
|
115
|
+
|
|
116
|
+
reason = 'other' unless reason.nil? || REASONS.include?(reason)
|
|
117
|
+
Entry.new(id:, date:, reason:, source_path: path)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def invalid(path, index, problem)
|
|
121
|
+
@logger.error "skipping entry #{index} in withdrawal log #{path}: #{problem}"
|
|
122
|
+
nil
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
data/lib/geo_combine.rb
CHANGED
|
@@ -40,12 +40,33 @@ module GeoCombine
|
|
|
40
40
|
GeoCombine::Geoblacklight.new(xsl_geoblacklight.apply_to(@metadata), fields)
|
|
41
41
|
end
|
|
42
42
|
|
|
43
|
+
##
|
|
44
|
+
# Perform an XSLT transformation on metadata using an object's Aardvark XSL
|
|
45
|
+
# @param [Hash] fields additional Aardvark fields to be passed
|
|
46
|
+
# @return [GeoCombine::GeoblacklightAardvark] the data transformed into the
|
|
47
|
+
# OGM Aardvark schema
|
|
48
|
+
def to_aardvark(fields = {})
|
|
49
|
+
GeoCombine::GeoblacklightAardvark.new(
|
|
50
|
+
xsl_aardvark.apply_to(@metadata, aardvark_xsl_params(fields)),
|
|
51
|
+
fields
|
|
52
|
+
)
|
|
53
|
+
end
|
|
54
|
+
|
|
43
55
|
##
|
|
44
56
|
# Perform an XSLT transformation to HTML using an object's XSL
|
|
45
57
|
# @return [String] the xml transformed to an HTML String
|
|
46
58
|
def to_html
|
|
47
59
|
xsl_html.transform(@metadata).to_html
|
|
48
60
|
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
def aardvark_xsl_params(fields)
|
|
65
|
+
Nokogiri::XSLT.quote_params(
|
|
66
|
+
'provider' => fields['schema_provider_s'].to_s,
|
|
67
|
+
'id' => fields['id'].to_s
|
|
68
|
+
)
|
|
69
|
+
end
|
|
49
70
|
end
|
|
50
71
|
end
|
|
51
72
|
|
|
@@ -64,13 +85,16 @@ require 'geo_combine/bounding_box'
|
|
|
64
85
|
# Require additional classes
|
|
65
86
|
require 'geo_combine/fgdc'
|
|
66
87
|
require 'geo_combine/geoblacklight'
|
|
88
|
+
require 'geo_combine/geoblacklight_aardvark'
|
|
67
89
|
require 'geo_combine/iso19139'
|
|
68
90
|
require 'geo_combine/esri_open_data'
|
|
69
91
|
require 'geo_combine/ckan_metadata'
|
|
70
92
|
require 'geo_combine/ogp'
|
|
71
93
|
|
|
72
94
|
# Require harvesting/indexing files
|
|
73
|
-
require 'geo_combine/
|
|
95
|
+
require 'geo_combine/harvester'
|
|
96
|
+
require 'geo_combine/indexer'
|
|
97
|
+
require 'geo_combine/withdrawals'
|
|
74
98
|
|
|
75
99
|
# Migrators
|
|
76
100
|
require 'geo_combine/migrators/v1_aardvark_migrator'
|
data/lib/tasks/geo_combine.rake
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'json'
|
|
4
|
-
require 'rsolr'
|
|
5
|
-
require 'find'
|
|
6
|
-
require 'faraday/net_http_persistent'
|
|
7
3
|
require 'geo_combine/harvester'
|
|
8
4
|
require 'geo_combine/indexer'
|
|
9
|
-
require 'geo_combine/
|
|
5
|
+
require 'geo_combine/withdrawals'
|
|
10
6
|
|
|
11
7
|
namespace :geocombine do
|
|
12
8
|
desc 'Clone OpenGeoMetadata repositories'
|
|
@@ -25,21 +21,24 @@ namespace :geocombine do
|
|
|
25
21
|
args[:repo] ? harvester.pull(args.repo) : harvester.pull_all
|
|
26
22
|
end
|
|
27
23
|
|
|
28
|
-
desc '
|
|
24
|
+
desc 'Reindex all records and delete withdrawn records'
|
|
29
25
|
task :index, [:ogm_path] => [:environment] do |_t, args|
|
|
30
26
|
ogm_path = args.ogm_path || ENV.fetch('OGM_PATH', 'tmp/opengeometadata')
|
|
31
27
|
|
|
32
28
|
indexer = GeoCombine::Indexer.new
|
|
33
29
|
harvester = GeoCombine::Harvester.new(ogm_path: ogm_path)
|
|
34
30
|
indexer.index(harvester.docs_to_index)
|
|
31
|
+
GeoCombine::Withdrawals.new(ogm_path: ogm_path).apply(harvester: harvester, indexer: indexer)
|
|
35
32
|
end
|
|
36
33
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
raise ArgumentError, 'A site argument is required' unless args.site
|
|
34
|
+
desc 'Delete withdrawn records without reindexing'
|
|
35
|
+
task :withdraw, [:ogm_path] => [:environment] do |_t, args|
|
|
36
|
+
ogm_path = args.ogm_path || ENV.fetch('OGM_PATH', 'tmp/opengeometadata')
|
|
41
37
|
|
|
42
|
-
|
|
43
|
-
|
|
38
|
+
indexer = GeoCombine::Indexer.new
|
|
39
|
+
harvester = GeoCombine::Harvester.new(ogm_path: ogm_path)
|
|
40
|
+
# Walk every record without indexing, to learn which ids are still published
|
|
41
|
+
harvester.docs_to_index.each { nil }
|
|
42
|
+
GeoCombine::Withdrawals.new(ogm_path: ogm_path).apply(harvester: harvester, indexer: indexer)
|
|
44
43
|
end
|
|
45
44
|
end
|