Jun 29 2010

Doctrine migrations

Posted by German Rumm in php, work

Finally learned how to use Doctrine migrations.

You have development and staging servers. Your development server moved forward – you did some database changes, removed some fields, added some indexes. Now it’s tricky to update staging server without rebuilding the db (and losing all the data).

Sure, you can alter database by hand, but if you made a lot of changes, that would be a long and error-prone process.

That’s when you are supposed to use migrations.

There was only one problem – I didn’t know how to use them correctly. Now I figured it out.

  1. Get a “staging” server model files (checkout or copy)
  2. Put new schema.yml into config/doctrine directory replacing old one.
  3. and run
./symfony doctrine:generate-migrations-diff

Symfony will now generate a migration file that brings your database up to date.

Now, when you deploy your changes just run

./symfony doctrine:migrate

Now I just have to remember doing generate-migrations-diff before every doctrine:build –all, so I will have all the necessary files before uploading.

Jun 09 2010

Installed ZenCoding plugin

Posted by German Rumm in Uncategorized

Installed ZenCoding plugins for Eclipse and GEdit yesterday.

ZenCoding is an editor plugin that helps speed up HTML creation. In short, you type something like

div#page>div.logo+ul#navigation>li*5>a

press “Expand Abbreviation” shortcut and get this

  <div id="page">
        <div class="logo"></div>
        <ul id="navigation">
                <li><a href=""></a></li>
                <li><a href=""></a></li>
                <li><a href=""></a></li>
                <li><a href=""></a></li>
                <li><a href=""></a></li>
        </ul>
  </div>

Thanks to different filters it can generate HAML

#page
	.logo
	%ul#navigation
		%li%a{:href => ""}
		%li%a{:href => ""}
		%li%a{:href => ""}
		%li%a{:href => ""}
		%li%a{:href => ""}

or html with comments:

  <!-- #page -->
<div id="page">
	<!-- .logo -->
	<div class="logo"></div>
	<!-- /.logo -->
	<!-- #navigation -->
	<ul id="navigation">
		<li><a href=""></a></li>
		<li><a href=""></a></li>
		<li><a href=""></a></li>
		<li><a href=""></a></li>
		<li><a href=""></a></li>
	</ul>
	<!-- /#navigation -->
</div>
<!-- /#page -->

Still didn’t figure out how to expand to CSS in Eclipse. Not that important for me, since I don’t usually do it.

May 16 2010

PHP Snippet: how to auto-crop image for thumbnail using imagemagick.

Posted by German Rumm in php

Had a need to create thumbnails of specified size for a site, but source images were of different dimensions.

Usually I use some kind of framework/plugin function for that, but this project is pure PHP, so I had to come up with a function. I did this several times already and I don’t like writing the same function over and over again. So, here’s the snippet which I can look up later. Maybe someone else will also find it useful:

/**
 * $image_path string Path to an image file.
 * $size string Thumbnail dimensions in <width>x<height> format
 * 
 * @returns string Path to created thumbnail
 */
function thumbnail($image_path, $size = '200x150') {
  list($width, $height) = getimagesize($image_path);
  $image_aspect = $width / $height;
 
  list($thumb_width, $thumb_height) = explode('x', $size);
  $thumb_aspect = $thumb_width / $thumb_height;
 
  if ($image_aspect > $thumb_aspect) {
    $crop_height = $height;
    $crop_width = round($crop_height * $thumb_aspect);
  } else {
    $crop_width = $width;
    $crop_height = round($crop_width / $thumb_aspect);
  }
 
  $crop_x_offset = round(($width - $crop_width) / 2);
  $crop_y_offset = round(($height - $crop_height) / 2);
 
  // crop parameter
  $crop_size = $crop_width.'x'.$crop_height.'+'.$crop_x_offset.'+'.$crop_y_offset;
 
  // thumbnail is created next to original image with th- prefix.
  $thumb = dirname($image_path).'/th-'.basename($image);
  exec('convert '. escapeshellarg($path).' -crop ' . $crop_size .' -thumbnail '.$size.' '. escapeshellarg($thumb));
 
  return $thumb;
}
May 16 2010

Proxy Auto Config and VirtualDocumentRoot

Posted by German Rumm in Uncategorized

Was looking for a way to automate setting up a new project on my dev machine.

Setting up a new project usually goes like this:

  1. Create a folder for a project
  2. Create new virtualhost config in /etc/apache2/sites-available (I usually copy and edit template I have)
  3. a2ensite
  4. /etc/init.d/apache reload
  5. Edit /etc/hosts – add another “127.0.0.1 project-name” line
  6. Create DB table and user using phpMyAdmin

Those steps are always the same, and I knew that there suppose to be an easier way. I decided to look it up and stumbled on Chris Morrell’s “Automatic Virtual Hosts w/ Proxy Auto-Config” post. Not only he provides exampe on how to use VirtualDocumentRoot directive, he also explains how to avoid editing hosts file by using .pac file.

So, in short: update your apache config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    VirtualDocumentRoot "/path/to/vhosts/%1/public"
    ServerName subdomains.localhost
    ServerAlias *.localhost
    LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
    ErrorLog "logs/vhosts-error_log"
    CustomLog "logs/vhosts-access_log" vcommon
</VirtualHost>
<Directory "/path/to/vhosts/*/public">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

create a .pac file:

function FindProxyForURL(url, host)
{
	if (dnsDomainIs(host, ".localhost")) {
		return "PROXY localhost";
	}
 
	return "DIRECT";
}

and make your browser use it.

Apr 25 2010

Windows, Linux and RAM

Posted by German Rumm in Uncategorized

Couple of weeks ago I installed Windows Vista. Usually I prefer XP, but I had Vista DVD and a license, and I wanted DirectX 11. After all, I was installing Vista solely for Call of Duty: Modern Warfare 2.

Almost immediately after installation I started experiencing a lot of problems: computer was rebooting by itself, everytime proudly informing me that it had “recovered from serious error”, MW2 kept crashing and behaving strangely. I heard that Vista was bad, but that was too much.

About the same time I upgraded to Zend Studio 7. Zend Studio is great, it comes bundled with Mylyn, which I instantly loved. But I enjoyed it only for limited time, because Zend was dying on me constantly. After using Mylyn, reopening all the closed editors was frustrating. I tried everything to fix it: I dug deep into Zend Studio and Eclipse forums, I was disabling one plugin after another, I created several separate workspaces, reinstalled Zend Studio several times into different locations, upgraded JRE and switch to alternative JRE packages. Nothing helped. I decided to return back to Zend Studio 6.

But Zend 6 also kept crashing on me and I was extemely annoyed by all of it. I considered doing clean install of Ubuntu and was creating a list of stuff I need to backup before wiping out harddrive.

But then something happened. I needed to debug some javascript in IE6, so I decided to install XP to my Virtualbox (IETester is just not the same). Installation gave me BSOD with BAD_MEMORY_MANAGEMENT or something. I googled it up, and turned out that you will get this error message when there’s a problem with your RAM. I checked it with Windows Memory Diagnostic and indeed, there were several problems with my memory. Then I ran memtest86+ that comes with Ubuntu, and found out that my RAM is bad, bad, bad.

We contacted our hardware guy, he brough two new sticks of RAM, I run memtest86+ on them – they were fine. I rebooted into Ubuntu and voila – my problems were gone. Zend worked all day without a single fail, Firefox didn’t die on me, even Vista and MW2 ran without a single error.

Now I am enjoying my computer again, enjoying Zend Studio with Mylyn (upgraded to TaskTop), and enjoying Vista w/ MW2.

So, my word of advice: if you experience sudden BSODs on Windows, or random crashes of programs on Linux (Linux itself never crashed, BTW) – check your memory first and do it using memtest86+. It finds errors even when Windows Memory Diagnostics says “everything is ok”.

Apr 25 2010

Week review

Posted by German Rumm in Uncategorized

Last week was not that productive for me. Spent a lot of time debugging javascript for IE6, while fighting computer problems. But still managed to learn some valuable things:

  1. Found out that most Windows problems are actually RAM problems
  2. How to create custom validators using jQuery.validate()
  3. That IE6 doesn’t like “wrong” javascript
  4. How to setup Proxy Auto Config and save couple of minutes when creating new project.
Dec 15 2009

How to add empty directory to Git repo

Posted by German Rumm in work

I keep all my Symfony projects in a Git repositories. Makes my life a lot easier – I can upload only changed files on update, which makes updating faster and more reliable.

But, apart from the code files logs and cache files are always changing, and I don’t really need and want to keep them in repo. I used .gitignore to filter out log and cache directories, but everytime I “checkout” repo I need to create them by hand. What I needed is to “git add” directories but ignore directory contents.

The problem is that if you ignore directory contents, directory appears empty to Git, and Git ignores empty directories automatically, so

cache/*

prevents me from adding cache directory at all.

Finally I found a solution to my problem in the Git Cheat Sheet at GitHub. http://github.com/guides/git-cheat-sheet

First, in root .gitignore file you ignore all the directories as usual, and add an exception for the .gitignore file

log/*
cache/*
!.gitignore

And then just add empty .gitignore files into log/ and cache/ firectories.

$ touch log/.gitignore cache/.gitignore

Now, when you do git add, only .gitignore files will be added for those directories, and all other files will be ignored.

Oct 13 2009

How to clean up your Linux directories.

Posted by German Rumm in Uncategorized

I need to clean up my working folders way too frequently. Got tired of googling required commands every time and decided to post them for everyone to see.

So, here goes.

Delete all files matching a pattern in a directory

$ find . -name "*.tmp" -print | xargs rm

Remove empty directories

$ cleanlinks

Both commands work on my Ubuntu 8.04 with no apt-getting required.

May 04 2009

Adobe Flex

Posted by German Rumm in flash

Started to learn Adobe Flex. Enjoying it very much.

Installed Adobe Flex Builder Linux Alpha on top of Eclipse 3.4 – mxml editor doesn’t work. Gives me “Could not open the editor: Assertion failed” error.

It seems that current version of Flex Builder Linux requires Eclipse 3.3 (which is available for download here)

Downloading Eclipse 3.3 right now, hope everything will work.

Apr 06 2009

Speed up symfony’s apps

Posted by German Rumm in php

Kris Wallsmith submitted a patch to symfony, that supposed to increase the speed of your apps, especially if you are using cache.

Currently it’s only available from SVN (or as a patch)

Hope Fabian will release 1.2.6 soon, my server’s admin doesn’t like to install unstable libs.