You are here

function node_accessibility_drush_callback_validate in Node Accessibility 7

Drush command callback.

Import nodes from data.

1 string reference to 'node_accessibility_drush_callback_validate'
node_accessibility_drush_command in ./node_accessibility.drush.inc
Implements hook_drush_command().

File

./node_accessibility.drush.inc, line 121
Defines the accessibilty page drush functions.

Code

function node_accessibility_drush_callback_validate() {
  $uid = drush_get_option('uid');
  $nids = array();
  $rows = array();
  $delete = TRUE;
  $revisions = TRUE;
  $chunk_size = drush_get_option('chunk');
  $node_type_settings = node_accessibility_load_node_type_settings();
  $methods = quail_api_get_validation_methods(NULL);
  if (is_null(drush_get_option('delete'))) {
    $delete = FALSE;
  }
  if (is_null(drush_get_option('revisions'))) {
    $revisions = FALSE;
  }
  if (empty($chunk_size) || !is_numeric($chunk_size)) {
    $chunk_size = 256;
  }
  if ($uid != 0) {
    if (is_null($uid)) {
      $uid = 1;
    }
    global $user;
    $user = user_load($uid);
  }
  if (drush_get_option('all')) {
    $query = db_select('node', 'n');
    $query
      ->fields('n', array(
      'nid',
    ));
    try {
      $rows = $query
        ->execute()
        ->fetchAllAssoc('nid');
    } catch (Exception $e) {
      if (module_exists('cf_error')) {
        cf_error::on_query_execution($e);
      }
    }
  }
  else {
    $node_type = drush_get_option('type');
    if (empty($node_type)) {
      $nids = array_filter(func_get_args(), 'is_numeric');
    }
    else {
      $query = db_select('node', 'n');
      $query
        ->fields('n', array(
        'nid',
      ));
      $query
        ->condition('type', check_plain($node_type), '=');
      try {
        $rows = $query
          ->execute()
          ->fetchAllAssoc('nid');
      } catch (Exception $e) {
        if (module_exists('cf_error')) {
          cf_error::on_query_execution($e);
        }
      }
    }
  }
  foreach ($rows as $row) {
    $nids[] = $row->nid;
  }
  unset($rows);

  // break the nids array into chunks of $chunk_size to reduce memory footprint when validating large quantities of nodes.
  $total_nodes = count($nids);
  $current_count = 0;
  if ($delete) {
    drush_print("Now deleting validation results for " . $total_nodes . " nodes in chunks of " . $chunk_size . ".");
  }
  else {
    drush_print("Now validating " . $total_nodes . " nodes in chunks of " . $chunk_size . ".");
  }
  while ($current_count < $total_nodes) {
    $current_nids = array();
    $counter = 0;
    if ($current_count + $chunk_size > $total_nodes) {
      $chunk_size = $total_nodes - $current_count;
    }
    while ($counter < $chunk_size) {
      $current_nids[] = array_pop($nids);
      $counter++;
    }
    $vid_query = db_select('node', 'n');
    $vid_query
      ->fields('n', array(
      'nid',
      'vid',
      'type',
    ));
    $vid_query
      ->condition('n.nid', $current_nids, 'IN');
    $vid_executed = $vid_query
      ->execute();
    $nodes = $vid_executed
      ->fetchAllAssoc('nid');
    if ($delete) {
      foreach ($current_nids as $nid) {
        if ($revisions) {
          $vid = NULL;
          $vid_string = "all";
        }
        else {
          $vid = $nodes[$node->nid]->vid;
          $vid_string = $vid;
        }
        $result = node_accessibility_delete_node_problems($nid, $vid);
        if ($result === FALSE) {
          drush_set_error('DRUSH_NOT_COMPLETED', "Failed to delete validation problems for node " . $nid . ":" . $vid_string . ".");
        }
        unset($result);
      }
    }
    else {
      $vids = array();
      if ($revisions) {
        $query = db_select('node_revision', 'nr');
        $query
          ->fields('nr');
        $query
          ->condition('nr.nid', $current_nids, 'IN');
        $executed = $query
          ->execute();
        $results = (array) $executed
          ->fetchAll();
        foreach ($results as $key => &$value) {
          if (!is_array($vids[$value->nid])) {
            $vids[$value->nid] = array();
          }
          if (!in_array($value->vid, $vids[$value->nid])) {
            $vids[$value->nid][] = $value->vid;
          }
        }
      }
      else {
        foreach ($nodes as $nid => $node) {
          $vids[$nid] = array(
            $node->vid,
          );
        }
      }
      $results = node_accessibility_perform_validation($current_nids, $vids, NULL, NULL);
      if ($result === FALSE) {
        drush_set_error('DRUSH_NOT_COMPLETED', "Failed to perform accessibility validation.");
      }
      else {
        foreach ($current_nids as $nid) {
          $vid = $nodes[$nid]->vid;
          $type = $nodes[$nid]->type;
          $reports = $results[$nid][$vid]['report'];
          $node_settings = $node_type_settings[$type];
          $database = isset($methods[$node_settings['method']]['database']) ? $methods[$node_settings['method']]['database'] : FALSE;
          if ($database && !empty($reports) && isset($results[$nid][$vid]['report'])) {
            $no_failures = TRUE;
            foreach ($reports as $severity => $severity_results) {
              if (isset($severity_results['total']) && $severity_results['total'] > 0) {
                $no_failures = FALSE;
                break;
              }
            }
            if ($no_failures) {
              node_accessibility_delete_node_problems($nid, $vid);
            }
            else {
              node_accessibility_save_node_problems($nid, $vid, $reports);
            }
          }
        }
      }
      unset($results);
    }
    drush_print("  [ " . ($current_count + count($current_nids)) . " / " . $total_nodes . " ]");
    $current_count += $chunk_size;
  }
  drush_print("Operation complete.");
}