Skip to content

Commit

Permalink
bug #319 - allow moving products to other instances of Product Opener…
Browse files Browse the repository at this point in the history
… running on the same server - OFF/OBF/OPFF
  • Loading branch information
stephanegigandet committed Mar 30, 2017
1 parent 7bf11ef commit caff350
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 22 deletions.
34 changes: 28 additions & 6 deletions cgi/product_multilingual.pl
Original file line number Diff line number Diff line change
Expand Up @@ -233,19 +233,35 @@
$debug and print STDERR "product.pl action: process - phase 1 - type: $type code $code\n";

# 26/01/2017 - disallow barcode changes until we fix bug #677
if (0 and (defined param('new_code'))) {
my $new_code = normalize_code(param('new_code'));
if ($admin and (defined param('new_code'))) {
my $new_code = param('new_code');
my $new_server = "";
my $new_data_root = $data_root;

if ($new_code =~ /^([a-z]+)$/) {
$new_server = $1;
if ((defined $options{other_servers}) and (defined $options{other_servers}{$new_server})
and ($options{other_servers}{$new_server}{data_root} ne $data_root)) {
$new_code = $code;
$new_data_root = $options{other_servers}{$new_server}{data_root};
}
}

$new_code = normalize_code($new_code);
if ($new_code =~ /^\d+$/) {
# check that the new code is available
if (-e "$data_root/products/" . product_path($new_code)) {
if (-e "$new_data_root/products/" . product_path($new_code)) {
push @errors, lang("error_new_code_already_exists");
print STDERR "product.pl - cannot change code $code to $new_code (already exists)\n";
print STDERR "product.pl - cannot change code $code to $new_code - $new_server (already exists)\n";
}
else {
$product_ref->{old_code} = $code;
$code = $new_code;
$product_ref->{code} = $code;
print STDERR "product.pl - changing code $product_ref->{old_code} to $code\n";
if ($new_server ne '') {
$product_ref->{new_server} = $new_server;
}
print STDERR "product.pl - changing code $product_ref->{old_code} to $code - $new_server\n";
}
}
}
Expand Down Expand Up @@ -1862,7 +1878,13 @@ ($$$$$$)
$comment = $comment . remove_tags_and_quote(decode utf8=>param('comment'));
store_product($product_ref, $comment);

$html .= "<p>" . lang("product_changes_saved") . "</p><p>&rarr; <a href=\"" . product_url($product_ref) . "\">"
my $product_url = product_url($product_ref);

if (defined $product_ref->{new_server}) {
$product_url = "https://" . $subdomain . "." . $options{other_servers}{$product_ref->{new_server}}{domain} . $product_url;
}

$html .= "<p>" . lang("product_changes_saved") . "</p><p>&rarr; <a href=\"" . $product_url . "\">"
. lang("see_product_page") . "</a></p>";

if ($type eq 'delete') {
Expand Down
31 changes: 31 additions & 0 deletions lib/ProductOpener/Config_obf.pm
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,35 @@ last_edit_dates

);

# allow moving products to other instances of Product Opener on the same server
# e.g. OFF -> OBF
$options{other_servers} = {
obf =>
{
name => "Open Beauty Facts",
data_root => "/home/obf",
www_root => "/home/obf/html",
mongodb => "obf",
domain => "openbeautyfacts.org",
},
off =>
{
name => "Open Food Facts",
data_root => "/home/off",
www_root => "/home/off/html",
mongodb => "off",
domain => "openfoodfacts.org",
},
opff =>
{
prefix => "opff",
name => "Open Pet Food Facts",
data_root => "/home/opff",
www_root => "/home/opff/html",
mongodb => "opff",
domain => "openpetfoodfacts.org",
}
};


1;
30 changes: 30 additions & 0 deletions lib/ProductOpener/Config_off.pm
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,34 @@ last_edit_dates

);

# allow moving products to other instances of Product Opener on the same server
# e.g. OFF -> OBF
$options{other_servers} = {
obf =>
{
name => "Open Beauty Facts",
data_root => "/home/obf",
www_root => "/home/obf/html",
mongodb => "obf",
domain => "openbeautyfacts.org",
},
off =>
{
name => "Open Food Facts",
data_root => "/home/off",
www_root => "/home/off/html",
mongodb => "off",
domain => "openfoodfacts.org",
},
opff =>
{
prefix => "opff",
name => "Open Pet Food Facts",
data_root => "/home/opff",
www_root => "/home/opff/html",
mongodb => "opff",
domain => "openpetfoodfacts.org",
}
};

1;
9 changes: 9 additions & 0 deletions lib/ProductOpener/Display.pm
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ $products_collection = $database->get_collection('products');
$emb_codes_collection = $database->get_collection('emb_codes');


if (defined $options{other_servers}) {

foreach my $server (keys %{$options{other_servers}}) {
$options{other_servers}{$server}{database} = $connection->get_database($options{other_servers}{$server}{mongodb});
$options{other_servers}{$server}{products_collection} = $options{other_servers}{$server}{database}->get_collection('products');
}
}


$default_request_ref = {
page=>1,
};
Expand Down
51 changes: 35 additions & 16 deletions lib/ProductOpener/Products.pm
Original file line number Diff line number Diff line change
Expand Up @@ -212,35 +212,52 @@ sub store_product($$) {
my $path = product_path($code);
my $rev = $product_ref->{rev};

# In case we need to move a product from OFF to OBF etc.
# then we first move the existing files (product and images)
# and then store the product with a comment.

my $new_data_root = $data_root;
my $new_www_root = $www_root;
my $new_products_collection = $products_collection;


# Changing the code?
# 26/01/2017 - disallow code changes until we fix #677
if (0 and (defined $product_ref->{old_code})) {
if ($admin and (defined $product_ref->{old_code})) {

my $old_code = $product_ref->{old_code};
my $old_path = product_path($old_code);

print STDERR "Products::store_product - move from $old_code to $code\n";

if (defined $product_ref->{new_server}) {
my $new_server = $product_ref->{new_server};
$new_data_root = $options{other_servers}{$new_server}{data_root};
$new_www_root = $options{other_servers}{$new_server}{data_root};
$new_products_collection = $options{other_servers}{$new_server}{products_collection};
}

print STDERR "Products::store_product - move from $old_code to $code - $new_data_root \n";

# Move directory

my $prefix_path = $path;
$prefix_path =~ s/\/[^\/]+$//; # remove the last subdir: we'll move it
print STDERR "Products::store_product - path: $path - prefix_path: $prefix_path\n";
# Create the directories for the product
foreach my $current_dir ($data_root . "/products", $www_root . "/images/products") {
foreach my $current_dir ($new_data_root . "/products", $new_www_root . "/images/products") {
(-e "$current_dir") or mkdir($current_dir, 0755);
foreach my $component (split("/", $prefix_path)) {
$current_dir .= "/$component";
(-e "$current_dir") or mkdir($current_dir, 0755);
}
}

if ((! -e "$data_root/products/$path")
and (! -e "$www_root/images/products/$path")) {
if ((! -e "$new_data_root/products/$path")
and (! -e "$new_www_root/images/products/$path")) {
use File::Copy;
print STDERR "Products::store_product - move from $data_root/products/$old_path to $data_root/products/$path (new)\n";
move("$data_root/products/$old_path", "$data_root/products/$path") or print STDERR "error moving data from $data_root/products/$old_path to $data_root/products/$path : $!\n";
move("$www_root/images/products/$old_path", "$www_root/images/products/$path") or print STDERR "error moving html from $www_root/images/products/$old_path to $www_root/images/products/$path : $!\n";
move("$data_root/products/$old_path", "$new_data_root/products/$path") or print STDERR "error moving data from $data_root/products/$old_path to $new_data_root/products/$path : $!\n";
move("$www_root/images/products/$old_path", "$new_www_root/images/products/$path") or print STDERR "error moving html from $www_root/images/products/$old_path to $new_www_root/images/products/$path : $!\n";

delete $product_ref->{old_code};

Expand All @@ -258,7 +275,7 @@ sub store_product($$) {

if ($rev < 1) {
# Create the directories for the product
foreach my $current_dir ($data_root . "/products", $www_root . "/images/products") {
foreach my $current_dir ($new_data_root . "/products", $new_www_root . "/images/products") {
(-e "$current_dir") or mkdir($current_dir, 0755);
foreach my $component (split("/", $path)) {
$current_dir .= "/$component";
Expand All @@ -268,7 +285,7 @@ sub store_product($$) {
}

# Check lock and previous version
my $changes_ref = retrieve("$data_root/products/$path/changes.sto");
my $changes_ref = retrieve("$new_data_root/products/$path/changes.sto");
if (not defined $changes_ref) {
$changes_ref = [];
}
Expand Down Expand Up @@ -331,23 +348,25 @@ sub store_product($$) {
$product_ref->{complete} += 0;
$product_ref->{sortkey} += 0;



if ($product_ref->{deleted}) {
$products_collection->remove({"_id" => $product_ref->{_id}});
$new_products_collection->remove({"_id" => $product_ref->{_id}});
}
else {
$products_collection->save($product_ref);
$new_products_collection->save($product_ref);
}

store("$data_root/products/$path/$rev.sto", $product_ref);
store("$new_data_root/products/$path/$rev.sto", $product_ref);
# Update link
my $link = "$data_root/products/$path/product.sto";
my $link = "$new_data_root/products/$path/product.sto";
if (-l $link) {
unlink($link) or print STDERR "Products::store_product could not unlink $link : $! \n";
}
#symlink("$data_root/products/$path/$rev.sto", $link) or print STDERR "Products::store_product could not symlink $data_root/products/$path/$rev.sto to $link : $! \n";
symlink("$rev.sto", $link) or print STDERR "Products::store_product could not symlink $data_root/products/$path/$rev.sto to $link : $! \n";
#symlink("$new_data_root/products/$path/$rev.sto", $link) or print STDERR "Products::store_product could not symlink $new_data_root/products/$path/$rev.sto to $link : $! \n";
symlink("$rev.sto", $link) or print STDERR "Products::store_product could not symlink $new_data_root/products/$path/$rev.sto to $link : $! \n";

store("$data_root/products/$path/changes.sto", $changes_ref);
store("$new_data_root/products/$path/changes.sto", $changes_ref);
}


Expand Down

0 comments on commit caff350

Please sign in to comment.