Commit 7ee0168c474a40700dcdb5d8aed5bddca71dcdad

Authored by Victor Costa
1 parent 5d6d1157

virtuoso: handle exceptions in noosfero harvest

plugins/virtuoso/lib/virtuoso_plugin/noosfero_harvest.rb
@@ -43,14 +43,19 @@ class VirtuosoPlugin::NoosferoHarvest @@ -43,14 +43,19 @@ class VirtuosoPlugin::NoosferoHarvest
43 delegate :settings, :to => :plugin 43 delegate :settings, :to => :plugin
44 44
45 include Rails.application.routes.url_helpers 45 include Rails.application.routes.url_helpers
  46 + include ActionView::Helpers::SanitizeHelper
46 47
47 def triplify_comments(article) 48 def triplify_comments(article)
48 total = article.comments.count 49 total = article.comments.count
49 count = 0 50 count = 0
50 article.comments.each do |comment| 51 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) 52 + begin
  53 + subject_identifier = url_for(comment.url)
  54 + puts "triplify #{subject_identifier} comment (#{count+=1}/#{total})"
  55 + triplify_mappings(COMMENT_MAPPING, subject_identifier, article, comment)
  56 + rescue => ex
  57 + puts "FAILED: #{ex}"
  58 + end
54 end 59 end
55 end 60 end
56 61
@@ -58,10 +63,14 @@ class VirtuosoPlugin::NoosferoHarvest @@ -58,10 +63,14 @@ class VirtuosoPlugin::NoosferoHarvest
58 total = profile.articles.count 63 total = profile.articles.count
59 count = 0 64 count = 0
60 profile.articles.public.each do |article| 65 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) 66 + begin
  67 + subject_identifier = url_for(article.url)
  68 + puts "triplify #{subject_identifier} article (#{count+=1}/#{total})"
  69 + triplify_mappings(ARTICLE_MAPPING, subject_identifier, profile, article)
  70 + triplify_comments(article)
  71 + rescue => ex
  72 + puts "FAILED: #{ex}"
  73 + end
65 end 74 end
66 end 75 end
67 76
@@ -69,9 +78,13 @@ class VirtuosoPlugin::NoosferoHarvest @@ -69,9 +78,13 @@ class VirtuosoPlugin::NoosferoHarvest
69 total = person.friends.count 78 total = person.friends.count
70 count = 0 79 count = 0
71 person.friends.each do |friend| 80 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) 81 + begin
  82 + subject_identifier = url_for(person.url)
  83 + puts "triplify #{subject_identifier} friendship (#{count+=1}/#{total})"
  84 + triplify_mappings(FRIENDSHIP_MAPPING, subject_identifier, person, friend)
  85 + rescue => ex
  86 + puts "FAILED: #{ex}"
  87 + end
75 end 88 end
76 end 89 end
77 90
@@ -79,11 +92,15 @@ class VirtuosoPlugin::NoosferoHarvest @@ -79,11 +92,15 @@ class VirtuosoPlugin::NoosferoHarvest
79 total = environment.profiles.count 92 total = environment.profiles.count
80 count = 0 93 count = 0
81 environment.profiles.each do |profile| 94 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? 95 + begin
  96 + subject_identifier = url_for(profile.url)
  97 + puts "triplify #{subject_identifier} profile (#{count+=1}/#{total})"
  98 + triplify_mappings(PROFILE_MAPPING, subject_identifier, environment, profile)
  99 + triplify_articles(profile) if profile.public?
  100 + triplify_friendship(profile) if profile.person?
  101 + rescue => ex
  102 + puts "FAILED: #{ex}"
  103 + end
87 end 104 end
88 end 105 end
89 106
@@ -106,12 +123,20 @@ class VirtuosoPlugin::NoosferoHarvest @@ -106,12 +123,20 @@ class VirtuosoPlugin::NoosferoHarvest
106 end 123 end
107 end 124 end
108 125
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 126 + def process_value(value)
  127 + if value.kind_of?(String)
  128 + value = /https?:\/\//.match(value) ? RDF::URI.new(value) : strip_tags(value)
  129 + elsif value.kind_of?(ActiveSupport::TimeWithZone)
  130 + value = RDF::Literal::DateTime.new(value)
  131 + elsif !!value == value
  132 + value = RDF::Literal::Boolean.new(value)
  133 + end
  134 + end
113 135
114 - query = RDF::Virtuoso::Query.insert_data([RDF::URI.new(subject), RDF::URI.new(predicate), value]).graph(RDF::URI.new(@graph)) 136 + def insert_triple(subject, predicate, value)
  137 + query = RDF::Virtuoso::Query.insert_data([RDF::URI.new(subject),
  138 + RDF::URI.new(predicate),
  139 + process_value(value)]).graph(RDF::URI.new(@graph))
115 plugin.virtuoso_client.insert(query) 140 plugin.virtuoso_client.insert(query)
116 end 141 end
117 142