#!/usr/bin/perl

use Modern::Perl '2012';
use utf8;

my $usage = <<TXT
update_recent_changes.pl is a script that updates the changes collection in MongoDB using the changes.sto file.
TXT
;

use CGI::Carp qw(fatalsToBrowser);

use ProductOpener::Config qw/:all/;
use ProductOpener::Store qw/:all/;
use ProductOpener::Index qw/:all/;
use ProductOpener::Display qw/:all/;
use ProductOpener::Tags qw/:all/;
use ProductOpener::Users qw/:all/;
use ProductOpener::Images qw/:all/;
use ProductOpener::Lang qw/:all/;
use ProductOpener::Mail qw/:all/;
use ProductOpener::Products qw/:all/;
use ProductOpener::Food qw/:all/;
use ProductOpener::Ingredients qw/:all/;
use ProductOpener::Images qw/:all/;
use ProductOpener::SiteQuality qw/:all/;


use CGI qw/:cgi :form escapeHTML/;
use URI::Escape::XS;
use Storable qw/dclone/;
use Encode;
use boolean; # imports 'true' and 'false'

# Get a list of all products not yet updated

my $query_ref = {};
my $cursor = $products_collection->query($query_ref)->fields({ code => 1 });
my $count = $products_collection->count($query_ref);

my $n = 0;
	
print STDERR "$count products to update\n";

$recent_changes_collection->drop;

my $cmd = [
	create => 'recent_changes',
	capped => true,
	size   => 102400104857600
];

$database->run_command($cmd);
$recent_changes_collection = $database->get_collection('recent_changes');

while (my $product_ref = $cursor->next) {
	
	my $code = $product_ref->{code};
	my $path = product_path($code);
	
	my $changes_path = "$data_root/products/$path/changes.sto";
	print STDERR "updating product $code from $changes_path\n";
	
	my $changes_ref = retrieve($changes_path);
	if (not defined $changes_ref) {
		$changes_ref = [];
	}

	foreach my $change_ref (@$changes_ref) {
		my $change_document = {
			code => $product_ref->{code},
			userid => $change_ref->{userid},
			ip => $change_ref->{ip},
			t => $change_ref->{t},
			comment => $change_ref->{comment},
			rev => $change_ref->{rev},
			diff => $change_ref->{diffs}
		};
		$recent_changes_collection->insert_one($change_document);
	}

	$n++;
}
			
print "$n products updated\n";
			
exit(0);


Add script to copy all changes.sto to MongoDB.
Add script to copy all changes.sto to MongoDB.
