Documentation

Migrating from D6 upload.module to filefield.module

So building on my last post for creating CCK fields, here's some code I whipped up to migrate from the D6's core upload.module to the filefield.module. This isn't general purpose code but might help someone else out. The catch is I'd built a video node with and was using the upload module to attach exactly two files, an image and a video. The new node will have separate thumbnail and video fields. If you'll be moving to a multi-value field this code won't work for you.

The gist is the same as before, setup your field for video and your field for images then export using:

<?php
var_export
(content_fields('field_web_video', 'video'), 1);
?>
and
<?php
var_export
(content_fields('field_video_thumb', 'video'), 1);
?>

Then roll that into an update function that also moves the file data around in the database. Code is after the jump.

Building the C2 Morse code trainer on OSX

I was trying to install Ward Cunningham and Jim Wilson's Morse code trainer on OS X and it looks like their .dmg file doesn't work on the new Intel machines. Here's what I did to get it to build.

Programatically creating a CCK field in Drupal 6

I spent some time today trying to figure out how to create a CCK field as part of an hook_update_N function. Unlike previous versions of CCK, in 6 it's very easy to manipulate the fields from code.

The first step is to create the field using CCK's UI. Once you've got the field setup the way you'd like it use PHP's var_export() to dump the contents of the node's field as an array:

var_export(content_fields('field_translator_note', 'feature'));

Using Growl to tail a log file

Mikey_p from the Portland Drupal Group pointed me towards a blog post he'd done on using Growl to watch for PHP errors. Always looking to put my fingerprints on something I decided to do the same but using PHP rather than Python.

The short version is install growlnotify then run the following from the terminal:

tail -n 0 -f ~/Sites/nm/logs/access_log | php -r 'while ($m = fgets(STDIN)) shell_exec("growlnotify -p 0 AlertTitle -m ". escapeshellarg($m));'
Obviously you'll need to use the right filename and change AlertTitle to something more to your liking.

Prefilling the ImageCache cache with wget

Today I needed to get Drupal's ImageCache module to regenerate a bunch of resized images. ImageCache doesn't create the images until a browser requests them and at that point the new image is saved to the disk for future use.

One way to generate the images would have been to just click my way through every page on the site but I'm way to lazy for that. So I used wget:

wget -r -nd --delete-after http://example.com

By using the recursive (-r) and --delete-after switches I was able to have it crawl the site and get all the images generated. Bonus points for running in on the server so that the transfers were via the loopback interface so the transfer didn't count against the monthly bandwidth limit.

Drupal6: Display block for one node type

This is a little snippet I came up with to get a block to show up on a single node type:

<?php
$menu
= menu_get_item();
if (
$menu['path'] == 'node/%' && isset($menu['page_arguments'][0]->type)) {
  return
$menu['page_arguments'][0]->type == 'story';
}
?>

It uses the node type stored in the menu system so you don't have to match arg(0) etc.

Getting PHP + GD working on Leopard (aka recompiling everything)

Here's my writeup on how to recompile PHP for Leopard to get GD working. The basic plan is recompile Apache and PHP from source. It may totally bork your system, I'm just writing it down so the next time I need to do this I can remember what I did. I wish I could give credit to all the places I stole bits from but I didn't do a good job of keeping notes early on.

Flex stops building the foo.html and foo-debug.html files

I've been trying to get up to speed on Adobe Flex for my CS Capstone project. I should mention, by some surprisingly enlightened decision, Adobe offers a free Flex license to students.

Everything was going great until I it suddenly stopped building the foo.html and foo-debug.html files that launch the foo.swf and foo-debug.swf files. Apparently other people ran into this too. I didn't figure out the proper fix for this but I developed a workaround based on a blog post by one of the Flex developers.

Basically, just rename the index.template.html to ${swf}.template.html and then do a clean rebuild. You should end up with bar.html and foo-test.html and be able to get back to work.

Replacing tabs with spaces in files

I've got a bunch of source code that I'd written with 4-character wide tabs. I needed to replace them with spaces. I'm ashamed to admit how long it took me to figure out.

#!/bin/sh
for i in *.[c,h]
do
    expand -t4 $i > tabfree.txt && mv tabfree.txt "$i"
done

Hopefully by posting this I'll save someone (read: me in six months) some time.

Sorting numeric values stored in character fields with views.module

I spent a good chunk of time this morning trying to figure out how to get the views module to sort a character field with numeric data correctly. The audio module has a normalized table of meta-data meaning that there's one column for the tag name and one for the value. The value is stored as a character string which causes problem when sorting numeric data like the track numbers or years. If you've got a SELECT value FROM audio_metadata ORDER BY value that returns the range of numbers 1...13 it ends up sorted as 1,10,11,12,13,2,3...9. The trick as I discovered is to add zero to the field to coerce it to a numeric value: SELECT value + 0 AS v FROM audio_metadata ORDER BY v.

The problem then is to figure out how to get the views module to generate this bit SQL to get the sorting right. The solution I came upon is when defining the field set 'notafield' to TRUE and provide a 'query_handler' to generate the correct SQL. I've included the relevant parts of the audio module below to demonstrate how it works. You can see the complete code here.

Using SimpleXML with HTML

PHP 5's SimpleXML module is one of the the biggest reasons to upgrade to 5. If you're parsing RSS feeds or the results of webservice requests it works beautifully and saves a ton of time. The only problem with it is that it'll only load valid XML. I banged my head against it for way to long before coming up with the following:

<?php
  $doc
= new DOMDocument();
 
$doc->strictErrorChecking = FALSE;
 
$doc->loadHTML($html);
 
$xml = simplexml_import_dom($doc);
?>

Using PHP and CURL to do an HTML file POST

After scouring the internet to find out how to do multipart post operations and finding nothing nothing, I decided to save someone else some time. The trick is that @ before the file name:

<?php
$fullflepath
= 'C:\temp\test.jpg';
$upload_url = 'http://www.example.com/uploadtarget.php';
$params = array(
 
'photo'=>"@$fullfilepath",
 
'title'=>$title
);       

$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $upload_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$response = curl_exec($ch);
curl_close($ch);
?>

Originally posted here: http://drewish.com/blogger/archives/2005/01/27/using_php_and_curl_to_do_...

Miscellaneous FreeBSD bits

There are a few other things that are worth configuring but don't really fall into any of the previous sections so I'll just lump them in together here.

ntpdate

The station module requires that the clock on the server be reasonably accurate. FreeBSD can use the Network Time Protocol (NTP) to synchronize your server's clock to a time server. To enable the ntpdate script, you'll need to add the following lines to your /etc/rc.conf file:

ntpdate_enable="YES"
ntpdate_flags="north-america.pool.ntp.org"

Next you'll want to add a cron job to the root user's crontab file:

$ crontab -e

The following entry will synchronize the clock every morning:

# Every morning at 4am update the clock.
0 4 * * * /etc/rc.d/ntpdate start > /dev/null

PHP One-liners

I'm starting a collection of one-line PHP programs that I've used on the command line.

This removes trailing white space from a DOS file:

C:\>php -r "foreach (file($argv[1]) as $l) {print rtrim($l).chr(13).chr(10);}" user.module > user.module.trimed

This little gem escapes code for inclusion in HTML. The invocation uses UNIX's cat utility, on Widows you could use type:

$ cat test.php | php -r "print htmlentities(file_get_contents('php://stdin'));"

Tools for reviewing patches on Windows

I've been meaning to do a write up on tools for applying patches on Windows but haven't had a chance. For now I'll just leave it at this list.

DiffUtils 2.8.7 - show differences between files
http://gnuwin32.sourceforge.net/packages/diffutils.htm

CoreUtils 5.3.0 - GNU file, shell and text utilities
http://gnuwin32.sourceforge.net/downlinks/coreutils.php

CygUtils 1.2.9 - miscellaneous utilities including dos2unix and unix2dos (which you'll need to convert the line endings to keep patch happy)
http://gnuwin32.sourceforge.net/packages/cygutils.htm

Patch 2.5.9 - apply a diff file to an original
http://gnuwin32.sourceforge.net/packages/patch.htm

Wget 1.10.1 - retrieve files from the WWW
http://gnuwin32.sourceforge.net/packages/wget.htm

WinMerge
http://winmerge.org/

for you, a photo

me on streetcar

ads

User login

Syndicate content