Drush PHP Memory Limit

I was banging my head against the wall this morning because Drush kept telling me that I was exhausting PHP's memory at 128MB. I finally figured out the problem and thought I'd share for others searching the internet for a solution.

The first thing I did was check my php.ini file. Sometimes PHP on the command line will use a different php.ini file than the Apache version. In my case, the php.ini file is shared for both.

Render a Single Field in Drupal 7

I needed a simple method to render a single field from a node object in Drupal 7, but couldn't find any good answers by searching the web.

Here is the solution I finally came to by searching the documentation:

  // Render the field named "field_image"
  $field = field_view_value('node', $node, 'field_image', $node->field_image[$node->language][0]);
  print render($field);

Command line bookmarks / shortcut script

I grew tired of typing some of the same long commands into my terminal; especially ssh commands to log into a myriad of remote machines. I needed a way to create bookmarks or shortcuts to these long commands that I use on a regular basis. So several month ago I decided solving this problem would be a good opportunity to dabble into some Ruby (which I'd been meaning to do for a long time).

The script is freely available on Github.com: https://github.com/nrambeck/Go

I was able to find a ruby command-line template as a starting point (I can't recall where I got the template from).

Copying objects in PHP 5 using clone

In PHP 4 object variables were simple. Assigning one object to another worked exactly as you'd expect.

$object1->title = "Title One";
$object2 = $object1;
$object2->title = "Title Two";
print $object1->title; // returns "Title One"

You could make a copy of the original, and modify your copy with full assurance that your original would remain untouched.

That changed in PHP 5. Now when you assign an object to a new variable.

Views Index Hint module

On one of my projects, I cam across a certain performance edge case on a site with a large volume of data. For whatever reason, the MySQL optimizer refused to use the fastest index in a handful of views queries, causing these queries to be painfully slow.

Here is a simplified example of a query generated by views:

SELECT nid, title FROM node WHERE status <> 0 AND type IN ("story") ORDER BY created DESC;

When viewing the explain plan for this query, it shows my that MySQL is choosing to use the node_type index instead of node_created.

Drush command to close comments

There is a great module called Comment Closer that allows you to automatically close comments on certain nodes after a certain time period.

On a recent project, needed to come up with a solution to close comments apart from a cron job and without adding yet another module to the site.

I decided to make use of drush and created my first drush command.

With the drush file properly installed, I can now execute the following command from the site root directory:

drush comment-close --days=7 --types=story,blog

This command will close comments on all story and blog nodes that are older than 7 days.

Drupal Camp Indy

The folks over in Indiana are organizing a Drupal Camp on October 23. Hope you can make it.

http://drupalcampindy.org/

DROP all MySQL tables from the command line

I had a situation today where I wanted to drop all the tables in a database, but didn't have access to a UI like phpMyAdmin. The easiest thing to do would have been to drop the entire database then re-create it, but I didn't have permissions to drop an entire database.

After searching around online, this is the best solution I found:

Export a single row from a mysql table

While it doesn't happen that often, occasionally I'll need to export a certain subset of data from a MySQL table instead of the entire dataset. You can do this with mysqldump using the --where option which allows you to apply a WHERE clause that filters the data in your tables based on a certain condition.

Let's say in Drupal, you'd like to copy a single comment from one table into another.

Drupal Performance: Pagination and COUNT queries

Drupal's core pagination system does not scale well on large, high-traffic sites because of it's need to count the total number of items in the list. This involves wrapping the original query around a COUNT query like below.

SELECT COUNT(*) FROM (SELECT nid FROM node);

Count queries are usually very fast for tables using the MyISAM engine, but most high-traffic Drupal sites use the InnoDB engine for most of the tables.