You are here

mongodb.install in MongoDB 7

Same filename and directory in other branches
  1. 8 mongodb.install
  2. 6 mongodb.install

Install file for MongoDB module.

File

mongodb.install
View source
<?php

/**
 * @file
 * Install file for MongoDB module.
 */
use Alcaeus\MongoDbAdapter\TypeInterface;

/**
 * Implements hook_requirements().
 */
function mongodb_requirements($phase) {
  $extension_name = 'mongo';
  $requirements = array();

  // Ensure translations don't break at install time.
  $t = get_t();
  $requirements['mongodb_extension'] = array(
    'title' => $t('MongoDB extension (!extension)', array(
      '!extension' => $extension_name,
    )),
  );
  if (extension_loaded('mongodb')) {
    if (class_exists(MongoClient::class)) {
      $requirements['mongodb_extension'] += array(
        'value' => $t('Properly replaced'),
        'description' => $t('The mongo-php-adapter library and the ext-mongodb extension are present, replacing the ext-mongo extension.'),
        'severity' => REQUIREMENT_OK,
      );
    }
    else {
      $requirements['mongodb_extension'] += array(
        'value' => $t('Insufficient'),
        'description' => $t('The ext-mongodb extension is present, but the mongo-php-adapter is missing, so it cannot replace the old ext-mongo extension.'),
        'severity' => REQUIREMENT_ERROR,
      );
    }
  }
  elseif (!extension_loaded('mongo')) {
    if (class_exists(MongoClient::class)) {
      $requirements['mongodb_extension'] += array(
        'value' => $t('Insufficient'),
        'description' => $t('The mongo-php-adapter library is present, but the ext-mongodb is missing, so it cannot replace the old ext-mongo extension.'),
        'severity' => REQUIREMENT_ERROR,
      );
    }
    else {
      $requirements['mongodb_extension'] += array(
        'value' => $t('Insufficient'),
        'description' => $t('Neither the old ext-mongo nor the new ext-mongodb extension nor the mongo-php-adapter library are present.'),
        'severity' => REQUIREMENT_ERROR,
      );
    }
  }
  else {
    if (class_exists(TypeInterface::class)) {
      $requirements['mongodb_library_redundant'] = array(
        'title' => 'mongo-php-adapter',
        'value' => $t('Redundant'),
        'description' => $t('The mongo-php-adapter library is installed while the old ext-mongo extension is present. This does not hurt, but is somewhat redundant.'),
        'severity' => REQUIREMENT_WARNING,
      );
    }
    $extension = new ReflectionExtension($extension_name);
    $version = $extension
      ->getVersion();
    $requirements['mongodb_extension'] += array(
      'value' => $version,
      'severity' => REQUIREMENT_OK,
    );

    // Server versions prior to 1.3 do not support findAndModify command
    // needed by mongodb_next_id() function.
    $min_version = '1.3';

    // During install, the module is not yet loaded by core.
    if ($phase == 'install') {
      require_once __DIR__ . '/mongodb.module';
    }
    $db = mongodb();
    $requirements['mongodb_server'] = array(
      'title' => $t('MongoDB server'),
    );
    $return = $db
      ->execute('db.version();');
    $version = $return['retval'];
    if (version_compare($version, $min_version) == -1) {
      $requirements['mongodb_server'] += array(
        'value' => $version,
        'description' => $t('MongoDB module requires MongoDB server version @min_version or higher.', array(
          '@min_version' => $min_version,
        )),
        'severity' => REQUIREMENT_ERROR,
      );
    }
    else {
      $requirements['mongodb_server'] += array(
        'value' => $version,
        'severity' => REQUIREMENT_OK,
      );
    }
  }
  return $requirements;
}

Functions

Namesort descending Description
mongodb_requirements Implements hook_requirements().