You are here

function advagg_insert_update_files in Advanced CSS/JS Aggregation 7.2

Insert/Update data in the advagg_files table.

Parameters

array $files: List of files in the aggregate including files meta data.

string $type: String; css or js.

Return value

bool Return TRUE if anything was written to the database.

3 calls to advagg_insert_update_files()
advagg_insert_update_db in ./advagg.inc
Insert/Update data in advagg tables.
advagg_push_new_changes in ./advagg.cache.inc
Flush the correct caches so CSS/JS changes go live.
advagg_update_7220 in ./advagg.install
Update the advagg_files table; use_strict column might have been incorrect.

File

./advagg.inc, line 157
Advanced CSS/JS aggregation module.

Code

function advagg_insert_update_files(array $files, $type) {

  // Record if a database write was done.
  $write_done = FALSE;
  $filename_hashes = array();
  foreach ($files as $file_meta_data) {
    $filename_hashes[] = $file_meta_data['filename_hash'];
  }
  $files_in_db = array();
  if (!empty($filename_hashes)) {
    $query = db_select('advagg_files', 'af')
      ->fields('af')
      ->condition('filename_hash', $filename_hashes)
      ->execute();
    foreach ($query as $row) {
      $files_in_db[$row->filename] = (array) $row;
    }
  }

  // Make drupal_get_installed_schema_version() available.
  include_once DRUPAL_ROOT . '/includes/install.inc';
  foreach ($files as $filename => $file_meta_data) {

    // Create record.
    $record = array(
      'filename' => $filename,
      'filename_hash' => $file_meta_data['filename_hash'],
      'content_hash' => $file_meta_data['content_hash'],
      'filetype' => $type,
      'filesize' => $file_meta_data['filesize'],
      'mtime' => $file_meta_data['mtime'],
      'linecount' => $file_meta_data['linecount'],
    );
    try {

      // Check the file in the database.
      if (empty($files_in_db[$filename])) {

        // Add in filesize_processed if the schema is 7210 or higher.
        if (drupal_get_installed_schema_version('advagg') >= 7210) {
          $record['filesize_processed'] = (int) advagg_generate_filesize_processed($filename, $type);
        }

        // Add in use_strict if the schema is 7212 or higher.
        if (drupal_get_installed_schema_version('advagg') >= 7212) {
          $record['use_strict'] = 0;
          if ($type === 'js') {
            $record['use_strict'] = (int) advagg_does_js_start_with_use_strict($filename);
          }
        }

        // Insert into database.
        $record['changes'] = 1;
        $return = db_merge('advagg_files')
          ->key(array(
          'filename_hash' => $record['filename_hash'],
        ))
          ->insertFields($record)
          ->execute();
        if ($return) {
          if (variable_get('advagg_debug', ADVAGG_DEBUG) >= 2) {
            $variables = array(
              '@record' => print_r($record, TRUE),
            );
            watchdog('advagg-debug', 'Inserting into db <pre>@record</pre>.', $variables, WATCHDOG_DEBUG);
          }
          $write_done = TRUE;
        }
      }
      else {

        // Take changes counter out of the diff equation.
        $changes = $files_in_db[$filename]['changes'];
        unset($files_in_db[$filename]['changes']);

        // If not in strict mode, only use mtime if newer than the existing one.
        if (!variable_get('advagg_strict_mtime_check', ADVAGG_STRICT_MTIME_CHECK)) {

          // Make sure mtime only moves forward.
          if ($record['mtime'] <= $files_in_db[$filename]['mtime']) {
            $record['mtime'] = $files_in_db[$filename]['mtime'];
          }
        }

        // If something is different, update.
        $diff = array_diff_assoc($record, $files_in_db[$filename]);
        if (!empty($diff)) {
          $diff['changes'] = $changes + 1;
          $diff['filename_hash'] = $record['filename_hash'];

          // Add in filesize_processed if the schema is 7210 or higher.
          if (drupal_get_installed_schema_version('advagg') >= 7210) {
            $diff['filesize_processed'] = (int) advagg_generate_filesize_processed($filename, $type);
          }
          if (drupal_get_installed_schema_version('advagg') >= 7212) {
            $diff['use_strict'] = 0;
            if ($type === 'js') {
              $diff['use_strict'] = (int) advagg_does_js_start_with_use_strict($filename);
              if (empty($diff['use_strict'])) {
                $diff['use_strict'] = 0;
              }
            }
          }
          $return = db_merge('advagg_files')
            ->key(array(
            'filename_hash' => $diff['filename_hash'],
          ))
            ->fields($diff)
            ->execute();
          if ($return) {
            if (variable_get('advagg_debug', ADVAGG_DEBUG) >= 2) {
              $variables = array(
                '@diff' => print_r($diff, TRUE),
              );
              watchdog('advagg-debug', 'Updating db <pre>@diff</pre>.', $variables, WATCHDOG_DEBUG);
            }
            $write_done = TRUE;
          }
        }
      }
    } catch (PDOException $e) {

      // If it fails we don't care, the file was added to the table by another
      // process then.
      // 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,
        ));
      }
    }
  }
  return $write_done;
}