#!/usr/bin/env perl

use warnings;
# vi: set ts=4 sw=4 :
#____________________________________________________________________________
#
#   MusicBrainz -- the open internet music database
#
#   Copyright (C) 2005 Robert Kaye
#
#   This program 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.
#
#   This program 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 this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#   $Id$
#____________________________________________________________________________

use strict;
use warnings;

use FindBin;
use lib "$FindBin::Bin/../lib";

use MusicBrainz::Server::Context;
use MusicBrainz::Server::Data::Utils qw( placeholders );
use Sql;
use Try::Tiny;

use constant MAX_TOP_LANGUAGES => 20;

# Always include the six official UN languages and the "Multiple languages"
# pseudo language.
#
use constant PRESELECTED_LANGUAGES => qw(
    ara eng fra spa rus zho mul
);


my $c = MusicBrainz::Server::Context->create_script_context;
my $sql = $c->sql;

#
# Set the frequency attribute of the language table.
#

my %languages = ( );

foreach my $code ( PRESELECTED_LANGUAGES )
{
    my $lang = $c->model('Language')->find_by_code($code);
    $languages{ $lang->id } = 1;
}

#
# Calculate language frequencies
#

$sql->select(<<'EOF', MAX_TOP_LANGUAGES + scalar(PRESELECTED_LANGUAGES));
    SELECT              language, COUNT(*)
    FROM                release
    WHERE NOT   language IS NULL
    GROUP BY    language
    ORDER BY    COUNT(*) DESC
    LIMIT               ?;
EOF


# put at most MAX_TOP_LANGUAGES into %languages
while ( my ($language, $count) = $sql->next_row() )
{
    last if keys(%languages) >= MAX_TOP_LANGUAGES;

    $languages{ $language } = 1;
}

$sql->finish();

print 'New top languages: ', join(' ', keys %languages), "\n";

#
# Start transaction here. No strict isolation required up to now.
#

try
{
    $sql->begin;

    my @top_languages = grep { m/^\d+$/ } keys %languages;

    # ignore languages with a frequency of 0 (never displayed in the UI)

    # reset current top languages to frequency 1 (default frequency)
    $sql->do(
        'UPDATE language SET frequency = ? WHERE frequency = ?',
        1, 2
    );

    # set new top languages to 2 (displayed in the short list)
    $sql->do(
        'UPDATE language SET frequency = 2
          WHERE id IN (' . placeholders(@top_languages) . ')',
        @top_languages
    );

    $sql->commit;
}
catch
{
    print "ERROR: Setting frequencies didn't work\n";
    print $_;
    $sql->rollback();
    exit 1;
}

#
# TODO: Invalidate memcached entries to use the new values sooner.
#

# eof SetLanguageFrequencies
