~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to cache/stores/mongodb/lib.php

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
106
106
    protected $isready = false;
107
107
 
108
108
    /**
 
109
     * Set to true if the Mongo extension is < version 1.3.
 
110
     * If this is the case we must use the legacy Mongo class instead of MongoClient.
 
111
     * Mongo is backwards compatible, although obviously deprecated.
 
112
     * @var bool
 
113
     */
 
114
    protected $legacymongo = false;
 
115
 
 
116
    /**
109
117
     * Constructs a new instance of the Mongo store.
110
118
     *
111
119
     * Noting that this function is not an initialisation. It is used to prepare the store for use.
140
148
            $this->extendedmode = $configuration['extendedmode'];
141
149
        }
142
150
 
 
151
        // Test if the MongoClient class exists, if not we need to switch to legacy classes.
 
152
        $this->legacymongo = (!class_exists('MongoClient'));
 
153
 
 
154
        // MongoClient from Mongo 1.3 onwards. Mongo for earlier versions.
 
155
        $class = ($this->legacymongo) ? 'Mongo' : 'MongoClient';
143
156
        try {
144
 
            $this->connection = new Mongo($this->server, $this->options);
 
157
            $this->connection = new $class($this->server, $this->options);
145
158
            $this->isready = true;
146
159
        } catch (MongoConnectionException $e) {
147
160
            // We only want to catch MongoConnectionExceptions here.
153
166
     * @return bool
154
167
     */
155
168
    public static function are_requirements_met() {
156
 
        return class_exists('Mongo');
 
169
        return class_exists('MongoClient') || class_exists('Mongo');
157
170
    }
158
171
 
159
172
    /**
175
188
     * @return int
176
189
     */
177
190
    public static function get_supported_modes(array $configuration = array()) {
178
 
        return self::MODE_APPLICATION + self::MODE_SESSION;
 
191
        return self::MODE_APPLICATION;
179
192
    }
180
193
 
181
194
    /**
193
206
        $this->database = $this->connection->selectDB($this->databasename);
194
207
        $this->definitionhash = $definition->generate_definition_hash();
195
208
        $this->collection = $this->database->selectCollection($this->definitionhash);
196
 
        $this->collection->ensureIndex(array('key' => 1), array(
197
 
            'safe' => $this->usesafe,
198
 
            'name' => 'idx_key'
199
 
        ));
 
209
 
 
210
        $options = array('name' => 'idx_key');
 
211
        if ($this->legacymongo) {
 
212
            $options['safe'] = $this->usesafe;
 
213
        } else {
 
214
            $options['w'] = $this->usesafe ? 1 : 0;
 
215
        }
 
216
        $this->collection->ensureIndex(array('key' => 1), $options);
200
217
    }
201
218
 
202
219
    /**
301
318
            $record = $key;
302
319
        }
303
320
        $record['data'] = serialize($data);
304
 
        $options = array(
305
 
            'upsert' => true,
306
 
            'safe' => $this->usesafe,
307
 
            'w' => $this->usesafe ? 1 : 0
308
 
        );
 
321
        $options = array('upsert' => true);
 
322
        if ($this->legacymongo) {
 
323
            $options['safe'] = $this->usesafe;
 
324
        } else {
 
325
            $options['w'] = $this->usesafe ? 1 : 0;
 
326
        }
309
327
        $this->delete($key);
310
328
        $result = $this->collection->insert($record, $options);
311
329
        if ($result === true) {
354
372
        } else {
355
373
            $criteria = $key;
356
374
        }
357
 
        $options = array(
358
 
            'justOne' => false,
359
 
            'safe' => $this->usesafe,
360
 
            'w' => $this->usesafe ? 1 : 0
361
 
        );
 
375
        $options = array('justOne' => false);
 
376
        if ($this->legacymongo) {
 
377
            $options['safe'] = $this->usesafe;
 
378
        } else {
 
379
            $options['w'] = $this->usesafe ? 1 : 0;
 
380
        }
362
381
        $result = $this->collection->remove($criteria, $options);
363
382
 
364
383
        if ($result === true) {
483
502
            $connection = $this->connection;
484
503
        } else {
485
504
            try {
486
 
               $connection = new Mongo($this->server, $this->options);
 
505
                // MongoClient from Mongo 1.3 onwards. Mongo for earlier versions.
 
506
                $class = ($this->legacymongo) ? 'Mongo' : 'MongoClient';
 
507
                $connection = new $class($this->server, $this->options);
487
508
            } catch (MongoConnectionException $e) {
488
509
                // We only want to catch MongoConnectionExceptions here.
489
510
                // If the server cannot be connected to we cannot clean it.