diff --git a/cgi/product_multilingual.pl b/cgi/product_multilingual.pl index e55ea9ef0a959..7eaf7bba081d5 100755 --- a/cgi/product_multilingual.pl +++ b/cgi/product_multilingual.pl @@ -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"; } } } @@ -1862,7 +1878,13 @@ ($$$$$$) $comment = $comment . remove_tags_and_quote(decode utf8=>param('comment')); store_product($product_ref, $comment); - $html .= "

" . lang("product_changes_saved") . "

" + 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 .= "

" . lang("product_changes_saved") . "

" . lang("see_product_page") . "

"; if ($type eq 'delete') { diff --git a/lib/ProductOpener/Config_obf.pm b/lib/ProductOpener/Config_obf.pm index 9e503172ed83d..829cf03c5fd7a 100644 --- a/lib/ProductOpener/Config_obf.pm +++ b/lib/ProductOpener/Config_obf.pm @@ -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; diff --git a/lib/ProductOpener/Config_off.pm b/lib/ProductOpener/Config_off.pm index b67d6efe321eb..1d8f52b583c71 100644 --- a/lib/ProductOpener/Config_off.pm +++ b/lib/ProductOpener/Config_off.pm @@ -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; diff --git a/lib/ProductOpener/Display.pm b/lib/ProductOpener/Display.pm index d690a086b068d..9dd1864b5e55a 100644 --- a/lib/ProductOpener/Display.pm +++ b/lib/ProductOpener/Display.pm @@ -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, }; diff --git a/lib/ProductOpener/Products.pm b/lib/ProductOpener/Products.pm index a1656bbda8bf1..6a9852e3abeb9 100644 --- a/lib/ProductOpener/Products.pm +++ b/lib/ProductOpener/Products.pm @@ -212,14 +212,31 @@ 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 @@ -227,7 +244,7 @@ sub store_product($$) { $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"; @@ -235,12 +252,12 @@ sub store_product($$) { } } - 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}; @@ -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"; @@ -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 = []; } @@ -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); }