Computing related posts

Minimize windows into application icon

I’ve just discovered this feature in Snow Leopard and it’s changing my life. It allows me to reduce a window into the application icon (in my dock), instead of going to the right of the dock next to the trash bin. I’m really happy of that feature especially because I’m keeping the MAMP window up every time, and I didn’t like to waste space in my dock for that!

To activate this feature, go to System Preferences, then to Dock, and check the box called Minimize windows into application icon. From now on, your windows will reduce and not take more space into the dock.

Screenshot of the dock preferences in Snow Leopard

Like it? Share it!

Rotating your log files with logrotate

If you’re like me and you’re used to log what’s going on your web application, you might want to know how to rotate your log files just like Linux commonly does for its system logs. No need to code anything for that purpose, you can simply use the same software as your system: logrotate.

Attention: I wrote this post based on my personal server which is a Linux Debian 5.0 Lenny and logrotate 3.7.1, but it’s very likely that it works more or less the same way for any Linux server and version of logrotate.

When installing logrotate, it’s likely that it’s already designed to rotate some system logs (syslog, kern.log …), and in the case of a web server, it also rotates Apache or MySQL (access.log, error.log, mysql.log…). Having a quick look at the manual (#man logrotate), you’ll see you can easily tell logrotate to handle your own log files. Simply open /etc/logrotate.conf (or touch a new file in /etc/logrotate.d/ if it exists for your version), and add something like the following:
"/home/mysite/log/visitors.log" "/home/mysite/log/downloads.log" {
rotate 5
mail anon@ymous.com
size 100k
postrotate
/usr/bin/killall -HUP httpd
endscript
nocompress
}

This example section will handle both visitors.log and downloads.log (you can add as many file locations as you wish). The options are:
  • rotate 5: it means it will rotate 5 times before deleting data
  • mail anon@ymous.com: will send an email with the content of the latest log file before deleting it
  • size 100k: maximum size for a log file before rotating it
  • postrotate [...] endscript: enclosed is a piece of script you want to be run after rotating logs
  • nocompress: add this line to prevent logrotate from compressing rotated log files

These are the main options for logrotate, but of course many others can be found, please read the manual for more.

And there you are, your custom log files are being rotated without having to code a custom PHP script :-)

Like it? Share it!

Proposal: best practices for writing PHP

My motivation in writing this proposal comes from years of PHP practice, when I had to use other people’s code and their code looks like garbage so much that I wanted to throw it to the their face rather than using it. I can’t expect everybody to write clear code, but if I could turn the actual world in a world of beauty and smartness, I’d like people to follow the guidelines in this paper. Note that I didn’t pretend to write complete PHP good practices, as I’m just covering the writing aspect of PHP coding, with concerns such as readability and logic in the flow of code.

Good practices for PHP writing

read more… »

Like it? Share it!

strtolower and UTF-8

Charset issues is something that always made me go mad. And since I’m french and I’ve designed many french websites, it’s something I couldn’t escape, thanks to all these special chars we have in our language :)

Well, today an issue came up with the strtolower function. Look at what follows:

$t1 = 'Expérience';
$t2 = strtolower($t1);
echo $t2; // echos 'expience'

See? It drops the two letters “ér”. No matter why and how it processes (for more details about UTF-8/ISO issues, please use google), what matters is that it totally screwed up my beautiful string.

On PHP.net, you can read this function uses the charset defined in the current locale. It means that whatever the encodage of your string is (UTF-8, ISO, …), even if you work with UTF-8 all along (database tables, database connection, page chars …), it will use the current locale charset anyway.

To this, I can see two options. read more… »

Like it? Share it!

Suexec behaviour with nginx

This week, I had to set up and configure an nginx server for the first time. If there is something that I think is essential for a web server, it’s to clearly separate the environment of each of the websites that run on it. Especially, when you execute PHP (or whatever) scripts on your website, security is something you have to pay attention to.

read more… »

Like it? Share it!

Sort an ArrayCollection in Flex

As nothing’s as straight forward as PHP, I had to write my own class to sort ArrayCollections. My work was inspired by what Peter Dehaan did.

The method is to be used as follows:

ArrayUtils.sort(games, "playerScore", "DESC", true);

to sort an ArrayCollection of games by player score, with a descending order. The last parameter indicates that we want a numeric sorting rather than a alphabetical sorting.

I encapsulated the method in an ArrayUtils class, but you’re free to do as you wish. That’s because I think I will add more methods to my class in a near future. You can download the class here: ArrayUtils.as

package classes.utils
{
import mx.collections.ArrayCollection;
import mx.collections.Sort;
import mx.collections.SortField;
 
/**
* Useful functions to manipulate arrays
*
* @author Cyril Mazur cyrilmazur.com
*/
public class ArrayUtils
{
/**
* Sort an arrayCollection by key
* order MUST be either "ASC" or "DESC"
*
* @author Cyril Mazur cyrilmazur.com
* @see http://blog.flexexamples.com/2007/08/05/sorting-an-arraycollection-
using-the-sortfield-and-sort-classes/
*
* @param ArrayCollection arrayCollection
* @param String sortBy
* @param String order
* @param Boolean numericSort
*
* @return ArrayCollection
*/
public static function sort(arrayCollection:ArrayCollection, sortBy:String,
order:String, numericSort:Boolean):ArrayCollection {
/* Create the sort field and fill it */
var dataSortField:SortField = new SortField();
dataSortField.name = sortBy;
dataSortField.caseInsensitive = true;
dataSortField.numeric = numericSort;
 
/* Set the order, by default it's ascending */
if (order.toUpperCase() == "DESC") {
dataSortField.descending = true;
}
 
/* Create the Sort object and add the SortField object
created earlier to the array of fields to sort on. */
var dataSort:Sort = new Sort();
dataSort.fields = [dataSortField];
 
/* Set the ArrayCollection object's sort property to our
custom sort, and refresh the ArrayCollection. */
arrayCollection.sort = dataSort;
arrayCollection.refresh();
 
/* Return the collection */
return arrayCollection;
}
}
}

Download the source here: ArrayUtils.as

I’m open to any suggestion to improve the code :-)

Like it? Share it!

getDefinitionByName(), ReferenceError: Error #1065: Variable is not defined

In Flex (as in Java, or even PHP) you can dynamically get references to a class from its name. Just to remind you, it’s that easy in PHP:

$className = 'User';
$myUser = new $className;

Not a lot more complicated in Java:

Class className = Class.forName("User");
Object myUser = className.newInstance();

The Flex way is as follows:

var className:Class = getDefinitionByName("package.User") as Class;
var user:Object = new className();

But despite PHP and Java, there is a little subtlety for Flex. You can encounter the following error when executing this code:

ReferenceError: Error #1065: Variable User is not defined

And it took some time for me to figure out what the problem was… After looking at the right speling of my class name and so worth, I figured out that the problem comes from the way that Flex compiles its code. Actually, Flex compiles its code so that if a class is not used, it will keep this class off the final compiled program. And even a

import package.User

won’t change a thing. The only way to get it done, is to put a reference to that class somewhere in your code, wherever … as long as it’s referenced somewhere, Flex will not forget the class at the compilation process. A simple and ugly way is to add a dummy reference to this class somewhere, like:

private var _dummyUser:User;

or shorter:

User;

and you’re done.

It has been already discussed on forums and blogs, but as I came across this I had to write an article about it :)

Like it? Share it!

Unique ID for Java classes

As I was writing some Java code for my finale dissertation, I wanted to give my objects an unique ID in order to distinguish them easily. I thus wrote the following abstract class that does the job well for me:


package motd.utils;
 
/**
* Makes a class identifiable with an unique ID for its objects
*
* @author Cyril Mazur cyrilmazur.com
*/
abstract public class Identifiable {
 
  /**
  * The object's ID
  */
  protected int id;
 
  /**
  * Constructor
  */
  public Identifiable() {
    this.id = getNextId();
  }
 
  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  // GETTERS AND SETTERS
 
  public int getId() {
    return id;
  }
 
  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  // STATIC
 
  /**
  * Index for the object's ID
  */
  protected static int index = 0;
 
  /**
  * Return next ID
  * @return int
  */
  protected static int getNextId() {
    return ++index;
  }
}

Download Identifiable.java here

Make your class to extend Identifiable, don’t forget to call the super() constructor, and you’re done.

Now, all of your objects will have an ID attribute which will identify them in a unique way. Of course this attribute is private, and I’ve added a getter for it, since it’s a good OOP practice.

Note 1: with my class, the counter is global. It means that when the counter increments, it increments for all the classes that extend Identifiable in your program. Two objects of different classes will never have the same ID. I don’t know if it’s wrong or if it’s right in the end… If someone has an idea on how to make the counter dedicated to each class, feel free to leave a comment :)

Note 2: I’m far from being a god of Java, and it’s likely that my tip is just for beginners who don’t use frameworks that would already encapsulate this feature

Like it? Share it!

onChange and checkbox/radio in IE

I was writing a bit of Javascript this afternoon. Everything was fine until it came to test it on IE, and there the nightmare began (again, and again, and again…). Yes, IE (all versions) sucks at handling onChange events for checkboxes and radio buttons. Sometimes it works, sometimes not at all, sometimes you gotta click somewhere else in the page to trigger it…

I’ve made some researches on the Internet, and there is no beautiful way to fix that. The only way is to totally give up onChange events for those two elements (yes, you HAVE to give up a very regular feature just because ONE browser doesn’t support it properly), and to replace it by an onClick event. You’ll then hope that your code doesn’t change the values of your checkboxes in another way than by clicking on it, otherwise you’re fucked. Or you have to call the related onClick function every time.

Let’s see for example the case when you use an HTML label element for your input:

<input type="checkbox" onclick="someFunction();" id="myCheckbox" />
 
<label
  for="myCheckbox"
  onclick="document.getElementById(this.for).onclick();"
>
    Check me
</label>

I just had to change that in my case hopefully.

Like it? Share it!

Only 2 USB ports on my MacbookPro

I’m very happy with my brand new MacBook Pro 2010 15″ and we’re living a beautiful love story together :) But WHY THE HELL ARE THERE ONLY 2 USB PORTS? What do you want to do with that?.. Imagine you use a USB mouse of any other device like a 3G dongle, you already find yourself with only one port effectively available… And if never you dare to plug a USB stick that is too fat, it will be even impossible to use the second one.

Notice that it’s possible to have a third USB port on your macbook pro, but it will cost you to get the 17″ model instead (that costs not lest than 500 euros more than the entry 15″)

Yes if there is something I’d have to have against my Macbook Pro, it’s definitely the low number of available USB ports, it’s a pain sometimes…

Like it? Share it!