| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
use strict; |
| 4 |
use CGI; |
| 5 |
use DBI; |
| 6 |
use DBIx::FullTextSearch; |
| 7 |
|
| 8 |
use vars qw! $q $dbh !; |
| 9 |
|
| 10 |
$q = new CGI; |
| 11 |
|
| 12 |
print $q->header('text/html'); |
| 13 |
|
| 14 |
print $q->start_html; |
| 15 |
|
| 16 |
print "<H1>DBIx::FullTextSearch Administration</H1>\n"; |
| 17 |
print $q->dump; |
| 18 |
|
| 19 |
if (defined $q->param('dsn')) { |
| 20 |
my $dsn = $q->param('dsn'); |
| 21 |
my $user = $q->param('user'); |
| 22 |
my $password = $q->param('password'); |
| 23 |
|
| 24 |
$dbh = DBI->connect($dsn, $user, $password, { |
| 25 |
'RaiseError' => 0, 'PrintError' => 1, |
| 26 |
}); |
| 27 |
if (defined $dbh and defined $q->param('test_connect')) { |
| 28 |
print $q->pre("Connect was successfull."), "\n"; |
| 29 |
} |
| 30 |
if (not defined $dbh) { |
| 31 |
print $q->pre("Connect failed:\n$DBI::errstr."), "\n"; |
| 32 |
offer_dbi_connect(); |
| 33 |
exit; |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
my $tables = $dbh->selectall_arrayref('show tables'); |
| 38 |
my %possible_indexes = map { ( $_->[0] => 1 ) } @$tables |
| 39 |
if defined $tables; |
| 40 |
for my $table (keys %possible_indexes) { |
| 41 |
local $dbh->{'PrintError'} = 0; |
| 42 |
unless ($dbh->selectrow_array( |
| 43 |
"select 1 from $table where param = 'data_table'")) { |
| 44 |
delete $possible_indexes{$table}; |
| 45 |
} |
| 46 |
} |
| 47 |
my @tables = sort keys %possible_indexes; |
| 48 |
|
| 49 |
if (@tables) { |
| 50 |
print "Select existing DBIx::FullTextSearch index to work with:\n", |
| 51 |
$q->start_form, |
| 52 |
$q->scrolling_list('index_name', \@tables), |
| 53 |
$q->br, |
| 54 |
$q->submit('test_index', 'Select index'), |
| 55 |
$q->hidden('dsn'), $q->hidden('user'), |
| 56 |
$q->hidden('password'), |
| 57 |
$q->end_form; |
| 58 |
} |
| 59 |
else { |
| 60 |
print "No existing DBIx::FullTextSearch index was found.\n"; |
| 61 |
} |
| 62 |
|
| 63 |
my $myself = $q->url('-relative' => 1); |
| 64 |
print $q->hr, "\n", |
| 65 |
$q->a({'href' => $myself}, 'Change database connection'), "\n"; |
| 66 |
} |
| 67 |
else { |
| 68 |
offer_dbi_connect(); |
| 69 |
} |
| 70 |
|
| 71 |
if (defined $q->param('index_name')) { |
| 72 |
my $fts_name = $q->param('index_name'); |
| 73 |
my $fts = DBIx::FullTextSearch->open($dbh, $fts_name); |
| 74 |
if (defined $fts and defined $q->param('test_index')) { |
| 75 |
print $q->pre("DBIx::FullTextSearch index $fts_name loaded OK.\n"); |
| 76 |
} |
| 77 |
if (not defined $fts) { |
| 78 |
print $q->pre("Loading DBIx::FullTextSearch index $fts_name failed: $DBIx::FullTextSearch::errstr.\n"); |
| 79 |
exit; |
| 80 |
} |
| 81 |
|
| 82 |
use Data::Dumper; |
| 83 |
print '<PRE>', Dumper($fts), "</PRE>\n"; |
| 84 |
|
| 85 |
if (defined $q->param('search')) { |
| 86 |
my $search = $q->param('search'); |
| 87 |
my @documents = $fts->contains($search); |
| 88 |
print "Documents containing string `$search' are @documents.<P>\n"; |
| 89 |
} |
| 90 |
elsif (defined $q->param('esearch')) { |
| 91 |
my $search = $q->param('esearch'); |
| 92 |
my @documents = $fts->econtains(split /\s+/, $search); |
| 93 |
print "Documents matching expression `$search' are @documents.<P>\n"; |
| 94 |
} |
| 95 |
|
| 96 |
print $q->start_form, |
| 97 |
$q->hidden('dsn'), $q->hidden('user'), |
| 98 |
$q->hidden('password'), $q->hidden('index_name'), |
| 99 |
$q->textfield('search'), $q->submit('Search in index'), |
| 100 |
$q->end_form, "<P>\n"; |
| 101 |
|
| 102 |
print $q->start_form, |
| 103 |
$q->hidden('dsn'), $q->hidden('user'), |
| 104 |
$q->hidden('password'), $q->hidden('index_name'), |
| 105 |
$q->textfield('esearch'), $q->submit('Search in index (extended)'), |
| 106 |
$q->end_form, "<P>\n"; |
| 107 |
} |
| 108 |
|
| 109 |
sub offer_dbi_connect { |
| 110 |
print "Please specify the DBI connect string, the user and |
| 111 |
password info for the database connection:\n"; |
| 112 |
print $q->start_form, |
| 113 |
$q->table( |
| 114 |
$q->Tr( $q->td("DBI connect"), |
| 115 |
$q->td($q->textfield('dsn', 'dbi:mysql:test')) |
| 116 |
), "\n", |
| 117 |
$q->Tr( $q->td("Database user"), |
| 118 |
$q->td($q->textfield('user', 'test')) |
| 119 |
), "\n", |
| 120 |
$q->Tr( $q->td("Password"), |
| 121 |
$q->td($q->password_field('password', 'test')) |
| 122 |
), "\n", |
| 123 |
), |
| 124 |
$q->submit('test_connect', 'Test connection'), |
| 125 |
$q->end_form; |
| 126 |
} |
| 127 |
|
| 128 |
END { print $q->end_html; } |
| 129 |
END { $dbh->disconnect if defined $dbh; } |
| 130 |
|