# # Copyright (c) 2002 Dr. Klemens Waldhör. All rights reserved. This program is free # software; you can redistribute it and/or modify it under the same terms # as Perl itself. package wktm::xindice; use Time::Local; use XML::DOM; use Frontier::Client; use strict; use vars qw(@EXPORT @ISA $VERSION); require Exporter; $VERSION = "1.0"; @ISA = qw(Exporter); @EXPORT = qw(); sub errorhandler { print "error !"; return; } my $DEBUGOUTPUT = *STDOUT; my $DEBUG = 0; # Function new # Description create an xindice XML RPC object # Parameter Type Comment # url string url to connect too # Returns object reference # Annotation: sub new { my $classname = shift; my $self = {}; bless($self, $classname); $self->{URL} = shift; # url $self->{COLLECTION} = "/db"; my $server = Frontier::Client->new( 'url' => $self->{URL} ); $self->{SERVER} = $server; $self->{ERROR} = 0; $self->{ERRORMSG} = ""; $self->{RECURSIVE} = 0; $self->{LASTCOMMAND} = 0; return $self; } # Function setRecursive # Description sets the that collections should be handled recursively # Parameter Type Comment # Returns list with collections # Annotation: sub setRecursive { my $self = shift; $self->{RECURSIVE} = 1; return $self->{RECURSIVE}; } # Function setNotRecursive # Description sets the that collections should not be handled recursively # Parameter Type Comment # Returns list with collections # Annotation: sub setNotRecursive { my $self = shift; $self->{RECURSIVE} = 0; return $self->{RECURSIVE}; } # Function setCollection # Description sets the default collection to be used # Parameter Type Comment # Returns list with collections # Annotation: sub setCollection { my $self = shift; return eval { $self->{COLLECTION} = shift; return $self->{COLLECTION}; }; } # Function runCommand # Description runs a specific XML RPC command # Parameter Type Comment # Returns result of command # Annotation: sub runCommand { my $self = shift; my $method= shift; my @args = @_; my $result; return eval { $result = $self->{SERVER}->call($method, @args); }; } # Function listCollections # Description list xindice collections # Parameter Type Comment # Returns list with collections # Annotation: sub listCollections { my $self = shift; my $collection = shift; if ($collection eq "") { $collection = $self->{COLLECTION}; } my $method = "db.listCollections"; $self->{LASTCOMMAND} = $method; my @args = ($collection); my $result; $result = eval { $result = $self->{SERVER}->call($method, @args); }; return $result; } # Function listAllCollections # Description list xindice collections including the sub collections # Parameter Type Comment # Returns list with all sub-collections # Annotation: my @allcollections = (); my $recounter = 0; sub listAllCollectionsRec { my $self = shift; my $collection = shift; $recounter++; my $result = $self->listCollections($collection); if (defined $result) { my @result = @$result; if (scalar @result == 0) { return; } foreach my $elem (@result) { my @resultnew = $self->listAllCollectionsRec("$collection/$elem"); push @allcollections, "$collection/$elem"; } } return; } sub listAllCollections { my $self = shift; my $collection = $self->{COLLECTION}; $recounter = 0; @allcollections = (); my @result = $self->listAllCollectionsRec($collection); $self->{LASTCOMMAND} = "listAllCollections"; return \@allcollections; } # Function listIndexers # Description list xindice indexers # Parameter Type Comment # Returns list with indexers # Annotation: sub listIndexers { my $self = shift; my $collection = shift; if ($collection eq "") { $collection = $self->{COLLECTION}; } return eval { my $method= 'db.listIndexers'; my @args = ($collection); my $result = $self->{SERVER}->call($method, @args); $self->{LASTCOMMAND} = $method; return $result; }; } # Function listAllIndexers # Description list all xindice indexers # Parameter Type Comment # Returns list with indexers # Annotation: sub listAllIndexers { my $self = shift; my $collection = $self->{COLLECTION}; my @allindexers = (); $recounter = 0; my $result = $self->listAllCollections; if (scalar @$result > 0) { foreach my $elem (@$result) { my $indresult = $self->listIndexers($elem); if (defined $indresult) { push @allindexers, @$indresult; } } } $self->{LASTCOMMAND} = "listAllIndexers"; return \@allindexers; } # Function listDocuments # Description list xindice documents # Parameter Type Comment # Returns list with documents # Annotation: sub listDocuments { my $self = shift; return eval { my $method= 'db.listDocuments'; my @args = ($self->{COLLECTION}); my $result = $self->{SERVER}->call($method, @args); $self->{LASTCOMMAND} = $method; return $result; }; } # Function getDocumentCount # Description get document count for collection # Parameter Type Comment # Returns list with documents # Annotation: sub getDocumentCount { my $self = shift; return eval { my $id = shift; my $method= 'db.getDocumentCount'; my @args = ($self->{COLLECTION}); my $result = $self->{SERVER}->call($method, @args); $self->{LASTCOMMAND} = $method; return $result; }; } # Function getDocument # Description gets a specific document # Parameter Type Comment # Returns document as string # Annotation: sub getDocument { my $self = shift; return eval { my $id = shift; my $method= 'db.getDocument'; my @args = ($self->{COLLECTION}, $id); my $result = $self->{SERVER}->call($method, @args); $self->{LASTCOMMAND} = $method; return $result; }; } # Function getDocuments # Description gets all documents of a collection # Parameter Type Comment # Returns document list # Annotation: sub getDocuments { my $self = shift; return eval { my $id = shift; my $method= 'db.getDocuments'; my @args = ($self->{COLLECTION}); my $result = $self->{SERVER}->call($method, @args); $self->{LASTCOMMAND} = $method; return $result; }; } # Function insertDocument # Description inserts a document into XINDICE collection # Parameter Type Comment # Returns document as string # Annotation: sub insertDocument { my $self = shift; return eval { my $id = shift; my $content = shift; my $method= 'db.insertDocument'; my @args = ($self->{COLLECTION}, $id, $content); my $result = $self->{SERVER}->call($method, @args); $self->{LASTCOMMAND} = $method; return $result; }; } # Function setDocument # Description sets a document into XINDICE collection # Parameter Type Comment # Returns document as string # Annotation: sub setDocument { my $self = shift; return eval { my $id = shift; my $content = shift; my $method= 'db.setDocument'; my @args = ($self->{COLLECTION}, $id, $content); my $result = $self->{SERVER}->call($method, @args); $self->{LASTCOMMAND} = $method; return $result; }; } # Function removeDocument # Description removes a document into XINDICE collection # Parameter Type Comment # Returns document as string # Annotation: sub removeDocument { my $self = shift; return eval { my $id = shift; my $method= 'db.removeDocument'; my @args = ($self->{COLLECTION}, $id); my $result = $self->{SERVER}->call($method, @args); $self->{LASTCOMMAND} = $method; return $result; }; } # Function createCollection # Description creates a new collection # Parameter Type Comment # Returns int # Annotation: sub createCollection { my $self = shift; return eval { my $collectionname = shift; my $method= 'db.createCollection'; my @args = ($self->{COLLECTION}, $collectionname); my $result = $self->{SERVER}->call($method, @args); $self->{LASTCOMMAND} = $method; return $result; }; } # Function dropCollection # Description drop a collection # Parameter Type Comment # Returns int # Annotation: sub dropCollection { my $self = shift; return eval { my $method= 'db.dropCollection'; my @args = ($self->{COLLECTION}); # print "Run $method\n"; my $result = $self->{SERVER}->call($method, @args); # print "After Run $method - $result\n"; $self->{COLLECTION} = ""; $self->{LASTCOMMAND} = $method; return $result; }; } # Function createIndexer # Description creates a new indexer # Parameter Type Comment # Returns int # Annotation: sub createIndexer { my $self = shift; return eval { my $indexname = shift; my $pattern = shift; my $method= 'db.createIndexer'; my @args = ($self->{COLLECTION}, $indexname, $pattern); my $result = $self->{SERVER}->call($method, @args); $self->{LASTCOMMAND} = $method; return $result; }; } # Function dropIndexer # Description creates a new OID # Parameter Type Comment # Returns int # Annotation: sub dropIndexer { my $self = shift; return eval { my $indexname = shift; my $method= 'db.dropIndexer'; my @args = ($self->{COLLECTION}, $indexname); my $result = $self->{SERVER}->call($method, @args); $self->{LASTCOMMAND} = $method; return $result; }; } # Function createNewOID # Description creates a new OID # Parameter Type Comment # Returns string with OID # Annotation: sub createNewOID { my $self = shift; return eval { my $method= 'db.createNewOID'; my @args = ($self->{COLLECTION}); my $result = $self->{SERVER}->call($method, @args); $self->{LASTCOMMAND} = $method; return $result; }; } # Function queryCollection # Description queries a collection # Parameter Type Comment # Returns string # Annotation: sub queryCollection { my $self = shift; return eval { my $query = shift; my $method= 'db.queryCollection'; my $type = "XPath"; my %hash = (); my @args = ($self->{COLLECTION}, $type, $query, {X => "http://www.xmldb.org/xpath"}); my $result = $self->{SERVER}->call($method, @args); $self->{LASTCOMMAND} = $method; return $result; }; } # Function queryAllCollections # Description queries all collection including sub-collections # Parameter Type Comment # Returns list of matches # Annotation: sub queryAllCollections { my $self = shift; my $query = shift; my $currentcollection = $self->{COLLECTION}; my @result = (); my $list = $self->listAllCollections; if (scalar @$list != 0) { foreach my $elem (@$list) { $self->{COLLECTION} = $elem; push @result, $self->queryCollection($query); } } $self->{LASTCOMMAND} = "queryAllCollections"; $self->{COLLECTION} = $currentcollection; return \@result; } # Function queryDocument # Description queries a collection # Parameter Type Comment # Returns string # Annotation: sub queryDocument { my $self = shift; return eval { my $query = shift; my $id = shift; my $method= 'db.queryDocument'; my $type = "XPath"; my %hash = (); my @args = ($self->{COLLECTION}, $type, $query, %hash, $id); my $result = $self->{SERVER}->call($method, @args); $self->{LASTCOMMAND} = $method; return $result; }; } 1;