How I got field diffs working with Drupal, PEAR Text_Diff, and Dreamhost

I have a Drupal site where I will propose major changes to a policy document. The site has nodes with current and proposed versions of document sections.

I want auto-generated diffs to make the proposed changes obvious. The diff needs to look like legislation, where deletions are struck through and additions are underlined.

Here’s all the steps to make this work. This assumes you already have a working Drupal install.

1. Drupal Computed Field module

The Computed Field module is a great concept: it executes PHP code to populate a new field with calculations based on other fields or any other data accessible to the PHP engine. Since the module can execute any PHP script, you can actually do anything available to the PHP system or Drupal API upon node save. It doesn’t have to save values to a field.

Computed Field for Drupal 6 has rough edges, however. It has been stuck on beta 1 for 7 months, and its MySql’s longtext field type is broken (I found a workaround).

How to configure the module:

  • Create a new Computed Field type in your node with Store using the database settings below set to varchar with a large enough Data Length to prevent data overflow errors. (This is the workaround to the broken longtext field.)
  • Put this code in the Computed Code field:
    $path = ‘/pathToPear’sParentDirectory/pear/PEAR’;
    set_include_path(get_include_path() . PATH_SEPARATOR . $path);
    require_once ‘PEAR.php’;
    include_once “Text/Diff.php”;
    include_once “Text/Diff/Renderer.php”;
    include_once “Text/Diff/Renderer/inline.php”;

    $diff = &new Text_Diff(‘auto’, array(array($node->field_nameOfOneFieldToCompare[0][‘value’]), array($node->field_nameOfOtherFieldToCompare[0][‘value’])));
    $renderer = &new Text_Diff_Renderer_inline();

    $node_field[0][‘value’] = $renderer->render($diff);

TechRepublic confused me: they got the Text_Diff constructor signature wrong in Compare file contents and render the output with PHP and PEAR. You don’t pass two files, you pass a string and an array. I credit them, however, for pointing me to the inline renderer.

2. Install your own PEAR

Dreamhost’s main PEAR install is out of date. It cannot install up to dateĀ  PEAR components such as Text_Diff 1.1.0.

Solution: install your own PEAR.

I used http://pear.php.net/go-pear. Save that page as go-pear.php in a pear directory off your account’s home directory (if you’re not sure, get to it with cd ~ from the command line). Run it from the command line using php -q go-pear.php.

I accepted all defaults.

It will instruct you to fix your php.ini. You may not need to do anything; see the optional section below.

3. Install Text_Diff

As simple as pear install pear/Text_Diff. You may need to prefix the pear executable with the path to your new install so you don’t run Dreamhost’s old install.

OPTIONAL: Override Dreamhost’s PHP configuration

Dreamhost runs PHP in CGI mode. That gives security and usability improvements, but it disallows local php.ini files or the php_value include_path “path statement goes here in the .htaccess file.

To change values in the php.ini, you must either use PHP’s set_include_path or override Dreamhost’s master php.ini.

I chose set_include_path. I probably won’t have many PEAR-dependent computed fields, so this is easy to maintain.

However, if you will use PEAR a lot, you may want to override the php.ini. Use the Custom php.ini across Multiple domains section as it is the most flexible solution.

A pitfall with overriding the php.ini is you won’t get php.ini changes made by Dreamhost. I just checekd, and the last update was only 5 days ago. While I can manage my own php.ini, I use a hosting provider because I’d rather let someone else handle infrastructure and operations.

The result

Field A: The quick brown fox jumped over the lazy dog.

Field B: The red fox is awesome.

Difference (auto generated): The quick brownred fox jumped over the lazy dogis awesome.

Finally, Palm OS is dead!

Palm finally did what they should have done years ago: they killed Palm OS.

Palm OS was junk when it came out and outclassed even by Windows Mobile 2002. Thankfully, it’s gone.

It’s about time!

(OK, technically Palm OS isn’t dead. Palm sold the software to a Japanese company a few years ago and licenses it back. When the largest licensee ditches an uncompetitve product, the product’s probably dead.)

Bye, bye 1and1.com

[CORRECTION: I lost no prepaid domain registration time. Dreamhost’s domain transfer requires purchase of a 1 year additional registration on top of existing registration. Existing registration time is retained.]

1and1.com lost my business.

Yesterday, that web host screwed up my hosting package, causing a multi-hour email and web outage.

Being sick of 1and1’s routine incompetence, I already plotted my escape. I changed settings so my domains would no longer auto-renew. I probably had $30-$35 of prepaid domain registration time left with the 6 domains I am keeping, so I figured I would keep them registered at 1and1 and transfer later.

Instead, 1and1 screwed up all my DNS settings and initiated a total package cancellation, causing a major service outage.

This was the last straw, so I expedited my move to Dreamhost.

I am almost running again. Let me know if you got any bounces on emails sent to me.

Even though Dreamhost has a mixed reputation, it can’t be worse than 1and1.com. Some of my web apps run noticably more quickly. And their support staff responded with a coherent answer. Wow!

Wikipedia wastes my time

Wikipedia logo
Wikipedia logo

Last night, I finally gave up Wikipedia editing. It’s not worth it.

Wikipeida is a bona fide nonprofit, and work on it is charitable. But what makes charitable work “worth it”? Here’s a few reasons:

  1. Have a connection. Charity work with a group of friends counts, as does charity work for an organization in which I have a relationship.
  2. Get value out of it. I like my volunteering with Boy Scouts and my neighborhood association because it’s as much an education for me as it is a benefit for them. Also fulfilling a religious calling is a value.
  3. Some kind of permanence. My charitable effort must make a lasting difference in someone’s life.

Wikipedia does none of these.

I have no connection. I only know two editors, and I have met netiher in person. I value relationships, but I only have “so much time” to develop them. I’m not interested in spending that scarce resource on people whose connection is only editing an encyclopeda.

I get little value out of it. I see no “higher purpose” merit. Sure, maybe a little entertainment on the debates, maybe a little pride in knowing I affected some articles. But whatever value I get is totally offset by the lack of permanence described below.

Among Wikipedia’s largest flaws is the lack of authority. Any clown can destroy your changes. Content that is both not part of common sense of laymen and not easily verifiable will be destroyed by successive edits.

I think it was Science magazine that found that Wikipedia is remarkably accurate for scientific articles. Maybe so, but it’s only because the facts are so easily verifiable. The accuracy and verifiability of other articles are debatable. I’ve especially noticed this in articles with a political slant; way too often they conform to how political authorities market things in ways they aren’t.

Good bye Wikipedia. It was interesting, but you’re not worth my time.

Prediction: Microsoft Money is dead

Microsoft Money Plus Deluxe
Microsoft Money Plus Deluxe

Last Wednesday, Microsoft announced the end of Microsoft Money’s annual release cycle.

I think Microsoft actually killed it. Here’s why:

  1. The announcement was routed through a fanboy, not an employee. Bob Peel is a “MVP,” which really means he is not an employee and donates a lot of time to Microsoft. (As much as I like Microsoft products, I think it’s absurd to donate time to a for profit corporation.)
  2. “Customer feedback” apparently convinced Microsoft that yearly upgrades weren’t helpful. I have no idea why they suddenly realized this. The microsoft.public.money news group has lambasted the annual upgrades’ minimal net value for years.
  3. The current version, released in mid 2007, is Money Plus. Normally it would be named Money 2008. By removing the date, the current product’s name won’t expire.
  4. Microsoft discontinued retail sales. Why would you abdicate retail sales channels to Quicken? Because your product is dead. All future sales will be over download channels.

I suspect Money was unprofitable. The frequency of and poor added value of upgrades suggests that Microsoft’s true goal was milking the revenue out of a dead product. Money Plus is so long in the tooth–it’s slow, its database is terribly inefficient, and it doesn’t work well with other products–that it needs a huge rewrite.

What’s in store for Microsoft Money? Probably nothing, at least not anything I can install on my computer. If it has any future, Money will probably morph into a web site. But knowing how poorly Microsoft does web products, don’t hold your breath.

My recommendation? The desktop version is dead, and Microsoft sucks at online services, so explore alternatives.

Quicken is more primitive than Money, but Intuit promised major enhancements for the upcoming Quicken 2009. Barring that, there’s online services like wesabe.com and mint.com.