Drupal Modules

I recently completed a project for a customer that afforded me the opportunity to dive into Drupal on better terms. On my first introduction to the software several years back (and Joomla for that matter) I determined I would be able to use my cross-browser theming skills more effectively if I chose WordPress as a base CMS.

This time around I was able to spend a considerable chunk of time reading the documentation (especially around the Theming Guide) and examples on other’s sites. Some topics online seemed incomplete so I did end up reviewing some chapters in a few books on Drupal 7 in particular. In my mind it seems like the developers of WordPress and Drupal are migrating toward each other in feature sets, but delivering solutions from different perspectives.

WordPress appears to be geared more toward out of the box social publishing, with an easy installer, and an easy updater. It can be pushed in almost any direction, but doesn’t seem to be the goal of the software creators. Drupal on the other hand feels more like a collection of building blocks for a web database. While Drupal has fields, blocks, regions, views, and fine grained permissions with roles… URL aliases require a module to automate their selection. There’s even a module to hook up Filemaker Pro and Drupal.

Back to the modules:

I utilized the following:

For spam free contact forms (almost):

For easy page editing:

For dynamic content:

For development and style:

Post via script

I just whipped up some PHP and threw it in the root of a brand new WordPress 2.9.2 install:

<?php
include_once('wp-config.php');
include_once('wp-load.php');
include_once('wp-includes/wp-db.php');
 
$my_post = array();
$my_post['post_title'] = 'My post';
$my_post['post_content'] = 'This is my post.';
$my_post['post_status'] = 'publish';
$my_post['post_author'] = 1;
 
wp_insert_post( $my_post );
?>

Voila. The post appeared when navigating to the .php file.

See: http://codex.wordpress.org/Function_Reference/wp_insert_post.

For more creativity, get into the database: http://codex.wordpress.org/Function_Reference/wpdb_Class

Recursive find and replace

From time to time I find myself wanting to replace a bit of text, in multiple files, throughout a huge directory tree (particularly in the case of a WordPress migration).

Here’s a method I frequently use (other examples I’ve seen out there make use of perl, xargs, grep, etc.):

user@host:~$ cd Directory_To_Start_From
user@host:~$ find . -type f -exec sed -i 's/Text_To_Find/Replacement_Text/g' {} ;

The find command will return a list of all the files in the directory tree and execute the sed command on each one it locates. In the example below, I added the “-name” option to the find command to allow for working only on files with the .js extension:

user@host:~$ cd website
user@host:~$ find . -type f -name "*.js" -exec sed -i 's/var pageName="example page";/var pageName="Final Page";/g' {} ;

In the next one, the text to find and replace has special characters (in this case the ‘/’ in the URL).

user@host:~$ cd website
user@host:~$ find . -type f -exec sed -i 's/http://www.example.com/old_directory//http://newsubdomain.example.com/new_directory//g' {} ;

See the following links to read more about escaping special characters:

Databases on 1and1

After wanting to test WordPress 3.0 on a 1and1 hosting account, I ran into the following issue… the particular setup I was dealing with had only one database available, and it wasn’t MySQL 5, but instead MySQL 4… furthermore, it was a version lower than 4.1.2, which more recent versions of WordPress require as a minimum.

So after doing a bit of research on what others had done, I went about fixing the problem. Interestingly enough, 1and1 could have made some money in this situation if they would allow customers to simply purchase another database to add to their packages, but no, they want an entire upgrade to be purchased.

The first step was to backup the website and database. I ssh’ed into the web host:

username@localhostname:~$ ssh $username@example.com

Then I backed up the database. The command below will dump all of the databases on the host (in this case, only one MySQL 4.x database) into a dated bzip file, in the home directory.

username@remotehostname:~$ mysqldump -C -A -u $databaseUsername -h $databaseHostName -p$databasePassword | bzip2 -cq9 &gt; ~/`date +%F-%I-%M-%p`-db-backup.sql.bz2

Now to backup the site itself (not only in case disaster struck, but this would get a local copy of the sql dump I just made as well). On a local OS X workstation (or Ubuntu, if that suits your tastes) I ran something similar to the following:

username@localhostname:~$ mkdir ~/website_backups
username@localhostname:~$ rsync -avz --exclude="logs" $username@example.com: ~/website_backups/

From there, I was able to log into the 1and1 control panel and delete the existing database. This allowed me to setup a new one, and in particular, choose MySQL 4 or 5 as the type.

Back to the 1and1 hosting account:

username@localhostname:~$ ssh $username@example.com

I uncompressed the sql dump:

username@remotehostname:~$ bunzip2 2010-01-14-01-59-PM-db-backup.sql.bz2

and was greeted with the raw sql in the file, “2010-01-14-01-59-PM-db-backup.sql”. From here it was only a hop skip and a jump away to restoration. It was necessary to edit the sql file in order to have it restore properly to the newly created database that was just created:

username@remotehostname:~$ nano -w 2010-01-14-01-59-PM-db-backup.sql

And I changed the $oldDatabaseName to $newDatabaseName.

--
-- Current Database: `$oldDatabaseName`
--
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `$oldDatabaseName`;
USE `$oldDatabaseName`;

ctrl+o, enter, and ctrl+x, to save the file and exit nano. The sql dump was now ready to restore to the new database.

username@remotehostname:~$ mysql -u $databaseUsername -h $databaseHostName -p$databasePassword $databaseName &lt; 2010-01-14-01-59-PM-db-backup.sql

The only other things to do were to update any existing applications that needed the new database name, username, hostname, and password.

As a note, if you are updating a WordPress install to point to a new database, this info can be changed in the file, wp-config.php.

Connecting MySQL with OpenOffice Base

OpenOffice Base makes it incredibly easy to hook up to a MySQL database. Once this is accomplished you can easily create a front end to browse through your data, edit it, report on it, etc. Below are a few steps I took to create a functional setup:

What I did was:

  1. Have OpenOffice installed
  2. Access to a database (for this example I used my wp_posts table from WordPress) from the workstation running OpenOffice
  3. Downloaded MySQL® Connector/J and installed
    1. Within OpenOffice clicked, “Tools > Options > OpenOffice.org > Java > Class Path… > Add Archive…”
    2. Selected “mysql-connector-java-5.1.5-bin.jar” or current version
    3. Restarted OpenOffice (had to make sure the process was fully “killed”)
  4. Created a new database within OpenOffice
    1. Within OpenOffice, clicked “File > New > Database”
    2. clicked the “Connect to an existing database” radio button
    3. selected “MySQL” from the drop down list
    4. clicked the “Next >>” button
    5. selected the “Connect using JDBC (Java Database Connectivity)” radio button
    6. clicked the “Next >>” button
    7. entered the “Name of the database” into the text field
    8. entered the MySQL “Server URL” into the text field
    9. clicked the “Test Class” button and received “The JDBC driver was loaded successfully”
    10. clicked the “Next >>” button
    11. entered as “User name” into the text field
    12. checked the “Password required” checkbox
    13. clicked the “Finish” button and save
  5. Made a form based on wp_posts:
    1. This screenshot is of the form I made to browse the raw data of my WordPress posts from my Thoughts and Notions weblog and is showing data from this post in particular:
    2. OpenOffice Base wp_posts

reCAPTCHA plugin error messages

By default, on two of my installs of reCAPTCHA Plugin for WordPress – v2.7, I receive no error message if the captcha is entered incorrectly… the user of the weblog is to assume that they made a mistake I guess…

That is until I wrote this script to check for error messages being passed around on the URL by the plugin:

if (window.location.search != "") {
	var searchArray = window.location.search.split("&amp;");
	for (var i=0; i &lt; searchArray.length; i++) {
		var searchSubArray = searchArray[i].split(&quot;=&quot;);
		if (searchSubArray[0] == &quot;rerror&quot; &amp;&amp; 
		searchSubArray[1] == &quot;incorrect-captcha-sol&quot;) {
			document.write(&quot;<p>The two words in the picture were 
			typed incorrectly.</p>");
		}				
	}
}

Just drop that snippet into your “comments.php” template. When the captcha is filled in incorrectly, the plugin adds “rerror=incorrect-captcha-sol” to the url. This checks for that string, and if found, writes an error message in red.