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 MongoDB\Driver\Command;
  21  use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
  22  use MongoDB\Driver\Server;
  23  use MongoDB\Driver\Session;
  24  use MongoDB\Driver\WriteConcern;
  25  use MongoDB\Exception\InvalidArgumentException;
  26  use MongoDB\Exception\UnsupportedException;
  27  
  28  use function current;
  29  use function is_array;
  30  use function is_integer;
  31  
  32  /**
  33   * Operation for the dropIndexes command.
  34   *
  35   * @api
  36   * @see \MongoDB\Collection::dropIndexes()
  37   * @see https://mongodb.com/docs/manual/reference/command/dropIndexes/
  38   */
  39  class DropIndexes implements Executable
  40  {
  41      /** @var string */
  42      private $databaseName;
  43  
  44      /** @var string */
  45      private $collectionName;
  46  
  47      /** @var string */
  48      private $indexName;
  49  
  50      /** @var array */
  51      private $options;
  52  
  53      /**
  54       * Constructs a dropIndexes command.
  55       *
  56       * Supported options:
  57       *
  58       *  * comment (mixed): BSON value to attach as a comment to this command.
  59       *
  60       *    This is not supported for servers versions < 4.4.
  61       *
  62       *  * maxTimeMS (integer): The maximum amount of time to allow the query to
  63       *    run.
  64       *
  65       *  * session (MongoDB\Driver\Session): Client session.
  66       *
  67       *  * typeMap (array): Type map for BSON deserialization. This will be used
  68       *    for the returned command result document.
  69       *
  70       *  * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
  71       *
  72       * @param string $databaseName   Database name
  73       * @param string $collectionName Collection name
  74       * @param string $indexName      Index name (use "*" to drop all indexes)
  75       * @param array  $options        Command options
  76       * @throws InvalidArgumentException for parameter/option parsing errors
  77       */
  78      public function __construct(string $databaseName, string $collectionName, string $indexName, array $options = [])
  79      {
  80          $indexName = $indexName;
  81  
  82          if ($indexName === '') {
  83              throw new InvalidArgumentException('$indexName cannot be empty');
  84          }
  85  
  86          if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
  87              throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
  88          }
  89  
  90          if (isset($options['session']) && ! $options['session'] instanceof Session) {
  91              throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class);
  92          }
  93  
  94          if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
  95              throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
  96          }
  97  
  98          if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
  99              throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], WriteConcern::class);
 100          }
 101  
 102          if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
 103              unset($options['writeConcern']);
 104          }
 105  
 106          $this->databaseName = $databaseName;
 107          $this->collectionName = $collectionName;
 108          $this->indexName = $indexName;
 109          $this->options = $options;
 110      }
 111  
 112      /**
 113       * Execute the operation.
 114       *
 115       * @see Executable::execute()
 116       * @return array|object Command result document
 117       * @throws UnsupportedException if write concern is used and unsupported
 118       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
 119       */
 120      public function execute(Server $server)
 121      {
 122          $inTransaction = isset($this->options['session']) && $this->options['session']->isInTransaction();
 123          if ($inTransaction && isset($this->options['writeConcern'])) {
 124              throw UnsupportedException::writeConcernNotSupportedInTransaction();
 125          }
 126  
 127          $cursor = $server->executeWriteCommand($this->databaseName, $this->createCommand(), $this->createOptions());
 128  
 129          if (isset($this->options['typeMap'])) {
 130              $cursor->setTypeMap($this->options['typeMap']);
 131          }
 132  
 133          return current($cursor->toArray());
 134      }
 135  
 136      /**
 137       * Create the dropIndexes command.
 138       */
 139      private function createCommand(): Command
 140      {
 141          $cmd = [
 142              'dropIndexes' => $this->collectionName,
 143              'index' => $this->indexName,
 144          ];
 145  
 146          foreach (['comment', 'maxTimeMS'] as $option) {
 147              if (isset($this->options[$option])) {
 148                  $cmd[$option] = $this->options[$option];
 149              }
 150          }
 151  
 152          return new Command($cmd);
 153      }
 154  
 155      /**
 156       * Create options for executing the command.
 157       *
 158       * @see https://php.net/manual/en/mongodb-driver-server.executewritecommand.php
 159       */
 160      private function createOptions(): array
 161      {
 162          $options = [];
 163  
 164          if (isset($this->options['session'])) {
 165              $options['session'] = $this->options['session'];
 166          }
 167  
 168          if (isset($this->options['writeConcern'])) {
 169              $options['writeConcern'] = $this->options['writeConcern'];
 170          }
 171  
 172          return $options;
 173      }
 174  }