Converting DOS to UNIX newlines

Annoying as it may be, Windows and some FTP clients insert “carriage return” characters into text files. This is commonly referred to as “DOS” mode – you’ll know you’re editing a DOS mode file if your editor tells you so, or (like I found this morning) git tells you. I downloaded a bunch of files I had passed to a client, and git told me they had all been changed – they simply had carriage return (\r or ^M) characters inserted. Meh.

You can convert files with the

fromdos

command, which is located in the

tofrodos

package. Doing this recursively is pretty easy as well:


ewiltshi@davinci$ find . -type f | xargs fromdos

The above pattern is very useful –

find . -type f

just finds all files from the current directory and all subdirectories. The output of this then gets piped to

xargs fromdos

which runs the

fromdos

command on the found files. If you only wanted to run it on .css files, you could just change it up like so:


ewiltshi@davinci$ find . -type f -name "*.php" | xargs fromdos

Google Instant with FireWatir

I’ve been playing around with FireWatir, which allows you to write ruby scripts that control a Firefox web browser. This has obvious white hat uses, such as automated testing for sites that use JavaScript. It also has black hat uses for things like automated account creation and article directory submitters.

The most basic example is searching Google for something, and the code looks like this:


ff = FireWatir::Firefox.new
ff.goto('http://www.google.com/ncr')
ff.text_field(:name => 'q').set('yoda')
ff.button(:name, 'btnG').click

The problem is Google instant – after FireWatir types the first character, Google instant executes some AJAX which interferes with FireWatir. I found I would only get the first character typed. If you’re not running Firefox in “porn mode” you can disable Instant and your settings will be saved in a cookie. If you have Firefox configured not to save cookies, this is a problem. Here’s a solution that is somewhat of a kludge, but it works:


ff = FireWatir::Firefox.new
ff.goto('http://www.google.com/ncr')
ff.text_field(:name => 'q').set('y')
sleep 0.3
ff.text_field(:name => 'q').set('yoda')
ff.button(:name, 'btnG').click

The difference here is we only set the first character in the text field, then pause the script giving Instant time to update. Then proceed with the rest of the code.

symfony caching – cross-application cache clearing

I’m writing a webapp that has an api application, and a frontend application. The frontend supports caching, since it’s providing RSS feeds that might be read quite frequently, and don’t change often. The currently timeout is set to 12 hours. The API allows the user to change aspects of the RSS feeds that will change the contents of the feed so much that it could be totally different, and 12 hours is too long for the user to wait.

What I needed to do was clear the frontend cache, but from within the API application. I found this post that described how to do it in symfony 1.2, and it works just great in 1.4! Here’s the post: clearing symfony cache for another application.

Here’s the code snippet that I ended up using:

// Save any changes, if any were made, and clear the frontend RSS cache for
// this feed.
if ($changes == true)
{
$wc->save();
// Clear cache, if it's turned on.
sfContext::switchTo('frontend'); //switch to the environment you wish to clear
sfContext::getInstance()->getViewCacheManager()->remove('static/rss?id=' . $wc->getId());
sfContext::switchTo('api'); //switch back to the environment you started from
}

symfony and cPanel “couldn’t locate driver named mysql”

I was experiencing this earlier today on a cPanel install to which I had deployed a symfony application. The solution was to install the PDO_MYSQL Pecl module. Here’s how you do it:

  1. Log in as root to your cPanel install
  2. Under the Software menu on the left, click “Module Installers”
  3. Click Manage, next to the PHP Pecl item
  4. Search for “PDO” using the search
  5. Click “Install” in the PDO_MYSQL row
  6. Clear your symfony cache, and away you go!
WORDPRESS