Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 400 and 401]

   1  <?php
   2  /*
   3   * Copyright 2020-present MongoDB, Inc.
   4   *
   5   * Licensed under the Apache License, Version 2.0 (the "License");
   6   * you may not use this file except in compliance with the License.
   7   * You may obtain a copy of the License at
   8   *
   9   *   https://www.apache.org/licenses/LICENSE-2.0
  10   *
  11   * Unless required by applicable law or agreed to in writing, software
  12   * distributed under the License is distributed on an "AS IS" BASIS,
  13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14   * See the License for the specific language governing permissions and
  15   * limitations under the License.
  16   */
  17  
  18  namespace MongoDB\Command;
  19  
  20  use MongoDB\Driver\Command;
  21  use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
  22  use MongoDB\Driver\Server;
  23  use MongoDB\Driver\Session;
  24  use MongoDB\Exception\InvalidArgumentException;
  25  use MongoDB\Exception\UnexpectedValueException;
  26  use MongoDB\Operation\Executable;
  27  
  28  use function current;
  29  use function is_array;
  30  use function is_bool;
  31  use function is_integer;
  32  use function is_object;
  33  
  34  /**
  35   * Wrapper for the ListDatabases command.
  36   *
  37   * @internal
  38   * @see https://mongodb.com/docs/manual/reference/command/listDatabases/
  39   */
  40  class ListDatabases implements Executable
  41  {
  42      /** @var array */
  43      private $options;
  44  
  45      /**
  46       * Constructs a listDatabases command.
  47       *
  48       * Supported options:
  49       *
  50       *  * authorizedDatabases (boolean): Determines which databases are returned
  51       *    based on the user privileges.
  52       *
  53       *    For servers < 4.0.5, this option is ignored.
  54       *
  55       *  * comment (mixed): BSON value to attach as a comment to this command.
  56       *
  57       *    This is not supported for servers versions < 4.4.
  58       *
  59       *  * filter (document): Query by which to filter databases.
  60       *
  61       *  * maxTimeMS (integer): The maximum amount of time to allow the query to
  62       *    run.
  63       *
  64       *  * nameOnly (boolean): A flag to indicate whether the command should
  65       *    return just the database names, or return both database names and size
  66       *    information.
  67       *
  68       *  * session (MongoDB\Driver\Session): Client session.
  69       *
  70       * @param array $options Command options
  71       * @throws InvalidArgumentException for parameter/option parsing errors
  72       */
  73      public function __construct(array $options = [])
  74      {
  75          if (isset($options['authorizedDatabases']) && ! is_bool($options['authorizedDatabases'])) {
  76              throw InvalidArgumentException::invalidType('"authorizedDatabases" option', $options['authorizedDatabases'], 'boolean');
  77          }
  78  
  79          if (isset($options['filter']) && ! is_array($options['filter']) && ! is_object($options['filter'])) {
  80              throw InvalidArgumentException::invalidType('"filter" option', $options['filter'], ['array', 'object']);
  81          }
  82  
  83          if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
  84              throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
  85          }
  86  
  87          if (isset($options['nameOnly']) && ! is_bool($options['nameOnly'])) {
  88              throw InvalidArgumentException::invalidType('"nameOnly" option', $options['nameOnly'], 'boolean');
  89          }
  90  
  91          if (isset($options['session']) && ! $options['session'] instanceof Session) {
  92              throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class);
  93          }
  94  
  95          $this->options = $options;
  96      }
  97  
  98      /**
  99       * Execute the operation.
 100       *
 101       * @see Executable::execute()
 102       * @return array An array of database info structures
 103       * @throws UnexpectedValueException if the command response was malformed
 104       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
 105       */
 106      public function execute(Server $server): array
 107      {
 108          $cursor = $server->executeReadCommand('admin', $this->createCommand(), $this->createOptions());
 109          $cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
 110          $result = current($cursor->toArray());
 111  
 112          if (! isset($result['databases']) || ! is_array($result['databases'])) {
 113              throw new UnexpectedValueException('listDatabases command did not return a "databases" array');
 114          }
 115  
 116          return $result['databases'];
 117      }
 118  
 119      /**
 120       * Create the listDatabases command.
 121       */
 122      private function createCommand(): Command
 123      {
 124          $cmd = ['listDatabases' => 1];
 125  
 126          if (! empty($this->options['filter'])) {
 127              $cmd['filter'] = (object) $this->options['filter'];
 128          }
 129  
 130          foreach (['authorizedDatabases', 'comment', 'maxTimeMS', 'nameOnly'] as $option) {
 131              if (isset($this->options[$option])) {
 132                  $cmd[$option] = $this->options[$option];
 133              }
 134          }
 135  
 136          return new Command($cmd);
 137      }
 138  
 139      /**
 140       * Create options for executing the command.
 141       *
 142       * Note: read preference is intentionally omitted, as the spec requires that
 143       * the command be executed on the primary.
 144       *
 145       * @see https://php.net/manual/en/mongodb-driver-server.executecommand.php
 146       */
 147      private function createOptions(): array
 148      {
 149          $options = [];
 150  
 151          if (isset($this->options['session'])) {
 152              $options['session'] = $this->options['session'];
 153          }
 154  
 155          return $options;
 156      }
 157  }