You are here

function _advagg_locale_js_alter in Advanced CSS/JS Aggregation 7.2

Implements hook_js_alter().

This is a locking wrapper for locale_js_alter().

1 call to _advagg_locale_js_alter()
_advagg_locale_changed_files in ./advagg.advagg.inc
If the locale module is enabled regenerate locale translations.

File

./advagg.module, line 692
Advanced CSS/JS aggregation module.

Code

function _advagg_locale_js_alter(&$js) {

  // If the variable is empty then get the latest variable from the database.
  $name = 'javascript_parsed';
  $parsed = variable_get($name, array());
  if (empty($parsed)) {
    $variables = array_map('unserialize', db_query('SELECT name, value FROM {variable} WHERE name = :name', array(
      ':name' => $name,
    ))
      ->fetchAllKeyed());
    if (!empty($variables[$name])) {
      $GLOBALS['conf'][$name] = $variables[$name];
    }
  }

  // See if locale_js_alter() needs to do anything.
  $dir = 'public://' . variable_get('locale_js_directory', 'languages');
  $new_files = FALSE;

  // See if a rebuild of the translation file for the current language is
  // needed.
  if (!empty($parsed['refresh:' . $GLOBALS['language']->language])) {
    $new_files = TRUE;
  }

  // Check for new js source files.
  if (empty($new_files)) {
    foreach ($js as $item) {
      if ($item['type'] === 'file' && !in_array($item['data'], $parsed) && substr($item['data'], 0, strlen($dir)) != $dir) {
        $new_files = TRUE;
        break;
      }
    }
  }
  if (empty($new_files)) {

    // No new files to manage, just add in available i18n files.
    advagg_locale_js_add_translations($js, $dir);

    // Exit function.
    return;
  }
  $count = 0;
  while (!lock_acquire('locale_js_alter', 10)) {
    ++$count;

    // If we've waited over 3 times then skip.
    if ($count > 3) {
      lock_release('locale_js_alter');

      // Add in available i18n files.
      advagg_locale_js_add_translations($js, $dir);

      // Disable saving to the cache as translations might be missing.
      drupal_page_is_cacheable(FALSE);
      if (variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) > 1) {
        $GLOBALS['conf']['advagg_cache_level'] = 0;
      }
      return;
    }

    // Wait for the lock to be available.
    lock_wait('locale_js_alter');
  }
  try {

    // Run the alter.
    locale_js_alter($js);
  } catch (PDOException $e) {

    // If it fails we don't care, javascript_parsed is either already written or
    // it will happen again on the next request.
    // Still log it if in development mode.
    if (variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) < 0) {
      watchdog('advagg', 'Development Mode - Caught PDO Exception: <code>@info</code>', array(
        '@info' => $e,
      ));
    }
  }
  lock_release('locale_js_alter');
}