May 2007

For the benefit of those who participated in the Google developers day presentation a French only post.

Google Developers Days Google Maps (présentation par Ori Pekelman)

The mysql prepare statement sucks heavy time. You can not (!?!!!) use it to template field and table names. So it is mostly useless for any heavy duty stored procedures that want to address different columns.

Here is a small function that will take care of that :)

  1. DELIMITER //
  2. DROP FUNCTION  IF EXISTS substrCount//
  3. CREATE FUNCTION substrCount(x varchar(255), delim varchar(12)) returns int
  4. RETURN (length(x)-length(REPLACE(x, delim, '')))/length(delim);//
  5.  
  6. DROP FUNCTION  IF EXISTS charsplit//
  7. CREATE FUNCTION charsplit(x varchar(255), delim varchar(12), pos int) returns varchar(255)

af83's very own Ori Pekelman, Clément Hallet and Louis Montagne will be hosting cool workshops at the Google Developer Day 2007 event here in Paris, on May 31st.

EN: Imagine you have a google maps application (or anything else that deals with places) with data in France, now often you would want to get places that are in the same area as one chosen by the user. Here is a bit of PHP code to help you with that...

table containing the correspondence between cities and postal codes in France; contient la correspondance entre les code postaux et les villes en France

If you want to log what a user is doing, or have any other reason to get his ip using HTTP_CLIENT_IP to get the remote user IP is not at all enough, if the client is connecting through a proxy (for an example your site is configured behind an apache_mod_proxy) you should also test for HTTP_X_FORWARDED_FOR and fall back on REMOTE_ADDR or you might always be getting the proxie's IP.

<?php
 
function getIP() {
$ip;

if (getenv("HTTP_CLIENT_IP")) $ip getenv("HTTP_CLIENT_IP");
else if(
getenv("HTTP_X_FORWARDED_FOR")) $ip getenv("HTTP_X_FORWARDED_FOR");?>

Ever needed to select in a form a start and an end time (meeting, event etc..) this little bit of code will help you have a default interval (here 2 hours). When the first combo box changes the second one is set to two hours later.
As usual this javascript is unobtrusive and degradable, just call somewhere initHourCombos() to initialize it. Make sure you change the name $('start') and $('end') if your elements have a different id.

/* Make connected combo box 'End' jump 2 hours when combo 'Start' changes
* (c) AF83 2007 Ori Pekelman

Sometimes (often) you would want your script to have different configurations based on the environment in which it runs (dev, test, prod). Sometimes the same script may even be run from the console and you really want to have a different configuration for that. Now you may run into some trouble using just the $_SERVER["HTTP_HOST"] variable. Even more so if you are running behind an proxy (such as mod_proxy for apache).

My javascript validated the e-mail addresses, my PHP code validated them why not let mysql have a go at it too?

Here is a view for mysql (don't even think about trying to use this with MySQL < 5.0.1) that will validate email addresses with a select statement, no need for UDF or anything fancy
this is based on code by Narayana Vyas Kondreddi http://vyaskn.tripod.com/handling_email_addresses_in_sql_server.htm

  1. CREATE VIEW `invalid_emails` AS SELECT `table_with_email_column`.`email` AS

Ever need to strip non numeric characters from a varchar column ? well here's a solution, That will keep leading zeros and all... this query will treat up to 24 characters but you can repeat this up to 255 if you want...

  1. SELECT (concat(
  2. CASE
  3. WHEN substring(phone_number, 1,1) REGEXP '[0-9]'
  4. THEN substring(phone_number, 1,1) ELSE '' END ,
  5. CASE
  6. WHEN substring(phone_number, 2,1) REGEXP '[0-9]'
  7. THEN substring(phone_number, 2,1) ELSE '' END ,
  8. CASE
  9. WHEN substring(phone_number, 3,1) REGEXP '[0-9]'
  10. THEN substring(phone_number, 3,1) ELSE '' END ,
  11. CASE

Ruby on Rails is a great framework, but would you believe it .. it has no built-in support for localized error messages for model level validation.

The problem is that you can internationalize the error messages but not the field name. Which makes for some very weird behavior.

Here is a bit of code to solve the problem. You have to install localization_generator before.

How to have clean internationalized validation error messages in Ruby on Rails?

in the file app/contollers/application.rb

  1. require 'localization'
  2.  
  3. class ActiveRecord::Errors
  4.   include Localization

Here's the script :

  1. #!/usr/bin/perl
  2. use strict;
  3. use JSON;
  4.  
  5. my $files = "[\n";
  6.  
  7. while(){
  8. 	chomp;$_ =~ s/([\ \(\)\&amp;'])/\$1/g;
  9. 	my ( $md5 ) = split( " ", `md5sum $_` );
  10. 	my $ffmpegInfo = `ffmpeg -i $_ 2&gt;/dev/stdout`;
  11. 	my $file = {fileName =&gt; $_,md5 =&gt; $md5,bruteInfo =&gt; `file $_`};
  12. 	for( split( "\n", $ffmpegInfo ) ){
  13. 		if( /bitrate\:\ ([\d]*?)\ kb\/s/ ){ $file-&gt;{ bitrate } = $1; }
  14. 		if( /Duration\:\ (.*?)\,\ start/ ){ $file-&gt;{ duration } = $1; }