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.

Today’s Gallup poll on abortion

An interesting Gallup poll on abortion was released today. Pro-life Americans surged recently. I think it’s a reaction to our recent abortion-friendly ideology shift.

I found two things more interesting.

First is how only 22% want a total abortion ban:
abortion_circumstances

This is anther example of where theĀ Texas Republican Party platform, which also calls for a total abortion ban, is out of step with reality.

The second interesting part is the stances of women and men:
women_abortion

men_abortion

They’re hardly different: within 5 points either way.

Pro-choicers complain that men are telling women what to do with their bodies. In other words, they say men cannot regulate female-specific issues. Sorry, but the numbers show women and men have roughly equivalent positions! If you substituted male legislatures with female legislatures, you would get the same results.

Swine flu? Let’s call it bacon lung!

baconI like the name “swine flu.”

Why? Some think that pork meat harbors the flu. Sorry, doesn’t work that way.

But I like this misperception. If people stop buying pork, that means a glut of bacon, which means lower bacon prices, which means MORE BACON FOR AREN! Bacon breakfast, bacon sandwiches, bacon ice cream, bacon, bacon, bacon!

Heck, let’s rename the flu. Call it “bacon lung.”

Mmm, bacon.

This year’s lent fast

I didn’t want to repeat the problems with last year’s Lent fast, where I was enmeshed with legalism and others felt like they needed to accommodate me.

I made it simple: no snacks. I eat at meals and that’s it.

I allowed myself exceptions only to avoid creating a burden on others. Because of that, I did break the fast four times.

But the good thing is nobody realized I did a fast until it was over. Not even my wife. That’s a good Lent fast.

Maybe next year I’ll devise a better fast that needs no exceptions?

9 reasons to stop Dallas’s convention center hotel

Dallas' convention center hotelDallas’s proposed city-owned convention center hotel is 9 mistakes wrapped in one package:

  1. Fiscally stupid. The convention center loses $3 million a year. The hotel costs half a billion dollars. That’s 166 years of convention center losses! So you say I should consider return on investment, but…
  2. Success not assured. The convention industry’s future is uncertain. In addition to broad economic pressure, telepresence technology is rapidly maturing, and the green movement frowns upon travel.
  3. It’s a distraction. City resources used developing and monitoring the hotel come from other city functions.
  4. Can hurt taxpayers. Even though revenue bonds finance the hotel, they are still backed by Dallas taxpayers. If the hotel can’t pay the bonds, taxpayers will!
  5. Can hurt credit rating. If taxpayers must pay the bonds, that effectively increases Dallas’s indebtedness, hurting credit ratings. Credit ratings affect how much Dallas can borrow. Since Dallas puts almost any improvement on the “bond credit card,” good credit ratings are critical.
  6. If it was so assured, a private company would have done it already. We have many wealthy developers with access to vast amounts of capital. They would have already done such an assured project.
  7. It’s socialism. The city has no business competing in a well-functioning, established private market.
  8. Ugly as hell. Looks like those ’50s-style “office buildings of the future”:

    conventioncenterhotel2
    Looks like some of those old, goofy office buildings.
  9. Harlan Crow is not evil. Yes, Harlan Crow is financing virtually all the anti-hotel effort. But he’s done a lot of good for Dallas. And he’s right.

Please join me in voting YES on Dallas Proposition 1. Stop the hotel. It’s 9 mistakes wrapped in one package.