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 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   1  <?php
   2  /*
   3   * Copyright 2015-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\Operation;
  19  
  20  use EmptyIterator;
  21  use MongoDB\Driver\Command;
  22  use MongoDB\Driver\Exception\CommandException;
  23  use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
  24  use MongoDB\Driver\Server;
  25  use MongoDB\Driver\Session;
  26  use MongoDB\Exception\InvalidArgumentException;
  27  use MongoDB\Model\CachingIterator;
  28  use MongoDB\Model\IndexInfoIterator;
  29  use MongoDB\Model\IndexInfoIteratorIterator;
  30  
  31  use function is_integer;
  32  
  33  /**
  34   * Operation for the listIndexes command.
  35   *
  36   * @api
  37   * @see \MongoDB\Collection::listIndexes()
  38   * @see https://mongodb.com/docs/manual/reference/command/listIndexes/
  39   */
  40  class ListIndexes implements Executable
  41  {
  42      /** @var integer */
  43      private static $errorCodeDatabaseNotFound = 60;
  44  
  45      /** @var integer */
  46      private static $errorCodeNamespaceNotFound = 26;
  47  
  48      /** @var string */
  49      private $databaseName;
  50  
  51      /** @var string */
  52      private $collectionName;
  53  
  54      /** @var array */
  55      private $options;
  56  
  57      /**
  58       * Constructs a listIndexes command.
  59       *
  60       * Supported options:
  61       *
  62       *  * comment (mixed): BSON value to attach as a comment to this command.
  63       *
  64       *    This is not supported for servers versions < 4.4.
  65       *
  66       *  * maxTimeMS (integer): The maximum amount of time to allow the query to
  67       *    run.
  68       *
  69       *  * session (MongoDB\Driver\Session): Client session.
  70       *
  71       * @param string $databaseName   Database name
  72       * @param string $collectionName Collection name
  73       * @param array  $options        Command options
  74       * @throws InvalidArgumentException for parameter/option parsing errors
  75       */
  76      public function __construct(string $databaseName, string $collectionName, array $options = [])
  77      {
  78          if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
  79              throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
  80          }
  81  
  82          if (isset($options['session']) && ! $options['session'] instanceof Session) {
  83              throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class);
  84          }
  85  
  86          $this->databaseName = $databaseName;
  87          $this->collectionName = $collectionName;
  88          $this->options = $options;
  89      }
  90  
  91      /**
  92       * Execute the operation.
  93       *
  94       * @see Executable::execute()
  95       * @return IndexInfoIterator
  96       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
  97       */
  98      public function execute(Server $server)
  99      {
 100          return $this->executeCommand($server);
 101      }
 102  
 103      /**
 104       * Create options for executing the command.
 105       *
 106       * Note: read preference is intentionally omitted, as the spec requires that
 107       * the command be executed on the primary.
 108       *
 109       * @see https://php.net/manual/en/mongodb-driver-server.executecommand.php
 110       */
 111      private function createOptions(): array
 112      {
 113          $options = [];
 114  
 115          if (isset($this->options['session'])) {
 116              $options['session'] = $this->options['session'];
 117          }
 118  
 119          return $options;
 120      }
 121  
 122      /**
 123       * Returns information for all indexes for this collection using the
 124       * listIndexes command.
 125       *
 126       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
 127       */
 128      private function executeCommand(Server $server): IndexInfoIteratorIterator
 129      {
 130          $cmd = ['listIndexes' => $this->collectionName];
 131  
 132          foreach (['comment', 'maxTimeMS'] as $option) {
 133              if (isset($this->options[$option])) {
 134                  $cmd[$option] = $this->options[$option];
 135              }
 136          }
 137  
 138          try {
 139              $cursor = $server->executeReadCommand($this->databaseName, new Command($cmd), $this->createOptions());
 140          } catch (CommandException $e) {
 141              /* The server may return an error if the collection does not exist.
 142               * Check for possible error codes (see: SERVER-20463) and return an
 143               * empty iterator instead of throwing.
 144               */
 145              if ($e->getCode() === self::$errorCodeNamespaceNotFound || $e->getCode() === self::$errorCodeDatabaseNotFound) {
 146                  return new IndexInfoIteratorIterator(new EmptyIterator());
 147              }
 148  
 149              throw $e;
 150          }
 151  
 152          $cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
 153  
 154          return new IndexInfoIteratorIterator(new CachingIterator($cursor), $this->databaseName . '.' . $this->collectionName);
 155      }
 156  }