| 1 | # RDF::Query::Util | ||
| 2 | # ----------------------------------------------------------------------------- | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | RDF::Query::Util - Miscellaneous utility functions to support work with RDF::Query. | ||
| 7 | |||
| 8 | =head1 VERSION | ||
| 9 | |||
| 10 | This document describes RDF::Query::Util version 2.919. | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | use RDF::Query::Util; | ||
| 15 | my $query = &RDF::Query::Util::cli_make_query; | ||
| 16 | my $model = &RDF::Query::Util::cli_make_model; | ||
| 17 | $query->execute( $model ); | ||
| 18 | ... | ||
| 19 | |||
| 20 | =head1 FUNCTIONS | ||
| 21 | |||
| 22 | =over 4 | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | package RDF::Query::Util; | ||
| 27 | |||
| 28 | use strict; | ||
| 29 | use warnings; | ||
| 30 | no warnings 'redefine'; | ||
| 31 | use Carp qw(carp croak confess); | ||
| 32 | |||
| 33 | use URI::file; | ||
| 34 | use RDF::Query; | ||
| 35 | use LWP::Simple; | ||
| 36 | use File::Spec; | ||
| 37 | use JSON; | ||
| 38 | |||
| 39 | ###################################################################### | ||
| 40 | |||
| 41 | our ($VERSION); | ||
| 42 | BEGIN { | ||
| 43 | $VERSION = '2.919'; | ||
| 44 | } | ||
| 45 | |||
| 46 | ###################################################################### | ||
| 47 | |||
| 48 | our $PREFIXES = <<"END"; | ||
| 49 | PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> | ||
| 50 | PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> | ||
| 51 | PREFIX owl: <http://www.w3.org/2002/07/owl#> | ||
| 52 | PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> | ||
| 53 | PREFIX air: <http://www.daml.org/2001/10/html/airport-ont#> | ||
| 54 | PREFIX bibtex: <http://purl.oclc.org/NET/nknouf/ns/bibtex#> | ||
| 55 | PREFIX bio: <http://purl.org/vocab/bio/0.1/> | ||
| 56 | PREFIX book: <http://purl.org/net/schemas/book/> | ||
| 57 | PREFIX contact: <http://www.w3.org/2000/10/swap/pim/contact#> | ||
| 58 | PREFIX cyc: <http://www.cyc.com/2004/06/04/cyc#> | ||
| 59 | PREFIX dc: <http://purl.org/dc/elements/1.1/> | ||
| 60 | PREFIX dcterms: <http://purl.org/dc/terms/> | ||
| 61 | PREFIX foaf: <http://xmlns.com/foaf/0.1/> | ||
| 62 | PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> | ||
| 63 | PREFIX ical: <http://www.w3.org/2002/12/cal/icaltzd#> | ||
| 64 | PREFIX lang: <http://purl.org/net/inkel/rdf/schemas/lang/1.1#> | ||
| 65 | PREFIX likes: <http://rdf.netalleynetworks.com/ilike/20040830#> | ||
| 66 | PREFIX quaff: <http://purl.org/net/schemas/quaffing/> | ||
| 67 | PREFIX rel: <http://purl.org/vocab/relationship/> | ||
| 68 | PREFIX trust: <http://trust.mindswap.org/ont/trust.owl#> | ||
| 69 | PREFIX visit: <http://purl.org/net/vocab/2004/07/visit#> | ||
| 70 | PREFIX whois: <http://www.kanzaki.com/ns/whois#> | ||
| 71 | PREFIX wn: <http://xmlns.com/wordnet/1.6/> | ||
| 72 | PREFIX wot: <http://xmlns.com/wot/0.1/> | ||
| 73 | END | ||
| 74 | |||
| 75 | { | ||
| 76 | my $file = File::Spec->catfile($ENV{HOME}, '.prefix-cmd', 'prefixes.json'); | ||
| 77 | if (-r $file) { | ||
| 78 | my $json = do { local($/) = undef; open( my $fh, '<', $file ) or next; <$fh> }; | ||
| 79 | my $prefixes = from_json($json); | ||
| 80 | $PREFIXES = join("\n", map { "PREFIX $_: <" . $prefixes->{$_} . ">" } (keys %$prefixes)); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | =item C<< cli_make_query_and_model >> | ||
| 85 | |||
| 86 | Returns a query object, model, and args HASHref based on the arguments in @ARGV. | ||
| 87 | These arguments are parsed using C<< cli_make_query >> and C<< make_model >>. | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | sub cli_make_query_and_model { | ||
| 92 | my ($query, $args) = cli_make_query(); | ||
| 93 | my $model = make_model( $args, @ARGV ); | ||
| 94 | return ($query, $model, $args); | ||
| 95 | } | ||
| 96 | |||
| 97 | =item C<< cli_make_query >> | ||
| 98 | |||
| 99 | Returns a RDF::Query object based on the arguments in @ARGV. These arguments | ||
| 100 | are parsed using C<< &cli_parse_args >>. If the -e flag is not present, the | ||
| 101 | query will be loaded from a file named by the argument in @ARGV immediately | ||
| 102 | following the final argument parsed by C<< &cli_parse_args >>. | ||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | sub cli_make_query { | ||
| 107 | my %args = cli_parse_args(@_); | ||
| 108 | my $class = delete $args{ class } || 'RDF::Query'; | ||
| 109 | my $sparql = delete $args{ query }; | ||
| 110 | my $l = Log::Log4perl->get_logger("rdf.query.util"); | ||
| 111 | $l->debug("creating sparql query with class $class"); | ||
| 112 | my $query = $class->new( $sparql, \%args ); | ||
| 113 | |||
| 114 | if (wantarray) { | ||
| 115 | return ($query, \%args); | ||
| 116 | } else { | ||
| 117 | return $query; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | =item C<< cli_make_model >> | ||
| 122 | |||
| 123 | Calls C<< make_model >> with arguments from C<< @ARGV >>, returning the | ||
| 124 | constructed model object. | ||
| 125 | |||
| 126 | C<< cli_make_model >> will usually be called after cli_make_query, allowing a | ||
| 127 | typical CLI invocation to look like `prog.pl [flags] [query file] [data files]`. | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | sub cli_make_model { | ||
| 132 | my %args; | ||
| 133 | my @files; | ||
| 134 | while (scalar(@ARGV) and $ARGV[0] ne '--') { | ||
| 135 | while (scalar(@ARGV) and $ARGV[0] =~ /^-(\w)$/) { | ||
| 136 | my $opt = shift(@ARGV); | ||
| 137 | if ($opt eq '-s') { | ||
| 138 | my $server = shift(@ARGV); | ||
| 139 | if ($server eq 'mysql') { | ||
| 140 | $args{ dsn } = "DBI:mysql:database="; | ||
| 141 | } elsif ($server eq 'sqlite') { | ||
| 142 | $args{ dsn } = "DBI:SQLite:dbname="; | ||
| 143 | } elsif ($server eq 'pg') { | ||
| 144 | $args{ dsn } = "DBI:Pg:dbname="; | ||
| 145 | } | ||
| 146 | } elsif ($opt eq '-d') { | ||
| 147 | $args{ dbname } = shift(@ARGV); | ||
| 148 | } elsif ($opt eq '-u') { | ||
| 149 | $args{ user } = shift(@ARGV); | ||
| 150 | } elsif ($opt eq '-p') { | ||
| 151 | $args{ pass } = shift(@ARGV); | ||
| 152 | } elsif ($opt eq '-m') { | ||
| 153 | $args{ model } = shift(@ARGV); | ||
| 154 | } elsif ($opt eq '--') { | ||
| 155 | last; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | if (@ARGV) { | ||
| 159 | my $file = shift(@ARGV); | ||
| 160 | push(@files, $file); | ||
| 161 | } | ||
| 162 | } | ||
| 163 | if (scalar(@ARGV) and $ARGV[0] eq '--') { | ||
| 164 | shift(@ARGV); | ||
| 165 | } | ||
| 166 | |||
| 167 | return make_model( \%args, @files ); | ||
| 168 | } | ||
| 169 | |||
| 170 | =item C<< make_model ( @files ) >> | ||
| 171 | |||
| 172 | Returns a model object suitable for use in a call to C<< $query->execute >>, | ||
| 173 | loaded with RDF from files and/or URLs listed in @files. This model may be any | ||
| 174 | of the supported models, but as currently implemented will be a | ||
| 175 | RDF::Trine::Model object. | ||
| 176 | |||
| 177 | =cut | ||
| 178 | |||
| 179 | sub make_model { | ||
| 180 | my $args = shift; | ||
| 181 | my %args = %$args; | ||
| 182 | my $l = Log::Log4perl->get_logger("rdf.query.util"); | ||
| 183 | |||
| 184 | while (scalar(@_) and $_[0] =~ m/^-(.)/) { | ||
| 185 | shift; | ||
| 186 | } | ||
| 187 | |||
| 188 | if ($args{ dsn } and $args{ user } and $args{ pass } and $args{ model } and $args{ dbname }) { | ||
| 189 | $args{ dsn } .= $args{ dbname }; | ||
| 190 | my $store = RDF::Trine::Store::DBI->new($args{ model }, $args{ dsn }, $args{ user }, $args{ pass }); | ||
| 191 | my $model = RDF::Trine::Model->new($store); | ||
| 192 | return $model; | ||
| 193 | } else { | ||
| 194 | # create a temporary triplestore, and wrap it into a model | ||
| 195 | my $model = RDF::Trine::Model->temporary_model; | ||
| 196 | |||
| 197 | # read in the list of files with RDF/XML content for querying | ||
| 198 | my @files = @_; | ||
| 199 | |||
| 200 | # loop over all the files | ||
| 201 | foreach my $i (0 .. $#files) { | ||
| 202 | my $file = $files[ $i ]; | ||
| 203 | my $pclass = RDF::Trine::Parser->guess_parser_by_filename( $file ) || 'RDF::Trine::Parser::RDFXML'; | ||
| 204 | my $parser = $pclass->new(); | ||
| 205 | |||
| 206 | if ($file =~ m<^https?:\/\/>) { | ||
| 207 | $l->debug("fetching RDF from $file ..."); | ||
| 208 | $parser->parse_url_into_model( $file, $model ); | ||
| 209 | } else { | ||
| 210 | $file = File::Spec->rel2abs( $file ); | ||
| 211 | # $uri is the URI object used as the base uri for parsing | ||
| 212 | my $uri = URI::file->new_abs( $file ); | ||
| 213 | my $content = do { open( my $fh, '<', $file ); local($/) = undef; <$fh> }; | ||
| 214 | $parser->parse_into_model( $uri, $content, $model ); | ||
| 215 | } | ||
| 216 | } | ||
| 217 | return $model; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | =item C<< cli_parse_args >> | ||
| 222 | |||
| 223 | Parses CLI arguments from @ARGV and returns a HASH with the recognized key/values. | ||
| 224 | The allowable arguments are listed below. | ||
| 225 | |||
| 226 | =cut | ||
| 227 | |||
| 228 | sub cli_parse_args { | ||
| 229 | my %args = @_; | ||
| 230 | $args{ class } = 'RDF::Query'; | ||
| 231 | my @service_descriptions; | ||
| 232 | |||
| 233 | while (scalar(@ARGV) and $ARGV[0] =~ /^-(\w+)/) { | ||
| 234 | my $opt = shift(@ARGV); | ||
| 235 | if ($opt eq '-e') { | ||
| 236 | $args{ query } = shift(@ARGV); | ||
| 237 | } elsif ($opt eq '-l') { | ||
| 238 | $args{ lang } = shift(@ARGV); | ||
| 239 | } elsif ($opt eq '-O') { | ||
| 240 | $args{ optimize } = 1; | ||
| 241 | } elsif ($opt eq '-o') { | ||
| 242 | $args{ force_no_optimization } = 1; | ||
| 243 | } elsif ($opt eq '-k') { | ||
| 244 | $args{ canonicalize } = 1; | ||
| 245 | } elsif ($opt eq '-C') { | ||
| 246 | my $k = shift(@ARGV); | ||
| 247 | my $v = shift(@ARGV); | ||
| 248 | $args{ $k } = $v; | ||
| 249 | } elsif ($opt eq '-c') { | ||
| 250 | my $class = shift(@ARGV); | ||
| 251 | eval "require $class"; | ||
| 252 | $args{ class } = $class; | ||
| 253 | } elsif ($opt eq '-f') { | ||
| 254 | require RDF::Query::Federate; | ||
| 255 | $args{ class } = 'RDF::Query::Federate'; | ||
| 256 | } elsif ($opt eq '-F') { | ||
| 257 | require RDF::Query::Federate; | ||
| 258 | require RDF::Query::ServiceDescription; | ||
| 259 | $args{ class } = 'RDF::Query::Federate'; | ||
| 260 | my $url_string = shift(@ARGV); | ||
| 261 | my $uri; | ||
| 262 | if ($url_string =~ m<^https?:\/\/>) { | ||
| 263 | $uri = URI->new( $url_string ); | ||
| 264 | } else { | ||
| 265 | $uri = URI::file->new_abs( $url_string ); | ||
| 266 | } | ||
| 267 | my $sd = RDF::Query::ServiceDescription->new_from_uri( $uri ); | ||
| 268 | push(@service_descriptions, $sd); | ||
| 269 | } elsif ($opt eq '-E') { | ||
| 270 | require RDF::Query::Federate; | ||
| 271 | require RDF::Query::ServiceDescription; | ||
| 272 | $args{ class } = 'RDF::Query::Federate'; | ||
| 273 | my $service_url = shift(@ARGV); | ||
| 274 | my $sd = RDF::Query::ServiceDescription->new( $service_url ); | ||
| 275 | push(@service_descriptions, $sd); | ||
| 276 | } elsif ($opt =~ /^-D([^=]+)(=(.+))?/) { | ||
| 277 | $args{ defines }{ $1 } = (defined($2) ? $3 : 1); | ||
| 278 | } elsif ($opt eq '-N') { | ||
| 279 | $args{ declare_namespaces } = 1; | ||
| 280 | } elsif ($opt eq '-s') { | ||
| 281 | my $server = shift(@ARGV); | ||
| 282 | if ($server eq 'mysql') { | ||
| 283 | $args{ dsn } = "DBI:mysql:database="; | ||
| 284 | } elsif ($server eq 'sqlite') { | ||
| 285 | $args{ dsn } = "DBI:SQLite:dbname="; | ||
| 286 | } elsif ($server eq 'pg') { | ||
| 287 | $args{ dsn } = "DBI:Pg:dbname="; | ||
| 288 | } | ||
| 289 | } elsif ($opt eq '-d') { | ||
| 290 | $args{ dbname } = shift(@ARGV); | ||
| 291 | } elsif ($opt eq '-u') { | ||
| 292 | $args{ user } = shift(@ARGV); | ||
| 293 | } elsif ($opt eq '-p') { | ||
| 294 | $args{ pass } = shift(@ARGV); | ||
| 295 | } elsif ($opt eq '-m') { | ||
| 296 | $args{ model } = shift(@ARGV); | ||
| 297 | } elsif ($opt eq '-w') { | ||
| 298 | if (exists($args{update}) and not($args{update})) { | ||
| 299 | warn "Model requested to be both read-only and read-write.\n"; | ||
| 300 | } else { | ||
| 301 | $args{ update } = 1; | ||
| 302 | } | ||
| 303 | } elsif ($opt eq '-r') { | ||
| 304 | if (exists($args{update}) and $args{update}) { | ||
| 305 | warn "Model requested to be both read-only and read-write.\n"; | ||
| 306 | } else { | ||
| 307 | $args{ update } = 0; | ||
| 308 | } | ||
| 309 | } elsif ($opt eq '--') { | ||
| 310 | last; | ||
| 311 | } | ||
| 312 | } | ||
| 313 | |||
| 314 | if (@service_descriptions) { | ||
| 315 | $args{ service_descriptions } = \@service_descriptions; | ||
| 316 | } | ||
| 317 | |||
| 318 | unless (defined($args{query})) { | ||
| 319 | if (@ARGV) { | ||
| 320 | my $file = shift(@ARGV); | ||
| 321 | my $sparql = ($file eq '-') | ||
| 322 | ? do { local($/) = undef; <> } | ||
| 323 | : do { local($/) = undef; open(my $fh, '<', $file) || croak $!; binmode($fh, ':utf8'); <$fh> }; | ||
| 324 | $args{ query } = $sparql; | ||
| 325 | } | ||
| 326 | } | ||
| 327 | |||
| 328 | if ($args{ query }) { | ||
| 329 | if (delete $args{ declare_namespaces }) { | ||
| 330 | $args{ query } = join('', $PREFIXES, $args{ query } ); | ||
| 331 | } | ||
| 332 | } | ||
| 333 | |||
| 334 | return %args; | ||
| 335 | } | ||
| 336 | |||
| 337 | 1; | ||
| 338 | |||
| 339 | __END__ |