Wednesday, December 8

Downgrading PHP in Maverick Meerkat from 5.3 to 5.2

While my source code is broken and it's getting fixed I write a few lines about how to downgrade PHP in the latest ubuntu release.

The process is rather simple, and there are many ways to accomplish this, I'll post the one I use every time I break the OS

1. create a file karmic.list on /etc/apt/sources.list.d
$ kdesudo kate /etc/apt/sources.list.d/karmic.list
and put the following content

# deb cdrom:[Ubuntu 10.04 LTS _Lucid Lynx_ - Release amd64 (20100429)]/ karmic main restricted
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.

deb http://us.archive.ubuntu.com/ubuntu/ karmic main restricted
deb-src http://us.archive.ubuntu.com/ubuntu/ karmic main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://us.archive.ubuntu.com/ubuntu/ karmic-updates main restricted
deb-src http://us.archive.ubuntu.com/ubuntu/ karmic-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://us.archive.ubuntu.com/ubuntu/ karmic universe
deb-src http://us.archive.ubuntu.com/ubuntu/ karmic universe
deb http://us.archive.ubuntu.com/ubuntu/ karmic-updates universe
deb-src http://us.archive.ubuntu.com/ubuntu/ karmic-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://us.archive.ubuntu.com/ubuntu/ karmic multiverse
deb-src http://us.archive.ubuntu.com/ubuntu/ karmic multiverse
deb http://us.archive.ubuntu.com/ubuntu/ karmic-updates multiverse
deb-src http://us.archive.ubuntu.com/ubuntu/ karmic-updates multiverse

## Uncomment the following two lines to add software from the 'backports'
## repository.
## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
# deb http://us.archive.ubuntu.com/ubuntu/ karmic-backports main restricted universe multiverse
# deb-src http://us.archive.ubuntu.com/ubuntu/ karmic-backports main restricted universe multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu karmic partner
# deb-src http://archive.canonical.com/ubuntu karmic partner

deb http://security.ubuntu.com/ubuntu karmic-security main restricted
deb-src http://security.ubuntu.com/ubuntu karmic-security main restricted
deb http://security.ubuntu.com/ubuntu karmic-security universe
deb-src http://security.ubuntu.com/ubuntu karmic-security universe
deb http://security.ubuntu.com/ubuntu karmic-security multiverse
deb-src http://security.ubuntu.com/ubuntu karmic-security multiverse


This just adds the repositories from Karmic Koala to our Maverick installation

2. Create a file php on /etc/apt/preferences.d/
$ kdesudo kate /etc/apt/preferences.d/php
And add this content
Package: php5
Pin: release a=karmic
Pin-Priority: 991

Package: php5-gd
Pin: release a=karmic
Pin-Priority: 991

Package: php5-common
Pin: release a=karmic
Pin-Priority: 991

Package: php5-curl
Pin: release a=karmic
Pin-Priority: 991

Package: php5-mysql
Pin: release a=karmic
Pin-Priority: 991


Package: php5-pear
Pin: release a=karmic
Pin-Priority: 991

Package: php5-xsl
Pin: release a=karmic
Pin-Priority: 991


Package: php-pear
Pin: release a=karmic
Pin-Priority: 991

Package: php5-cli
Pin: release a=karmic
Pin-Priority: 991

Package: php5-memcache
Pin: release a=karmic
Pin-Priority: 991

Package: php5-pgsql
Pin: release a=karmic
Pin-Priority: 991

Package: php-apc
Pin: release a=karmic
Pin-Priority: 991

Package: php5-xmlrpc
Pin: release a=karmic
Pin-Priority: 991

Package: libapache2-mod-php5
Pin: release a=karmic
Pin-Priority: 991

Package: php5-mcrypt
Pin: release a=karmic
Pin-Priority: 991

Package: php5-imagick
Pin: release a=karmic
Pin-Priority: 991

Package: php5-imap
Pin: release a=karmic
Pin-Priority: 991

Package: libmagickcore2
Pin: release a=karmic
Pin-Priority: 991

Package: libgraphviz4
Pin: release a=karmic
Pin-Priority: 991

Package: imagemagick
Pin: release a=karmic
Pin-Priority: 991

Package: libmagickwand2
Pin: release a=karmic
Pin-Priority: 991

Package: php5-xdebug
Pin: release a=karmic
Pin-Priority: 991

Package: php5-dev
Pin: release a=karmic
Pin-Priority: 991


The last 3 entries are intended to prevent imagemagick from updating to a version that cannot be used with php5-imagick, if you don't need this module just remove them to avoid downgrading imagemagick without need

3. Closing to the end, update your packages
$ sudo apt-get update

4. Now just install the php modules, the ones installed will be downgraded to v5.2 and any new one installed will be on v5.2
$ sudo apt-get install php5 php5-dev php5-curl php5-gd php5-xdebug php-pear php5-cli php5-common libapache2-mod-php5 php5-mysql php5-mcrypt php5-imagick php5-imap libmagickcore2 libgraphviz4

Tuesday, August 24

Using Yammer with Empathy

Yammer is a micro blogging site for companies, it's amazing how information and constant updates can save money and time so this is a great solution to keep all your teams informed at all times.

Unfortunately Yammer's air client has some issues with some linux installations, so here's the solution, use the OS chat client with yammer

First, you'll need a jabber account, you can create one at their site or you can create one from your IM client, Empathy allows this but not all clients can do it.

Now you need to make the account work with Empathy (or pidgin or kopete or whatever you like)

For empathy press F4 then click on "Add ..." below the account list, you'll get this screen
Just type your jabber account (remember the @jabber.org) and your password and you are ready to go

Now it's time to make it work with Yammer, go to Yammer page and log in, then go to notifications section inside your profile and click on "Subscribe to feeds in real time (by email, SMS, or IM)"
On IM column click "enable" then type your jabber account
After that a message will arrive to your IM client, just type the code and you are ready to go, all messages sent to yammer through your IM will be published and all messages in your network will arrive as instant messages

Friday, June 18

Netbeans IDE 6.9

Lots of times fellow developers told me the great features of eclipse or dreamweaver, I temporary switch IDE but in the end the features in NetBeans always take me back to it.

Yesterday I was pleasantly surprised when I found out that the new release of NetBeans has been launched, for PHP developers the main changes are:
  • Better code formatting rules (really helpful when not all your team follows the coding standards)
  • Zend Framework support
  • Some subversion updates
  • Better response times
  • etc etc
This has become in a "must have" tool for me, give it a try http://netbeans.org/

Wednesday, May 12

Downgrading php in lucid

Currently I work with CodeIgniter 1.7.1 and one of the biggest issues with this release is the usage of deprecated functions in PHP 5.3, so it's imperative to downgrade the PHP version.
There are several methods, the most straightforward one is described in trk's weblog

The idea is to duplicate the repositories from lucid with the ones from karmic
$ sed s/lucid/karmic/g /etc/apt/sources.list | sudo tee /etc/apt/sources.list.d/karmic.list

Now we need to create a preference file where we'll add every file we need from PHP and set them to be installed from the karmic repository

$ gksudo gedit /etc/apt/preferences.d/php
Package: php5
Pin: release a=karmic
Pin-Priority: 991

Package: php5-dev
Pin: release a=karmic
Pin-Priority: 991

Package: php5-curl
Pin: release a=karmic
Pin-Priority: 991

Package: php5-gd
Pin: release a=karmic
Pin-Priority: 991

Package: php5-xdebug
Pin: release a=karmic
Pin-Priority: 991

Package: php-pear
Pin: release a=karmic
Pin-Priority: 991

Package: php5-cli
Pin: release a=karmic
Pin-Priority: 991

Package: php5-common
Pin: release a=karmic
Pin-Priority: 991

Package: libapache2-mod-php5
Pin: release a=karmic
Pin-Priority: 991

Package: php5-mysql
Pin: release a=karmic
Pin-Priority: 991

Now all we need is to "reinstall" the packages

$ sudo apt-get update && sudo apt-get install php5 php5-dev php5-curl php5-gd php5-xdebug php5-cli php-pear php5-mysql

And that's it, we have php 5.2 in lucid

Tuesday, May 11

Why we should use phpdoc with netbeans

It's said that a good developer learns something about his IDE every day, today I start coding again after a long pause doing management and a feature of netbeans greatly surprised me.

When using object oriented programming in PHP is easy to loose track of the type of object we're handling, plus the code completion of IDEs is very poor because of the polyphormic features in PHP.

Here enters a good documentation, I always ask every developer I work with to write phpdoc in any function they made, now another feature of netbeans make it more important, the pseudo class completion makes the IDE know what type of object is returning a function, allowing a fully working autocomplete even in funciton chains.

Good documentation plus good IDE equals great productivity.

Thursday, April 29

Lucid is here

With tons of bugfixes and updates the new release of ubuntu is finally here.

While I play with it I'm upgrading my project's server to the latest LTS release.

$ nohup nice do-release-upgrade -d &

Executing shell scripts from NetBeans

One of the most interesting things in Komodo was the option to add some scripts and execute them in the project without leaving the IDE, usually this looks trivial but it's a great way of performing some batch processes and speed up your development.

Now we'll review how to do the same thing in NetBeans

1. At the project list, right click the project you want to add the script, select preferences
2. At the open preferences go to "Run Configuration"
3. Create a new configuration, give any name you want
4. At the combo-box select the last option "Script (run in command line)"
5. Uncheck the "Use default PHP Interpreter"
6. At "PHP Interpreter" box type the interpreter of your script, for example for a bash script /bin/bash
7. For the index file select the script you want to run
8. Optionally add the arguments needed for your script
9. Close the window
10. Now at the menubar open the Run submenu
11. Set the Project Configuration to run your newly created configuration

12. Just press F6 (default) or click the run button to run your script

Monday, April 5

Executing mysql instructions from the shell

I bet many already know this, but whatever ...

the -e option for the mysql command allows us to execute an instruction without entering into the mysql itself, for example.

$ mysql -uroot -ppassword -e "SELECT * FROM `Database`.`Table`"

this gives us an excellent way of printing results into a file, for example:

$ mysql -uroot -ppassword -e "USE Database; SELECT id, name FROM Table WHERE arbirtary_id IS NULL;" > nullRecords.txt

and with a simple parser we can use this data to get rid of inconsistent information or obsolete data.

Monday, February 22

Using PhpUnit with NetBeans

With the boom of TDD is amazing how slowly the IDEs are integrating unit testing to their features, for PHP I've only found a few, and the best result (for me at least) was using NetBeans.

Here we'll cover how to install PHPunit and integrate it into NetBeans

First, as always install some application we'll need in the future
$ sudo apt-get install php5-dev php-pear

If you installed xdebug from ubuntu repositories you'll need to remove it
$ sudo apt-get remove php5-xdebug

Now discover phpunit servers
$ sudo pear channel-discover pear.phpunit.de
$ sudo pear channel-discover pear.symfony-project.com

And install it
$ sudo pear install --alldeps phpunit/PHPUnit

I strongly recommend installing xdebug
$ sudo pecl install xdebug


Now that you have phpUnit installed just need to add some generic tests to your project, what I've used is

1. Create a tests directory on the project root
$ mkdir tests

2. Create a php file that will recursively load all the files in the directory and run a class of phpUnit to run all the classes loaded, some proposed content is displayed here.
$ kate allTests.php
//add the phpunit framework
require_once 'PHPUnit/Framework.php';

//including all the files available in the current directory
$filesIncluded = array();
includeRecurse('tests', $filesIncluded);

//generic class
class phpucAllTests
{
public static function suite()
{
$suite = new PHPUnit_Framework_TestSuite('Package');
foreach ($GLOBALS['filesIncluded'] as $fileincluded)
{
$filename = explode('.', $fileincluded);
$suite->addTestSuite($filename[0]);
}
return $suite;
}
}

3. Any file inside the tests directory should be something like:
class InstallJobTest extends PHPUnit_Framework_TestCase
{
public function testAapplyFilters()
{
//test the null query
$query = null;
InstallJob::applyFilters($query);
$sql = $query->getSQL();
$this->assertRegExp('/^SELECT (.*) FROM InstallJob/', $sql);

//Testing club number filter
$this->clubNumberFilterTests();

//Testing installation companies
$this->installationCompanyFilterTests();
}
}

Now that all the required files are created, it's time to test the phpUnit

Open a console and into your project directory run
$ phpunit tests/allTests.php

If all is working is time to use NetBeans

Select the project in the "Projects" panel and right click it, select "test" from the context menu.


A dialog will open prompting for a unit test folder, select the tests directory you created

Now below "Source Files" a new item called "Test Files" appear with the contents of the tests directory

To run the tests right click allTests.php file and select "run" from the context menu


NetBeans will display some useful information such as the code coverage, executed test cases, asserts, etc.

Thursday, February 18

Installing PHPunderControl in ubuntu

First of all we need to install the applications and extensions we need
$ sudo apt-get install gitosis php5-dev
$ sudo pear channel-discover pear.phpunit.de
$ sudo pear channel-discover pear.symfony-project.com
$ sudo pear install --alldeps phpunit/PHPUnit

Now we have to download CruiseControl, pick any method you want, here a wget example
$ wget http://downloads.sourceforge.net/project/cruisecontrol/CruiseControl/2.8.3/cruisecontrol-bin-2.8.3.zip?use_mirror=ufpr

We must unzip the downloaded file, I'm decompressing it directly where it will reside
$ sudo unzip cruisecontrol.zip /opt

Now we must create a group and user for the system
$ sudo groupadd cimasters
$ sudo useradd -G cimasters cimaster

Change the owner of CruiseControl files
$ sudo chown cimaster:cimasters /opt/cruisecontrol* -R

Create a script to launch it directly as a service
$ sudo kate /etc/init.d/cruisecontrol

with the contents
#!/bin/sh
CC_USER=cimaster
CC_INSTALL_DIR=/opt/cruisecontrol
CC_WORK_DIR=$CC_INSTALL_DIR
CC_LOGFILE_DIR=$CC_INSTALL_DIR
export JAVA_HOME=/opt/jdk1.5.0_11
PATH_ADDITIONS=
CC_WEBPORT=8484
CC_JMXPORT=8888
CC_RMIPORT=8585
NAME=cruisecontrol
DESC="CruiseControl - continuous integration build loop"
PATH=/usr/lib/jdk/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/bin:/bin
if [ -n "$PATH_ADDITIONS" ]; then
PATH=$PATH_ADDITIONS:$PATH
fi
export PATH
CC_DAEMON=$CC_INSTALL_DIR/cruisecontrol.sh
CC_CONFIG_FILE=$CC_INSTALL_DIR/config.xml
CC_LOG_FILE=$CC_LOGFILE_DIR/cruisecontrol.log
CC_COMMAND="cd $CC_WORK_DIR; $CC_DAEMON -configfile $CC_CONFIG_FILE -webport $CC_WEBPORT -jmxport $CC_JMXPORT"
if [ -f /etc/default/cruisecontrol ]; then
. /etc/default/cruisecontrol
fi
test -f $CC_DAEMON || (echo "The executable $CC_DAEMON does not exist!" && exit 0)
if [ `id -u` -ne 0 ]; then
echo "Not starting/stopping $DESC, you are not root."
exit 4
fi
PARPID=`ps -ea -o "pid ppid args" | grep -v grep | grep "${CC_DAEMON}" | sed -e 's/^ *//' -e 's/ .*//'`
if [ "${PARPID}" != "" ]
then
PID=`ps -ea -o "pid ppid args" | grep -v grep | grep java | grep "${PARPID}" | \
sed -e 's/^ *//' -e 's/ .*//'`
fi
case "$1" in
'start')
env >> cc.startup.env
su $CC_USER -c "/bin/sh -c \"$CC_COMMAND >> $CC_LOG_FILE 2>&1\"" & RETVAL=$?
echo "$NAME started with jmx on port ${CC_JMXPORT}, web on ${CC_WEBPORT}, rmi on ${CC_RMIPORT}"
;;
'stop')
if [ "${PID}" != "" ]; then
kill -9 ${PID} ${PARPID}
$0 status
RETVAL=$?
else
echo "$NAME is not running"
RETVAL=1
fi
;;
'status')
# echo PARPIDs $PARPID
# echo PIDs $PID
kill -0 $PID >/dev/null 2>&1
if [ "$?" = "0" ]; then
echo $NAME \(pids $PARPID $PID\) is running
RETVAL=0
else
echo "$NAME is stopped"
RETVAL=1
fi
;;
'restart')
$0 stop && $0 start
RETVAL=$?
;;
*)
echo "Usage: $0 { start | stop | status | restart }"
exit 1
;;
esac
#echo ending $0 $$....
exit 0

Give it permissions to run
$ sudo chmod +x /etc/init.d/cruisecontrol

For some reason, there are some hardcoded scripts and some failures in setting the java_home, so to take a sortcut, we'll just create symlinks to the correct places
$ sudo ln -s /usr/lib/jdk/ /opt/jdk1.5.0_11
$ sudo ln -s /usr/lib/jdk/bin/java /bin/java

With this CruiseControl should be ready to run
$ sudo service cruisecontrol start

The message tells us which ports are being used, to change them modify the script in /etc/init.d/

Now PHPunderControl
We create a temp directory
$ cd ~ && mkdir phpundercontrol && cd phpundercontrol

We'll download from the git repository
$ git clone git://github.com/manuelpichler/phpUnderControl.git

Now we only need to install it
$ cd phpUnderControl/bin && sudo php phpuc.php install /opt/cruisecontrol/

With this PHPunderControl should be ready, we only need to restart CruiseControl
$ sudo service cruisecontrol restart

Now we need a project, we have to go to the cruise control directory to create the directories
$ cd /opt/cruisecontrol/projects && sudo mkdir project && cd project

The folder will need sublic acces so CruiseControl can execute and write
$ sudo chmod -rv 777 ./

One we have the project directory we must create some directories for the builds and source code
$ mkdir source && mkdir build && mkdir build/api && mkdir build/coverage && mkdir build/logs

Now inside source directory, we'll download our code from our subversion server
$ cd source && svn co http://localhost/svn/trunk/

Now we have to create the build configurations for the project
$ cd .. && kate build.xml

And put the following code, the idea is that build will require all the other actions
1. update the code from svn
2. generate documentation
3. sniff the code looking for errors
4. apply phpunit test cases, the phpunit will look for a class phpucAllTests inside source/tests/AllTests.php

<?xml version="1.0" encoding="UTF-8"?>
<project name="project" basedir=".">
  <target name="checkout">
    <exec executable="svn" dir="${basedir}/source">
      <arg line="up" />
    </exec>
  </target>

  <target name="php-documentor">
    <exec executable="phpdoc" dir="${basedir}/source">
      <arg line="-ct type -ue on -t ${basedir}/build/api
           -tb /usr/share/php/phpUnderControl/Data/phpdoc
           -d ./"/>
    </exec>
  </target>

  <target name="php-codesniffer">
    <exec executable="phpcs"
          dir="${basedir}/source"
          output="${basedir}/build/logs/checkstyle.xml">
      <arg line="--report=checkstyle
           --standard=PEAR
           --ignore=src/autoload src/"/>
    </exec>
  </target>

  <target name="phpunit">
    <exec executable="phpunit" dir="${basedir}/source" failonerror="off">
      <arg line="--log-xml ${basedir}/build/logs/phpunit.xml
           --log-pmd ${basedir}/build/logs/phpunit.pmd.xml
           --log-metrics ${basedir}/build/logs/phpunit.metrics.xml
           --coverage-xml  ${basedir}/build/logs/phpunit.coverage.xml
           --coverage-html ${basedir}/build/coverage
           phpucAllTests tests/AllTests.php" />
    </exec>
  </target>

  <target name="build"
          depends="checkout,php-documentor,php-codesniffer,phpunit" />
</project>


We can test our build typing
$ ../../apache-ant-1.7.0/bin/ant build

Now we just need to add the project to the cruise control configuration file
$ cd ../.. && kate config.xml

add the following code inside tags, the name of the project should be the same as the name of the directory containing the build and source directories

<cruisecontrol>
<project name="project" buildafterfailed="true">
  <plugin name="svn" classname="net.sourceforge.cruisecontrol.sourcecontrols.SVN" />
  <modificationset quietperiod="60">
    <svn localWorkingCopy="projects/${project.name}/source/"/>
  </modificationset>
  <bootstrappers>
    <svnbootstrapper localWorkingCopy="projects/${project.name}/" file="build.xml"/>
  </bootstrappers>
  <schedule interval="300">
    <ant anthome="apache-ant-1.7.0" buildfile="projects/${project.name}/build.xml"/>
  </schedule>
  <listeners>
    <currentbuildstatuslistener file="logs/${project.name}/status.txt"/>
  </listeners>
  <log dir="logs/${project.name}">
    <merge dir="projects/${project.name}/build/logs/"/>
  </log>
  <publishers>
    <artifactspublisher dir="projects/${project.name}/build/api" dest="artifacts/${project.name}" subdirectory="api"/>
    <artifactspublisher dir="projects/${project.name}/build/coverage" dest="artifacts/${project.name}" subdirectory="coverage"/>
    <execute command="phpuc graph logs/${project.name} artifacts/${project.name}"/>
  </publishers>
</project>
</cruisecontrol>

And that's it ... more simple than it looks ...