Self executing functions in javascript

When I first saw this programming pattern I found very difficult to understand why would people want to make their code so confusing. When I finally understood why this is used I knew I had to write about it someday. Today is that day.

Self executing functions are useful because using them you avoid creating global variables that could interfere with other parts of your code.

To understand this better I will give three examples of how you pollute and avoid the pollution of the global space.

Read More

Dealing with Apache's limit on back-references when rewriting URLs

For the ones of you who didn’t know (I didn’t know either), Apache has a limit on the number of back references you can use when rewriting a URL.

For the people who don’t know I will explain what is a back reference when talking about apache rewrites.

When you do a rewrite of a URL using apache mod_rewrite you translate a URL into an actual resource that apache can find.

For my examples I will use the domain http://ncona.com. If I wanted that the URL http://ncona.com/file loaded the file other_file.html located in my web root I would use a rule like this one:

1
RewriteRule ^file$ /other_file.php

That is a static rewrite but you can also use back reference to make dynamic URLs:

1
RewriteRule ^product/([0-9]+)$ /product_details.php?id=$1
Read More

Transforming XHTML documents with XSLT 1.0 and PHP

XSLT (Extensible Stylesheet Language Transformations) is a language used to transform XML documents. It is mostly used to transform an XML document into a different XML document with a different XML Schema.

XHTML (eXtensible HyperText Markup Language) is an XML version of the HTML markup language. It was created so websites could be parsed using standard XML processors instead of having to depend on specific HTML processors.

The problem

So far everything sounds good, but there are some problems. Following the standard you should be able (as a matter of fact you must) to send an XHTML document to the browser with a mime type of “application/xhtml+xml”. The problem here is that some browsers (IE) don’t understand that mime type and thus don’t render your document.

Read More

Run PHP unit tests automatically using git hooks

Git hooks

From git documentation:

Hooks are little scripts you can place in $GIT_DIR/hooks directory to trigger action at certain points. When git-init is run, a handful example hooks are copied in the hooks directory of the new repository, but by default they are all disabled. To enable a hook, make it executable with chmod +x.

If you take a look at your .git/hooks folder you will probably see a bunch of hooks created by default. In my current version of git all my hooks have a .sample extension. To make a hook executable you must remove .sample and make it executable. If you don’t want to remove the default example you can create a new file without .sample.

Read More

Getting more from git log

This post is basically a copy of pimping out git log. I just made some modifications to Bart’s alias to fit my personal preferences.

This code creates an alias named lg for the git:

1
git config --global alias.lg "log --graph --pretty=format:'%Cred%h -%C(yellow)%d%Creset %s %Cgreen(%ci) %C(bold blue)<%an>'"

Here is an explanation of what that command does:

1
git config --global alias.lg "..."
Read More

How to delete a commit in git, local and remote

It has happened to me more than once that I make a commit without verifying the changes I am committing. Time after that I review the commit and I notice that there is something in the commit that doesn’t belong there.

In those times what I want to do is make a patch with the changes of the commit, delete the commit, apply the patch and then redo the commit only with the changes I intended. In this post I will only explain how to delete a commit in your local repository and in a remote repository in case you have already pushed the commit.

Read More

Publish an extension to firefox directory

Once you have created an extension, you will probably want to make it easily available to the public.

For my tntfixer extension that I explained in some previous posts I had a folder structure similar to this one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/tntfixer
|----chrome.manifest
|----install.rdf
|----README
|----/chrome
|    |----/content
|    |    |----tntfixer.js
|    |    |----tntfixer.xul
|    |
|    |----/skin
|    |    |----overlay.css
|    |    |----/images
|    |    |    |----status-bar-icon-active.png
|    |    |    |----status-bar-icon-inactive.png
Read More

Using UTF-8 characters on an e-mail subject

If you have sent e-mails in a different language than English or using characters outside the ASCII range you have probably already used utf8 to send them.

Specifying the use of UTF-8 in the body of an e-mail is very similar to doing it for a HTTP response. You can specify the content-type in an e-mail header like this:

1
Content-Type: text/plain; charset=utf-8

But there is catch. The subject line of an e-mail is a header by itself, and headers must contain only ASCII characters. Happily, there is a work around. RFC 1342 is a recommendation that provides a way to represent non ASCII characters inside e-mail headers in a way that won’t confuse e-mail servers.

Read More

apache2: Could not reliably determine your servers’s fully qualified domain name, using 127.0.0.1 for ServerName

This is a common problem I find when I do a new Apache 2 install on Ubuntu. There is a simple solution to stop seeing this message. Edit httpd.conf, type this command in a terminal:

1
sudo gedit /etc/apache2/httpd.conf

Add this line at the end of the file

1
ServerName myserver

You can replace myserver with whatever name you want to use for your server.

If that doesn’t completely fix the problem try adding the same line:

1
ServerName myserver

At the end of /etc/apache2/apache2.conf

That should do it.

Read More

Creating local virtual hosts with apache

If you are a web developer, virtual hosts is probably something you already use, if not, you should.

Virtual hosts allow you to have multiple domains configured in your computer so you can have different web sites in different locations on your hard drive and keep your environment better organized when developing. Virtual servers are also used by shared web hosting companies to host multiple web sites in the same machine, the difference is that they deal with a bunch more stuff that I am not going to deal with in here. This guide should only be used to create virtual servers for local development.

I am currently using Ubuntu 11.04 (Natty Narwhal) but I am pretty sure the instructions are the same for other versions of Ubuntu. Other Linux distributions not based on Debian have a slightly different folder structure, but you should be able to adapt this guide easily.

Read More