You are here

function _patterns_scan_analyze_patternscan in Patterns 7.2

Same name and namespace in other branches
  1. 7 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

5 calls to _patterns_scan_analyze_patternscan()
PatternsParserTestCase::analyze_scan in tests/parser/parser.test
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
_patterns_scan_validate_patternscan in includes/parser/scan.inc
Analyze the result of a call to patterns_scan_pattern, and check whether the pattern was valid.

File

includes/parser/scan.inc, line 152
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 SEMANTIC VALIDATION:
  // This level ensures the pattern will run without execution errors

  ////////////////////////////
  if ($level < PATTERNS_VALIDATE_TAG_SYNTAX) {
    return $msgs;
  }
  $tag_errors = NULL;

  //Go through all the semantic warnings performing a code-description conversion
  if (count($patternscan['tag_syntactic_warnings']) > 0) {
    $tag_errors = $patternscan['tag_syntactic_warnings'];
  }
  else {
    if (count($patternscan['tag_syntactic_errors']) > 0) {
      $tag_errors = $patternscan['tag_syntactic_errors'];
    }
  }
  if (isset($tag_errors)) {
    $warnings = '';
    foreach ($tag_errors as $warn_data) {
      $warnings .= '[' . $warn_data['action'] . ':' . $warn_data['tag'] . '] ';
      $warnings .= $warn_data['msg'] . $br;
    }
    $msgs[] = t('Tag syntactic errors/warnings: ') . $br . $warnings;
  }

  // FULL SEMANTIC VALIDATION:
  // This level ensures the pattern will run without execution errors

  ////////////////////////////
  if ($level < PATTERNS_VALIDATE_SEMANTIC) {
    return $msgs;
  }

  //Go through all the semantic warnings performing a code-description conversion
  if (count($patternscan['tag_semantic_warnings']) > 0) {
    $warnings = '';
    foreach ($patternscan['tag_semantic_warnings'] as $warn_data) {
      $warnings .= '[' . $warn_data['action'] . ':' . $warn_data['tag'] . '] ';
      $type = key($warn_data['warning']);
      switch ($type) {
        case -1:
          $warnings .= t('[Already defined element]: ');
          break;
        case -2:
          $warnings .= t('[Undefined element]: ');
          break;
        case -3:
          $warnings .= t('[Unmet dependency]: ');
          break;
        case -4:
          $warnings .= t('[Remaining dependency]: ');
          break;
        case -5:
          $warnings .= t('[Not unique alias]: ');
          break;
      }
      $warnings .= reset($warn_data['warning']) . $br;
    }
    $msgs[] = t('Tag semantic warnings: ') . $br . $warnings;
  }
  return $msgs;
}