You are here

public function W3CProcessor::validateAllPages in W3C Validator 8

Validates all flagged pages in the limit of the number given.

File

src/W3CProcessor.php, line 172

Class

W3CProcessor
Processor for page validation.

Namespace

Drupal\w3c_validator

Code

public function validateAllPages(&$context) {
  $token = NULL;
  $user = NULL;
  $query_options = [];

  // Retrieve all pages to validate.
  $pages_to_validate = $this
    ->findAllPages();
  $context['message'] = $this
    ->t('Validating all pages ...');
  $context['sandbox']['max'] = count($pages_to_validate);
  $context['sandbox']['progress'] = 0;
  $context['results']['failures'] = 0;
  $context['results']['current_id'] = 0;
  $context['results']['processed'] = 0;

  // If we are using the "validate as user" option.
  if ($this
    ->moduleSettings()
    ->get('use_token')) {

    // Retrieve token.
    $token = $this->w3cTokenManager
      ->createAccessToken($this->currentUser);

    // Add it to query options.
    $query_options['query'] = [
      'HTTP_W3C_VALIDATOR_TOKEN' => $token,
    ];

    // Get current user.
    $user = $this->currentUser;
  }
  else {
    $user = new AnonymousUserSession();
  }

  // Validate each page one by one.
  foreach ($pages_to_validate as $page) {

    // Set validation message.
    $context['message'] = $this
      ->t('Validation for page %title...', [
      '%title' => $page['title'],
    ]);

    // Check if validation user will be able to validate the page.
    if (!$this->accessManager
      ->checkNamedRoute($page['route'], [
      'node' => isset($page['nid']) ? $page['nid'] : '',
    ], $user)) {
      $this->logger
        ->debug($page['title'] . ' at ' . $page['url'] . ' is not accessible for validation.');

      // Update operation data.
      $context['results']['failures']++;
    }
    else {

      // Valdiate the page using the specified $query_options.
      $this
        ->validatePage($page, $query_options);
    }

    // Update operation data.
    $context['sandbox']['progress']++;
    $context['results']['current_id']++;
    $context['results']['processed']++;
  }

  // Rewoke token.
  $this->w3cTokenManager
    ->rewokeAccessToken($token);
  $context['finished'] = 1;
}