Commit c69c6c7cde3b77385451a2c7366f2e4a75688cf3
1 parent
b1499fb9
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
virtuoso: noosfero triplification script
Showing
1 changed file
with
110 additions
and
0 deletions
Show diff stats
plugins/virtuoso/lib/virtuoso_plugin/noosfero_harvest.rb
0 → 100644
... | ... | @@ -0,0 +1,110 @@ |
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 | + article.comments.each do |comment| | |
49 | + subject_identifier = url_for(comment.url) | |
50 | + puts "triplify #{subject_identifier} comment" | |
51 | + triplify_mappings(COMMENT_MAPPING, subject_identifier, article, comment) | |
52 | + end | |
53 | + end | |
54 | + | |
55 | + def triplify_articles(profile) | |
56 | + profile.articles.public.each do |article| | |
57 | + subject_identifier = url_for(article.url) | |
58 | + puts "triplify #{subject_identifier} article" | |
59 | + triplify_mappings(ARTICLE_MAPPING, subject_identifier, profile, article) | |
60 | + triplify_comments(article) | |
61 | + end | |
62 | + end | |
63 | + | |
64 | + def triplify_friendship(person) | |
65 | + person.friends.each do |friend| | |
66 | + subject_identifier = url_for(person.url) | |
67 | + puts "triplify #{subject_identifier} friendship" | |
68 | + triplify_mappings(FRIENDSHIP_MAPPING, subject_identifier, person, friend) | |
69 | + end | |
70 | + end | |
71 | + | |
72 | + def triplify_profiles | |
73 | + environment.profiles.each do |profile| | |
74 | + subject_identifier = url_for(profile.url) | |
75 | + puts "triplify #{subject_identifier} profile" | |
76 | + triplify_mappings(PROFILE_MAPPING, subject_identifier, environment, profile) | |
77 | + triplify_articles(profile) if profile.public? | |
78 | + triplify_friendship(profile) if profile.person? | |
79 | + end | |
80 | + end | |
81 | + | |
82 | + def run | |
83 | + triplify_profiles | |
84 | + end | |
85 | + | |
86 | + protected | |
87 | + | |
88 | + def triplify_mappings(mapping, subject_identifier, source, target) | |
89 | + COMMON_MAPPING.merge(mapping).each do |k, v| | |
90 | + next unless v | |
91 | + value = nil | |
92 | + if v[:value] | |
93 | + value = v[:value].kind_of?(Proc) ? v[:value].call(source, target) : v[:value] | |
94 | + elsif target.respond_to?(k) | |
95 | + value = target.send(k) | |
96 | + end | |
97 | + insert_triple(RDF::URI.new(subject_identifier), RDF::URI.new(v[:predicate]), value) if value.present? | |
98 | + end | |
99 | + end | |
100 | + | |
101 | + def insert_triple(subject, predicate, value) | |
102 | + value = RDF::URI.new(value) if value.kind_of?(String) && /https?:\/\//.match(value) | |
103 | + value = RDF::Literal::DateTime.new(value) if value.kind_of?(ActiveSupport::TimeWithZone) | |
104 | + value = RDF::Literal::Boolean.new(value) if !!value == value | |
105 | + | |
106 | + query = RDF::Virtuoso::Query.insert_data([RDF::URI.new(subject), RDF::URI.new(predicate), value]).graph(RDF::URI.new(@graph)) | |
107 | + plugin.virtuoso_client.insert(query) | |
108 | + end | |
109 | + | |
110 | +end | ... | ... |