######################################################################
#
# EPrints::MetaField::Longtext;
#
######################################################################
#
#  This file is part of GNU EPrints 2.
#  
#  Copyright (c) 2000-2004 University of Southampton, UK. SO17 1BJ.
#  
#  EPrints 2 is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  EPrints 2 is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with EPrints 2; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
######################################################################

=pod

=head1 NAME

B<EPrints::MetaField::Fulltext> - no description

=head1 DESCRIPTION

not done

=over 4

=cut

package EPrints::MetaField::Fulltext;

use strict;
use warnings;

BEGIN
{
	our( @ISA );

	@ISA = qw( EPrints::MetaField::Text );
}

use EPrints::MetaField::Text;

sub is_browsable
{
	return( 0 );
}

sub get_value
{
	my( $self, $object ) = @_;
	my @docs = $object->get_all_documents;
	my $r = [];
	foreach my $doc ( @docs )
	{
		push @{$r}, "_FULLTEXT_:".$doc->get_id;
	}
	return $r;
}

sub get_index_codes_basic
{
	my( $self, $session, $value ) = @_;

	if( $value !~ s/^_FULLTEXT_:// )
	{
		return $self->SUPER::get_index_codes_basic( $session, $value );
	}
	my $doc = EPrints::DataObj::Document->new( $session, $value );

	my $eprint =  $doc->get_eprint;
	return( [], [], [] ) unless( defined $eprint );

	my $words_file = $doc->words_file;
	my $indexcodes_file = $doc->indexcodes_file;
	my @s1 = stat( $words_file );
	my @s2 = stat( $indexcodes_file );
    	if( defined $s1[9] && defined $s2[9] && $s2[9] > $s1[9] )
	{
		my $codes = [];
		unless( open( CODELOG, $indexcodes_file ) )
		{
			$session->get_repository->log( "Failed to open $indexcodes_file: $!" );
		}
		else
		{
			foreach( <CODELOG> ) { chomp; push @$codes, $_; }
			close CODELOG;
		}
		return( $codes, [], [] );
	}

	$value = $doc->get_text;
	my( $codes, $badwords ) = ( [], [] );
	if( EPrints::Utils::is_set( $value ) )
	{
		( $codes, $badwords ) = EPrints::MetaField::Text::_extract_words( $session, $value );
	}
	
	unless( open( CODELOG, ">".$indexcodes_file ) )
	{
		$session->get_repository->log( "Failed to write to $indexcodes_file: $!" );
	}
	else
	{
		print CODELOG join( "\n", @$codes );
		close CODELOG;
	}
		
	# does not return badwords
	return( $codes, [], [] );
}


######################################################################
1;
