#!/usr/bin/perl -w -I/opt/eprints3/perl_lib 

######################################################################
#
#
# Copyright 2000-2008 University of Southampton. All Rights Reserved.
# 
#  This file is part of GNU EPrints 3.
#  
#  Copyright (c) 2000-2008 University of Southampton, UK. SO17 1BJ.
#  
#  EPrints 3 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 3 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 3; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
######################################################################

=pod

=head1 NAME

B<generate_views> - Generate static browse pages for an EPrint repository

=head1 SYNOPSIS

B<generate_views> I<repository_id> [B<options>] 

=head1 DESCRIPTION

This script creates some or all of the pages used in the /view/ section of the website. Since 3.1 these pages update themselves if they get only than a certain age, but this can cause delays for the viewer, so this script is still provided to pre-prepare them to provide a smoother experience.

Note: Since EPrints 3.1 it is not essential to run generate_views periodically, but it is still recommended.

What this does is generate browse pages for each field configured as browsable in B<ArchiveConfig.pm>. It creates a static web page for each value of that field, and index pages to navigate to them. 

For example, if we make "year" browseable then this script will generate one page for each unique value of the year field. So a user can then view the 1995 page and see links to all the 1995 eprints.

Advantages of this are that this puts less load on the database than user searches. Assuming you pick two or three sensible fields to make browsable. 

This script should be run every hour or so, but that should once a day or even once a week on large repositories, as the more eprints the longer it will take to run. The rough length of time to run this is of the order of O( B<languages> * B<eprints> * B<browsable fields> ).  You can automate running this with the B<cron> system.

If viewid is specified then only that view is updated. This can be useful if some views need updating more often then others. The top /view/ page which links to each view is always updated.

=head1 ARGUMENTS

=over 8

=item B<repository_id> 

The ID of the eprint repository to use.

=back

=head1 OPTIONS

=over 8

=item B<--generate menus> 

Only generate the menu pages, not the pages with links to records.

=item B<--generate lists> 

Only generate the pages with lists of records, not the menu pages.

=item B<--view> I<view_id>

Generate only the view with this ID.

=item B<--lang> I<lang_id>

Generate only pages for the language with this ID. 

=item B<--help>

Print a brief help message and exit.

=item B<--man>

Print the full manual page and then exit.

=item B<--quiet>

Be vewwy vewwy quiet. This option will supress all output unless an error occurs.

=item B<--verbose>

Explain in detail what is going on.
May be repeated for greater effect.

=item B<--version>

Output version information and exit.

=back   

=head1 AUTHOR

This is part of this EPrints 3 system. EPrints 3 is developed by Christopher Gutteridge.

=head1 VERSION

EPrints Version: eprints-3.1.3

=head1 CONTACT

For more information goto B<http://www.eprints.org/> which give information on mailing lists and the like.

Chris Gutteridge may be contacted at B<support@eprints.org>

Should you need a real world address for some reason, EPrints can be contacted in the real world at

 EPrints c/o Christopher Gutteridge
 Department of Electronics and Computer Science
 University of Southampton
 SO17 1BJ
 United Kingdom

=head1 COPYRIGHT

This file is part of GNU EPrints 3.

Copyright (c) 2000-2008 University of Southampton, UK. SO17 1BJ.

EPrints 3 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 3 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 3; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


=cut


use EPrints;

use File::Copy;
use strict;
use Getopt::Long;
use Pod::Usage;

my $version = 0;
my $verbose = 0;
my $quiet = 0;
my $help = 0;
my $man = 0;
my $generate_opt;
my $view_opt;
my $lang_opt;

Getopt::Long::Configure("permute");

GetOptions( 
	'help|?' => \$help,
	'man' => \$man,
	'version' => \$version,
	'verbose+' => \$verbose,
	'silent' => \$quiet,
	'quiet' => \$quiet,
	'generate=s' => \$generate_opt,
	'view=s' => \$view_opt,
	'lang=s' => \$lang_opt,
) || pod2usage( 2 );
EPrints::Utils::cmd_version( "generate_views" ) if $version;
pod2usage( 1 ) if $help;
pod2usage( -exitstatus => 0, -verbose => 2 ) if $man;
pod2usage( 2 ) if( scalar @ARGV != 1 ); 

my $noise = 1;
$noise = 0 if( $quiet );
$noise = 1+$verbose if( $verbose );

my $do_menus = 1;
my $do_lists = 1;
if( defined $generate_opt )
{
	if( $generate_opt eq "menus" ) { $do_lists = 0; }
	elsif( $generate_opt eq "lists" ) { $do_menus = 0; }
	else 
	{
		print STDERR "--generate must be either menus or lists.\n";
		exit 1;
	}
}

my $PATH = "view";

# Set STDOUT to auto flush (without needing a \n)
$|=1;

binmode( STDOUT, ":utf8" );

my $session = new EPrints::Session( 1 , $ARGV[0] , $noise );
exit( 1 ) unless( defined $session );

$session->cache_subjects;

my $repository = $session->get_repository;

my $views = $repository->get_conf( "browse_views" );

my $ds = $repository->get_dataset( "archive" );

if( defined $view_opt )
{
	my $ok = 0;
	foreach my $view ( @{$views} )
	{
		$ok = 1 if( $view->{id} eq $view_opt );
	}
	if( !$ok )
	{
		EPrints::abort( "Unknown view: $view_opt" );
	}
}

LANGUAGE: foreach my $langid ( @{$repository->get_conf( "languages" )} )
{
	next LANGUAGE if( defined $lang_opt && $langid ne $lang_opt );

	$session->change_lang( $langid );

	my @files = ();
	my $dir =  $repository->get_conf( "htdocs_path" )."/".$langid."/view";

	VIEW: foreach my $view ( @{$views} )
	{
		next VIEW if( defined $view_opt && $view->{id} ne $view_opt );

		push @files, update_view_by_path(
			session => $session,
			view => $view, 
			langid => $langid, 
			path => [],
			do_menus => $do_menus,
			do_lists => $do_lists );
	}

	if( $do_menus )
	{
		# make views index page
		my $file = EPrints::Update::Views::update_browse_view_list( $session, $langid );
		print "Wrote: $file\n" if( $noise > 1 );
		push @files, $file;
	}


	# TODO: remove any pages we didn't create.0l
	

	print "done\n" if( $noise > 1 );
}

$session->terminate();
exit;


sub update_view_by_path
{
	my( %opts ) = @_;

	my @files = ();

	my $sizes = EPrints::Update::Views::get_sizes( $opts{session}, $opts{view}, $opts{path} );

	if( defined $sizes )
	{
		# has sub levels
		if( $opts{do_menus} )
		{
			my @menu_files = EPrints::Update::Views::update_view_menu( $opts{session}, $opts{view}, $opts{langid}, $opts{path} );
			foreach my $file ( @menu_files )
			{
				print "Wrote: $file\n" if( $noise > 1 );
			}
			push @files, @menu_files;
		}

		foreach my $menu_value ( keys %{$sizes} )
		{
			my %newopts = %opts;
			$menu_value = EPrints::Utils::escape_filename( $menu_value );
			$newopts{path} = [@{$newopts{path}}, $menu_value];
			push @files, update_view_by_path( %newopts );
		}
	}

	if( !defined $sizes && $opts{do_lists} )
	{
		# is a leaf node
		my @leaf_files = EPrints::Update::Views::update_view_list( $opts{session}, $opts{view}, $opts{langid}, $opts{path} );
		foreach my $file ( @leaf_files )
		{
			print "Wrote: $file\n" if( $noise > 1 );
		}
		push @files, @leaf_files;
	}

	return @files;
}

	
