From 1f306847fd55cd405bf7b6b86ed2261e70a2d329 Mon Sep 17 00:00:00 2001
From: Thomas Skovgaard Gielfeldt <thomas@gielfeldt.com>
Date: Mon, 7 Jan 2013 11:04:25 +0100
Subject: [PATCH] Issue #1829832 by gielfeldt: Cron detection works in mysterious ways

---
 boost.admin.expiration.inc |   12 ++++++++++++
 boost.module               |   26 +++++++++++++++++++++++---
 2 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/boost.admin.expiration.inc b/boost.admin.expiration.inc
index fb306ae..bee1819 100644
--- a/boost.admin.expiration.inc
+++ b/boost.admin.expiration.inc
@@ -29,6 +29,18 @@ function boost_admin_expiration_settings() {
     '#default_value' => variable_get('boost_expire_cron', BOOST_EXPIRE_CRON),
     '#description' => t('If enabled, each time cron runs Boost will check each cached page and delete those that have expired (maximum cache lifetime). The expiration time is displayed in the comment that Boost adds to the bottom of the html pages it creates. This setting is recommended for most sites.'),
   );
+  $form['boost_cron_script_name'] = array(
+    '#type' => 'textfield',
+    '#title' => t('File name of cron script.'),
+    '#default_value' => variable_get('boost_cron_script_name', BOOST_CRON_SCRIPT_NAME),
+    '#description' => t('Enter here the name of the script used for cron (usually cron.php). This is used by Boost to determine whether or not the cache flush has been invoked by cron.'),
+  );
+  $form['boost_cron_drush_commands'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Cron drush commands.'),
+    '#default_value' => variable_get('boost_cron_drush_commands', BOOST_CRON_DRUSH_COMMANDS),
+    '#description' => t('Enter here a comma separated list of the names of the drush commands used for cron (usually core-cron). This is used by Boost to determine whether or not the cache flush has been invoked by cron through drush.'),
+  );
 
   // reset htaccess on submit;
   $form['#submit'][] = 'boost_form_submit_handler';
diff --git a/boost.module b/boost.module
index d4825ad..fa4e0da 100644
--- a/boost.module
+++ b/boost.module
@@ -52,6 +52,16 @@ define('BOOST_IGNORE_FLUSH', TRUE);
 define('BOOST_EXPIRE_CRON', TRUE);
 
 /**
+ * Default value for cron script name.
+ */
+define('BOOST_CRON_SCRIPT_NAME', 'cron.php');
+
+/**
+ * Default value for cron drush commands.
+ */
+define('BOOST_CRON_DRUSH_COMMANDS', 'core-cron');
+
+/**
  * Default etag settings.
  */
 define('BOOST_APACHE_ETAG', 3);
@@ -362,9 +372,19 @@ function boost_flush_caches() {
   // check to avoid caching requests from the CLI
   $_boost = boost_transform_url();
 
-  // The lock_may_be_available() checks to see if the flush was requested by
-  // the core cron, since we may want to ignore it (boost_ignore_flush)
-  if (isset($_boost['base_dir']) && (lock_may_be_available('cron') || variable_get('boost_ignore_flush', BOOST_IGNORE_FLUSH) == FALSE)) {
+  // Since we may want to ignore flushing (boost_ignore_flush), we check to
+  // see if it was requested by cron
+  if (drupal_is_cli() && function_exists('drush_get_command')) {
+    // Cron invoked from Drush
+    $command = drush_get_command();
+    $commands = explode(',', variable_get('boost_cron_drush_commands', BOOST_CRON_DRUSH_COMMANDS));
+    $cron_invoked = in_array($command['command'], $commands);
+  }
+  else {
+    // Cron invoked by webserver
+    $cron_invoked = basename($_SERVER['PHP_SELF']) == variable_get('boost_cron_script_name', BOOST_CRON_SCRIPT_NAME);
+  }
+  if (isset($_boost['base_dir']) && (!$cron_invoked || variable_get('boost_ignore_flush', BOOST_IGNORE_FLUSH) == FALSE)) {
     $count = _boost_rmdir($_boost['base_dir'], TRUE);
     watchdog('boost', 'Flushed all files (%count) from static page cache.', array('%count' => $count), WATCHDOG_NOTICE);
   }
-- 
1.7.0.4
