Marius van Witzenburg "Learned my lesson in life, now setting my action to stay in life."

13Apr/101

Creating a patch to update one of your project its external vendor code.

I use this way to upgrade projects such as DokuWiki and CakePHP.

The advantage of this method is you will keep modifications you have made in the code of that vendor.
In my description I use FreeBSD and Mac OS X so you might need to adjust the commands for a different Operating System.

Create the patch file

First download the old and the new version code from your vendor site. In this example this is the website of CakePHP.
You need this that you can match the old version to the new version you want to upgrade to.

Create a temp directory to work in.

mkdir myUpgrade
cd myUpgrade

Download the two versions of CakePHP, your current and the version you desire to upgrade to

  • http://github.com/cakephp/cakephp1x/tarball/1.3.0-RC3
  • http://github.com/cakephp/cakephp1x/tarball/1.3.0-RC4

Unpack tarballs:

tar -xzvf cakephp-cakephp1x-348e5f0.tar.gz
tar -xzvf cakephp-cakephp1x-f63ab48.tar.gz

Now create the aptch width using the diff utility:

diff -Naur -x '.svn' -x '._*' cakephp-cakephp1x-348e5f0 cakephp-cakephp1x-f63ab48 > cakephp.diff

Applying the patch

To apply the patch file you created in the previous step, change into your real vendor directory and run the patch tool.
You may first want to do a test run to see if any conflicts may happen and what files are affected:

cd myUpgrade
patch -E -p1 < ../cakephp.diff --dry-run

The output should be something like this:

patching file cake/VERSION.txt
patching file cake/basics.php
patching file cake/config/config.php
*etc*

When you're happy, apply the patch:

patch -E -p1 < ../cakephp.diff

Once patch is applied you're done :-)

24Mar/100

Test-driven development (TDD)

When I was googling for development flows I found the following page which almost describes completely how I work in my development projects.

A flowchart of how this goes:

Test driven development 512x367 Test driven development (TDD)

For more info see: http://en.wikipedia.org/wiki/Test-driven_development

3Mar/101

Easy replace php short open tags with long php open tags

I wrote a quite simple script to fix short open tags to long open tags.

This script is compatible with CakePHP 1.1 and 1.2, on request :-)

Have fun using it!

I suggest saving this file to /usr/local/bin/php_fix_short_open_tag

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/sh
# Written by Marius van Witzenburg <info@mariusvw.com>
 
path="."
if [ ! -z "$1" ] && [ -d "$1" ]
then
    path="$1"
elif [ ! -z "$1" ]
then
    echo "Invalid path defined, using default."
fi
 
echo "Searching for php short open tags..."
echo "Starting path: $path"
find $path -name '*.php' -type f -exec perl -i -wpe 's/<\?=/<\?php echo /g' '{}' \; 
find $path -name '*.php' -type f -exec perl -i -wpe 's/<\?/<\?php/g' '{}' \; 
find $path -name '*.php' -type f -exec perl -i -wpe 's/<\?phpphp/<\?php/g' '{}' \; 
find $path -name '*.ctp' -type f -exec perl -i -wpe 's/<\?=/<\?php echo /g' '{}' \; 
find $path -name '*.ctp' -type f -exec perl -i -wpe 's/<\?/<\?php/g' '{}' \; 
find $path -name '*.ctp' -type f -exec perl -i -wpe 's/<\?phpphp/<\?php/g' '{}' \; 
find $path -name '*.inc' -type f -exec perl -i -wpe 's/<\?=/<\?php echo /g' '{}' \; 
find $path -name '*.inc' -type f -exec perl -i -wpe 's/<\?/<\?php/g' '{}' \; 
find $path -name '*.inc' -type f -exec perl -i -wpe 's/<\?phpphp/<\?php/g' '{}' \; 
find $path -name '*.thtml' -type f -exec perl -i -wpe 's/<\?=/<\?php echo /g' '{}' \; 
find $path -name '*.thtml' -type f -exec perl -i -wpe 's/<\?/<\?php/g' '{}' \; 
find $path -name '*.thtml' -type f -exec perl -i -wpe 's/<\?phpphp/<\?php/g' '{}' \; 
 
echo 'Done!'

Please leave the credits in if you want to modify it.
Donations are welcome haha :-P

2Mar/100

Shellproxy, an easy tool to run shell commands from PHP or other languages

This is a simple script that runs a shell command from PHP with the function system().
It sometimes happens that a command doesn't close the stdin or stdout, resulting the system() function to halt and wait for it to finish...
This causes PHP to reach the 30 second time limit and kill the script.

This script runs it in the background and you could make your commands send their output to a log file and still having full control.

Read the code below how to use it.

You can compile it by placing the code into 'shellproxy.c' and then compile it with the following command:

gcc -o shellproxy shellproxy.c

I hope this is something useful for you as it was for me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio .h>
#include <stdlib .h>
#include <string .h>
 
int main(int argc, char *argv[]) {
    char command[1024];
    int t;
 
    if (argc == 1) {
        printf("\nShell Proxy\n");
        printf("By Marius van Witzenburg <http ://mariusvw.com>\n\n");
        printf("You will mostly use this program from PHP or any other\n");
        printf("webscripting language to background run programs\n\n");
        printf("Usage:\n");
        printf("- CLI:\n");
        printf("shellproxy \"<cmd> <arg>\"\n");
        printf("shellproxy \"<cmd> <arg> > redirect.txt\"\n");
        printf("- PHP:\n");
        printf("system('shellproxy \"<cmd> <arg> &\"');\n");
        printf("system('shellproxy \"<cmd> <arg> > redirect.txt\"');\n\n");
    }
 
    command[0] = 0;
 
    for (t = 1; t < argc; t++) {
        strcat(command, argv[t]);
        strcat(command, " ");
    }
 
    fclose(stdin);
    fclose(stdout);
    fclose(stderr);
 
    system(command);
}

It would be nice if you keep my name mentioned into this code if you use it.

Ps. Donations are welcome... ;-)