| 1 | package Wiki::Toolkit::Setup::DBIxFTSMySQL; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | use vars qw( $VERSION ); | ||
| 6 | $VERSION = 0.04; | ||
| 7 | |||
| 8 | use DBI; | ||
| 9 | use DBIx::FullTextSearch; | ||
| 10 | use Carp; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Wiki::Toolkit::Setup::DBIxFTSMySQL - set up fulltext indexes for Wiki::Toolkit | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | use Wiki::Toolkit::Setup::DBIxFTSMySQL; | ||
| 19 | Wiki::Toolkit::Setup::DBIxFTSMySQL::setup($dbname, $dbuser, $dbpass, $dbhost); | ||
| 20 | |||
| 21 | Omit $dbhost if the database is local. | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | Set up DBIx::FullTextSearch indexes for use with Wiki::Toolkit. Has only | ||
| 26 | one function, C<setup>, which takes as arguments B<either> the | ||
| 27 | database name, the username and the password B<or> a database handle | ||
| 28 | . The username must be able to create and drop tables in the database. | ||
| 29 | |||
| 30 | The $dbhost argument is optional -- omit it if the database is local. | ||
| 31 | |||
| 32 | Note that any pre-existing L<Wiki::Toolkit> indexes stored in the database | ||
| 33 | will be I<cleared> by this function, so if you have existing data you | ||
| 34 | probably want to use the C<store> parameter to get it re-indexed. | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | sub setup { | ||
| 39 | my $dbh = _get_dbh( @_ ); | ||
| 40 | |||
| 41 | # Drop FTS indexes if they already exist. | ||
| 42 | my $fts = DBIx::FullTextSearch->open($dbh, "_content_and_title_fts"); | ||
| 43 | $fts->drop if $fts; | ||
| 44 | $fts = DBIx::FullTextSearch->open($dbh, "_title_fts"); | ||
| 45 | $fts->drop if $fts; | ||
| 46 | |||
| 47 | # Set up FullText indexes and index anything already extant. | ||
| 48 | my $fts_all = DBIx::FullTextSearch->create($dbh, "_content_and_title_fts", | ||
| 49 | frontend => "table", | ||
| 50 | backend => "phrase", | ||
| 51 | table_name => "node", | ||
| 52 | column_name => ["name","text"], | ||
| 53 | column_id_name => "name", | ||
| 54 | stemmer => "en-uk"); | ||
| 55 | |||
| 56 | my $fts_title = DBIx::FullTextSearch->create($dbh, "_title_fts", | ||
| 57 | frontend => "table", | ||
| 58 | backend => "phrase", | ||
| 59 | table_name => "node", | ||
| 60 | column_name => "name", | ||
| 61 | column_id_name => "name", | ||
| 62 | stemmer => "en-uk"); | ||
| 63 | |||
| 64 | my $sql = "SELECT name FROM node"; | ||
| 65 | my $sth = $dbh->prepare($sql); | ||
| 66 | $sth->execute(); | ||
| 67 | while (my ($name, $version) = $sth->fetchrow_array) { | ||
| 68 | $fts_title->index_document($name); | ||
| 69 | $fts_all->index_document($name); | ||
| 70 | } | ||
| 71 | $sth->finish; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub _get_dbh { | ||
| 75 | return $_[0] if ( ref $_[0] and ref $_[0] eq 'DBI::db' ); | ||
| 76 | my ($dbname, $dbuser, $dbpass, $dbhost) = @_; | ||
| 77 | my $dsn = "dbi:mysql:$dbname"; | ||
| 78 | $dsn .= ";host=$dbhost" if $dbhost; | ||
| 79 | my $dbh = DBI->connect($dsn, $dbuser, $dbpass, | ||
| 80 | { PrintError => 1, RaiseError => 1, | ||
| 81 | AutoCommit => 1 } ) | ||
| 82 | or croak DBI::errstr; | ||
| 83 | return $dbh; | ||
| 84 | } | ||
| 85 | |||
| 86 | =head1 AUTHOR | ||
| 87 | |||
| 88 | Kake Pugh (kake@earth.li). | ||
| 89 | |||
| 90 | =head1 COPYRIGHT | ||
| 91 | |||
| 92 | Copyright (C) 2002-2004 Kake Pugh. All Rights Reserved. | ||
| 93 | |||
| 94 | This module is free software; you can redistribute it and/or modify it | ||
| 95 | under the same terms as Perl itself. | ||
| 96 | |||
| 97 | =head1 SEE ALSO | ||
| 98 | |||
| 99 | L<Wiki::Toolkit>, L<Wiki::Toolkit::Setup::MySQL>, L<DBIx::FullTextSearch> | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | 1; |