diff --git a/cgi/search.pl b/cgi/search.pl index 3c02a39c2c82f..8ebae126e1646 100755 --- a/cgi/search.pl +++ b/cgi/search.pl @@ -53,7 +53,7 @@ $action = 'process'; } -foreach my $parameter ('json', 'jsonp', 'jqm', 'jqm_loadmore', 'xml', 'rss') { +foreach my $parameter ('fields', 'json', 'jsonp', 'jqm', 'jqm_loadmore', 'xml', 'rss') { if (defined param($parameter)) { $request_ref->{$parameter} = param($parameter); diff --git a/lib/ProductOpener/Display.pm b/lib/ProductOpener/Display.pm index ca06f4f0995b7..851baa87b13c0 100644 --- a/lib/ProductOpener/Display.pm +++ b/lib/ProductOpener/Display.pm @@ -395,28 +395,34 @@ sub analyze_request($) print STDERR "analyze_request : query_string 1 : $request_ref->{query_string} \n"; + # Process API parameters: fields, formats, revision + # API calls may request JSON, JSONP or XML by appending .json, .jsonp or .xml at the end of the query string # .jqm returns results in HTML specifically formated for the OFF mobile app (which uses jquerymobile) # for calls to /cgi/ actions (e.g. search.pl), the format can also be indicated with a parameter &json=1 &jsonp=1 &xml=1 &jqm=1 # (or ?json=1 if it's the first parameter) - # first check for the rev parameter (revision of a product) - - foreach my $parameter ('rev', 'json', 'jsonp', 'jqm','xml') { + # first check parameters in the query string + + foreach my $parameter ('fields', 'rev', 'json', 'jsonp', 'jqm','xml') { - if ($request_ref->{query_string} =~ /(\&|\?)$parameter=(\d+)/) { - $request_ref->{query_string} =~ s/(\&|\?)$parameter=(\d+)//; + if ($request_ref->{query_string} =~ /(\&|\?)$parameter=([^\&]+)/) { + $request_ref->{query_string} =~ s/(\&|\?)$parameter=([^\&]+)//; $request_ref->{$parameter} = $2; print STDERR "analyze_request : $parameter = $request_ref->{$parameter} \n"; } - + } + + # then check suffixes .json etc. + + foreach my $parameter ('json', 'jsonp', 'jqm','xml') { + if ($request_ref->{query_string} =~ /\.$parameter$/) { $request_ref->{query_string} =~ s/\.$parameter$//; $request_ref->{$parameter} = 1; print STDERR "analyze_request : $parameter = 1 (.$parameter) \n"; } - - } + } print STDERR "analyze_request : query_string 2 : $request_ref->{query_string} \n"; @@ -2747,6 +2753,24 @@ HTML } + # If the request specified a value for the fields parameter, return only the fields listed + if (defined $request_ref->{fields}) { + + my $compact_products = []; + + for my $product_ref (@{$request_ref->{structured_response}{products}}) { + + my $compact_product_ref = {}; + foreach my $field (split(/,/, $request_ref->{fields})) { + if (defined $product_ref->{$field}) { + $compact_product_ref->{$field} = $product_ref->{$field}; + } + } + push @$compact_products, $compact_product_ref; + } + + $request_ref->{structured_response}{products} = $compact_products; + } # Pagination @@ -7698,10 +7722,23 @@ HTML else { $response{status} = 1; $response{status_verbose} = 'product found'; - $response{product} = $product_ref; add_images_urls_to_product($product_ref); + $response{product} = $product_ref; + + # If the request specified a value for the fields parameter, return only the fields listed + if (defined $request_ref->{fields}) { + my $compact_product_ref = {}; + foreach my $field (split(/,/, $request_ref->{fields})) { + if (defined $product_ref->{$field}) { + $compact_product_ref->{$field} = $product_ref->{$field}; + } + } + $response{product} = $compact_product_ref; + } + + if ($request_ref->{jqm}) { # return a jquerymobile page for the product