Commit 3e85b02e68f68509fd0f06575057d4ae60b2176f

Authored by Victor Costa
2 parents 864b0f2d 3eef3769

Merge branch 'virtuoso_integration' into stable

plugins/virtuoso/lib/virtuoso_plugin/noosfero_harvest.rb
... ... @@ -43,14 +43,19 @@ class VirtuosoPlugin::NoosferoHarvest
43 43 delegate :settings, :to => :plugin
44 44  
45 45 include Rails.application.routes.url_helpers
  46 + include ActionView::Helpers::SanitizeHelper
46 47  
47 48 def triplify_comments(article)
48 49 total = article.comments.count
49 50 count = 0
50 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 59 end
55 60 end
56 61  
... ... @@ -58,10 +63,14 @@ class VirtuosoPlugin::NoosferoHarvest
58 63 total = profile.articles.count
59 64 count = 0
60 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 74 end
66 75 end
67 76  
... ... @@ -69,9 +78,13 @@ class VirtuosoPlugin::NoosferoHarvest
69 78 total = person.friends.count
70 79 count = 0
71 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 88 end
76 89 end
77 90  
... ... @@ -79,11 +92,15 @@ class VirtuosoPlugin::NoosferoHarvest
79 92 total = environment.profiles.count
80 93 count = 0
81 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 104 end
88 105 end
89 106  
... ... @@ -106,12 +123,20 @@ class VirtuosoPlugin::NoosferoHarvest
106 123 end
107 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 140 plugin.virtuoso_client.insert(query)
116 141 end
117 142  
... ...
plugins/virtuoso/public/triples_management.js
  1 +function is_valid_url(url) {
  2 + var pattern =/^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$/i;
  3 + return pattern.test(url);
  4 +}
  5 +
  6 +function validate_search_form() {
  7 + graph_uri = jQuery("input#graph_uri");
  8 + query = jQuery("textarea#query");
  9 +
  10 + if ( !is_valid_url(graph_uri.val())) {
  11 + alert( TRIPLES_MANAGEMENT_GRAPH_URI_REQUIRED_MESSAGE );
  12 + graph_uri.focus();
  13 + return false;
  14 + }
  15 +
  16 + pattern = /.*select\s.*/i
  17 + if (!pattern.test(query.val())) {
  18 + alert( TRIPLES_MANAGEMENT_QUERY_REQUIRED_MESSAGE );
  19 + query.focus();
  20 + return false;
  21 + }
  22 +
  23 + jQuery("#form-triples-search").submit();
  24 +
  25 +}
  26 +
1 27 function update_triple(triple_id) {
2 28 graph = jQuery("input#graph_uri").val();
3 29  
... ...
plugins/virtuoso/views/virtuoso_plugin_admin/triple_management.html.erb
1 1 <div id="virtuoso-triples-management">
2 2 <h1><%= _('Virtuoso settings &raquo; Triples Management')%></h1>
3 3  
4   - <%= form_tag('/admin/plugin/virtuoso/triples_management', :method => 'post') do %>
  4 + <%= form_tag('/admin/plugin/virtuoso/triples_management', :method => 'post', :id => 'form-triples-search') do %>
5 5 <%= labelled_form_field(_('Graph URI:'), text_field_tag(:graph_uri, @graph_uri) ) %>
6 6 <%= labelled_form_field(_('Query SPARQL:'), text_area_tag(:query, @query, :rows => 7)) %>
7 7 <% button_bar do %>
8   - <%= submit_button(:search, _('Search triples')) %>
  8 + <%= submit_button(:search, _('Search triples'), :onclick => "validate_search_form(); return false;") %>
9 9 <%= colorbox_button('add', _('Add a triple'), { :action => 'add_triple' }) %>
10 10 <% end %>
11 11 <% end %>
... ... @@ -54,4 +54,9 @@
54 54  
55 55 </div>
56 56  
  57 +<script type="text/javascript">
  58 + TRIPLES_MANAGEMENT_GRAPH_URI_REQUIRED_MESSAGE = '<%= _('A valid GRAPH URI is required') %>';
  59 + TRIPLES_MANAGEMENT_QUERY_REQUIRED_MESSAGE = '<%= _('A valid QUERY is required') %>';
  60 +</script>
  61 +
57 62 <%= javascript_include_tag '/plugins/virtuoso/triples_management.js' %>
... ...