Skip to content

Commit

Permalink
added status to lod_links and corrected approval mechanism + warning …
Browse files Browse the repository at this point in the history
…if not approved
  • Loading branch information
MartinHinz committed Dec 15, 2024
1 parent f298979 commit 5538e3e
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 82 deletions.
3 changes: 2 additions & 1 deletion app/controllers/lod_links_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ def lod_link_params
:source,
:linkable_type,
:linkable_id,
:revision_comment
:revision_comment,
:status
])
end

Expand Down
28 changes: 18 additions & 10 deletions app/models/lod_link.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
# == Schema Information
#
# Table name: wikidata_links
# Table name: lod_links
#
# id :bigint not null, primary key
# qid :integer
# wikidata_linkable_type :string
# created_at :datetime not null
# updated_at :datetime not null
# wikidata_linkable_id :bigint
# id :bigint not null, primary key
# data :jsonb
# linkable_type :string not null
# source :string not null
# status :string default("pending"), not null
# created_at :datetime not null
# updated_at :datetime not null
# external_id :string not null
# linkable_id :bigint not null
#
# Indexes
#
# index_wikidata_links_on_linkable_type_and_linkable_id (wikidata_linkable_type,wikidata_linkable_id)
# index_wikidata_links_on_qid (qid)
# index_wikidata_links_on_wikidata_linkable (wikidata_linkable_type,wikidata_linkable_id)
# index_lod_links_on_linkable_type_and_linkable_id (linkable_type,linkable_id)
# index_lod_links_on_source_and_external_id (source,external_id) UNIQUE
#
class LodLink < ApplicationRecord
include Turbo::Broadcastable
Expand All @@ -29,8 +31,14 @@ class LodLink < ApplicationRecord

validates :external_id, presence: true, numericality: { only_integer: true }
validates :source, presence: true

enum status: { pending: "pending", approved: "approved"}

belongs_to :linkable, polymorphic: true

# Scopes for filtering matches
scope :pending, -> { where(status: "pending") }
scope :approved, -> { where(status: "approved") }

def qcode
"Q#{external_id}"
Expand Down
70 changes: 32 additions & 38 deletions app/models/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Site < ApplicationRecord
@issues = [ :missing_coordinates, :invalid_coordinates, :missing_country_code ]

include NeedsLods
@lods = [ :needs_wikidata_link ]
@lods = [ :needs_wikidata_link, :pending_wikidata_link ]

include PgSearch::Model
pg_search_scope :search,
Expand Down Expand Up @@ -122,21 +122,35 @@ def country_code_from_coordinates
end

def self.wikidata_match_candidates_batch(sites)

# Filter for sites without a wikidata_link
sites_without_wikidata_link = sites.select { |site| site.wikidata_link.nil? }
# Filter for sites without a Wikidata link
sites_without_wikidata_link = sites.select { |site| site.lod_links.where(source: "Wikidata").empty? }

return [] if sites_without_wikidata_link.empty?

# Generate a cache key based on site names
cache_key = generate_cache_key(sites_without_wikidata_link)

Rails.cache.fetch(cache_key, expires_in: 24.hours) do
site_names = extract_site_names(sites_without_wikidata_link)
sparql_query = build_sparql_query(site_names)
response = execute_sparql_request(sparql_query)
parse_wikidata_response(response)
# Prepare the data for SPARQL query
site_names = extract_site_names(sites_without_wikidata_link)
sparql_query = build_sparql_query(site_names)
response = execute_sparql_request(sparql_query)
wikidata_results = parse_wikidata_response(response)

# Iterate through the results and populate lod_links
wikidata_results.each do |site_name, matches|
site = sites_without_wikidata_link.find { |s| s.name == site_name }
next unless site

matches.each do |match|
# Find or create the LOD link for this Wikidata match
site.lod_links.find_or_create_by(source: "Wikidata", external_id: match.qid) do |lod_link|
lod_link.data = {
label: match.label,
description: match.description
}
lod_link.save!
end
end
end

wikidata_results # Return results for reference
end

def n_c14s
Expand Down Expand Up @@ -193,32 +207,12 @@ def needs_wikidata_link?
wikidata_link.blank?
end

# scope :with_potential_wikidata_match, -> {
# cache_keys = Rails.cache.instance_variable_get(:@data).keys # Inspect cached keys
#
# matched_ids = []
#
# cache_keys.each do |key|
# cached_data = Rails.cache.read(key)
# next if cached_data.blank?
#
# logger.debug "*******************"
# logger.debug cached_data.to_yaml
# logger.debug "*******************"
#
# # Find sites matching cached names but exclude those with existing wikidata_links
# matched_ids += Site.where(name: cached_data.keys).where.not(id: LodLink.where(linkable_type: "Site", source: "Wikidata").select(:linkable_id)).pluck(:id)
# end
#
# where(id: matched_ids.uniq)
# }
# def with_potential_wikidata_match?
# return false if wikidata_link.present? # Exclude if a Wikidata link already exists
#
# cache_key = Site.generate_cache_key([self])
# cached_matches = Rails.cache.read(cache_key) || {}
# cached_matches.key?(self.name)
# end
scope :pending_wikidata_link, -> {
joins(:lod_links).where(lod_links: { source: "Wikidata", status: "pending" })
}
def pending_wikidata_link?
wikidata_link&.status == "pending"
end

private

Expand Down
27 changes: 27 additions & 0 deletions app/views/lod_links/_lod_link.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
<dd class="col-sm text-sm-end">
<%= link_to @wikidata_link.qcode, @wikidata_link.url, title: "#{@wikidata_link.title} - Wikidata" %>
<% if item.present? %>(<%= item.label("en") %>)<% end %>
<% if @wikidata_link.status == "pending" %>
<span class="text-muted">
<%= bs_icon "exclamation-circle",
class: "text-warning",
data: { "bs-toggle": "tooltip", controller: "tooltips" },
title: "Wikidata link inferred from site name."
%>
</span>
<% end %>
</dd>
</div>

Expand All @@ -26,6 +35,15 @@
<dd class="col-sm text-sm-end">
<span class="badge text-bg-info">en</span>
<%= link_to item.sitelink_title("enwiki"), item.sitelink_url("enwiki"), title: "#{item.sitelink_title('enwiki')} - Wikipedia" %>
<% if @wikidata_link.status == "pending" %>
<span class="text-muted">
<%= bs_icon "exclamation-circle",
class: "text-warning",
data: { "bs-toggle": "tooltip", controller: "tooltips" },
title: "Wikipedia link link inferred from site name."
%>
</span>
<% end %>
</dd>
</div>
<% end %>
Expand All @@ -38,6 +56,15 @@
</dt>
<dd class="col-sm text-sm-end">
<%= link_to item.sitelink_title("commonswiki"), item.sitelink_url("commonswiki"), title: "#{item.sitelink_title('commonswiki')} - Wikimedia Commons" %>
<% if @wikidata_link.status == "pending" %>
<span class="text-muted">
<%= bs_icon "exclamation-circle",
class: "text-warning",
data: { "bs-toggle": "tooltip", controller: "tooltips" },
title: "Wikicommons link inferred from site name."
%>
</span>
<% end %>
</dd>
</div>
<% end %>
Expand Down
64 changes: 32 additions & 32 deletions app/views/lods/sites/_table.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@
<td class="font-monospace text-end"><%= site.c14s_count %></td>
<td class="font-monospace text-end"><%= site.typos_count %></td>
<td class="font-monospace text-end">
<% if site.wikidata_link %>
<% if site.wikidata_link && site.wikidata_link.status == "approved" %>
<%= link_to site.wikidata_link.external_id, site.wikidata_link.url %>
<% elsif @wikidata_matches.key?(site.name) && @wikidata_matches[site.name].present? %>
<% suggested_match = @wikidata_matches[site.name].first %>
<i>Suggestion: <%= suggested_match.qid %>:
<%= link_to "#{suggested_match.label} (#{suggested_match.description})",
suggested_match.url, target: "_blank" %>
<% elsif site.wikidata_link && site.wikidata_link.status == "pending" %>
<i>Suggestion: <%= site.wikidata_link.external_id %>:
<%= link_to "#{site.wikidata_link.data['label']} (#{site.wikidata_link.data['description']})",
site.wikidata_link.data['url'], target: "_blank" %>
</i>
<% else %>
<span class="text-muted">No suggestions</span>
Expand All @@ -67,33 +66,34 @@
<%= lod_badge(lod) %>
<% end %>
</td>
<% if can?(:manage, Site) %>
<td>
<div class="btn-toolbar justify-content-end">
<div class="btn-group me-2">
<%= bootstrap_form_for LodLink.new, url: lod_links_path, method: :post do |f| %>
<%= f.hidden_field :linkable_type, value: "Site" %>
<%= f.hidden_field :linkable_id, value: site.id %>
<%= f.hidden_field :source, value: "Wikidata" %>
<% if can?(:manage, Site) %>
<td>
<div class="btn-toolbar justify-content-end">
<div class="btn-group me-2">
<% if site.wikidata_link %>
<%= bootstrap_form_for site.wikidata_link, url: lod_link_path(site.wikidata_link), method: :patch do |f| %>
<%= f.hidden_field :linkable_type, value: "Site" %>
<%= f.hidden_field :linkable_id, value: site.id %>
<%= f.hidden_field :source, value: "Wikidata" %>

<% if @wikidata_matches.key?(site.name) && @wikidata_matches[site.name].present? %>
<% suggested_match = @wikidata_matches[site.name].first %>
<%= f.hidden_field :external_id, value: suggested_match.qid %>
<%= f.hidden_field :revision_comment, value: "Matched to Wikidata" %>
<%= f.primary class: "btn btn-sm btn-primary", title: "Use suggested Wikidata match" do %>
<%= bs_icon "check-lg" %>
<% end %>
<% else %>
<%= button_tag class: "btn btn-sm btn-primary", disabled: true do %>
<%= bs_icon "check-lg" %>
<% end %>
<% end %>
<% end %>
</div>
<%= render "shared/action_buttons_compact", i: site %>
</div>
</td>
<% end %>
<% if site.wikidata_link.status == "pending" %>
<%= f.hidden_field :status, value: "approved" %>
<%= f.hidden_field :revision_comment, value: "Matched to Wikidata" %>
<%= f.primary class: "btn btn-sm btn-primary", title: "Use suggested Wikidata match" do %>
<%= bs_icon "check-lg" %>
<% end %>
<% else %>
<%= button_tag class: "btn btn-sm btn-primary", disabled: true do %>
<%= bs_icon "check-lg" %>
<% end %>
<% end %>
<% end %>
<% end %>
</div>
<%= render "shared/action_buttons_compact", i: site %>
</div>
</td>
<% end %>
</tr>
<% end %>
</tbody>
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20241215102659_add_status_to_lod_links.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddStatusToLodLinks < ActiveRecord::Migration[7.0]
def change
add_column :lod_links, :status, :string, default: "pending", null: false
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2024_12_15_071622) do
ActiveRecord::Schema[7.0].define(version: 2024_12_15_102659) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
enable_extension "plpgsql"
Expand Down Expand Up @@ -139,6 +139,7 @@
t.jsonb "data"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "status", default: "pending", null: false
t.index ["linkable_type", "linkable_id"], name: "index_lod_links_on_linkable_type_and_linkable_id"
t.index ["source", "external_id"], name: "index_lod_links_on_source_and_external_id", unique: true
end
Expand Down

0 comments on commit 5538e3e

Please sign in to comment.