Commit c635e251 authored by Adrian Greeve's avatar Adrian Greeve
Browse files

Merge branch 'MDL-54592-master' of git://github.com/vmdef/moodle

parents 319b14b2 02a5d26b
......@@ -6,6 +6,7 @@ vendor/
admin/tool/policy/amd/src/jquery-eu-cookie-law-popup.js
admin/tool/usertours/amd/src/tour.js
auth/cas/CAS/
cache/stores/mongodb/MongoDB/
enrol/lti/ims-blti/
filter/algebra/AlgParser.pm
filter/tex/mimetex.*
......
......@@ -9,6 +9,7 @@ vendor/
admin/tool/policy/amd/src/jquery-eu-cookie-law-popup.js
admin/tool/usertours/amd/src/tour.js
auth/cas/CAS/
cache/stores/mongodb/MongoDB/
enrol/lti/ims-blti/
filter/algebra/AlgParser.pm
filter/tex/mimetex.*
......
<?php
/*
* Copyright 2015-2017 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace MongoDB;
use MongoDB\Driver\WriteResult;
use MongoDB\Exception\BadMethodCallException;
/**
* Result class for a bulk write operation.
*/
class BulkWriteResult
{
private $writeResult;
private $insertedIds;
private $isAcknowledged;
/**
* Constructor.
*
* @param WriteResult $writeResult
* @param mixed[] $insertedIds
*/
public function __construct(WriteResult $writeResult, array $insertedIds)
{
$this->writeResult = $writeResult;
$this->insertedIds = $insertedIds;
$this->isAcknowledged = $writeResult->isAcknowledged();
}
/**
* Return the number of documents that were deleted.
*
* This method should only be called if the write was acknowledged.
*
* @see BulkWriteResult::isAcknowledged()
* @return integer
* @throws BadMethodCallException is the write result is unacknowledged
*/
public function getDeletedCount()
{
if ($this->isAcknowledged) {
return $this->writeResult->getDeletedCount();
}
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
}
/**
* Return the number of documents that were inserted.
*
* This method should only be called if the write was acknowledged.
*
* @see BulkWriteResult::isAcknowledged()
* @return integer
* @throws BadMethodCallException is the write result is unacknowledged
*/
public function getInsertedCount()
{
if ($this->isAcknowledged) {
return $this->writeResult->getInsertedCount();
}
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
}
/**
* Return a map of the inserted documents' IDs.
*
* The index of each ID in the map corresponds to each document's position
* in the bulk operation. If a document had an ID prior to inserting (i.e.
* the driver did not generate an ID), the index will contain its "_id"
* field value. Any driver-generated ID will be a MongoDB\BSON\ObjectId
* instance.
*
* @return mixed[]
*/
public function getInsertedIds()
{
return $this->insertedIds;
}
/**
* Return the number of documents that were matched by the filter.
*
* This method should only be called if the write was acknowledged.
*
* @see BulkWriteResult::isAcknowledged()
* @return integer
* @throws BadMethodCallException is the write result is unacknowledged
*/
public function getMatchedCount()
{
if ($this->isAcknowledged) {
return $this->writeResult->getMatchedCount();
}
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
}
/**
* Return the number of documents that were modified.
*
* This value is undefined (i.e. null) if the write executed as a legacy
* operation instead of command.
*
* This method should only be called if the write was acknowledged.
*
* @see BulkWriteResult::isAcknowledged()
* @return integer|null
* @throws BadMethodCallException is the write result is unacknowledged
*/
public function getModifiedCount()
{
if ($this->isAcknowledged) {
return $this->writeResult->getModifiedCount();
}
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
}
/**
* Return the number of documents that were upserted.
*
* This method should only be called if the write was acknowledged.
*
* @see BulkWriteResult::isAcknowledged()
* @return integer
* @throws BadMethodCallException is the write result is unacknowledged
*/
public function getUpsertedCount()
{
if ($this->isAcknowledged) {
return $this->writeResult->getUpsertedCount();
}
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
}
/**
* Return a map of the upserted documents' IDs.
*
* The index of each ID in the map corresponds to each document's position
* in bulk operation. If a document had an ID prior to upserting (i.e. the
* server did not need to generate an ID), this will contain its "_id". Any
* server-generated ID will be a MongoDB\BSON\ObjectId instance.
*
* This method should only be called if the write was acknowledged.
*
* @see BulkWriteResult::isAcknowledged()
* @return mixed[]
* @throws BadMethodCallException is the write result is unacknowledged
*/
public function getUpsertedIds()
{
if ($this->isAcknowledged) {
return $this->writeResult->getUpsertedIds();
}
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
}
/**
* Return whether this update was acknowledged by the server.
*
* If the update was not acknowledged, other fields from the WriteResult
* (e.g. matchedCount) will be undefined.
*
* @return boolean
*/
public function isAcknowledged()
{
return $this->isAcknowledged;
}
}
<?php
/*
* Copyright 2017 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace MongoDB;
use MongoDB\BSON\Serializable;
use MongoDB\Driver\Cursor;
use MongoDB\Driver\Exception\ConnectionException;
use MongoDB\Driver\Exception\RuntimeException;
use MongoDB\Driver\Exception\ServerException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\ResumeTokenException;
use IteratorIterator;
use Iterator;
/**
* Iterator for a change stream.
*
* @api
* @see \MongoDB\Collection::watch()
* @see http://docs.mongodb.org/manual/reference/command/changeStream/
*/
class ChangeStream implements Iterator
{
/**
* @deprecated 1.4
* @todo Remove this in 2.0 (see: PHPLIB-360)
*/
const CURSOR_NOT_FOUND = 43;
private static $errorCodeCappedPositionLost = 136;
private static $errorCodeInterrupted = 11601;
private static $errorCodeCursorKilled = 237;
private $resumeToken;
private $resumeCallable;
private $csIt;
private $key = 0;
private $hasAdvanced = false;
/**
* Constructor.
*
* @internal
* @param Cursor $cursor
* @param callable $resumeCallable
*/
public function __construct(Cursor $cursor, callable $resumeCallable)
{
$this->resumeCallable = $resumeCallable;
$this->csIt = new IteratorIterator($cursor);
}
/**
* @see http://php.net/iterator.current
* @return mixed
*/
public function current()
{
return $this->csIt->current();
}
/**
* @return \MongoDB\Driver\CursorId
*/
public function getCursorId()
{
return $this->csIt->getInnerIterator()->getId();
}
/**
* @see http://php.net/iterator.key
* @return mixed
*/
public function key()
{
if ($this->valid()) {
return $this->key;
}
return null;
}
/**
* @see http://php.net/iterator.next
* @return void
*/
public function next()
{
try {
$this->csIt->next();
if ($this->valid()) {
if ($this->hasAdvanced) {
$this->key++;
}
$this->hasAdvanced = true;
$this->resumeToken = $this->extractResumeToken($this->csIt->current());
}
/* If the cursorId is 0, the server has invalidated the cursor so we
* will never perform another getMore. This means that we cannot
* resume and we can therefore unset the resumeCallable, which will
* free any reference to Watch. This will also free the only
* reference to an implicit session, since any such reference
* belongs to Watch. */
if ((string) $this->getCursorId() === '0') {
$this->resumeCallable = null;
}
} catch (RuntimeException $e) {
if ($this->isResumableError($e)) {
$this->resume();
}
}
}
/**
* @see http://php.net/iterator.rewind
* @return void
*/
public function rewind()
{
try {
$this->csIt->rewind();
if ($this->valid()) {
$this->hasAdvanced = true;
$this->resumeToken = $this->extractResumeToken($this->csIt->current());
}
// As with next(), free the callable once we know it will never be used.
if ((string) $this->getCursorId() === '0') {
$this->resumeCallable = null;
}
} catch (RuntimeException $e) {
if ($this->isResumableError($e)) {
$this->resume();
}
}
}
/**
* @see http://php.net/iterator.valid
* @return boolean
*/
public function valid()
{
return $this->csIt->valid();
}
/**
* Extracts the resume token (i.e. "_id" field) from the change document.
*
* @param array|document $document Change document
* @return mixed
* @throws InvalidArgumentException
* @throws ResumeTokenException if the resume token is not found or invalid
*/
private function extractResumeToken($document)
{
if ( ! is_array($document) && ! is_object($document)) {
throw InvalidArgumentException::invalidType('$document', $document, 'array or object');
}
if ($document instanceof Serializable) {
return $this->extractResumeToken($document->bsonSerialize());
}
$resumeToken = is_array($document)
? (isset($document['_id']) ? $document['_id'] : null)
: (isset($document->_id) ? $document->_id : null);
if ( ! isset($resumeToken)) {
throw ResumeTokenException::notFound();
}
if ( ! is_array($resumeToken) && ! is_object($resumeToken)) {
throw ResumeTokenException::invalidType($resumeToken);
}
return $resumeToken;
}
/**
* Determines if an exception is a resumable error.
*
* @see https://github.com/mongodb/specifications/blob/master/source/change-streams/change-streams.rst#resumable-error
* @param RuntimeException $exception
* @return boolean
*/
private function isResumableError(RuntimeException $exception)
{
if ($exception instanceof ConnectionException) {
return true;
}
if ( ! $exception instanceof ServerException) {
return false;
}
if (in_array($exception->getCode(), [self::$errorCodeCappedPositionLost, self::$errorCodeCursorKilled, self::$errorCodeInterrupted])) {
return false;
}
return true;
}
/**
* Creates a new changeStream after a resumable server error.
*
* @return void
*/
private function resume()
{
$newChangeStream = call_user_func($this->resumeCallable, $this->resumeToken);
$this->csIt = $newChangeStream->csIt;
$this->csIt->rewind();
}
}
<?php
/*
* Copyright 2015-2017 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace MongoDB;
use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Exception\InvalidArgumentException as DriverInvalidArgumentException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedValueException;
use MongoDB\Exception\UnsupportedException;
use MongoDB\Model\DatabaseInfoIterator;
use MongoDB\Operation\DropDatabase;
use MongoDB\Operation\ListDatabases;
use MongoDB\Operation\Watch;
class Client
{
private static $defaultTypeMap = [
'array' => 'MongoDB\Model\BSONArray',
'document' => 'MongoDB\Model\BSONDocument',
'root' => 'MongoDB\Model\BSONDocument',
];
private static $wireVersionForReadConcern = 4;
private static $wireVersionForWritableCommandWriteConcern = 5;
private $manager;
private $readConcern;
private $readPreference;
private $uri;
private $typeMap;
private $writeConcern;
/**
* Constructs a new Client instance.
*
* This is the preferred class for connecting to a MongoDB server or
* cluster of servers. It serves as a gateway for accessing individual
* databases and collections.
*
* Supported driver-specific options:
*
* * typeMap (array): Default type map for cursors and BSON documents.
*
* Other options are documented in MongoDB\Driver\Manager::__construct().
*
* @see http://docs.mongodb.org/manual/reference/connection-string/
* @see http://php.net/manual/en/mongodb-driver-manager.construct.php
* @see http://php.net/manual/en/mongodb.persistence.php#mongodb.persistence.typemaps
* @param string $uri MongoDB connection string
* @param array $uriOptions Additional connection string options
* @param array $driverOptions Driver-specific options
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverInvalidArgumentException for parameter/option parsing errors in the driver
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function __construct($uri = 'mongodb://127.0.0.1/', array $uriOptions = [], array $driverOptions = [])
{
$driverOptions += ['typeMap' => self::$defaultTypeMap];
if (isset($driverOptions['typeMap']) && ! is_array($driverOptions['typeMap'])) {
throw InvalidArgumentException::invalidType('"typeMap" driver option', $driverOptions['typeMap'], 'array');
}
$this->uri = (string) $uri;
$this->typeMap = isset($driverOptions['typeMap']) ? $driverOptions['typeMap'] : null;
unset($driverOptions['typeMap']);
$this->manager = new Manager($uri, $uriOptions, $driverOptions);
$this->readConcern = $this->manager->getReadConcern();
$this->readPreference = $this->manager->getReadPreference();
$this->writeConcern = $this->manager->getWriteConcern();
}
/**
* Return internal properties for debugging purposes.
*
* @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
* @return array
*/
public function __debugInfo()
{
return [
'manager' => $this->manager,
'uri' => $this->uri,
'typeMap' => $this->typeMap,
'writeConcern' => $this->writeConcern,
];
}
/**
* Select a database.
*
* Note: databases whose names contain special characters (e.g. "-") may
* be selected with complex syntax (e.g. $client->{"that-database"}) or
* {@link selectDatabase()}.
*
* @see http://php.net/oop5.overloading#object.get
* @see http://php.net/types.string#language.types.string.parsing.complex
* @param string $databaseName Name of the database to select
* @return Database
*/
public function __get($databaseName)
{
return $this->selectDatabase($databaseName);
}
/**
* Return the connection string (i.e. URI).
*
* @return string
*/
public function __toString()
{
return $this->uri;
}
/**
* Drop a database.
*
* @see DropDatabase::__construct() for supported options
* @param string $databaseName Database name
* @param array $options Additional options
* @return array|object Command result document
* @throws UnsupportedException if options are unsupported on the selected server
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function dropDatabase($databaseName, array $options = [])
{
if ( ! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
}
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern)) {
$options['writeConcern'] = $this->writeConcern;
}
$operation = new DropDatabase($databaseName, $options);
return $operation->execute($server);
}
/**
* Return the Manager.
*
* @return Manager
*/
public function getManager()
{
return $this->manager;
}
/**
* Return the read concern for this client.
*
* @see http://php.net/manual/en/mongodb-driver-readconcern.isdefault.php
* @return ReadConcern
*/
public function getReadConcern()
{
return $this->readConcern;