Commit ca204d73f8ce04a5260374aeacf213d347005859

Authored by Evandro Jr
2 parents 92d68b61 91c84051

Merge branch 'stable' of gitlab.com:participa/noosfero into stable

plugins/virtuoso/lib/virtuoso_plugin/noosfero_harvest.rb
1 class VirtuosoPlugin::NoosferoHarvest 1 class VirtuosoPlugin::NoosferoHarvest
2 2
3 COMMON_MAPPING = { 3 COMMON_MAPPING = {
4 - :type => {:predicate => "http://purl.org/dc/terms/type", :value => lambda {|s, t| t.class.name}}, 4 + :type => {:predicate => ["http://www.w3.org/1999/02/22-rdf-syntax-ns#type", "http://purl.org/dc/terms/type"], :value => lambda {|s, t| t.class.name}},
5 :created_at => {:predicate => "http://purl.org/dc/terms/created"}, 5 :created_at => {:predicate => "http://purl.org/dc/terms/created"},
6 :updated_at => {:predicate => "http://purl.org/dc/terms/modified"}, 6 :updated_at => {:predicate => "http://purl.org/dc/terms/modified"},
7 } 7 }
@@ -10,19 +10,32 @@ class VirtuosoPlugin::NoosferoHarvest @@ -10,19 +10,32 @@ class VirtuosoPlugin::NoosferoHarvest
10 :title => {:predicate => "http://purl.org/dc/terms/title"}, 10 :title => {:predicate => "http://purl.org/dc/terms/title"},
11 :abstract => {:predicate => "http://purl.org/dc/terms/abstract"}, 11 :abstract => {:predicate => "http://purl.org/dc/terms/abstract"},
12 :body => {:predicate => "http://purl.org/dc/terms/description"}, 12 :body => {:predicate => "http://purl.org/dc/terms/description"},
  13 + :participation => {:predicate => "http://purl.org/socialparticipation/ops#performsParticipation", :value => lambda {|s,t| url_for(t.url)}, :subject => lambda {|s,t| url_for(s.url)} },
13 :part_of => {:predicate => "http://purl.org/dc/terms/isPartOf", :value => lambda {|s, t| url_for(s.url)} }, 14 :part_of => {:predicate => "http://purl.org/dc/terms/isPartOf", :value => lambda {|s, t| url_for(s.url)} },
14 :published_at => {:predicate => "http://purl.org/dc/terms/issued"}, 15 :published_at => {:predicate => "http://purl.org/dc/terms/issued"},
15 :author => {:predicate => "http://purl.org/dc/terms/creator", :value => lambda {|s, t| url_for(t.author_url) if t.author_url} }, 16 :author => {:predicate => "http://purl.org/dc/terms/creator", :value => lambda {|s, t| url_for(t.author_url) if t.author_url} },
16 } 17 }
17 PROFILE_MAPPING = { 18 PROFILE_MAPPING = {
18 - :name => {:predicate => "http://purl.org/dc/terms/title"}, 19 + :name => {:predicate => "http://xmlns.com/foaf/0.1/name"},
  20 + :contributor => {:predicate => "http://purl.org/dc/terms/contributor", :value => lambda {|s, t| url_for(s.top_url)} },
19 :public? => {:predicate => "http://purl.org/socialparticipation/opa#publicProfile"}, 21 :public? => {:predicate => "http://purl.org/socialparticipation/opa#publicProfile"},
  22 + :visible => {:predicate => "http://purl.org/socialparticipation/opa#visibleProfile"},
  23 + :lat => {:predicate => "http://www.w3.org/2003/01/geo/wgs84_pos#lat"},
  24 + :lng => {:predicate => "http://www.w3.org/2003/01/geo/wgs84_pos#lng"},
  25 + :type_community => {:predicate => "http://xmlns.com/foaf/0.1/Group", :condition => lambda {|s,t| t.community?} },
  26 + :type_organization => {:predicate => "http://xmlns.com/foaf/0.1/Organization", :condition => lambda {|s,t| !t.person? && !t.community?} },
  27 + }
  28 + PERSON_MAPPING = {
  29 + :type_participant => {:predicate => "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", :value => "http://purl.org/socialparticipation/ops#Participant"},
  30 + :type_person => {:predicate => "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", :value => "http://xmlns.com/foaf/0.1/Person"},
  31 + :email => {:predicate => "http://xmlns.com/foaf/0.1/mbox", :condition => lambda {|s,t| t.public? } }
20 } 32 }
21 COMMENT_MAPPING = { 33 COMMENT_MAPPING = {
22 :title => {:predicate => "http://purl.org/dc/terms/title"}, 34 :title => {:predicate => "http://purl.org/dc/terms/title"},
23 - :body => {:predicate => "http://purl.org/dc/terms/description"}, 35 + :body => {:predicate => "http://schema.org/text"},
24 :part_of => {:predicate => "http://purl.org/dc/terms/isPartOf", :value => lambda {|s, t| url_for(s.url)} }, 36 :part_of => {:predicate => "http://purl.org/dc/terms/isPartOf", :value => lambda {|s, t| url_for(s.url)} },
25 :author => {:predicate => "http://purl.org/dc/terms/creator", :value => lambda {|s, t| url_for(t.author_url) if t.author_url} }, 37 :author => {:predicate => "http://purl.org/dc/terms/creator", :value => lambda {|s, t| url_for(t.author_url) if t.author_url} },
  38 + :participation => {:predicate => "http://purl.org/socialparticipation/ops#performsParticipation", :value => lambda {|s,t| url_for(t.url)}, :subject => lambda {|s,t| url_for(s.profile.url)} },
26 } 39 }
27 FRIENDSHIP_MAPPING = { 40 FRIENDSHIP_MAPPING = {
28 :knows => {:predicate => "http://xmlns.com/foaf/0.1/knows", :value => lambda {|s, t| url_for(t.url)} }, 41 :knows => {:predicate => "http://xmlns.com/foaf/0.1/knows", :value => lambda {|s, t| url_for(t.url)} },
@@ -97,7 +110,10 @@ class VirtuosoPlugin::NoosferoHarvest @@ -97,7 +110,10 @@ class VirtuosoPlugin::NoosferoHarvest
97 puts "triplify #{subject_identifier} profile (#{count+=1}/#{total})" 110 puts "triplify #{subject_identifier} profile (#{count+=1}/#{total})"
98 triplify_mappings(PROFILE_MAPPING, subject_identifier, environment, profile) 111 triplify_mappings(PROFILE_MAPPING, subject_identifier, environment, profile)
99 triplify_articles(profile) if profile.public? 112 triplify_articles(profile) if profile.public?
100 - triplify_friendship(profile) if profile.person? 113 + if profile.person?
  114 + triplify_friendship(profile)
  115 + triplify_mappings(PERSON_MAPPING, subject_identifier, environment, profile, false)
  116 + end
101 rescue => ex 117 rescue => ex
102 puts "FAILED: #{ex}" 118 puts "FAILED: #{ex}"
103 end 119 end
@@ -110,16 +126,23 @@ class VirtuosoPlugin::NoosferoHarvest @@ -110,16 +126,23 @@ class VirtuosoPlugin::NoosferoHarvest
110 126
111 protected 127 protected
112 128
113 - def triplify_mappings(mapping, subject_identifier, source, target)  
114 - COMMON_MAPPING.merge(mapping).each do |k, v| 129 + def triplify_mappings(mapping, subject_identifier, source, target, include_common = true)
  130 + target_mapping = include_common ? COMMON_MAPPING.merge(mapping) : mapping
  131 + target_mapping.each do |k, v|
115 next unless v 132 next unless v
  133 + next if v[:condition] && v[:condition].call(source, target)
  134 +
  135 + subject_identifier = v[:subject].call(source, target) if v[:subject]
  136 +
116 value = nil 137 value = nil
117 if v[:value] 138 if v[:value]
118 value = v[:value].kind_of?(Proc) ? v[:value].call(source, target) : v[:value] 139 value = v[:value].kind_of?(Proc) ? v[:value].call(source, target) : v[:value]
119 elsif target.respond_to?(k) 140 elsif target.respond_to?(k)
120 value = target.send(k) 141 value = target.send(k)
121 end 142 end
122 - insert_triple(RDF::URI.new(subject_identifier), RDF::URI.new(v[:predicate]), value) if value.present? 143 + [v[:predicate]].flatten.compact.each do |predicate|
  144 + insert_triple(RDF::URI.new(subject_identifier), RDF::URI.new(predicate), value) if value.present?
  145 + end
123 end 146 end
124 end 147 end
125 148
@@ -130,6 +153,10 @@ class VirtuosoPlugin::NoosferoHarvest @@ -130,6 +153,10 @@ class VirtuosoPlugin::NoosferoHarvest
130 value = RDF::Literal::DateTime.new(value) 153 value = RDF::Literal::DateTime.new(value)
131 elsif !!value == value 154 elsif !!value == value
132 value = RDF::Literal::Boolean.new(value) 155 value = RDF::Literal::Boolean.new(value)
  156 + elsif value.kind_of?(Float)
  157 + value = RDF::Literal::Double.new(value)
  158 + else
  159 + value
133 end 160 end
134 end 161 end
135 162