You are here

function patterns_io_scan_directories in Patterns 7

Same name and namespace in other branches
  1. 7.2 includes/io/io.inc \patterns_io_scan_directories()

Scan directories looking for patterns files.

Checks inside the directories defined by patterns_config_get_paths().

Parameters

boolean $verbose if TRUE, displays on the screen information: about the scan.

Return value

array An associative array of informative messages

See also

patterns_config_get_paths()

patterns_io_analyze_scandir_messages()

1 call to patterns_io_scan_directories()
_patterns_io_get_patterns in includes/io/io.inc
Loads all the available patterns from the database. It also checks against the file system, and, if the patterns global configuration options allow it, updates the database with the latest modifications. Notice: enabled patterns will not be updated…

File

includes/io/io.inc, line 93
Functions related to input/output operations.

Code

function patterns_io_scan_directories($verbose = FALSE) {
  $existing = patterns_db_get_patterns_array('name', TRUE);

  // Patterns already enabled won't be updated in the database
  $enabled = patterns_db_get_enabled_patterns_array('name', TRUE);
  $messages = array();
  $messages['errors'] = array();
  $messages['skipped'] = array();
  $messages['permission'] = array();
  $messages['found'] = array();
  $messages['updated'] = array();
  $messages['error_found'] = array();
  $messages['error_updated'] = array();

  // Get list of directories to scan for patterns.
  $patterns_paths = patterns_path_get_patterns_dirs();

  // Get valid file extensions.
  $mask = '/.\\.(' . implode('|', patterns_parser_get_formats()) . ')$/';

  // Prepare list of files/folders to be excluded.
  // 'enabled' - Don't save enabled pattern backups.
  $no_mask = array(
    '.',
    '..',
    'CVS',
    '.svn',
    '.git',
    '.bzr',
    'enabled',
  );
  foreach ($patterns_paths as $path) {
    foreach (file_scan_directory($path, $mask, $no_mask) as $file) {
      $format = substr($file->filename, strlen($file->name) + 1);
      if (in_array($file->filename, $existing)) {
        if (!variable_get('patterns_update_db_from_fs', TRUE)) {
          continue;
        }
        if (!patterns_db_is_pattern_updated($file->filename)) {

          // the database is already updated to the most recent version
          continue;
        }
        if (!empty($enabled)) {
          if (in_array($file->filename, $enabled)) {
            $messages['skipped'][] = $file->filename;
            continue;

            // Skip updating enabled patterns.
          }
        }
      }
      if (!is_readable($file->uri)) {
        $messages['permission'][] = $file->filename;
        continue;
      }

      // Choose appropriate function based on the file extension.
      // Can be FALSE, if no parser is found.
      $load_function = patterns_parser_get_parser_function($format, PATTERNS_PARSER_LOAD);

      // Load and save pattern.
      if (!$load_function || !($pattern = $load_function($file->uri))) {
        $messages['errors'][] = $file->filename;
        continue;
      }
      if (!patterns_validate_pattern($pattern, $format, PATTERNS_VALIDATE_SYNTAX)) {
        $messages['invalid'][] = $file->filename;
        continue;
      }

      // If everything was fine save it to the database
      $result = patterns_db_save_pattern($pattern, $file->uri, $file->filename, $format);
      if ($result && !in_array($file->filename, $existing)) {
        $messages['found'][] = $file->filename;
        continue;
      }
      if (!$result && !in_array($file->filename, $existing)) {
        $messages['error_found'][] = $file->filename;
        continue;
      }
      if ($result && in_array($file->filename, $existing)) {
        $messages['updated'][] = $file->filename;
        continue;
      }
      if (!$result && in_array($file->filename, $existing)) {
        $messages['error_updated'][] = $file->filename;
        continue;
      }
    }
  }
  variable_set('patterns_loaded', time());
  if ($verbose) {
    _patterns_io_analyze_scandir_messages($messages);
  }
  return $messages;
}