You are here

cdn.install in CDN 6.2

Same filename and directory in other branches
  1. 8.3 cdn.install
  2. 7.2 cdn.install

Install file.

File

cdn.install
View source
<?php

/**
 * @file
 * Install file.
 */

/**
 * Implementation of hook_install().
 */
function cdn_install() {

  // CDN must run after JavaScript aggregator (weight 9999) in fallback mode.
  db_query("UPDATE {system} SET weight = 10000 WHERE name = 'cdn'");
  drupal_rebuild_theme_registry();
}

/**
 * Implementation of hook_uninstall().
 */
function cdn_uninstall() {
  db_query("DELETE FROM {variable} WHERE name LIKE 'cdn_%%'");
}

/**
 * Implementation of hook_requirements().
 */
function cdn_requirements($phase) {
  $requirements = array();
  $t = get_t();

  // We can't use module_load_include() when we're in the install phase (i.e.
  // when the module is being installed through an install profile).
  require_once dirname(__FILE__) . '/cdn.module';
  require_once dirname(__FILE__) . '/cdn.requirements.inc';

  // Detect whether the fallback should be enabled or not. Next, get the
  // integration that's being used: fallback, PressFlow, Cocomore or
  // core patch.
  _cdn_requirements_detect_fallback();
  $integration_method = _cdn_requirements_get_integration_mechanism();
  switch ($phase) {
    case 'runtime':
      module_load_include('module', 'cdn');

      // CDN  status.
      $status = variable_get(CDN_STATUS_VARIABLE, CDN_DISABLED);
      $mode = variable_get(CDN_MODE_VARIABLE, CDN_MODE_BASIC);
      $requirements['cdn']['title'] = $t('CDN');

      // Set the basic info: disabled/testing/enabled.
      if ($status == CDN_DISABLED) {
        $requirements['cdn'] += array(
          'description' => $t('CDN integration is disabled for all users.'),
          'severity' => REQUIREMENT_WARNING,
        );
        $requirements['cdn']['value'] = $t('Disabled');
      }
      elseif ($status == CDN_TESTING) {
        $requirements['cdn'] += array(
          'description' => $t('CDN integration is only enabled for users with the
            %cdn-testing-mode-permission permission', array(
            '%cdn-testing-mode-permission' => CDN_PERM_ACCESS_TESTING,
          )),
          'severity' => REQUIREMENT_WARNING,
        );
        $requirements['cdn']['value'] = $t('Testing');
      }
      else {
        $requirements['cdn'] += array(
          'description' => $t('CDN integration is enabled for all users.'),
          'severity' => REQUIREMENT_OK,
        );
        $requirements['cdn']['value'] = $t('Enabled');
      }

      // When enabled, add more information.
      if ($status != CDN_DISABLED) {
        if ($mode == CDN_MODE_BASIC) {
          $requirements['cdn']['value'] .= ' – ' . t('Origin Pull mode');
          $farfuture = variable_get(CDN_BASIC_FARFUTURE_VARIABLE, CDN_BASIC_FARFUTURE_DEFAULT);
          $preprocess_css = variable_get('preprocess_css', FALSE);
          if (!$preprocess_css && !module_exists('advagg')) {
            $args = array(
              '!preprocess-css-link' => l('"Aggregate and compress CSS files"', 'admin/settings/performance', array(
                'fragment' => 'bw-optimizations',
              )),
            );
            if ($farfuture) {
              $requirements['cdn']['description'] = t('CSS aggregation is disabled. Files referenced by CSS files
                will fail to load!<br />
                Please enable the !preprocess-css-link performance setting.', $args);
              $requirements['cdn']['severity'] = REQUIREMENT_ERROR;
            }
            else {
              $requirements['cdn']['description'] = t('CSS aggregation is disabled. Files referenced by CSS files
                such as background images and fonts will <em>not</em> be
                served from the CDN!<br />
                Please consider enabling the !preprocess-css-link performance
                setting.', $args);
              $requirements['cdn']['severity'] = REQUIREMENT_WARNING;
            }
          }
          if ($farfuture) {
            $requirements['cdn']['value'] .= ' (' . t('with Far Future expiration') . ')';
          }
          else {
            $requirements['cdn']['value'] .= ' (' . t('without Far Future expiration') . ')';
          }
        }
        else {
          $requirements['cdn']['value'] .= ' – ' . t('File Conveyor mode');
          $items = array();
          $daemon_pid_file = variable_get(CDN_ADVANCED_PID_FILE_VARIABLE, FALSE);
          $synced_files_db = variable_get(CDN_ADVANCED_SYNCED_FILES_DB_VARIABLE, FALSE);
          if ($synced_files_db !== FALSE) {
            $persistent_data_db = str_replace(CDN_DAEMON_SYNCED_FILES_DB, CDN_DAEMON_PERSISTENT_DATA_DB, $synced_files_db);
            $drupal_root_path = variable_get(CDN_DRUPAL_ROOT_VARIABLE, realpath('.'));
            $synced_files_db_exists = file_exists($synced_files_db);
            $synced_files_db_readable = @fopen($synced_files_db, 'r');
            $persistent_data_db_exists = file_exists($persistent_data_db);
            $persistent_data_db_readable = @fopen($persistent_data_db, 'r');
            $daemon_pid_file_exists = file_exists($daemon_pid_file);
            cdn_load_include('advanced');
            $db = _cdn_advanced_get_db_connection();
            if ($db !== FALSE) {
              $input_file_mask = $drupal_root_path . '%';
              $sql = "SELECT COUNT(*) AS count, server\n                      FROM synced_files\n                      WHERE input_file LIKE :input_file\n                      GROUP BY server";
              $stmt = $db
                ->prepare($sql);
              $stmt
                ->bindParam(':input_file', $input_file_mask, PDO::PARAM_STR);
              $stmt
                ->execute();
              $result = $stmt
                ->fetchAll();
              $synced_file_stats = $result;
            }
            if ($persistent_data_db_exists && $persistent_data_db_readable) {
              try {
                $db = new PDO('sqlite:' . $persistent_data_db);
              } catch (PDOException $e) {
                $items[] = t("Could not connect to persistent data database.");
              }
              if ($db !== FALSE) {
                $sql = "SELECT COUNT(*)\n                        FROM pipeline_queue";
                $stmt = $db
                  ->prepare($sql);
                $stmt
                  ->execute();
                $result = $stmt
                  ->fetchAll();
                $pipeline_queue_count = $result[0][0];
                $sql = "SELECT COUNT(*)\n                        FROM pipeline_list";
                $stmt = $db
                  ->prepare($sql);
                $stmt
                  ->execute();
                $result = $stmt
                  ->fetchAll();
                $pipeline_list_count = $result[0][0];
              }
            }
            $items[] = $synced_files_db_exists ? t('The synced files database exists.') : t("The synced files database doesn't exist.");
            $items[] = $synced_files_db_readable ? t('The synced files database is readable.') : t("The synced files database isn't readable.");
            if ($synced_files_db_readable) {
              foreach ($synced_file_stats as $row) {
                $items[] = t('!synced-file-count files have been synced to the %server server.', array(
                  '!synced-file-count' => $row['count'],
                  '%server' => $row['server'],
                ));
              }
            }
            else {
              $items[] = t("Number of synced files is unknown.");
            }
            $items[] = $daemon_pid_file_exists ? t('File Conveyor is currently running.') : '<strong>' . t('File Conveyor is currently not running.') . '</strong>';
            if (isset($pipeline_queue_count)) {
              $items[] = t("!pipeline-queue-count files are waiting to be synced.", array(
                '!pipeline-queue-count' => $pipeline_queue_count,
              ));
              $items[] = t("!pipeline-list-count files are currently being synced.", array(
                '!pipeline-list-count' => $pipeline_list_count,
              ));
            }

            // If either of these 3 checks failed, mark this requirement's
            // severity as being an error.
            if (!($synced_files_db_exists && $synced_files_db_readable && $daemon_pid_file_exists)) {
              $requirements['cdn']['severity'] = REQUIREMENT_ERROR;
            }
          }
          else {
            $items[] = t('The synced files database setting has not yet been configured.');
            $requirements['cdn']['severity'] = REQUIREMENT_ERROR;
          }
          $requirements['cdn']['description'] .= '<br />' . theme('item_list', $items);
        }
      }

      // Finally, add very brief info about the integration mechanism in use.
      switch ($integration_method) {
        case 'fallback':
          $requirements['cdn']['value'] .= ' — ' . $t('Fallback mechanism');
          $requirements['cdn']['description'] .= '<br /><br />' . t('If you want complete CDN coverage, you should either apply the included Drupal core patch or switch to <a href="http://pressflow.org/">Pressflow</a>.');
          break;
        case 'pressflow':
          $requirements['cdn']['value'] .= ' — ' . $t('Pressflow');
          break;
        case 'cocomore':
          $requirements['cdn']['value'] .= ' — ' . $t('Cocomore');
          break;
        case 'core patch':
          $requirements['cdn']['value'] .= ' — ' . $t('Core patch');
          break;
      }
    case 'install':

      // Only check if the CDN Drupal core patch is properly applied if this
      // Drupal installation is not a patched Drupal distribution. Show as a
      // warning when the integration method is 'fallback'.
      if (_cdn_requirements_is_patched_distribution() === FALSE) {
        _cdn_requirements_generate_requirement_for_patch($requirements, 'core', $integration_method == 'fallback' ? $t('CDN — Drupal core patch (for complete CDN coverage)') : $t('CDN — Drupal core patch'), REQUIREMENT_WARNING);
      }
  }
  return $requirements;
}

//----------------------------------------------------------------------------

// Update hooks.

/**
 * In version 2 of the CDN integration module, basic mode is more flexible
 * than in version 1: there now is the ability to have multiple CDN URLs.
 */
function cdn_update_6200() {
  $url = variable_get('cdn_basic_url', '');
  $exts = variable_get('cdn_basic_extensions', '');

  // Store URL variable in new format, then delete old variables.
  $cdn_basic_mapping = $url;
  if (!empty($exts)) {
    $cdn_basic_mapping .= '|' . $exts;
  }
  variable_set('cdn_basic_mapping', $url . '|' . $exts);
  variable_del('cdn_basic_url');
  variable_del('cdn_basic_extensions');
  $ret[] = array(
    'success' => TRUE,
    'query' => 'Updated variables to allow for multiple Origin Pull CDN URLs instead of just one.',
  );
  return array();
}

/**
 * More consistent variable names: upgrade path.
 */
function cdn_update_6201() {
  $ret = array();
  $cdn_exception_path_blacklist = variable_get('cdn_exclusion', NULL);
  variable_del('cdn_exclusion');
  if (isset($cdn_exception_path_blacklist)) {
    variable_set('cdn_exception_path_blacklist', $cdn_exception_path_blacklist);
  }
  $cdn_exception_auth_users_blacklist = variable_get('cdn_exclusion_logged_in', NULL);
  variable_del('cdn_exclusion_logged_in');
  if (isset($cdn_exception_auth_users_blacklist)) {
    variable_set('cdn_exception_auth_users_blacklist', $cdn_exception_auth_users_blacklist);
  }
  $ret[] = array(
    'success' => TRUE,
    'query' => 'Updated variables (internal name changes only, for improved consistency).',
  );
  return $ret;
}

/**
 * CDN must run after JavaScript aggregator (weight 9999) in fallback mode.
 */
function cdn_update_6202() {
  $ret = array();
  $ret[] = update_sql("UPDATE {system} SET weight = 10000 WHERE name = 'cdn'");

  // update.php runs drupal_rebuild_theme_registry();
  return $ret;
}

/**
 * More consistent variable names: upgrade path.
 */
function cdn_update_6203() {
  $ret = array();
  $ret[] = update_sql("UPDATE {variable} SET name = 'cdn_exception_file_path_blacklist' WHERE name = 'cdn_exception_path_blacklist'");
  $ret[] = update_sql("UPDATE {variable} SET name = 'cdn_exception_file_path_whitelist' WHERE name = 'cdn_exception_path_whitelist'");
  return $ret;
}

/**
 * New: File Conveyor PID file variable.
 */
function cdn_update_6210() {
  $ret = array();
  if (variable_get('cdn_mode', FALSE) == 'advanced') {
    $synced_files_db = variable_get('cdn_advanced_synced_files_db', FALSE);
    $daemon_pid_file = str_replace('synced_files.db', 'daemon.pid', $synced_files_db);
    variable_set('cdn_advanced_pid_file', $daemon_pid_file);
    $ret[] = array(
      'success' => TRUE,
      'query' => 'Created File Conveyor PID file variable.',
    );
  }
  return $ret;
}

Functions

Namesort descending Description
cdn_install Implementation of hook_install().
cdn_requirements Implementation of hook_requirements().
cdn_uninstall Implementation of hook_uninstall().
cdn_update_6200 In version 2 of the CDN integration module, basic mode is more flexible than in version 1: there now is the ability to have multiple CDN URLs.
cdn_update_6201 More consistent variable names: upgrade path.
cdn_update_6202 CDN must run after JavaScript aggregator (weight 9999) in fallback mode.
cdn_update_6203 More consistent variable names: upgrade path.
cdn_update_6210 New: File Conveyor PID file variable.