Apache, Subversion and post-commit hook

The following short tutorial will guide you through setting up mod dav with apache virtual hosts on ubuntu linux.  Assuming you have apache web server installed and running.

The first thing we need to do is  run the following commands in the terminal to install the required packages.


sudo apt-get install subversion libapache2-svn

with subversion installed we now need to create the directories for our subversion repository.


sudo mkdir /var/svn

sudo mkdir /var/svn/repository

sudo chown www-data:www-data -R /var/svn/repository

sudo chmod 770 -R /var/svn/repository

next we are going to create a structure which will define how our directories are laid out.


cd ~

mkdir svn-temp

mkdir svn-temp/svn

mkdir svn-temp/svn/branches

mkdir svn-temp/svn/tags

mkdir svn-temp/svn/trunk

touch svn-temp/svn/trunk/index.html

now we are going to create our repository for our test site `test.example.com` and import our directory structure we just created.


sudo -u www-data svnadmin create /var/svn/repository/test.example.com

sudo -u www-data svn import ~/svn-temp file:///var/svn/repository/test.example.com -m"initial import"

Its time for the apache stuff now. We need to create a virtual host and create our web root directory.


sudo mkdir /var/www/test.example.com

sudo chown www-data:www-data /var/www/test.example.com

create the file ‘/etc/apache2/sites-available/test.example.com’ and add the following to it:

<VirtualHost *>
DocumentRoot /var/www/test.example.com/svn/trunk
ServerName test.example.com
ServerAlias www.test.example.com
<Location /svn>
DAV svn
SVNPath /var/svn/repository/test.example.com
</Location>
</VirtualHost>

enable the virtual host by typing the following command in the terminal


sudo a2ensite test.example.com

we’re getting there. we need to create our post-commit hook so that when we commit our repository the code will also be updated in the sites web directory also.

create the file ‘/var/svn/repository/test.example.com/hooks/post-commit’ and wirte the following two lines:

#!/bin/sh
/usr/bin/svn update /var/www/test.example.com/ –username www-data

then change the permissions and make it executable

sudo chmod +x /var/svn/repository/test.example.com/hooks/post-commit
sudo chown www-data:www-data /var/svn/repository/test.example.com/hooks/post-commit

we now need to be able to view the site in the browser. If you have a dns server already setup you can just configure that, if not you can add test.example.com to the ‘/etc/hosts’ file at the end of the line which starts with 127.0.0.1. so it should look like the following

127.0.0.1 localhost test.example.com

and restart the networking


sudo /etc/init.d/networking restart

Almost there. The last thing we need to do before we can load our website in the browser is the initial checkout and restart apache.

sudo -u www-data svn co file:///var/svn/repository/test.example.com /var/www/test.example.com
sudo /etc/init.d/apache2 restart

You can now fire up the browser and type test.example.com in the address bar and should see a blank page, that means everything went fine

Start working on your code by checking out a fresh copy.

cd ~
svn co http://test.example.com/svn testproj

thats all!

Comments (1)

Back references

Back references are a means to use a previous captured sub-expression in the regular expression itself. It can be useful in situations such as matching html tags where you want to match the ending tag when the starting tag is not known.

The syntax for back references is: `\1` or any digit above one, maximum number of back references allowed are 99.


<?php
$pattern = '!<(.*?)>.*?</\1>!';
$string = 'some text <tag> text </tag> some text';
preg_match($pattern, $string, $matches);
?>

Back references must refer to capturing sub-expressions, they can not be used with non-capturing sub-expressions. The following will not work, it will raise an error.

$pattern = '!<(?:.*?)>.*?</\1>!';

because you are referencing a sub-expression which does not exist, as it was not captured. It is the same as matching a sub-expression which was not used because of another alternative being used as in the following

$pattern = '!(a|(bc))\1!';

This will not match if the string starts with `a` but will match if the string starts with `bc`.

Comments (1)

Non-capturing Parentheses

Sometimes we add sub-expressions as part of a larger expression but we don’t need the match data for that sub-expression. This is where non-capturing parentheses (aka. grouping-only parentheses) come in use. non-capturing parentheses prevent the sub-expression match being stored in the match array.

The syntax for non-capturing parenthese is: (?:…)


<?php

$string = '<tr><td>table data 1</td><td>table data 2</td></tr>';

$pattern = '/<tr><td>(.*?)<\/td><td>(.*?)<\/td><\/tr>/';

preg_match($pattern, $string, $match);

print_r($match);

?>

The above code snippet will print:

Array
(
[0] => <tr><td>table data 1</td><td>table data 2</td></tr>
[1] => table data 1
[2] => table data 2
)
Assuming that we only require data in the second <td> we can change the sub-expression in the first <td> into non-capturing as follows.


$pattern = '/<tr><td>(?:.*?)<\/td><td>(.*?)<\/td><\/tr>/';

and this will print:

Array
(
[0] => <tr><td>table data 1</td><td>table data 2</td></tr>
[1] => table data 2
)

It is said that the second method is more efficient and faster than the first (capturing everything) and also uses less memory (logical).

Leave a Comment

Named Capture

Named capturing means to capture a part of an expression into a named location, i.e. the match array will contain an element where the key will be the name specified in the named capture and the value will be the matched expression.

The syntax for named capturing is: (?P<name>…)

Look at the following code snippet and it will become clear.


<?php

$string = "<head><title>my title</title></head>";

$pattern = "/<title>(?P<page_title>.*?)<\/title>/";

preg_match($pattern, $string, $match);

echo $match['page_title'];

?>

What are the benefits of named capturing?

  1. Easier to access the captured data rather than having to work out the array index, especially in much larger and complex regular expressions which contain many sub-expressions.
  2. You don’t have to modify existing code, i.e. if another matching sub-expression is added before the named capture you can still access the value of the named capture using the same key. Whereas if named capturing was not used the array index of all the matches after it would change to $i+1.
  3. Easier code readability.

Leave a Comment

Locating the matching brace in vi

vi  has excellent syntax highlighting, it also highlights the matching braces and brackets aswell when you enter a corresponding closing one. But sometimes the matching brace could be out of the viewable textarea, such as writing a relatively long method. In vi you can quite easily switch to the matching brace by placing the cursor over the opening or closing brace (in command mode) and typing `%` (shift+5).

Leave a Comment

Indenting source code in vi

To enable automatic indenting of source code add the following line to your .vimrc file


set autoindent

Indenting the whole file

Ever copied and pasted source code in vi and dreaded the idea of having to go through each line and using the tab key, well you don’t have to because it can be done for you automatically with a few key strokes.

press ‘esc’ to go into command mode

then type `gg` (g key twice) to place the cursor at the start of the file

then type `=G` (equals key followed by uppercase g) this is the magic

thats all, you will see the code being indented right before your eyes.

You can also indent part of the file by placing the cursor from where you want the indenting to start and typing `=G`

Indenting a single line

To indent a single line place the cursor on the line you want to indent and press `CTRL+T`. You must be in insert mode for this to work.

Copy and paste float-right problem

When copying and pasting source code with the autoindent feature enabled you will find that each line which vi would indent floats further to the right than the previous line.

To fix this you need to go into command mode and type the following


:set noautoindent

This will temporarily disable the autoindent feature. You can then copy and paste your code (paste your code in insert mode or else you might motice that the first letter or two are missing) and enable the autoindent feature again by typing


:set autoindent

Leave a Comment

right-click macbook + ubuntu

The touchpad on the macbook has only one mouse click button, but it is possible to map a keystroke to act as the mouse right-click.

To map the right command key as the mouse right-click add the following command to /etc/rc.local

xmodmap -e ‘keycode 116 = Pointer_Button3′

You then need to open ‘System->Preferences->Sessions’ and click ‘add’ in the ‘Startup Programs’ tab to execute the command when you login.  Fill in the Name field with whatever you want and then enter ‘/etc/rc.local’ in the command entry field and click ‘ok’.

Next time you login you can use the right command key as the mouse right-click.

Comments (1)

wireless on macbook + ubuntu

This is a quick guide which shows how to get your wireless card working with ubuntu hardy heron installed on a macbook that came with leopard pre-installed in 4 easy steps. It assumes that you already have ubuntu installed.

Step 1

First you need to identify the network controller that your macbook has. So run the ubuntu ‘hardware testing’ program which is located at ‘System->Administration->Hardware Testing’. Keep pressing next until you see the ‘Detecting your network controller(s)’ screen which will display the network controller detected.  Note down the name of your network controller as you will need this to install the correct driver. My macbook had the Broadcom controller, so for the remainder of this tutorial wherever i use ‘broadcom’ you will need to replace it with your own.

Step 2

Next you need to install the following packages using the synaptic packet manager (you will need the multiverse repository enabled):

rar

unrar

ndisgtk

ndiswrapper-common

ndiswrapper-utils-1.9

Step 3

Insert the leopard install disc 1 which came with your macbook and navigate to ‘boot camp->drivers->broadcom’. Create a new directory in your home folder and copy the ‘broadcomxpinstaller.exe’ file into it. Open a terminal and cd to the directory where you copied the exe file and run the following command:

unrar x broadcomxpinstaller.exe .

This will unpack the exe file and you should see a bunch of files, the one we are interested in is the .inf file. On my machine it was bcmwl5.inf

Step 4

Open ‘System->Administration->Windows Wireless Drivers’ and click on ‘Install New Driver’, locate the .inf file from the previous step and press ok.

The wireless driver installation is now complete all you need to do is configure your wireless network settings and you are all ready to start surfing the web.

Leave a Comment