Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

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