The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl -Iblib/arch -Iblib/lib -w
#!/usr/bin/perl -w

use strict;
use DBI;
use DBIx::FullTextSearch;
use Getopt::Long;

my $dummy;

my $USAGE = <<'EOF';
Usage: ftsadmin [ --version | [ datasource [ --create | --list |
		fts_index_name [ --contains list_of_words |
			--econtains list_of_expressions ] ] ] ]

Examples:
ftsadmin user/pass@db --list	# lists fts_indexes
ftsadmin user/pass@db --create index1 --frontend file --backend blob
						# creates index
EOF

unless (@ARGV) { die $USAGE; }


if ($ARGV[0] eq '--version' or $ARGV[0] eq '-v') {
	print "This is ftsadmin version $DBIx::FullTextSearch::VERSION.\n";
	exit;
	}


unless (@ARGV) { die "Specify the user/password\@database (or just the database) data source.\n"; }


my $datasource = shift;
my ($user, $password, $dsn) = ($datasource =~ m!^(?:(.*?)(?:/(.*))?\@)?(.*)$!);
my $text_dsn = (defined $user ? $user.'@' : '' ) . $dsn;

$dsn = 'dbi:mysql:' . $dsn unless $dsn =~ /^dbi:/;
my $dbh = DBI->connect($dsn, $user, $password)
	or die "Error connecting to database: $DBI::errstr\n";



unless (@ARGV) { die "Command or DBIx::FullTextSearch index name expected after datasource.\n"; }

if ($ARGV[0] eq '--list') {
	my @list = DBIx::FullTextSearch->list_fts_indexes($dbh);
	for my $name (@list) { print $name, "\n"; }
	exit;
	}

if ($ARGV[0] eq '--create') {
	$dummy = shift;
	if (not @ARGV) { die "Name of index to create expected.\n"; }
	my $create_name = shift;

	my %options = ();
	GetOptions(\%options,
		'frontend=s', 'backend=s',
		'word_id_bits=i', 'doc_id_bits=i', 'count_bits=i',
		'word_length=i', 'blob_direct_fetch=i', 'data_table=s',
		'name_length=i', 'position_bits=i', 'filter=s',
		'splitter=s', 'init_env=s', 'word_id_table=s',
		'doc_id_table=s', 'table_name=s', 'column_name=s',
		'column_id_name=s',
		);

	DBIx::FullTextSearch->create($dbh, $create_name, %options)
						or die $DBIx::FullTextSearch::errstr, "\n";
	exit;
	}


unless (@ARGV) { die "Name of index to work with expected.\n"; }

my $index_name = shift;
my $fts = DBIx::FullTextSearch->open($dbh, $index_name) or die $DBIx::FullTextSearch::errstr, "\n";


unless (@ARGV) { die "Command for index `$index_name' expected.\n"; }

if ($ARGV[0] eq '--contains') {
	$dummy = shift;
	print join "\n", $fts->contains(@ARGV), '';
	}
elsif ($ARGV[0] eq '--econtains') {
	$dummy = shift;
	print join "\n", $fts->econtains(@ARGV), '';
	}
elsif ($ARGV[0] eq '--drop') {
	$fts->drop;
	}
elsif ($ARGV[0] eq '--index') {
	$dummy = shift;
	$fts->index_document(@ARGV);
	}
else {
	die "Command `$ARGV[0]' not recognized.\n";
	}

1;

=head1 NAME

ftsadmin - command line admin utility for DBIx::FullTextSearch

=head1 SYNOPSIS

  ftsadmin jez/hes@test --create zvirata --frontend=string
  ftsadmin jez/hes@test zvirata --index slon 'Slon ma chobot'
  ftsadmin jez/hes@test zvirata --index krtek 'Krtek ma bodliny'
  ftsadmin jez/hes@test zvirata --contains bodliny

=head1 DESCRIPTION

ftsadmin is a command line utility for listing, creating
and dropping of DBIx::FullTextSearch indexes and for indexing new documents and
searching for matches. The schematic listing of ftsadmin arguments
is:

  ftsadmin --version
  ftsadmin user/pass@db --list
  ftsadmin user/pass@db --create index_name [ parameters ]
  ftsadmin user/pass@db index_name --index doc_name [ content ]
  ftsadmin user/pass@db index_name --contains list_of_words
  ftsadmin user/pass@db index_name --econtains list_of_words
  ftsadmin user/pass@db index_name --drop

For command --version that return the version information of the
underlying DBIx::FullTextSearch module no user or database specification is needed.

For all other commands you need to specify a way to connect to the
database. The general way is user@password/database but you can omit the
password or even the username part, thus

	jezek/heslo@test
	jezek@test
	test

are all valid database specification (valid semantically, of course; you
should specify one that will elad to access to the database).

After the database specification, you can either pass commands that do
not operate on existing indexes, or add a name of the index and then
commands with possible further arguments.

The command --list lists all available DBIx::FullTextSearch indexes in the
database.

The command --create creates new index. The name of the index is the
first mandatory parameter after the --create command, after that you can
specify index options. For the list of them and their meaning, please
see the DBIx::FullTextSearch(3) man page.

If you want to work with existing index, you hve to specify the index
name as the second parameter, and as third the command.

To index a document (add new document or update existing document in
the index), use the --index command. This is followed by either the
document name (file and url frontends) or the name and the content of the
document (the default and string frontends).

Commands --contains and --econtains return list of documents as their
counterpart DBIx::FullTextSearch methods do.

You can drop existing index with --drop command.

This program is meant as a fast utility that you can use to easily test
various storage parameters of the indexes. For production use you'll
probably want to write your own Perl code, using the DBIx::FullTextSearch module
directly.

=head1 AUTHOR

(c) 1999 Jan Pazdziora, adelton@fi.muni.cz,
http://www.fi.muni.cz/~adelton/ at Faculty of Informatics, Masaryk
University in Brno, Czech Republic

All rights reserved. This package is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.

=head1 SEE ALSO

L<DBIx::FullTextSearch>

=cut