From 01a326b2eeb06e9e4a6ed6be96ea2db4ff36fdee Mon Sep 17 00:00:00 2001 From: Kosuke Tanabe Date: Thu, 15 Feb 2024 23:57:16 +0900 Subject: [PATCH 1/3] add EnjuOai::Jpcoar20 module --- app/controllers/oai_controller.rb | 17 +++- app/models/agent.rb | 5 + .../enju_oai/{jpcoar.rb => jpcoar20.rb} | 15 ++- app/models/concerns/enju_oai/jpcoar20dummy.rb | 26 ++++++ app/models/concerns/enju_oai/oai_model.rb | 91 ++++++++++++++++--- spec/requests/oai_controller_spec.rb | 20 +--- 6 files changed, 132 insertions(+), 42 deletions(-) rename app/models/concerns/enju_oai/{jpcoar.rb => jpcoar20.rb} (70%) create mode 100644 app/models/concerns/enju_oai/jpcoar20dummy.rb diff --git a/app/controllers/oai_controller.rb b/app/controllers/oai_controller.rb index 169f350f3b..bb43b0d5f6 100644 --- a/app/controllers/oai_controller.rb +++ b/app/controllers/oai_controller.rb @@ -1,9 +1,18 @@ -class OaiController < ApplicationController - skip_after_action :verify_authorized - +class OaiController < ActionController::API def index provider = OaiProvider.new - response = provider.process_request(oai_params.to_h) + + # ruby-oaiではフォーマット名を含んだメソッド(e.g. to_jpcoar)が呼ばれるが、 + # メソッド名にはドットを含められないため、リクエストを受け取ったときに変換 + # している + case params['metadataPrefix'] + when 'jpcoar_2.0' + request_hash = oai_params.to_h.merge({'metadataPrefix': 'jpcoar_20'}) + else + request_hash = oai_params.to_h + end + + response = provider.process_request(request_hash) render body: response, content_type: 'text/xml' end diff --git a/app/models/agent.rb b/app/models/agent.rb index d30da56682..a4351aad9a 100644 --- a/app/models/agent.rb +++ b/app/models/agent.rb @@ -288,6 +288,11 @@ def self.new_agents(agents_params) agents end + + def to_jpcoar + xml = ::Builder::XmlMarkup.new + xml.tag! 'jpcoar:creatorName', full_name + end end # == Schema Information diff --git a/app/models/concerns/enju_oai/jpcoar.rb b/app/models/concerns/enju_oai/jpcoar20.rb similarity index 70% rename from app/models/concerns/enju_oai/jpcoar.rb rename to app/models/concerns/enju_oai/jpcoar20.rb index 495befddca..9b61d9d4a4 100644 --- a/app/models/concerns/enju_oai/jpcoar.rb +++ b/app/models/concerns/enju_oai/jpcoar20.rb @@ -1,10 +1,10 @@ +# OAI-PMHでJPCOARスキーマ2.0を扱うためのモジュール module EnjuOai - class Jpcoar < OAI::Provider::Metadata::Format - + class Jpcoar20 < OAI::Provider::Metadata::Format def initialize - @prefix = 'jpcoar' - @schema = 'https://github.com/JPCOAR/schema/blob/master/1.0/jpcoar_scm.xsd' - @namespace = 'https://github.com/JPCOAR/schema/blob/master/1.0/' + @prefix = 'jpcoar_20' + @schema = 'https://github.com/JPCOAR/schema/blob/master/2.0/jpcoar_scm.xsd' + @namespace = 'https://github.com/JPCOAR/schema/blob/master/2.0/' @element_namespace = 'dc' @fields = [ :title, :creator, :subject, :description, :publisher, :contributor, :date, :type, :format, :identifier, @@ -13,14 +13,13 @@ def initialize def header_specification { - 'xmlns:jpcoar' => "https://github.com/JPCOAR/schema/blob/master/1.0/", + 'xmlns:jpcoar' => "https://github.com/JPCOAR/schema/blob/master/2.0/", 'xmlns:dc' => "http://purl.org/dc/elements/1.1/", 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance", 'xsi:schemaLocation' => - %{https://github.com/JPCOAR/schema/blob/master/1.0/ + %{https://github.com/JPCOAR/schema/blob/master/2.0/ jpcoar_scm.xsd}.gsub(/\s+/, ' ') } end - end end diff --git a/app/models/concerns/enju_oai/jpcoar20dummy.rb b/app/models/concerns/enju_oai/jpcoar20dummy.rb new file mode 100644 index 0000000000..8c5a3db9e9 --- /dev/null +++ b/app/models/concerns/enju_oai/jpcoar20dummy.rb @@ -0,0 +1,26 @@ +# OAI-PMHでJPCOARスキーマ2.0を扱うためのダミーモジュール +# ruby-oaiでメソッド名にドットが含められないため、ダミーのprefixを設定している +module EnjuOai + class Jpcoar20dummy < Jpcoar20 + def initialize + @prefix = 'jpcoar_2.0' + @schema = 'https://github.com/JPCOAR/schema/blob/master/2.0/jpcoar_scm.xsd' + @namespace = 'https://github.com/JPCOAR/schema/blob/master/2.0/' + @element_namespace = 'dc' + @fields = [ :title, :creator, :subject, :description, :publisher, + :contributor, :date, :type, :format, :identifier, + :source, :language, :relation, :coverage, :rights] + end + + def header_specification + { + 'xmlns:jpcoar' => "https://github.com/JPCOAR/schema/blob/master/2.0/", + 'xmlns:dc' => "http://purl.org/dc/elements/1.1/", + 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance", + 'xsi:schemaLocation' => + %{https://github.com/JPCOAR/schema/blob/master/2.0/ + jpcoar_scm.xsd}.gsub(/\s+/, ' ') + } + end + end +end diff --git a/app/models/concerns/enju_oai/oai_model.rb b/app/models/concerns/enju_oai/oai_model.rb index fbc2459156..99c627523f 100644 --- a/app/models/concerns/enju_oai/oai_model.rb +++ b/app/models/concerns/enju_oai/oai_model.rb @@ -1,7 +1,8 @@ module EnjuOai module OaiModel extend ActiveSupport::Concern - OAI::Provider::Base.register_format(EnjuOai::Jpcoar.instance) + OAI::Provider::Base.register_format(EnjuOai::Jpcoar20.instance) + OAI::Provider::Base.register_format(EnjuOai::Jpcoar20dummy.instance) OAI::Provider::Base.register_format(EnjuOai::Dcndl.instance) def to_oai_dc @@ -26,9 +27,11 @@ def to_oai_dc xml.target! end + # jpcoarフォーマットでの出力 def to_jpcoar xml = Builder::XmlMarkup.new - xml.tag!('jpcoar:jpcoar', "xsi:schemaLocation" => "https://github.com/JPCOAR/schema/blob/master/1.0/jpcoar_scm.xsd", + xml.tag!( + 'jpcoar:jpcoar', "xsi:schemaLocation" => "https://github.com/JPCOAR/schema/blob/master/2.0/jpcoar_scm.xsd", "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", "xmlns:rdf" => "http://www.w3.org/1999/02/22-rdf-syntax-ns#", "xmlns:rioxxterms" => "http://www.rioxx.net/schema/v2.0/rioxxterms/", @@ -36,24 +39,84 @@ def to_jpcoar "xmlns:oaire" => "http://namespace.openaire.eu/schema/oaire/", "xmlns:dcndl" => "http://ndl.go.jp/dcndl/terms/", "xmlns:dc" => "http://purl.org/dc/elements/1.1/", - "xmlns:jpcoar" => "https://github.com/JPCOAR/schema/blob/master/1.0/") do + "xmlns:dcterms" => "http://purl.org/dc/terms/", + "xmlns:jpcoar" => "https://github.com/JPCOAR/schema/blob/master/2.0/" + ) do + # タイトル + # https://schema.irdb.nii.ac.jp/ja/schema/2.0/1 xml.tag! 'dc:title', original_title - xml.tag! 'dc:language', language.iso_639_2 - xml.tag! 'jpcoar:creators' do - creators.readable_by(nil).each do |creator| - xml.tag! 'jpcoar:creatorName', creator.full_name + + # 作成者 + # https://schema.irdb.nii.ac.jp/ja/schema/2.0/3 + creators.readable_by(nil).each do |creator| + xml.tag! 'jpcoar:creator' do + xml.target! << creator.to_jpcoar end end + # 寄与者 + # https://schema.irdb.nii.ac.jp/ja/schema/2.0/4 + contributors.readable_by(nil).each do |contributor| + xml.tag! 'jpcoar:contributor' do + xml.target! << contributor.to_jpcoar + end + end + + # アクセス権 + # https://schema.irdb.nii.ac.jp/ja/schema/2.0/5 + # TBD + + # 権利情報 + # https://schema.irdb.nii.ac.jp/ja/schema/2.0/6 + # TBD + + # 主題 + # https://schema.irdb.nii.ac.jp/ja/schema/2.0/8 subjects.each do |subject| - xml.tag! 'jpcoar:subject', subject.term + xml.tag! 'jpcoar:subject', subject.term, subjectScheme: 'Other' end - if attachment.attached? - xml.tag! 'jpcoar:file' do - xml.tag! 'jpcoar:URI', Rails.application.routes.url_helpers.rails_storage_proxy_url(fileset.attachment, host: ENV['ENJU_LEAF_BASE_URL']) - end + # 出版者 + # https://schema.irdb.nii.ac.jp/ja/schema/2.0/10 + if publishers.first + xml.tag! 'dc:publisher', publishers.first.full_name end + + # 日付 + # https://schema.irdb.nii.ac.jp/ja/schema/2.0/12 + xml.tag! 'datacite:date', date_of_publication&.iso8601, dateType: 'Issued' if date_published + xml.tag! 'datacite:date', updated_at.to_date.iso8601, dateType: 'Updated' + xml.tag! 'dcterms:date', pub_date if pub_date + + # 資源タイプ + # https://schema.irdb.nii.ac.jp/ja/schema/2.0/15 + # TBD + + # 出版タイプ + # https://schema.irdb.nii.ac.jp/ja/schema/2.0/17 + # TBD + + # 識別子 + # https://schema.irdb.nii.ac.jp/ja/schema/2.0/18 + xml.tag! 'jpcoar:identifier', Rails.application.routes.url_helpers.manifestation_url(self, host: ENV['ENJU_LEAF_BASE_URL']), identifierType: 'URI' + + # 関連情報 + # https://schema.irdb.nii.ac.jp/ja/schema/2.0/20 + # TBD + + # 収録物識別子 + # https://schema.irdb.nii.ac.jp/ja/schema/2.0/24 + # 収録物名 + # https://schema.irdb.nii.ac.jp/ja/schema/2.0/25 + # TBD + + # 会議記述 + # https://schema.irdb.nii.ac.jp/ja/schema/2.0/35 + # TBD + + # ファイル情報 + # https://schema.irdb.nii.ac.jp/ja/schema/2.0/43 + # TBD end xml.target! @@ -262,10 +325,14 @@ def to_dcndl xml.target! end + # OAI-PMHのbaseURLを取得する + # @return [String] def self.repository_url URI.join(ENV['ENJU_LEAF_BASE_URL'], '/oai') end + # OAI-PMHのレコードプレフィックスを取得する + # @return [String] def self.record_prefix "oai:#{URI.parse(ENV['ENJU_LEAF_BASE_URL']).host}:manifestations" end diff --git a/spec/requests/oai_controller_spec.rb b/spec/requests/oai_controller_spec.rb index 17d92db588..0ef424402e 100644 --- a/spec/requests/oai_controller_spec.rb +++ b/spec/requests/oai_controller_spec.rb @@ -33,9 +33,9 @@ end end - describe 'jpcoar' do + describe 'jpcoar_2.0' do before(:each) do - @metadata_format = 'jpcoar' + @metadata_format = 'jpcoar_2.0' end it "should get ListRecords" do @@ -64,21 +64,5 @@ expect(response).to be_successful end end - - describe 'junii2' do - before(:each) do - @metadata_format = 'junii2' - end - - it "should get ListRecords" do - get oai_path(verb: 'ListRecords', metadataPrefix: @metadata_format) - expect(response).to be_successful - end - - it "should get GetRecord" do - get oai_path(verb: 'GetRecord', identifier: "oai:localhost:manifestations:#{@manifestation.id}", metadataPrefix: @metadata_format) - expect(response).to be_successful - end - end end end From d25a74be9f1021fe2313917d502ce55a20461a35 Mon Sep 17 00:00:00 2001 From: Kosuke Tanabe Date: Fri, 16 Feb 2024 00:05:39 +0900 Subject: [PATCH 2/3] fix method name --- app/models/concerns/enju_oai/oai_model.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/concerns/enju_oai/oai_model.rb b/app/models/concerns/enju_oai/oai_model.rb index 99c627523f..ea7e2c1ac4 100644 --- a/app/models/concerns/enju_oai/oai_model.rb +++ b/app/models/concerns/enju_oai/oai_model.rb @@ -27,8 +27,8 @@ def to_oai_dc xml.target! end - # jpcoarフォーマットでの出力 - def to_jpcoar + # jpcoar_2.0フォーマットでの出力 + def to_jpcoar_20 xml = Builder::XmlMarkup.new xml.tag!( 'jpcoar:jpcoar', "xsi:schemaLocation" => "https://github.com/JPCOAR/schema/blob/master/2.0/jpcoar_scm.xsd", From 35c6134e1d20c55a86023511fc055df302cab213 Mon Sep 17 00:00:00 2001 From: Kosuke Tanabe Date: Fri, 16 Feb 2024 00:31:02 +0900 Subject: [PATCH 3/3] fix date_of_publication --- app/models/concerns/enju_oai/oai_model.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/concerns/enju_oai/oai_model.rb b/app/models/concerns/enju_oai/oai_model.rb index ea7e2c1ac4..84e2069cf0 100644 --- a/app/models/concerns/enju_oai/oai_model.rb +++ b/app/models/concerns/enju_oai/oai_model.rb @@ -84,7 +84,7 @@ def to_jpcoar_20 # 日付 # https://schema.irdb.nii.ac.jp/ja/schema/2.0/12 - xml.tag! 'datacite:date', date_of_publication&.iso8601, dateType: 'Issued' if date_published + xml.tag! 'datacite:date', date_of_publication&.iso8601, dateType: 'Issued' if date_of_publication xml.tag! 'datacite:date', updated_at.to_date.iso8601, dateType: 'Updated' xml.tag! 'dcterms:date', pub_date if pub_date