You are here

function _patterns_scan_analyze_patternscan in Patterns 7

Same name and namespace in other branches
  1. 7.2 includes/parser/scan.inc \_patterns_scan_analyze_patternscan()

Analyze the result of a call to patterns_scan_pattern, and check whether the pattern was valid.

Parameters

array $analysis : The analysis array obtained from patterns_scan_pattern.

bool $include (optional): If TRUE, it means that the pattern to scan is an include, and a laxer scan is performed. E.g. Info section can be missing, actions can be outside of a section. Default FALSE.

integer $level (optional) : The level of of the analysis. Defaults PATTERNS_VALIDATE_ALL

Return value

TRUE if valid, FALSE otherwise

6 calls to _patterns_scan_analyze_patternscan()
PatternsParserTestCase::basic_checks in tests/parser/parser.test
patterns_enable_pattern in ./patterns.module
Form constructor for the Patterns enabling form.
patterns_import_validate in includes/forms/import.inc
patterns_validate_service in includes/parser/parser.inc
Callback of the url patterns/validate.
_patterns_io_import_validate_content in includes/io/import.inc

... See full list

File

includes/parser/scan.inc, line 147
Helping functions for the parser.

Code

function _patterns_scan_analyze_patternscan($patternscan, $include = FALSE, $level = PATTERNS_VALIDATE_ALL, $br = '<br/>') {
  $msgs = array();
  if ($patternscan['info'] == 0 && !$include) {
    $msgs[] = t('The info section is missing.');
  }
  if ($patternscan['info'] > 1) {
    $msgs[] = t('Pattern can contain only one \'info\' section.');
  }
  if ($patternscan['modules'] > 1) {
    $msgs[] = t('Pattern can contain only one \'modules\' section.');
  }
  if (count($patternscan['other_sections']) == 0 && !$include) {
    $msgs[] = t('Pattern does not contain any actions.');
  }
  if (count($patternscan['generic_errors']) != 0) {
    $msgs[] = t('Generic errors in the patterns were found. Probably a tag was misplaced. Please verify: !found', array(
      '!found' => implode(', ', $patternscan['generic_errors']),
    ));
  }
  if (count($patternscan['invalid_actions']) != 0) {
    $invalidactions = array();
    foreach ($patternscan['invalid_actions'] as $key => $value) {
      $invalidactions[] = $value['key'];
    }
    $msgs[] = t('Only %actions are valid actions. Found: %found.', array(
      '%actions' => implode(', ', patterns_actions()),
      '%found' => implode(', ', $invalidactions),
    ));
  }
  if (count($patternscan['extra_actions']) != 0) {
    $extraactions = array();
    foreach ($patternscan['extra_actions'] as $key => $value) {
      $extraactions[] = $value['key'];
    }
    $msgs[] = t('Extra actions have been found on one level: %found.', array(
      '%found' => implode(', ', $extraactions),
    ));
  }

  // TODO: This is not yet working properly. Check when it is applicable!
  if (count($patternscan['missing_tag']) != 0) {
    foreach ($patternscan['missing_tag'] as $key => $value) {
      $msgs[] = t('A mandatory \'tag\' was missing for action %action.', array(
        '%action' => $value['key'],
      ));
    }
  }
  if (count($patternscan['empties']) > 0) {
    $msgs[] = t('Pattern contains empty sections or actions:') . implode(', ', $patternscan['empties']);
  }

  // INCLUDE LEVEL

  ////////////////
  if ($level < PATTERNS_VALIDATE_INCLUDE) {
    return $msgs;
  }
  if (count($patternscan['include_scans']) > 0) {
    foreach ($patternscan['include_scans'] as $i) {

      //$msgs[] = print_r($patternscan['includes'], true);
      $msgs = array_merge($msgs, _patterns_scan_analyze_patternscan($i, TRUE));
    }
  }

  // TAG EXISTS LEVEL

  ///////////////////
  if ($level < PATTERNS_VALIDATE_TAG_EXISTS) {
    return $msgs;
  }
  if (count($patternscan['unknown_tag']) != 0) {
    $utags = implode(', ', $patternscan['unknown_tag']);
    $msgs[] = t('The following unknown tags were found: %utags.', array(
      '%utags' => $utags,
    ));
  }

  // FULL VALIDATION

  ///////////////////
  if ($level < PATTERNS_VALIDATE_ALL) {
    return $msgs;
  }
  if (count($patternscan['tag_errors']) != 0) {
    $errors = '';
    foreach ($patternscan['tag_errors'] as $error) {
      $errors .= '[' . $error['action'] . '|' . $error['tag'] . '] ' . $error['msg'] . '</br>';
    }
    $msgs[] = t('Components reported errors about tags: ') . $br . $errors;
  }
  return $msgs;
}