CeltigirlDesigns

Blog Entry - 16 Jan 2011

Drupal Search Module Causing Cron Job Failure

Challenge: Drupal's internal search module does not index all pages and causes the cron job to fail.
Solution: It turns out that the drupal_goto() statement was the culprit.

For several years, the search functionality was not enabled on my client's website. However, search functionality was wanted for the redesign, so I enabled Drupal's core search module. After that, the cron job failed every night. I knew that the search module was the problem because the Search settings administration page indicated that only 40% of the site had been indexed.

In googling the problem, I found that other people reported search module failure due to bad PHP code, duplicate form names, as well as when the search indexing encountered the drupal_goto() statement. I did find a few duplicate form names, but once those were resolved, the search indexing still never completed. The drupal_goto() statement was used all over the site, so I was really hoping to find a solution that did not involve find & replace.

To be honest, I was a bit put out that the drupal_goto() statement was the issue because it is the recommended redirect approach from the "official" Drupal documentation on forms. Why would the developers recommend a method that breaks a core module??? In any case, the answer I settled on was to surround the drupal_goto() code in the following if statement:

<?php
if (arg(0) == 'node') {
 if (!isset($_SESSION['USERID'])) {
  drupal_goto('login_page');
 }
}
?>

The key is the " if (arg(0) == 'node')" condition, as the search module returns FALSE when indexing the page from the cron job. It's a very simple solution, but it requires a lot of cut & paste if you have many PHP forms on your site. To find out which of your pages has PHP, login to your database from phpMyAdmin or a MySQL command line and enter:

SELECT * FROM YOUR_DATABASE_NAME.'node_revisions' WHERE 'format' = '3';

There are two discussion threads at Drupal.org that cover this topic in gory detail:

Please email me at celtigirl@gmail.com with any questions or comments about the code. I look forward to your letters.

Back to the Main Blog Page