Commit bce911727243d3eb516cb91c2bfb4f65c470c355
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
Merge branch 'virtuoso_integration' of gitlab.com:participa/noosfero into virtuoso_integration
Showing
1 changed file
with
118 additions
and
0 deletions
Show diff stats
plugins/virtuoso/lib/virtuoso_plugin/noosfero_harvest.rb
0 → 100644
@@ -0,0 +1,118 @@ | @@ -0,0 +1,118 @@ | ||
1 | +class VirtuosoPlugin::NoosferoHarvest | ||
2 | + | ||
3 | + COMMON_MAPPING = { | ||
4 | + :type => {:predicate => "http://purl.org/dc/terms/type", :value => lambda {|s, t| t.class.name}}, | ||
5 | + :created_at => {:predicate => "http://purl.org/dc/terms/created"}, | ||
6 | + :updated_at => {:predicate => "http://purl.org/dc/terms/modified"}, | ||
7 | + } | ||
8 | + | ||
9 | + ARTICLE_MAPPING = { | ||
10 | + :title => {:predicate => "http://purl.org/dc/terms/title"}, | ||
11 | + :abstract => {:predicate => "http://purl.org/dc/terms/abstract"}, | ||
12 | + :body => {:predicate => "http://purl.org/dc/terms/description"}, | ||
13 | + :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 | + :author => {:predicate => "http://purl.org/dc/terms/creator", :value => lambda {|s, t| url_for(t.author_url) if t.author_url} }, | ||
16 | + } | ||
17 | + PROFILE_MAPPING = { | ||
18 | + :name => {:predicate => "http://purl.org/dc/terms/title"}, | ||
19 | + :public? => {:predicate => "http://purl.org/socialparticipation/opa#publicProfile"}, | ||
20 | + } | ||
21 | + COMMENT_MAPPING = { | ||
22 | + :title => {:predicate => "http://purl.org/dc/terms/title"}, | ||
23 | + :body => {:predicate => "http://purl.org/dc/terms/description"}, | ||
24 | + :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} }, | ||
26 | + } | ||
27 | + FRIENDSHIP_MAPPING = { | ||
28 | + :knows => {:predicate => "http://xmlns.com/foaf/0.1/knows", :value => lambda {|s, t| url_for(t.url)} }, | ||
29 | + :type => nil, :created_at => nil, :updated_at => nil, | ||
30 | + } | ||
31 | + | ||
32 | + def initialize(environment) | ||
33 | + @environment = environment | ||
34 | + @graph = environment.top_url | ||
35 | + end | ||
36 | + | ||
37 | + attr_reader :environment | ||
38 | + | ||
39 | + def plugin | ||
40 | + @plugin ||= VirtuosoPlugin.new(self) | ||
41 | + end | ||
42 | + | ||
43 | + delegate :settings, :to => :plugin | ||
44 | + | ||
45 | + include Rails.application.routes.url_helpers | ||
46 | + | ||
47 | + def triplify_comments(article) | ||
48 | + total = article.comments.count | ||
49 | + count = 0 | ||
50 | + article.comments.each do |comment| | ||
51 | + subject_identifier = url_for(comment.url) | ||
52 | + puts "triplify #{subject_identifier} comment (#{count+=1}/#{total})" | ||
53 | + triplify_mappings(COMMENT_MAPPING, subject_identifier, article, comment) | ||
54 | + end | ||
55 | + end | ||
56 | + | ||
57 | + def triplify_articles(profile) | ||
58 | + total = profile.articles.count | ||
59 | + count = 0 | ||
60 | + profile.articles.public.each do |article| | ||
61 | + subject_identifier = url_for(article.url) | ||
62 | + puts "triplify #{subject_identifier} article (#{count+=1}/#{total})" | ||
63 | + triplify_mappings(ARTICLE_MAPPING, subject_identifier, profile, article) | ||
64 | + triplify_comments(article) | ||
65 | + end | ||
66 | + end | ||
67 | + | ||
68 | + def triplify_friendship(person) | ||
69 | + total = person.friends.count | ||
70 | + count = 0 | ||
71 | + person.friends.each do |friend| | ||
72 | + subject_identifier = url_for(person.url) | ||
73 | + puts "triplify #{subject_identifier} friendship (#{count+=1}/#{total})" | ||
74 | + triplify_mappings(FRIENDSHIP_MAPPING, subject_identifier, person, friend) | ||
75 | + end | ||
76 | + end | ||
77 | + | ||
78 | + def triplify_profiles | ||
79 | + total = environment.profiles.count | ||
80 | + count = 0 | ||
81 | + environment.profiles.each do |profile| | ||
82 | + subject_identifier = url_for(profile.url) | ||
83 | + puts "triplify #{subject_identifier} profile (#{count+=1}/#{total})" | ||
84 | + triplify_mappings(PROFILE_MAPPING, subject_identifier, environment, profile) | ||
85 | + triplify_articles(profile) if profile.public? | ||
86 | + triplify_friendship(profile) if profile.person? | ||
87 | + end | ||
88 | + end | ||
89 | + | ||
90 | + def run | ||
91 | + triplify_profiles | ||
92 | + end | ||
93 | + | ||
94 | + protected | ||
95 | + | ||
96 | + def triplify_mappings(mapping, subject_identifier, source, target) | ||
97 | + COMMON_MAPPING.merge(mapping).each do |k, v| | ||
98 | + next unless v | ||
99 | + value = nil | ||
100 | + if v[:value] | ||
101 | + value = v[:value].kind_of?(Proc) ? v[:value].call(source, target) : v[:value] | ||
102 | + elsif target.respond_to?(k) | ||
103 | + value = target.send(k) | ||
104 | + end | ||
105 | + insert_triple(RDF::URI.new(subject_identifier), RDF::URI.new(v[:predicate]), value) if value.present? | ||
106 | + end | ||
107 | + end | ||
108 | + | ||
109 | + def insert_triple(subject, predicate, value) | ||
110 | + value = RDF::URI.new(value) if value.kind_of?(String) && /https?:\/\//.match(value) | ||
111 | + value = RDF::Literal::DateTime.new(value) if value.kind_of?(ActiveSupport::TimeWithZone) | ||
112 | + value = RDF::Literal::Boolean.new(value) if !!value == value | ||
113 | + | ||
114 | + query = RDF::Virtuoso::Query.insert_data([RDF::URI.new(subject), RDF::URI.new(predicate), value]).graph(RDF::URI.new(@graph)) | ||
115 | + plugin.virtuoso_client.insert(query) | ||
116 | + end | ||
117 | + | ||
118 | +end |