De vuelta de las Jornadas Symfony

Esta semana he estado en las jornadas de symfony en la Universitat Jaume I de Castellón, que tuvieron la amabilidad de invitarme como ponente.Tenéis una crónica muy completa de Javier Eguiluz en symfony.es. Fue un éxito de asistencia, participación y organización. David Castelló merece mención especial por lo mucho que se lo curró. Dejo aquí las transparencias de mi presentación sobre Doctrine y más abajo el vídeo.

Doctrine – Nacho Martín – Jornadas Symfony en Castellón. Julio 2010

ORM Doctrine – decharlas 2010 from decharlas.com on Vimeo.

Automating the creation of new symfony 1.4 projects (doctrine + git)

Sometimes I want to create new symfony projects to try something, or, well, sometimes even to do some actual work.

Then, I go to the Jobeet tutorial and follow the first steps. After that, since I use git, I check out this post to setup symfony as a submodule.

I have written a simple bash script with that post and a few more lines, to automatize the process. It may be useful for others, and for me to have it accesible, so I will post it here:

setupsymfony.sh:


#!/bin/bash

SYMFREPO="git://symfony-git.git.sourceforge.net/gitroot/symfony-git/symfony-git"
DBUSER="root"
DBPASS=""
VENDOR_DIR="lib/vendor/symfony"

if [ -z "$DBPASS" ]; then
 echo edit setupsymfony.sh to add your mysql password on line 4: DBPASS=\"password\"
 exit
fi

if [ -z "$1" ]; then
 echo usage: $0 project name
 exit
fi

mkdir $1
cd $1

git init
git clone $SYMFREPO $VENDOR_DIR
git submodule add $SYMFREPO $VENDOR_DIR/

git submodule foreach 'git submodule init && git submodule update'

git commit -m 'Initial commit; initialized symfony-git (1.4)'

$VENDOR_DIR/data/bin/symfony generate:project $1
echo "config/databases.yml" > .gitignore
echo "cache/*" >> .gitignore
echo "log/*" >> .gitignore

./symfony project:permissions
git add .
git commit -m 'Initialized symfony project'

./symfony generate:app frontend
git add .
git commit -m 'Initialized frontend application'

mysqladmin -u$DBUSER -p$DBPASS create $1
./symfony configure:database "mysql:dbname=$1;hostname=localhost" $DBUSER $DBPASS

usage:  ./setupsymfony.sh name_of_the_new_project

remember to write your database password in the line 4 of the script.

#Edit: updated to include the changes proposed by kbsali. Thanks!

#Edit2: I have posted it in a gist.

List & Check, an open source symfony project

I have just released a pet project I have been developing lately. It is called List & check, and it is about making (beautiful) lists easily.

What it is about

I use lists for almost everything: things I want t o try, things I want to do this week, books to read, movies to watch, places to visit, steps to do before a deployment… and since I saw the beautiful symfony-check website, I just wanted to be able to create lists with that beautiful look and great user experience.

I think that other list fans may enjoy it.

Things I wanted to try

I wanted to use this pet project as playground to try some things:

  • I used the Doctrine ORM, because I was using before Propel. I think that the change is worthwile only looking at the amount of documentation available for each ORM. I used the csDoctrineActAsSortablePlugin to make items sortable and it worked smoothly.
  • Redmine as a replacement of trac and pivotal tracker. I hate the installation of trac, Redmine gives me just what I wanted with a good user interface. It integrates well with git as well.
  • Git for Symfony. I am big fan of git, but I have had problems in the past working with Symfony and the huge amount of svn:externals you have to deal with. I have tried several scripts for doing the job, but none of them worked properly, and I had to tweak them… But, thanks to vjousse, that has placed a Symfony git repository in github, this time was very easy.
  • Ajaxized edit view. I wanted to have only a view both for editing the lists and for showing them better than having separate views for these actions. I think that for this project it makes sense to have everything in one place and the user experience is better, as in projects like Pivotal Tracker.
  • Focus in user experience. Related to the previous point, it is important that you can create lists, create new items, sorting and deleting them quickly, just writing the title and pressing enter over and over. At the same time, I wanted to make possible to detail the items. It may be stupid for a list of places to visit or a to-do list for this evening, but it makes sense for lists like steps to set up a workstation. This is a feature I miss in Tadalist. So now I can create lists quicky and as detailed as I want, given the markdown editor. I used also jquery-ui that made my life easier.
  • Markdown editor. Markdown is just great for creating content, and was very useful as format for the items’ description.
  • Selenium. In my roadmap I want to create selenium tests for the whole site. I have not done it yet, because I have been tuning the user interface until the last moment, but looks like the perefect project to test with Selenium.

It is Open source

The full code is hosted at github. The idea is more to give ideas to other people for doing their websites, but if you want you can set up a list&check server. I have learnt a lot from the source code of symfonians and other OS projects, and I wanted to give something back to the community.

My experience with Diem CMS

Diem CMS logoLately, I wanted to do a website for a non profit association in which I am involved. Since I really like symfony, I decided to do it using one of the CMS that extend symfony. Looks like there are three main choices: Apostrophe, Diem and Sympal.

I tried first Apostrophe, because I liked a lot the demo video they have in their page. The concept is awesome and it is really worth a shot. Unfortunatelly, we wanted to have a blog, and the blog plugin for apostrophe is not ready yet. I had to abandon it, although I want to use it back in the future.

Then, I asked other symfonians on Twitter (@davidcastello, @loalf & @ivandebenito are always very helpful :), and, after seeing the demo on the Diem site, I installed it and followed the tutorial A Week of Diem Ipsum.

The tutorial is just great. Well explained and illustrative, and the CMS itself suited my needs, although I was disappointed when I saw that the last lesson ends with “On the next chapter we will add comments to the blog.”, and… there is not next lesson. Anyways, I asked on IRC and they pointed me to dmCommentPlugin, which is the way to do it. #EDIT: That chapter of the tutorial is now available! :)

After following the tutorial you can notice that Diem gives what they promise:

  • You don’t need to develop all those boring thinks common to lots of pages. They are already implemented, as you can expect from a CMS, but moreover everything works smoothly.
  • The wysiwyg editor of layout of pages is great. It makes the task of designing the layout almost pleasing (I hate dealing with CSS).
  • Extending it is quite easy, if you know symfony.

I created two new widgets for the landing page, a sort of squares with rounding boxes, a background image and some text inside… not very complicated, but it was easy, although I had to dig a bit in the code of already implemented widgets to solve some issues. I suppose that the documentation will grow and this won’t be necessary in the future, but anyways it is not a big pain. It was quite easy. After all, I was new in Diem.

I developed a system to synchronize pages and the blog with pages in Mediawiki (so that you can edit the wiki and the content is shown on the Diem website) too, and this apparently not so easy task was just smooth because, hey, you have symfony there and you can write tasks using doctrine records and so on.

So, given that I was a total newbie at Diem and I could do everything I wanted without much pain (less than expected for a new platform), and that the base (layout, editor, admin panel, search engine, etc.) worked just fine, I am very glad.

PS: Given that I found that Diem suited my needs, I didn’t try Sympal, so I cannot say anything about it. It was a matter of luck and perhaps eye candy to try Diem before Sympal, I don’t have rational arguments for this choice. I will install it in the future and play a bit with it.

Changing colors of ls

I have been quite a long time living with files with 777 permission being displayed in blue over green background whenever i do ls, which makes text quite illegible. Sometimes you don’t see a problem even when it is in front of you. Well, I took the time to fix it today. Here is the solution:

dircolors --print-database > ~/.dircolors

It will create a .dircolors file with the color specification
vim .dircolors. Now we edit this file and change this line:

OTHER_WRITABLE 34;42 # dir that is other-writable (o+w) and not sticky

for this one:

OTHER_WRITABLE 01;37;42 # dir that is other-writable (o+w) and not sticky

That will tell ls to display these files with white bold letters over green background, mucho more legible.

In case you prefer different colors, here is what these codes mean:

# Attribute codes:
# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
# Text color codes:
# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
# Background color codes:
# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white

Last step is to edit ~/.bashrc to make dircolors load these colors each time a new term is opened. Open .bashrc and add these lines:

if [ -x /usr/bin/dircolors ]; then
 d=.dircolors
 test -r $d && eval "$(dircolors $d)" || eval "`dircolors -b`"
fi

Open a new terminal to see the changes o just run

source .bashrc

If you want to see the changes applied in the current terminal.

A workflow with remote branches on git

This is the workflow that we are currently using for work with remote branches.

Pushing local branch to remote

git push origin my-branch:refs/heads/my-branch
git config branch.my-branch.remote origin
git config branch.my-branch.merge refs/heads/my-branch

That will create a new branch in the remote repository and the appropriate ref.

Pulling from remote to local branch

Before checking out, we need to fetch the branch, because our repo is not aware of it. Run this to understand what I mean.:

git remote show origin

So… let’s fetch and then we’ll be able to do the checkout.

git fetch
git checkout -b my-branch origin/my-branch

You can add a –track to the last command to set up upstream configuration (see the docs)

Grabar lo que está sonando (con pulseaudio)

A veces interesa grabar en un fichero lo que está sonoando. Por ejemplo, yo me grabo una sesión de spotify para oirla cuando voy a correr. Aquí está el cómo hacerlo en Linux con pulseaudio: Encontré este script que explicaba cómo hacerlo, pero no me iba por tener el sistema en español. Un par de cambios tontos en el script y queda:


#!/bin/bash
WAV="$1"
if [ -z "$WAV" ]; then
 echo "Uso: $0 OUTPUT.WAV" >&2
 exit 1
fi
rm -f "$WAV"

# Get sink monitor:
MONITOR=$(pactl list | grep -A1 '^\*\*\* Origen #' | \
 grep '^Nombre: .*\.monitor$' | cut -d" " -f2 | tail -n1)

# Record it raw, and convert to a wav
echo "Grabando a $WAV ..."
echo "Cierra esta ventana para terminar"
parec -d "$MONITOR" | sox -t raw -r 44k -sLb 16 -c 2 - "$WAV"

Lo graba en un wav que luego puede uno pasar a mp3 o a lo que quiera.

Forcing a javascript file to be loaded at the end in symfony

Very often, we  want javascript files to be loaded after another. For instance, you may your files containing jquery functions to be loaded after the main jquery.js file to avoid the typical error:

$ is not defined

use_javascript (and addJavascript) accepts a ‘position’ parameter to deal with this. Position can be ‘first’ or ‘last’. For instance:

<?php use_javascript('file-you-want-to-load-at-the-end.js', 'last')?>

Mechanize force encoding error

Ruby’s mechanize gem is very useful, specially when combined with Nokogiri. But if you find an encoding error like:

/var/lib/gems/1.8/gems/mechanize-0.9.3/lib/www/mechanize/page.rb:37:in `initialize': undefined method `force_encoding' for <String:0xb7410070> (NoMethodError)

Then you will need to apply this patch ;)

http://github.com/eric/mechanize/commit/7fd877c60cbb3855652c390c980df1dedfaed820

Gzipping js and css files

This week we are optimizing Makoondi to make it load faster. I am using the page speed plugin by Google. One of the first advices it gives is Enable gzip compression, Compressing the following resources with gzip could reduce their transfer size by about two thirds.

I got it working pretty eadily for the .html files, just by adding this to http.conf:


AddOutputFilterByType DEFLATE text/css text/html text/javascript application/x-javascript application/javascript

But it was not working for .js and .css files, no wonder how many filetypes I could add to the list. It took me a while to find the solution here by Sören Kuklau, and I am so happy that I will replicate it here :) I added this chunk of code to my http.conf:


<Directory /var/www/makoondi/web/js/>
    AddHandler application/x-httpd-php .js php_value auto_prepend_file gzip-js.php php_flag zlib.output_compression On
</Directory>
<Directory /var/www/makoondi/web/css/>
    AddHandler application/x-httpd-php .css php_value auto_prepend_file gzip-css.php php_flag <lib.output_compression On
</Directory>

Then in the …/web/css/ directory I put this simple php file called gzip-css.php:

<?php
       header("Content-type: text/css; charset: UTF-8");
?>

And in the …/web/css/ directory gzip-js.php:

<?php
        header("Content-type: text/javascript; charset: UTF-8");
?>
Compressing the following resources with gzip could reduce their transfer size by about two thirds