You are here

protected function UpgradeStatusForm::buildEnvironmentChecks in Upgrade Status 8.2

Same name and namespace in other branches
  1. 8.3 src/Form/UpgradeStatusForm.php \Drupal\upgrade_status\Form\UpgradeStatusForm::buildEnvironmentChecks()

Builds a list of environment checks.

Return value

array Build array.

1 call to UpgradeStatusForm::buildEnvironmentChecks()
UpgradeStatusForm::buildForm in src/Form/UpgradeStatusForm.php
Form constructor.

File

src/Form/UpgradeStatusForm.php, line 548

Class

UpgradeStatusForm

Namespace

Drupal\upgrade_status\Form

Code

protected function buildEnvironmentChecks() {
  $header = [
    'requirement' => [
      'data' => $this
        ->t('Requirement'),
      'class' => 'requirement-label',
    ],
    'status' => [
      'data' => $this
        ->t('Status'),
      'class' => 'status-info',
    ],
  ];
  $build['data'] = [
    '#type' => 'table',
    '#header' => $header,
    '#rows' => [],
  ];

  // Check Drupal version. Link to update if available.
  $core_version_info = [
    '#type' => 'markup',
    '#markup' => $this
      ->t('Version @version and up to date.', [
      '@version' => \Drupal::VERSION,
    ]),
  ];
  $has_core_update = FALSE;
  $core_update_info = $this->releaseStore
    ->get('drupal');
  if (isset($core_update_info['releases']) && is_array($core_update_info['releases'])) {

    // Find the latest release that are higher than our current and is not beta/alpha/rc.
    foreach ($core_update_info['releases'] as $version => $release) {
      $major_version = explode('.', $version)[0];
      if (version_compare($version, \Drupal::VERSION) > 0 && empty($release['version_extra']) && $major_version === '8') {
        $link = $core_update_info['link'] . '/releases/' . $version;
        $core_version_info = [
          '#type' => 'link',
          '#title' => $this
            ->t('Version @current allows to upgrade but @new is available.', [
            '@current' => \Drupal::VERSION,
            '@new' => $version,
          ]),
          '#url' => Url::fromUri($link),
        ];
        $has_core_update = TRUE;
        break;
      }
    }
  }
  if (version_compare(\Drupal::VERSION, '8.8.0') >= 0) {
    if (!$has_core_update) {
      $class = 'no-known-error';
    }
    else {
      $class = 'known-warnings';
    }
  }
  else {
    $class = 'known-errors';
  }
  $build['data']['#rows'][] = [
    'class' => $class,
    'data' => [
      'requirement' => [
        'class' => 'requirement-label',
        'data' => $this
          ->t('Drupal core should be 8.8.x or 8.9.x'),
      ],
      'status' => [
        'data' => $core_version_info,
        'class' => 'status-info',
      ],
    ],
  ];

  // Check PHP version.
  $version = PHP_VERSION;
  $build['data']['#rows'][] = [
    'class' => [
      version_compare($version, '7.3.0') >= 0 ? 'no-known-error' : 'known-errors',
    ],
    'data' => [
      'requirement' => [
        'class' => 'requirement-label',
        'data' => $this
          ->t('PHP version should be at least 7.3.0'),
      ],
      'status' => [
        'data' => $this
          ->t('Version @version', [
          '@version' => $version,
        ]),
        'class' => 'status-info',
      ],
    ],
  ];

  // Check database version.
  $database = \Drupal::database();
  $type = $database
    ->databaseType();
  $version = $database
    ->version();

  // MariaDB databases report as MySQL. Detect MariaDB separately based on code from
  // https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Database%21Driver%21mysql%21Connection.php/function/Connection%3A%3AgetMariaDbVersionMatch/9.0.x
  // See also https://www.drupal.org/node/3119156 for test values.
  if ($type == 'mysql') {

    // MariaDB may prefix its version string with '5.5.5-', which should be
    // ignored.
    // @see https://github.com/MariaDB/server/blob/f6633bf058802ad7da8196d01fd19d75c53f7274/include/mysql_com.h#L42.
    $regex = '/^(?:5\\.5\\.5-)?(\\d+\\.\\d+\\.\\d+.*-mariadb.*)/i';
    preg_match($regex, $version, $matches);
    if (!empty($matches[1])) {
      $type = 'MariaDB';
      $version = $matches[1];
      $requirement = $this
        ->t('When using MariaDB, minimum version is 10.3.7');
      if (version_compare($version, '10.3.7') >= 0) {
        $class = 'no-known-error';
      }
      elseif (version_compare($version, '10.1.0') >= 0) {
        $class = 'known-warnings';
        $requirement .= ' ' . $this
          ->t('Alternatively, <a href=":driver">install the MariaDB 10.1 driver for Drupal 9</a> for now.', [
          ':driver' => 'https://www.drupal.org/project/mysql56',
        ]);
      }
      else {
        $class = 'known-errors';
        $requirement .= ' ' . $this
          ->t('Once updated to at least 10.1, you can also <a href=":driver">install the MariaDB 10.1 driver for Drupal 9</a> for now.', [
          ':driver' => 'https://www.drupal.org/project/mysql56',
        ]);
      }
    }
    else {
      $type = 'MySQL or Percona Server';
      $requirement = $this
        ->t('When using MySQL/Percona, minimum version is 5.7.8');
      if (version_compare($version, '5.7.8') >= 0) {
        $class = 'no-known-error';
      }
      elseif (version_compare($version, '5.6.0') >= 0) {
        $class = 'known-warnings';
        $requirement .= ' ' . $this
          ->t('Alternatively, <a href=":driver">install the MySQL 5.6 driver for Drupal 9</a> for now.', [
          ':driver' => 'https://www.drupal.org/project/mysql56',
        ]);
      }
      else {
        $class = 'known-errors';
        $requirement .= ' ' . $this
          ->t('Once updated to at least 5.6, you can also <a href=":driver">install the MySQL 5.6 driver for Drupal 9</a> for now.', [
          ':driver' => 'https://www.drupal.org/project/mysql56',
        ]);
      }
    }
  }
  elseif ($type == 'pgsql') {
    $type = 'PostgreSQL';
    $requirement = $this
      ->t('When using PostgreSQL, minimum version is 10 <a href=":trgm">with the pg_trgm extension</a> (The extension is not checked here)', [
      ':trgm' => 'https://www.postgresql.org/docs/10/pgtrgm.html',
    ]);
    $class = version_compare($version, '10') >= 0 ? 'no-known-error' : 'known-errors';
  }
  elseif ($type == 'sqlite') {
    $type = 'SQLite';
    $requirement = $this
      ->t('When using SQLite, minimum version is 3.26');
    $class = version_compare($version, '3.26') >= 0 ? 'no-known-error' : 'known-errors';
  }
  $build['data']['#rows'][] = [
    'class' => [
      $class,
    ],
    'data' => [
      'requirement' => [
        'class' => 'requirement-label',
        'data' => [
          '#type' => 'markup',
          '#markup' => $requirement,
        ],
      ],
      'status' => [
        'data' => $type . ' ' . $version,
        'class' => 'status-info',
      ],
    ],
  ];

  // Check Apache. Logic is based on system_requirements() code.
  $request_object = \Drupal::request();
  $software = $request_object->server
    ->get('SERVER_SOFTWARE');
  if (strpos($software, 'Apache') !== FALSE && preg_match('!^Apache/([\\d\\.]+) !', $software, $found)) {
    $version = $found[1];
    $class = [
      version_compare($version, '2.4.7') >= 0 ? 'no-known-error' : 'known-errors',
    ];
    $label = $this
      ->t('Version @version', [
      '@version' => $version,
    ]);
  }
  else {
    $class = '';
    $label = $this
      ->t('Version cannot be detected or not using Apache, check manually.');
  }
  $build['data']['#rows'][] = [
    'class' => $class,
    'data' => [
      'requirement' => [
        'class' => 'requirement-label',
        'data' => $this
          ->t('When using Apache, minimum version is 2.4.7'),
      ],
      'status' => [
        'data' => $label,
        'class' => 'status-info',
      ],
    ],
  ];

  // Check Drush. We only detect site-local drush for now.
  if (class_exists('\\Drush\\Drush')) {
    $version = call_user_func('\\Drush\\Drush::getMajorVersion');
    $class = [
      version_compare($version, '10') >= 0 ? 'no-known-error' : 'known-errors',
    ];
    $label = $this
      ->t('Version @version', [
      '@version' => $version,
    ]);
  }
  else {
    $class = '';
    $label = $this
      ->t('Version cannot be detected, check manually.');
  }
  $build['data']['#rows'][] = [
    'class' => $class,
    'data' => [
      'requirement' => [
        'class' => 'requirement-label',
        'data' => $this
          ->t('When using Drush, minimum version is 10'),
      ],
      'status' => [
        'data' => $label,
        'class' => 'status-info',
      ],
    ],
  ];
  return $build;
}