Archive for the ‘Uncategorized’ Category

svn copy between different repositories

Monday, November 2nd, 2009

svnforeigncopy is a nice script that allows you to copy svn branches from foreign svn repositories into your working copy or svn repository while keeping svn:ignore and svn:external properties intact (you still lose the svn history). Much better then ex- and importing the sources.

I found out about this script when I was looking at the pkContextCMS symfony plugin, which looks promising, too. I think, I’ll give it a try for my own website and I that will write some posts about it soon.

Symfony link tags (e.g. canonical)

Friday, October 23rd, 2009

Unfortunately, symfony does not (yet, see http://trac.symfony-project.org/ticket/7282#comment:3) provide an easy and intuitive way to add ‘<link rel=”XXX” href=yyy” />’ tags which you might need e.g. to avoid duplicate content using canonical tags. Therefore I added a simple helper method ‘add_link’ which takes an href and a rel parameter and then creates and stores the according link tag in a slot named ‘links’:

(more…)

Enable maintenance mode using .htaccess rewrite rule

Thursday, October 22nd, 2009

With some simple .htaccess (or vhost) settings you can redirect all requests to a specified maintenance file /(maintenance.html in this example) by simply uploading a file named “maintenance” into your document root. Afte maintaining your site, you can delete the file “maintenance” and all request will work as before:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

ErrorDocument 503 /maintenance.html

# Check for maintenance file and redirect all requests
# If your Deployment is finished, delete it
RewriteCond %{DOCUMENT_ROOT}/write/maintenance -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteCond %{SCRIPT_FILENAME} !maintenance.php
RewriteCond %{REQUEST_FILENAME} !.css$
RewriteCond %{REQUEST_FILENAME} !.png$
RewriteCond %{REQUEST_FILENAME} !.jpg$
RewriteCond %{REQUEST_FILENAME} !.gif$
RewriteRule ^.*$ / [R=503,L]

With a simple PHP Script (this is just a basic example with room left for optimization) you can automate the creation/deletion of the file “write/maintenance”. Of course, this file should be at least password or ip protected:

< ?php
$maintenanceActivationFile = $_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'write'.DIRECTORY_SEPARATOR.'maintenance';
if (file_exists($maintenanceActivationFile)) {
if ($_REQUEST['action'] === 'stop') {
unlink($maintenanceActivationFile);
echo 'Maintenance mode stopped';
} else {
echo '<a href="'.$_SERVER['PHP_SELF'].'?action=stop">Stop maintenance mode';
}
} else {
if ($_REQUEST['action'] === 'start') {
$handle = fopen ($maintenanceActivationFile, 'w');
fclose($handle);
echo 'Maintenance mode started';
} else {
echo '<a href="'.$_SERVER['PHP_SELF'].'?action=start">Start maintenance mode</a>';
}
}
?>

You can test this here:

When maintenance mode is enabled, http://demo-maintenance.jonasfischer.net/* will show “maintenance.html”. When maintenance mode is not enabled http://demo-maintenance.jonasfischer.net/ will show “index.html”.

Symfony integration

Instead of using a php script I created a symfony task that creates and deploys or deletes the file “maintenance” so I can automatically couple this step with my existing deployment tasks.

You could also adjust your rewrite rule to listen to the symfony *.lck files managed by the standard symfony enable/disbale tasks:


$ symfony project:disable APPLICATION_NAME ENVIRONMENT_NAME

$ symfony project:enable APPLICATION_NAME ENVIRONMENT_NAME

These tasks create/delete files using the following naming schema:

sfConfig::get('sf_data_dir').'/'.$app.'_'.$env.'.lck'

However, unless you have shell access to your production servers you still have to deploy the files to your webfarm.

The benefit of using the rewrite rule over using symfony’s built in disable mechanism is tha you can disable all your php scripts/html files, not only those dispatched through your sfFrontWebContoller.

Memcached and namespaces

Wednesday, October 21st, 2009

To flush a whole bunch of memcache entries, some kind of namespace mechanism is needed. Unfortunately, memcached does not provide namespaces, that is why you have to fake them:

Instead of deleting a memcache entry, you can simply invalidate it by changing the key used to retrieve the value.

So if you include a variable part into your key generation mechanism, you can change this variable part and thus invalidate all keys that include this variable part.

If you take this one step further and use different variable parts for different “namespaces”, then you can invalidate the namespace by only changing the variable part for this namespace.

ModRewrite Rule to move from one domain to another

Wednesday, October 14th, 2009

Often needed and always forgotten how to do it:

If you want to move a project from one domain to another, you can use the following rewrite rule on the old server where “http://jnotes.jonasfischer.net” is the new target:

RewriteRule (.*) http://jnotes.jonasfischer.net/$1 [R=301,L]

By the way: JNotes moved from http://z-net.de/jnotes to http://jnotes.jonasfischer.net

Symfony Forms and “old” dates

Monday, October 12th, 2009

I just contributed a patch to the Symfony Project that caused errors when populating forms with dates prior to Fri, 13 Dec 1901 i.e. exactly 81 years earlier than my date o brith (which was not a friday, luckily).

I noticed the problem when working on Brigitte.de’s baby name finder: Under http://www.brigitte.de/brigitte.php/liebe-sex/vornamen-finden/isabel.html you see the birth date of “Isabella von Frankreich”, the daugthe of King Philipp IV is 1970 -01-01 but actually it should be 1292-03-17.

(more…)

Test Site in different Internet Explorer Versions (IE 5.5, IE 6, IE 7 and IE8)

Thursday, October 8th, 2009

Unfortunately, there are still many different version of Microsoft Internet Explorer out there in the web and testing a site in everey browser is quite difficult as you can normally only install one IE Version on your system. Luckily, the IE Tester (http://www.my-debugbar.com/wiki/IETester/HomePage), which is tightly coupled with the IE Developer Toolbar (http://www.my-debugbar.com/wiki/) lets you test all browser engines within a single application.

This works very well for spotting and fixing layout problems. But beware! I had some serious problems while debugging JavaScript, especially Ajax Problems in IE Tester. I struggled several hours with one Javascript Problem and with every step I made it seemed getting worse until I checked the result in a “real” Internet Explorer where the behaviour was substantially different. Anyway, I still like IETester.

Ubuntu LAMP Development Server VirtualBox image

Saturday, October 3rd, 2009

I created a Sun VirtualBox image with Ubuntu 9.04 (Jaunty jackalope, GUI version) to help me ease my php/mysql web development process.

To use it, you need to download the Virtual Machine: Ubuntu LAMP Development Server VirtualBox Image

Then import it into the Sun VirtualBox Client. Under Windows it is something like

C:\Program files\Sun\VirtualBox\VBoxManage.exe import PATH_TO\devvm.ovf

Afterwards you can start the virtual machine using the Sun VirtualBox Client and then log in as “dev” with password “dev”.

Note: You probably have to change the network device settings of the virtual box in order to access it via ssh/smb and to access the internet from within your development server.

Installed Software

  • LAMP (Ubuntu Linux, Apache 2, Mysql Server 5, Php 5.2.6-3ubuntu4.2)
  • phpMyAdmin
  • mysql client
  • subversion client
  • xdebug
  • samba
  • ssh
  • pear
  • vim

For the protocol: sudo apt-get install apache2 php5-dev php-pear mysql-server-5.0 mysql-client-5.0 subversion phpmyadmin php5-xdebug ssh samba vim

(more…)

Search (and replace) in multiple files from command line

Saturday, September 26th, 2009

Another remidner to myself …

Search in multiple files

Easy to remember:

grep -R "SEARCHTERM" *

However, on some systems, grep -R won’t work and then you have to do something like:

find . -exec grep -nsl "SUCHWORT" {} \;

Search & Replace

In a single file

sed -i 's/boring/interesting/g' /home/jonfis/filename.txt

In multiple files

 find . -name "*.php" -exec sed -i 's/boring/interesting/g' {} \;

compress and decompress tar.gz files

Friday, September 25th, 2009

Because I always forget how to do this:

compress:

tar -cvzf filename.tar.gz directoryToArchive/

decompress:

tar -xvzf filename.tar.gz