{"thread":{"id":"5341","subject":"[RFD] gitweb: href() function to generate URLs for CGI","startedAt":"2006-08-21T15:21:39Z","lastAt":"2006-08-21T15:21:39Z","messageCount":1,"participants":["Jakub Narebski"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"25689","messageId":"eccj1n$sio$1@sea.gmane.org","threadId":"5341","inReplyTo":null,"subject":"[RFD] gitweb: href() function to generate URLs for CGI","fromName":"Jakub Narebski","fromEmail":"jnareb@gmail.com","sentAt":"2006-08-21T15:21:39Z","receivedAt":"2006-08-21T15:21:39Z","isPatch":false,"sender":{"key":"jnareb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2706?v=4"},"body":"In first version of href() function we had\n(commit 06a9d86b49b826562e2b12b5c7e831e20b8f7dce)\n\n        my $href = \"$my_uri?\";\n        $href .= esc_param( join(\";\",\n                map {\n                        \"$mapping{$_}=$params{$_}\"\n                } keys %params\n        ) );\n\nFirst, there was a question what happend if someone would enter parameter\nname incorrectly, and some key of %params is not found in %mapping hash.\nThe above code would generate warnings (which web admins frown upon), and\nempty (because undef) parameters corresponding to e.g. mistyped parameter\nname. \n\nOne solution (sweeping under the carpet) would be to use parameter key as\nCGI parameter name if it is not found in the %mapping, i.e.\n\n        my $href = \"$my_uri?\";\n        $href .= esc_param( join(\";\",\n                map {\n                        if (exists $mapping{$_}) {\n                                \"$mapping{$_}=$params{$_}\"\n                        } else {\n                                \"$_=$params{$_}\"\n                        }\n                } keys %params\n        ) );\n\nAnother solution would be to skip parameters which are not found in\n%mapping. Correct way to do this is:\n\n        my $href = \"$my_uri?\";\n        $href .= esc_param( join(\";\",\n                map {\n                        \"$mapping{$_}=$params{$_}\"\n                } grep { exists $mapping{$_} } keys %params\n        ) );\n\n(we cannot put condition in map BLOCK, because map does not filter, only act\non elements, so the result would be empty parameter (e.g. \";;\" in generated\nURL), I guess without warnings).\n\nWhich solutions should be chosen? If the one is chosen, I can send the\npatch.\n\n\nSecond problem is that using href() function, although it consolidates to\ngenerate URL for CGI, it changes the order of CGI parameters. It used to be\nthat 'p' (project) parameter was first, then 'a' (action) parameter, then\nhashes ('h', 'hp', 'hb'), last 'f' (filename) or 'p' (page) or\n's' (searchtext). The simplest and fastest solution would be to create\narray with all keys of %mapping in appropriate order and do something like\nthis:\n\n        my @mapping_sorted = ('project', 'action', 'hash', 'hash_parent',\n                'hash_base', 'file_name', 'searchtext');\n\n        my $href = \"$my_uri?\";\n        $href .= esc_param( join(\";\",\n                map {\n                        \"$mapping{$_}=$params{$_}\"\n                } grep { exists $params{$_}} @mapping_sorted;\n        ) );\n\nThe problem is of course updating both %mappings and @mapping_sorted.\n\nIs this really a problem, should this (ordering of CGI parameters)\naddressed?\n\n-- \nJakub Narebski\nWarsaw, Poland\nShadeHawk on #git\n"}]}