Skip to content
boost.install 21.7 KiB
Newer Older
<?php
// $Id$

/**
 * @file
 * Handles Boost module installation and upgrade tasks.
 */

//////////////////////////////////////////////////////////////////////////////
// Core API hooks

/**
 * Implementation of hook_enable().
 */
function boost_enable() {
  drupal_set_message(t('Boost successfully installed. Please review the available <a href="@config">configuration</a> <a href="@settings">settings</a>.', array('@config' => url('admin/settings/performance'), '@settings' => url('admin/settings/performance/boost'))));

  // Forcibly disable Drupal's built-in SQL caching to prevent any conflicts of interest:
  if (variable_get('cache', CACHE_DISABLED) != CACHE_DISABLED) {
    variable_set('cache', CACHE_DISABLED);
    drupal_set_message(t('Drupal\'s standard page caching disabled by Boost.'));
  }
/**
 * Implementation of hook_disable().
 */
function boost_disable() {
  // Make sure that the static page cache is wiped when the module is disabled:
  drupal_set_message(t('Static page cache cleared.'));
}

  // Ensure that the module is loaded early in the bootstrap:
  db_query("UPDATE {system} SET weight = -90 WHERE name = '%s'", 'boost');
  variable_set('boost_crawler_key', md5(mt_rand()));
  // Create tables.
  drupal_install_schema('boost');
/**
 * Implementation of hook_uninstall().
 */
function boost_uninstall() {
  db_query("DELETE FROM {variable} WHERE name LIKE '%s_%%'", 'boost');
  cache_clear_all('variables', 'cache');
  // Delete tables.
  drupal_uninstall_schema('boost');
/**
 * Implementation of hook_requirements().
 */
function boost_requirements($phase) {
  $requirements = array();
  $t = get_t();
  switch ($phase) {
    case 'runtime':
      $cache_directories = array();
      $cache_directories[] = BOOST_FILE_PATH;
      if (BOOST_GZIP) {
        $cache_directories[] = BOOST_GZIP_FILE_PATH;
      $htaccess = file_get_contents('.htaccess');
      $char = BOOST_CHAR;
      $crawler_response = drupal_http_request(BOOST_CRAWLER_SELF);
      $crawler_response = $crawler_response->code;
      foreach($cache_directories as $cache_directory) {
        _boost_mkdir_p($cache_directory);
        $root_file = file_put_contents($cache_directory . '/' . variable_get('boost_root_file', '.boost'), $cache_directory);
        if (!is_dir($cache_directory)) {
            'title'       => $t('Boost'),
            'description' => $t('!cache_dir: does not exist.', array('!cache_dir' => $cache_directory)),
            'severity'    => REQUIREMENT_ERROR,
            'value'       => $t('Cache path'),
          );
        }
        if (!$root_file || !is_writable($cache_directory)) {
          $requirements['boost_permissions'] = array(
            'title'       => $t('Boost'),
            'description' => $t('Directory %dir credentials - Permissions: %fp. Owner %fo. Group %fg.<br /> Your credentials - Group ID: %gid. User ID: %uid. Current script owner: %user.', array('%dir' => getcwd() . '/' . $cache_directory, '%gid' => getmygid(), '%uid' => getmyuid(), '%user' => get_current_user(), '%fp' => substr(sprintf('%o', fileperms($cache_directory)), -4), '%fo' => fileowner($cache_directory), '%fg' => filegroup($cache_directory) )),
            'severity'    => REQUIREMENT_ERROR,
            'value'       => $t('Can not write to file-system'),
      if (BOOST_FILE_PATH != boost_cache_directory(NULL, FALSE)) {
          $requirements['boost_dir_exists'] = array(
            'title'       => $t('Boost'),
            'description' => $t('<a href="@url">Cache file path</a>: is not set to the default(!default). ', array('@url' => url('admin/settings/performance/boost#edit-boost-file-path'), '!default'    => boost_cache_directory(NULL, FALSE))),
            'severity'    => REQUIREMENT_WARNING,
            'value'       => $t('Cache path'),
          );
        }
      if ($htaccess && count(explode("%{REQUEST_URI}$char%{QUERY_STRING}\.html", $htaccess)) < 3) {
        $requirements['boost_htaccess'] = array(
          'title'       => $t('Boost'),
          'description' => $t('.htaccess file does not contain the boost specific rewrite rules, or the rewrite rules have changed and they need to be updated. Get rules: <a href="@url">Boost Apache .htaccess settings generation</a>.', array('@url' => url('admin/settings/performance/boost#edit-boost-verbose'))),
          'severity'    => REQUIREMENT_ERROR,
          'value'       => $t('.htaccess file'),
      if (!boost_chk_module_versions()) {
        $requirements['boost_other_modules'] = array(
          'title'       => $t('Boost'),
          'description' => $t('You need to get a newer version of <a href="@url">poormanscron</a>, most likely the latest dev.', array('@url' => "http://drupal.org/project/poormanscron")),
          'severity'    => REQUIREMENT_ERROR,
          'value'       => $t('poormanscron is the wrong version'),
        );
      }
      if (variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE) == LANGUAGE_NEGOTIATION_PATH) {
        $requirements['boost_language'] = array(
          'title'       => $t('Boost'),
          'description' => $t('Boost is not compatible with '.
            '<a href="!url">language negotiation</a> set to "language '.
            'fallback".  An anonymous user with their browser set to a '.
            "language other than the site's default language will navigate ".
            "to a non-cached page.  The page will be displayed in the user's ".
            'browser language.  Boost will cache the non-default-language '.
            'page. ', array('!url' => url('admin/settings/language/configure'))),
          'severity'    => REQUIREMENT_ERROR,
          'value'       => $t('Cannot use "language fallback" language negotiation'),
        );
      }
      if (!variable_get('boost_crawler_key', FALSE)) {
        $requirements['boost_crawler'] = array(
          'title'       => $t('Boost'),
          'description' => $t('Please Run the database update, as the crawler key is not generated yet.'),
          'severity'    => REQUIREMENT_ERROR,
          'value'       => $t('Boost Crawler Key Not Found.'),
        );
      }
      elseif ($crawler_response != 200) {
        $requirements['boost_crawler'] = array(
          'title'       => $t('Boost'),
          'description' => $t('@number returned. Crawler URL (@url) is not available, please <a href="@link">report this issue</a>', array('@number' => $crawler_response, '@url' => BOOST_CRAWLER_SELF, '@link' => 'http://drupal.org/node/add/project-issue/boost')),
          'severity'    => REQUIREMENT_ERROR,
          'value'       => $t('Boost crawler did not get a 200 response.'),
        );
      }
        $requirements['boost'] = array(
          'title'       => $t('Boost'),
          'severity'    => REQUIREMENT_OK,
          'value'       => t('Configured correctly, should be working.'),
/**
 * Check for bad versions of modules.
 */
function boost_chk_module_versions() {
  // chk poormanscron
  if (module_exists('poormanscron')) {
    $info = unserialize(db_result(db_query("SELECT info FROM {system} WHERE name LIKE 'poormanscron'")));
    if ($info['version'] == '6.x-1.0') {
      return FALSE;
    }
    else {
      return TRUE;
    }
  }
  else {
    return TRUE;
  }
}

/**
 * Implementation of hook_schema().
 */
function boost_schema() {
  $schema['boost_cache'] = array(
    'description' => t('List of the cached page'),
    'fields' => array(
      'filename' => array(
        'description' => 'Path of the cached file relative to Drupal webroot.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'base_dir' => array(
        'description' => 'Path of the cache root dir relative to Drupal webroot.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
      ),
      'expire' => array(
        'description' => t('UNIX timestamp for the expiration date of cached page.'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'lifetime' => array(
        'description' => t('Number of seconds this page should be considered fresh. Used to set the expiration column.'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => -1,
      ),
      'push' => array(
        'description' => 'A flag to indicate whether page should be crawled so it is fresh in the cache.',
        'type' => 'int',
        'size' => 'small',
        'not null' => TRUE,
        'default' => -1,
      ),
      'page_callback' => array(
        'description' => 'The name of the function that renders the page.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''
      ),
      'page_arguments' => array(
        'description' => 'The name of the content type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''
      ),
      'page_id' => array(
        'description' => 'The ID of the page.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'extension' => array(
        'description' => 'File Extension/Mime content type of this page.',
        'type' => 'varchar',
        'length' => 8,
        'not null' => TRUE,
        'default' => '',
      ),
      'timer' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Time in milliseconds that the page took to be generated.',
      ),
      'timer_average' => array(
        'type' => 'float',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Average time in milliseconds that the page took to be generated.',
      ),
      'url' => array(
        'description' => 'URL of cached page',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'indexes' => array(
      'expire' => array('expire'),
      'push' => array('push'),
      'base_dir' => array('base_dir'),
      'page_id' => array('page_id'),
      'timer' => array('timer'),
      'timer_average' => array('timer_average'),
      'page_callback' => array('page_callback'),
      'page_arguments' => array('page_arguments'),
      'extension' => array('extension'),
    ),
    'primary key' => array('filename'),
  );
  $schema['boost_cache_settings'] = array(
    'description' => t('Boost cache settings'),
    'fields' => array(
      'csid' => array(
        'description' => 'Primary Key: Unique cache settings ID.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE
      ),
      'base_dir' => array(
        'description' => 'Path of the cache root dir relative to Drupal webroot.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
      ),
      'page_callback' => array(
        'description' => 'The name of the function that renders the page.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''
      ),
      'page_arguments' => array(
        'description' => 'The name of the content type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '0'
      ),
      'page_id' => array(
        'description' => 'The ID of the page.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      'extension' => array(
        'description' => 'File Extension/Mime content type of this page.',
        'type' => 'varchar',
        'length' => 8,
        'not null' => TRUE,
        'default' => '',
      ),
      'lifetime' => array(
        'description' => t('Number of seconds this page should be considered fresh. Used to set the expiration column.'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => -1,
      ),
      'push' => array(
        'description' => 'A flag to indicate whether page should be crawled so it is fresh in the cache.',
        'type' => 'int',
        'size' => 'small',
        'not null' => TRUE,
        'default' => -1,
      ),
    ),
    'indexes' => array(
      'page_callback' => array('page_callback'),
      'page_arguments' => array('page_arguments'),
      'base_dir' => array('base_dir'),
      'page_id' => array('page_id'),
      'extension' => array('extension'),
  $schema['boost_crawler'] = array(
    'description' => t('Boost crawler - temp table'),
    'fields' => array(
      'id' => array(
        'description' => 'Primary Key: Unique ID.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE
      ),
      'url' => array(
        'description' => 'URL of page',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'primary key' => array('id'),
    'unique keys' => array(
      'url' => array('url'),
    ),
  );
  return $schema;
}

/**
 * Update 6100 - Install Boost Database.
 */
function boost_update_6100() {
  // Create tables.
  $schema['boost_cache'] = array(
    'description' => t('List of the cached page'),
    'fields' => array(
      'filename' => array(
        'description' => 'Path of the cached file relative to Drupal webroot.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'url' => array(
        'description' => 'URL of cached page',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'expire' => array(
        'description' => t('UNIX timestamp for the expiration date of cached page.'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'lifetime' => array(
        'description' => t('Number of seconds this page should be considered fresh. Used to set the expiration column.'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => -1,
      ),
      'push' => array(
        'description' => 'A flag to indicate whether page should be crawled so it is fresh in the cache.',
        'type' => 'int',
        'size' => 'small',
        'not null' => TRUE,
        'default' => -1,
      ),
      'page_callback' => array(
        'description' => 'The name of the function that renders the page.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''
      ),
      'page_arguments' => array(
        'description' => 'The name of the content type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''
      ),
    ),
    'indexes' => array(
      'expire' => array('expire'),
      'push' => array('push'),
    ),
    'primary key' => array('filename'),
  );
  $schema['boost_cache_settings'] = array(
    'description' => t('Boost cache settings'),
    'fields' => array(
      'csid' => array(
        'description' => 'Primary Key: Unique cache settings ID.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE
      ),
      'page_callback' => array(
        'description' => 'The name of the function that renders the page.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''
      ),
      'page_arguments' => array(
        'description' => 'The name of the content type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''
      ),
      'lifetime' => array(
        'description' => t('Number of seconds this page should be considered fresh. Used to set the expiration column.'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => -1,
      ),
      'push' => array(
        'description' => 'A flag to indicate whether page should be crawled so it is fresh in the cache.',
        'type' => 'int',
        'size' => 'small',
        'not null' => TRUE,
        'default' => -1,
      ),
    ),
    'indexes' => array(
      'page_callback' => array('page_callback'),
      'page_arguments' => array('page_arguments'),
    ),
    'primary key' => array('csid'),
  );

  $ret = array();
  db_create_table($ret, 'boost_cache', $schema['boost_cache']);
  db_create_table($ret, 'boost_cache_settings', $schema['boost_cache_settings']);
  return $ret;
}

/**
 * Update 6101 - Copy old variable to new one.
 */
function boost_update_6101() {
  return array(update_sql("UPDATE {variable} SET name = 'boost_enabled' WHERE name = 'boost'"));
 * Update 6102 - Delete old boost permissions variable.
  variable_del('boost_permissions');
  return array(array('success' => TRUE, 'query' => 'Old permissions variable deleted.'));

/**
 * Update 6103 - Add new columns to tables
 */
function boost_update_6103() {
  $ret = array();
  // Add in base_dir column
  db_add_field($ret, 'boost_cache', 'base_dir', array(
        'description' => 'Path of the cache root dir relative to Drupal webroot.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
      ));
  db_add_field($ret, 'boost_cache_settings', 'base_dir', array(
        'description' => 'Path of the cache root dir relative to Drupal webroot.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
      ));
  db_add_index($ret, 'boost_cache', 'base_dir', array('base_dir'));
  db_add_index($ret, 'boost_cache_settings', 'base_dir', array('base_dir'));

  // Add in page_id column
  db_add_field($ret, 'boost_cache', 'page_id', array(
        'description' => 'The ID of the page.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ));
  db_add_field($ret, 'boost_cache_settings', 'page_id', array(
        'description' => 'The ID of the page.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ));
  db_add_index($ret, 'boost_cache', 'page_id', array('page_id'));
  db_add_index($ret, 'boost_cache_settings', 'page_id', array('page_id'));

  // Add in timer column
  db_add_field($ret, 'boost_cache', 'timer', array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Time in milliseconds that the page took to be generated.',
      ));
  db_add_index($ret, 'boost_cache', 'timer', array('timer'));

  // Add in timer_average column
  db_add_field($ret, 'boost_cache', 'timer_average', array(
        'type' => 'float',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Average time in milliseconds that the page took to be generated.',
      ));
  db_add_index($ret, 'boost_cache', 'timer_average', array('timer_average'));

  // Add indexes
  db_add_index($ret, 'boost_cache', 'page_callback', array('page_callback'));
  db_add_index($ret, 'boost_cache', 'page_arguments', array('page_arguments'));

  // Set Defaults
  db_field_set_default($ret, 'boost_cache_settings', 'page_arguments', '0');

  return $ret;
}

/**
 * Update 6104 - Add new column to tables
 *  Add in mime_type column
 */
function boost_update_6104() {
  $ret = array();
  // Add in extension column
  db_add_field($ret, 'boost_cache', 'extension', array(
        'description' => 'File Extension/Mime content type of this page.',
        'type' => 'varchar',
        'length' => 8,
        'not null' => TRUE,
        'default' => '',
      ));
  db_add_field($ret, 'boost_cache_settings', 'extension', array(
        'description' => 'File Extension/Mime content type of this page.',
        'type' => 'varchar',
        'length' => 8,
        'not null' => TRUE,
        'default' => '',
      ));
  db_add_index($ret, 'boost_cache', 'extension', array('extension'));
  db_add_index($ret, 'boost_cache_settings', 'extension', array('extension'));

  return $ret;
}

/**
 * Update 6105 - Add new column to boost_cache table
 *  Add in url column
 */
function boost_update_6105() {
  $ret = array();
  db_add_field($ret, 'boost_cache', 'url', array(
        'description' => 'URL of cached page',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ));
  return $ret;
}

/**
 * Update 6106 - Add boost_crawler table to DB
 */
function boost_update_6106() {
  $schema['boost_crawler'] = array(
    'description' => t('Boost crawler - temp table'),
    'fields' => array(
      'id' => array(
        'description' => 'Primary Key: Unique ID.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE
      ),
      'url' => array(
        'description' => 'URL of page',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'primary key' => array('id'),
    'unique keys' => array(
      'url' => array('url'),
    ),
  );

  $ret = array();
  db_create_table($ret, 'boost_crawler', $schema['boost_crawler']);
  return $ret;
}

/**
 * Update 6107 - Flush core caches
 */
function boost_update_6107() {
  // Flush caches, needed since some functions got renamed.
  drupal_flush_all_caches();
  return array(array('success' => TRUE, 'query' => 'Core Caches Flushed.'));
}

/**
 * Update 6108 - Create crawler key
 */
function boost_update_6108() {
  variable_set('boost_crawler_key', md5(mt_rand()));
  return array(array('success' => TRUE, 'query' => 'Crawler key generated.'));