
use EPrints;

use strict;

my $session = EPrints::Session->new();

# security?

my $content = "text/xml";
$session->send_http_header( content_type=>$content );
my $q = $session->param( "q" );

my $sql = "SELECT COUNT(eprintid) AS num_matches,publication,publisher,issn from eprint WHERE eprint_status='archive' AND publication IS NOT NULL AND publication LIKE '".EPrints::Database::prep_like_value($q)."\%' GROUP BY publication ORDER BY num_matches DESC LIMIT 40";

print <<END;
<?xml version="1.0" encoding="UTF-8" ?>

<ul>
END

my $sth = $session->get_database->prepare( $sql );
$session->get_database->execute( $sth , $sql );
my $first = 1;
while( my( $n,$pub,$org,$issn ) = $sth->fetchrow_array )
{
	if( $first )
	{
		print "<li class='ep_first'>";
		$first = 0;
	}
	else
	{
		print "<li>";
	}
	print "\"$pub\"";
	print " published by \"$org\"" if defined $org;
	print " ISSN:$issn" if defined $issn;
	print " ";
	print "<small>(found on $n record".($n>1?"s":"")." in this repository)</small>";
	print "<ul>";
	print "<li id='for:value:component:_publication'>$pub</li>";
	print "<li id='for:value:component:_publisher'>$org</li>";
	print "<li id='for:value:component:_issn'>$issn</li>";
	print "</ul></li>\n";
}
print "</ul>";

$session->terminate;

