Skip to content

Commit f298979

Browse files
committed
moved wikidata_links to more general lod_links
1 parent 8bd98b7 commit f298979

File tree

15 files changed

+166
-79
lines changed

15 files changed

+166
-79
lines changed
Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
class WikidataLinksController < ApplicationController
1+
class LodLinksController < ApplicationController
22
load_and_authorize_resource
33

4-
before_action :set_wikidata_link, only: [:show, :edit, :update, :destroy]
4+
before_action :set_lod_link, only: [:show, :edit, :update, :destroy]
55

66
def show
77
@wikidata_link.request_item
88
if @wikidata_link.item.sitelink_title("enwiki").present?
99
@wikidata_link.item.request_wikipedia_extract
1010
end
1111

12-
render partial: "wikidata_link"
12+
render partial: "lod_link"
1313
end
1414

1515
def new
@@ -19,27 +19,27 @@ def edit
1919
end
2020

2121
def create
22-
@wikidata_link = WikidataLink.new(wikidata_link_params)
22+
@lod_link = LodLink.new(lod_link_params)
2323

2424
respond_to do |format|
25-
if @wikidata_link.save
25+
if @lod_link.save
2626
format.html { redirect_back fallback_location: root_path, notice: success_notice }
27-
format.json { render :show, status: :created, location: @wikidata_link }
27+
format.json { render :show, status: :created, location: @lod_link }
2828
else
2929
format.html { render :new, status: :unprocessable_entity }
30-
format.json { render json: @wikidata_link.errors, status: :unprocessable_entity }
30+
format.json { render json: @lod_link.errors, status: :unprocessable_entity }
3131
end
3232
end
3333
end
3434

3535
def update
3636
respond_to do |format|
37-
if @wikidata_link.update(wikidata_link_params)
37+
if @lod_link.update(lod_link_params)
3838
format.html { redirect_back fallback_location: root_path, notice: success_notice }
39-
format.json { render :show, status: :ok, location: @wikidata_link }
39+
format.json { render :show, status: :ok, location: @lod_link }
4040
else
4141
format.html { render :edit, status: :unprocessable_entity }
42-
format.json { render json: @wikidata_link.errors, status: :unprocessable_entity }
42+
format.json { render json: @lod_link.errors, status: :unprocessable_entity }
4343
end
4444
end
4545
end
@@ -49,24 +49,26 @@ def destroy
4949

5050
private
5151

52-
def set_wikidata_link
53-
@wikidata_link = WikidataLink.find(params[:id])
52+
def set_lod_link
53+
@lod_link = LodLink.find(params[:id])
54+
@wikidata_link = LodLink.where(source: "Wikidata").find(params[:id])
5455
end
5556

56-
def wikidata_link_params
57-
params.require(:wikidata_link).permit([
58-
:qid,
59-
:wikidata_linkable_type,
60-
:wikidata_linkable_id,
57+
def lod_link_params
58+
params.require(:lod_link).permit([
59+
:external_id,
60+
:source,
61+
:linkable_type,
62+
:linkable_id,
6163
:revision_comment
6264
])
6365
end
6466

6567
def success_notice
66-
@wikidata_link.wikidata_linkable_type +
68+
@lod_link.linkable_type +
6769
":" +
68-
@wikidata_link.wikidata_linkable_id.to_s +
70+
@lod_link.linkable_id.to_s +
6971
" is now linked to Wikidata item Q" +
70-
@wikidata_link.qid.to_s
72+
@lod_link.external_id.to_s
7173
end
7274
end

app/models/wikidata_link.rb renamed to app/models/lod_link.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# index_wikidata_links_on_qid (qid)
1616
# index_wikidata_links_on_wikidata_linkable (wikidata_linkable_type,wikidata_linkable_id)
1717
#
18-
class WikidataLink < ApplicationRecord
18+
class LodLink < ApplicationRecord
1919
include Turbo::Broadcastable
2020
include Versioned
2121

@@ -27,20 +27,21 @@ class WikidataLink < ApplicationRecord
2727

2828
attr_reader :item
2929

30-
validates :qid, presence: true, numericality: { only_integer: true }
30+
validates :external_id, presence: true, numericality: { only_integer: true }
31+
validates :source, presence: true
3132

32-
belongs_to :wikidata_linkable, polymorphic: true
33+
belongs_to :linkable, polymorphic: true
3334

3435
def qcode
35-
"Q#{qid}"
36+
"Q#{external_id}"
3637
end
3738

3839
def url
3940
BASE_URL + qcode
4041
end
4142

4243
def request_item
43-
@item = WikidataItem.new(qid)
44+
@item = WikidataItem.new(external_id)
4445
end
4546

4647
def title

app/models/site.rb

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ class Site < ApplicationRecord
2929
has_many :typos, through: :contexts
3030
has_many :citations, as: :citing
3131
has_many :references, through: :citations
32-
has_one :wikidata_link, as: :wikidata_linkable
33-
has_one :wikidata_item, as: :wikidata_link
32+
has_many :lod_links, as: :linkable, dependent: :destroy
3433

3534
has_and_belongs_to_many :site_types, optional: true
3635

@@ -95,6 +94,10 @@ def country
9594
ISO3166::Country[country_code] ||
9695
ISO3166::Country.find_country_by_any_name(country_code)
9796
end
97+
98+
def wikidata_link
99+
lod_links.where(source: "Wikidata").first
100+
end
98101

99102
def country_from_coordinates
100103
return nil if lat.blank? || lng.blank?
@@ -117,10 +120,9 @@ def country_code_from_coordinates
117120
return nil
118121
end
119122
end
120-
121-
122123

123124
def self.wikidata_match_candidates_batch(sites)
125+
124126
# Filter for sites without a wikidata_link
125127
sites_without_wikidata_link = sites.select { |site| site.wikidata_link.nil? }
126128

@@ -183,18 +185,47 @@ def missing_country_code?
183185
country_code.blank?
184186
end
185187

188+
# LODs
186189
scope :needs_wikidata_link, -> {
187-
where.not(id: WikidataLink.where(wikidata_linkable_type: "Site").select(:wikidata_linkable_id).distinct)
190+
where.not(id: LodLink.where(linkable_type: "Site", source: "Wikidata").select(:linkable_id).distinct)
188191
}
189192
def needs_wikidata_link?
190193
wikidata_link.blank?
191194
end
195+
196+
# scope :with_potential_wikidata_match, -> {
197+
# cache_keys = Rails.cache.instance_variable_get(:@data).keys # Inspect cached keys
198+
#
199+
# matched_ids = []
200+
#
201+
# cache_keys.each do |key|
202+
# cached_data = Rails.cache.read(key)
203+
# next if cached_data.blank?
204+
#
205+
# logger.debug "*******************"
206+
# logger.debug cached_data.to_yaml
207+
# logger.debug "*******************"
208+
#
209+
# # Find sites matching cached names but exclude those with existing wikidata_links
210+
# matched_ids += Site.where(name: cached_data.keys).where.not(id: LodLink.where(linkable_type: "Site", source: "Wikidata").select(:linkable_id)).pluck(:id)
211+
# end
212+
#
213+
# where(id: matched_ids.uniq)
214+
# }
215+
# def with_potential_wikidata_match?
216+
# return false if wikidata_link.present? # Exclude if a Wikidata link already exists
217+
#
218+
# cache_key = Site.generate_cache_key([self])
219+
# cached_matches = Rails.cache.read(cache_key) || {}
220+
# cached_matches.key?(self.name)
221+
# end
192222

193223
private
194224

195225
# Generate a unique cache key for the batch of sites
196226
def self.generate_cache_key(sites)
197-
"wikidata_match_batch/#{Digest::MD5.hexdigest(sites.pluck(:name).sort.join(','))}"
227+
site_names = sites.pluck(:name).compact.reject(&:blank?) # Remove nil and blank names
228+
"wikidata_match_batch/#{Digest::MD5.hexdigest(site_names.sort.join(','))}"
198229
end
199230

200231
# Extracts names from the sites

app/views/lod_links/_fields.html.erb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!-- Hidden Fields -->
2+
<%= f.hidden_field :linkable_type %>
3+
<%= f.hidden_field :linkable_id %>
4+
<%= f.hidden_field :source, value: "Wikidata" %>
5+
6+
<!-- QID Input Field -->
7+
<div class="input-group">
8+
<%= f.text_field :external_id, prepend: "Q", hide_label: true, placeholder: "Enter QID" %>
9+
</div>
10+
11+
<!-- Suggestions from Lod Match -->
12+
<p>Suggestion from Name:</p>
13+
<ul>
14+
<% if wikidata_matches_for(f.object.linkable).present? %>
15+
<% wikidata_matches_for(f.object.linkable)[f.object.linkable.name]&.each do |match| %>
16+
<li>
17+
<%= match.qid %>:
18+
<%= link_to "#{match.label} (#{match.description})", match.url, target: "_blank" %>
19+
</li>
20+
<% end %>
21+
<% else %>
22+
<li>No suggestions available</li>
23+
<% end %>
24+
</ul>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
<%= bootstrap_form_with(model: @wikidata_link, id: "wikidataLinkForm") do |f| %>
1+
<%= bootstrap_form_with(model: @lod_link, id: "lodLinkForm") do |f| %>
22
<%= render "fields", f: f %>
33

44
<%= render "application/paper_trail/revision_comment_field", f: f %>
55

66
<%= content_for :buttons do %>
77
<%= button_tag "Cancel", type: "reset", class: "btn btn-outline-danger",
8-
form: "wikidataLinkForm", data: { "bs-dismiss": "modal" } %>
9-
<%= button_tag "Save", class: "btn btn-primary", form: "wikidataLinkForm" %>
8+
form: "lodLinkForm", data: { "bs-dismiss": "modal" } %>
9+
<%= button_tag "Save", class: "btn btn-primary", form: "lodLinkForm" %>
1010
<% end %>
1111
<% end %>

app/views/lods/sites/_table.html.erb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
<td class="font-monospace text-end"><%= site.typos_count %></td>
5252
<td class="font-monospace text-end">
5353
<% if site.wikidata_link %>
54-
<%= link_to site.wikidata_link.qid, site.wikidata_link.url %>
54+
<%= link_to site.wikidata_link.external_id, site.wikidata_link.url %>
5555
<% elsif @wikidata_matches.key?(site.name) && @wikidata_matches[site.name].present? %>
5656
<% suggested_match = @wikidata_matches[site.name].first %>
5757
<i>Suggestion: <%= suggested_match.qid %>:
@@ -71,13 +71,14 @@
7171
<td>
7272
<div class="btn-toolbar justify-content-end">
7373
<div class="btn-group me-2">
74-
<%= bootstrap_form_for WikidataLink.new, url: wikidata_links_path, method: :post do |f| %>
75-
<%= f.hidden_field :wikidata_linkable_type, value: "Site" %>
76-
<%= f.hidden_field :wikidata_linkable_id, value: site.id %>
74+
<%= bootstrap_form_for LodLink.new, url: lod_links_path, method: :post do |f| %>
75+
<%= f.hidden_field :linkable_type, value: "Site" %>
76+
<%= f.hidden_field :linkable_id, value: site.id %>
77+
<%= f.hidden_field :source, value: "Wikidata" %>
7778

7879
<% if @wikidata_matches.key?(site.name) && @wikidata_matches[site.name].present? %>
7980
<% suggested_match = @wikidata_matches[site.name].first %>
80-
<%= f.hidden_field :qid, value: suggested_match.qid %>
81+
<%= f.hidden_field :external_id, value: suggested_match.qid %>
8182
<%= f.hidden_field :revision_comment, value: "Matched to Wikidata" %>
8283
<%= f.primary class: "btn btn-sm btn-primary", title: "Use suggested Wikidata match" do %>
8384
<%= bs_icon "check-lg" %>

app/views/sites/show.html.erb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,19 @@
7272
<h3 class="mb-0">
7373
<i class="bi-link-45deg"></i> Linked Data</h3>
7474
<% if @site.wikidata_link.present? %>
75-
<% if can? :manage, WikidataLink %>
76-
<%= floating_button edit_wikidata_link_path(@site.wikidata_link),
75+
<% if can? :manage, LodLink %>
76+
<%= floating_button edit_lod_link_path(@site.wikidata_link),
7777
data: { turbo_frame: "remote_modal" } %>
7878
<% end %>
7979
<%= turbo_frame_tag @site.wikidata_link, src: @site.wikidata_link %>
8080
<% else %>
8181
<p>There is no linked data available for this record.</p>
82-
<% if can? :manage, WikidataLink %>
82+
<% if can? :manage, LodLink %>
8383
<ul>
8484
<li><%= link_to "Add Wikidata link",
85-
new_wikidata_link_path(wikidata_link: {
86-
wikidata_linkable_type: "Site",
87-
wikidata_linkable_id: @site
85+
new_lod_link_path(lod_link: {
86+
linkable_type: "Site",
87+
linkable_id: @site
8888
}),
8989
data: { turbo_frame: "remote_modal" } %>
9090
</li>

0 commit comments

Comments
 (0)