From 4907a8ed70503fd6f82e78cb26984a5f1e459fc5 Mon Sep 17 00:00:00 2001 From: Sam Hemelryk <sam@moodle.com> Date: Tue, 6 May 2014 09:29:45 +1200 Subject: [PATCH 1/2] MDL-45412 cachestore_mongodb: fixed use of deprecated Mongo class --- cache/stores/mongodb/lib.php | 53 +++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/cache/stores/mongodb/lib.php b/cache/stores/mongodb/lib.php index cdf764f4cde..0213b7f86ec 100644 --- a/cache/stores/mongodb/lib.php +++ b/cache/stores/mongodb/lib.php @@ -105,6 +105,14 @@ class cachestore_mongodb extends cache_store implements cache_is_configurable { */ protected $isready = false; + /** + * Set to true if the Mongo extension is < version 1.3. + * If this is the case we must use the legacy Mongo class instead of MongoClient. + * Mongo is backwards compatible, although obviously deprecated. + * @var bool + */ + protected $legacymongo = false; + /** * Constructs a new instance of the Mongo store. * @@ -140,8 +148,13 @@ class cachestore_mongodb extends cache_store implements cache_is_configurable { $this->extendedmode = $configuration['extendedmode']; } + // Test if the MongoClient class exists, if not we need to switch to legacy classes. + $this->legacymongo = (!class_exists('MongoClient')); + + // MongoClient from Mongo 1.3 onwards. Mongo for earlier versions. + $class = ($this->legacymongo) ? 'Mongo' : 'MongoClient'; try { - $this->connection = new Mongo($this->server, $this->options); + $this->connection = new $class($this->server, $this->options); $this->isready = true; } catch (MongoConnectionException $e) { // We only want to catch MongoConnectionExceptions here. @@ -193,10 +206,14 @@ class cachestore_mongodb extends cache_store implements cache_is_configurable { $this->database = $this->connection->selectDB($this->databasename); $this->definitionhash = $definition->generate_definition_hash(); $this->collection = $this->database->selectCollection($this->definitionhash); - $this->collection->ensureIndex(array('key' => 1), array( - 'safe' => $this->usesafe, - 'name' => 'idx_key' - )); + + $options = array('name' => 'idx_key'); + if ($this->legacymongo) { + $options['safe'] = $this->usesafe; + } else { + $options['w'] = $this->usesafe ? 1 : 0; + } + $this->collection->ensureIndex(array('key' => 1), $options); } /** @@ -301,11 +318,12 @@ class cachestore_mongodb extends cache_store implements cache_is_configurable { $record = $key; } $record['data'] = serialize($data); - $options = array( - 'upsert' => true, - 'safe' => $this->usesafe, - 'w' => $this->usesafe ? 1 : 0 - ); + $options = array('upsert' => true); + if ($this->legacymongo) { + $options['safe'] = $this->usesafe; + } else { + $options['w'] = $this->usesafe ? 1 : 0; + } $this->delete($key); $result = $this->collection->insert($record, $options); if ($result === true) { @@ -354,11 +372,12 @@ class cachestore_mongodb extends cache_store implements cache_is_configurable { } else { $criteria = $key; } - $options = array( - 'justOne' => false, - 'safe' => $this->usesafe, - 'w' => $this->usesafe ? 1 : 0 - ); + $options = array('justOne' => false); + if ($this->legacymongo) { + $options['safe'] = $this->usesafe; + } else { + $options['w'] = $this->usesafe ? 1 : 0; + } $result = $this->collection->remove($criteria, $options); if ($result === true) { @@ -483,7 +502,9 @@ class cachestore_mongodb extends cache_store implements cache_is_configurable { $connection = $this->connection; } else { try { - $connection = new Mongo($this->server, $this->options); + // MongoClient from Mongo 1.3 onwards. Mongo for earlier versions. + $class = ($this->legacymongo) ? 'Mongo' : 'MongoClient'; + $connection = new $class($this->server, $this->options); } catch (MongoConnectionException $e) { // We only want to catch MongoConnectionExceptions here. // If the server cannot be connected to we cannot clean it. From 6deb0813d0cac785e296930bf1993c9d0105d305 Mon Sep 17 00:00:00 2001 From: Sam Hemelryk <sam@moodle.com> Date: Tue, 6 May 2014 09:30:27 +1200 Subject: [PATCH 2/2] MDL-45412 cachestore_mongodb: added notification of old Mongo version use --- cache/stores/mongodb/addinstanceform.php | 5 +++++ cache/stores/mongodb/lang/en/cachestore_mongodb.php | 1 + 2 files changed, 6 insertions(+) diff --git a/cache/stores/mongodb/addinstanceform.php b/cache/stores/mongodb/addinstanceform.php index ada3220c0aa..c7d31048704 100644 --- a/cache/stores/mongodb/addinstanceform.php +++ b/cache/stores/mongodb/addinstanceform.php @@ -48,8 +48,13 @@ class cachestore_mongodb_addinstance_form extends cachestore_addinstance_form { * The forms custom definitions. */ protected function configuration_definition() { + global $OUTPUT; $form = $this->_form; + if (!class_exists('MongoClient')) { + $form->addElement('html', $OUTPUT->notification(get_string('pleaseupgrademongo', 'cachestore_mongodb'))); + } + $form->addElement('text', 'server', get_string('server', 'cachestore_mongodb'), array('size' => 72)); $form->addHelpButton('server', 'server', 'cachestore_mongodb'); $form->addRule('server', get_string('required'), 'required'); diff --git a/cache/stores/mongodb/lang/en/cachestore_mongodb.php b/cache/stores/mongodb/lang/en/cachestore_mongodb.php index d787d97fba4..5af0c73cc6c 100644 --- a/cache/stores/mongodb/lang/en/cachestore_mongodb.php +++ b/cache/stores/mongodb/lang/en/cachestore_mongodb.php @@ -28,6 +28,7 @@ $string['extendedmode'] = 'Use extended keys'; $string['extendedmode_help'] = 'If enabled full key sets will be used when working with the plugin. This isn\'t used internally yet but would allow you to easily search and investigate the MongoDB plugin manually if you so choose. Turning this on will add an small overhead so should only be done if you require it.'; $string['password'] = 'Password'; $string['password_help'] = 'The password of the user being used for the connection.'; +$string['pleaseupgrademongo'] = 'You are using an old version of the PHP Mongo extension (> 1.3). Support for old versions of the Mongo extension will be dropped in the future. Please consider upgrading.'; $string['pluginname'] = 'MongoDB'; $string['replicaset'] = 'Replica set'; $string['replicaset_help'] = 'The name of the replica set to connect to. If this is given the master will be determined by using the ismaster database command on the seeds, so the driver may end up connecting to a server that was not even listed.';