# Reindex-content plugin for Big Medium
# by Josh Clark and Global Moxie
# http://www.globalmoxie.com/
# $Id: BMreindex.pm 3295 2008-08-29 10:46:26Z josh $
#
# Copyright 2007 Josh Clark and Global Moxie. This code cannot be
# redistributed without permission from www.globalmoxie.com.  For more
# information, consult your Big Medium license.
#
# The reindex-content set of plugin files reindexes all of the site-specific
# data in all sites of the Big Medium installation. Useful if you've added
# new data columns to any of Big Medium's data classes or if you've manually
# moved a new site into the installation from another installation.
#
# To install:
#
# Upload these files to the moxiebin/plugins directory in your
# web account:
#
# * bm-reindex.cgi
# * modules/BMreindex.pm
#
# To run:
#
# Browse to the location of the bm-reindex.cgi script. For example:
# http://www.example.com/cgi-bin/moxiebin/plugins/bm-reindex.cgi
#
# Make no changes below this line ------------------------------------------

package BMreindex;
use strict;
use warnings;
use Carp;
$Carp::Verbose = 1;
use base qw(BigMed::App::Web);
use BigMed::Status;

sub setup {
    my $app = shift;
    $app->start_mode('reindex');
    $app->run_modes(
        'AUTOLOAD'   => sub { $_[0]->rm_update() },
        'reindex'    => 'rm_reindex',
        'do-reindex' => 'rm_do_reindex',
    );
    return;
}

sub rm_reindex {
    my $app       = shift;
    my $login_url =
      $app->build_url( script => 'bm-login.cgi', rm => 'login' );

    #meh, don't worry about localization for a plugin
    my $finished =
        q{Your data has been reindexed and scrubbed clean, }
      . q{fresh as a daisy in springtime.};

    my $update_url = $app->build_url(
        plugin => 'bm-reindex.cgi',
        rm     => 'do-reindex',
    );

    $app->js_add_onload( 'new BM.StatusDriver("bmcpUpdateStatus","'
          . $update_url
          . '",{onfinish:function(){'
          . 'BM.FX.hide($("bmcpUpdateStatus").parentNode,{effect:"slide"});'
          . 'BM.FX.reveal("updateComplete")'
          . '}});' );

    return $app->html_template_screen(
        'screen_updater.tmpl',
        bmcp_title   => 'Reindexing Data',
        complete     => $finished,
        disp_updated => 'none',
    );

}

my @CLASSES = qw(
  BigMed::CSS
  BigMed::Comment
  BigMed::Content::Page
  BigMed::Content::Annc
  BigMed::Content::Tip
  BigMed::ExtURL
  BigMed::Media::AV
  BigMed::Media::Document
  BigMed::Media::Image
  BigMed::Search::PageIndex
  BigMed::PageVersion
  BigMed::Person
  BigMed::Pointer
  BigMed::Prefs
  BigMed::Pullquote
  BigMed::Search::PageIndex
  BigMed::Section
  BigMed::Tag
  BigMed::URL
);

sub rm_do_reindex {
    my $app       = shift;
    my $bigmed    = $app->bigmed;
    my $statusbar = BigMed::Status->new() or return;

    my $nat = $bigmed->env('LANGUAGE') || 'en-US';
    my $lang = BigMed::Language->get_handle($nat);
    $lang->customize_lang( 'BMREINDEX_Reindexing data for site' =>
          'Reindexing [_1] for "%BM[_2]%"', );

    #do the update
    $app->log( notice => 'Reindex: Starting reindexing' );

    my $progress = $statusbar->progress;
    my $steps = @CLASSES;
    my %sitename;
    foreach my $class (@CLASSES) {
        $bigmed->load_required_class($class);
        my $source = $class->data_source;

        $progress++;
        $class->add_trigger(
            'before_reindex',
            sub {
                my ( $class, $site, $rcols ) = @_;
                $sitename{$site->id} ||= $site->name;
                $statusbar->update_status(
                    message => [
                        'BMREINDEX_Reindexing data for site', $source,
                        $site->name
                    ],
                    progress => $progress,
                    steps => $steps,
                );
                return 1;
            }
        );
        $class->add_trigger( 'mid_reindex', sub {
            my ($class, $site_id, $count) = @_;
            $statusbar->update_status(
                message => [
                    'BMREINDEX_Reindexing data for site', $source,
                    "$sitename{$site_id} ($count)"
                ]
            );
          } );
        $app->log( info => "Reindex: Reindexing $source info" );
        if ( !$class->reindex() ) {
            $statusbar->send_error();
            exit(0);
        }
    }
    $statusbar->mark_done();
    $app->teardown();
    exit(0);
}

1;

__END__

