/**
  * @param Database $db
  */
 public function down(Database $db)
 {
     $db->execute('
         db.status.update({}, {$unset: {"outOfWorkflow":1}} , {"multi": true});;
         db.content_type.update({}, {$unset: {"definingStatusable":1}} , {"multi": true});;
     ');
 }
 /**
  * @param Database $db
  */
 public function down(Database $db)
 {
     $this->getKeywordableCollection();
     $this->loadConfiguration();
     foreach ($this->collections as $collection) {
         $db->execute('
             db.' . $collection . '.find().forEach(function(item) {
                 if (typeof item.keywords != \'undefined\') {
                     keywords = item.keywords;
                     item.keywords = [];
                     for (var i in keywords) {
                         keyword = {};
                         keyword._id = keywords[i].$id
                         db.keyword.find({"_id" : keywords[i].$id}).forEach(function(record) {
                             for (property in record) {
                                 keyword[property] = record[property];
                             }
                         });
                         item.keywords.push(keyword);
                     }
                     db.' . $collection . '.update({ _id: item._id }, item);
                 }
             });
         ');
     }
     $this->parseNode('down');
 }
 /**
  * @param Database $db
  */
 public function down(Database $db)
 {
     $db->execute('
         db.status.remove({"name": "outOfWorkflow"});
         db.status.update({}, {$unset: {"blockedEdition":1}} , {"multi": true});;
     ');
 }
 /**
  * @param Database $db
  */
 public function down(Database $db)
 {
     $db->execute('
         db.users_group.find({ roles: \'ROLE_ACCESS_MOVE_TREE\' }).forEach(function(item) {
             db.users_group.update({ _id: item._id}, { $pull: { roles : \'ROLE_ACCESS_MOVE_TREE\' }});
             db.users_group.update({ _id: item._id}, { $push: { roles : \'ROLE_ACCESS_MOVE_NODE\' }});
         });
         db.users_group.find({ \'modelRoles\' : { $exists:1 }}).forEach(function(item) {
                 var currentNodeId = [];
                 var length = item.modelRoles.length;
                 for (i = 0; i != length; ++i) {
                    if (currentNodeId.indexOf(item.modelRoles[i].id) == -1 && item.modelRoles[i].type == \'node\') {
                       currentNodeId.push(item.modelRoles[i].id);
                       var nodeRole = {};
                       nodeRole[\'id\'] = item.modelRoles[i].id;
                       nodeRole[\'accessType\'] = \'denied\';
                       nodeRole[\'granted\'] = 0;
                       nodeRole[\'type\'] = \'node\';
                       nodeRole[\'role\'] = \'ROLE_ACCESS_MOVE_NODE\';
                       item.modelRoles.push(nodeRole);
                    }
                 }
                 db.users_group.update({ _id: item._id }, item);
             }
         );
     ');
 }
Beispiel #5
0
 public function testSocketTimeoutOptionIsConverted()
 {
     $mongoDB = $this->getMockMongoDB();
     $mongoDB->expects($this->any())->method('command')->with(['count' => 'foo'], ['socketTimeoutMS' => 1000]);
     $database = new Database($this->getMockConnection(), $mongoDB, $this->getMockEventManager());
     $database->command(['count' => 'foo'], ['timeout' => 1000]);
 }
 public function testDrop()
 {
     $result = array('dropped' => 'databaseName', 'ok' => 1);
     $this->mongoDB->expects($this->once())->method('drop')->will($this->returnValue($result));
     $db = new Database($this->connection, $this->mongoDB, $this->eventManager);
     $this->expectEvents(array(array(Events::preDropDatabase, new EventArgs($db)), array(Events::postDropDatabase, new EventArgs($db))));
     $this->assertSame($result, $db->drop());
 }
 private function doQuery(Description $queryDescription, QueryOptions $queryOptions = null, $entityType)
 {
     $cursor = $this->database->selectCollection($entityType)->find($this->buildQueryForDescription($queryDescription, new Expr())->getQuery(), $this->buildQueryModifiers());
     if ($queryOptions === null) {
         return $cursor;
     }
     return $this->applyOptionsToCursor($cursor, $queryOptions);
 }
 /**
  * @see EntityDocumentLookup::getEntityDocumentsForTerm
  */
 public function getEntityIdsForTerm(Term $term, $entityType)
 {
     $documents = $this->database->selectCollection($entityType)->find($this->buildGetEntityIdForTermQuery($term), array('_id' => 1));
     $entities = array();
     foreach ($documents as $document) {
         $entities[] = $this->documentBuilder->buildEntityIdForDocument($document);
     }
     return $entities;
 }
Beispiel #9
0
 public function testSetSlaveOkayPreservesReadPreferenceTags()
 {
     if (version_compare(phpversion('mongo'), '1.3.0', '<')) {
         $this->markTestSkipped('This test is not applicable to driver versions < 1.3.0');
     }
     $this->mongodb->expects($this->exactly(2))->method('getReadPreference')->will($this->returnValue(array('type' => 1, 'type_string' => 'primary preferred', 'tagsets' => array(array('dc:east')))));
     $this->mongodb->expects($this->once())->method('setReadPreference')->with(\MongoClient::RP_SECONDARY_PREFERRED, array(array('dc' => 'east')))->will($this->returnValue(false));
     $database = new Database($this->connection, 'test', $this->getMockEventManager(), '$');
     $this->assertEquals(true, $database->setSlaveOkay(true));
 }
 /**
  * @param Database $db
  */
 public function down(Database $db)
 {
     $db->execute('
         db.site.find().forEach(function(item) {
             aliases = item.aliases;
             item.aliases = [];
             for (i in aliases) {
                 item.aliases.push(aliases[i]);
             }
             db.site.update({_id: item._id}, item);
         });
     ');
 }
 /**
  * @param Database $db
  */
 public function down(Database $db)
 {
     $db->execute('
         db.node.find().forEach(function(item) {
             delete item.currentlyPublished;
             db.node.update({_id: item._id}, item);
         });
         db.content.find().forEach(function(item) {
             delete item.currentlyPublished;
             db.content.update({_id: item._id}, item);
         });
     ');
 }
 /**
  * Downgrade db
  *
  * @param Database $db
  */
 public function down(Database $db)
 {
     $db->execute('
         db.users_group.find().forEach(function(item) {
             var modelRoles = [];
             for (var i in item.modelRoles) {
                 var modelRole = item.modelRoles[i];
                 modelRoles.push(modelRole);
             };
             item.modelRoles = modelRoles;
             db.users_group.update({_id: item._id}, item);
         })
     ');
 }
 /**
  * @param string   $key
  * @param string   $value
  * @param string   $collectionName
  * @param Database $dataBase
  *
  * @return array
  */
 protected function generateCriteria($key, $value, $collectionName, $dataBase)
 {
     $map = new \MongoCode("function() {\n                    for (var key in eval('this.' + preColumn)) {\n                        emit(preColumn + '.' + key, null);\n                    }\n            }");
     $reduce = new \MongoCode("function(k, vals) {  return null; }");
     preg_match('/(.*)\\.\\*/', $key, $column);
     $preColumnn = $column[1];
     $commandResult = $dataBase->command(array("mapreduce" => $collectionName, "map" => $map, "reduce" => $reduce, "out" => array("inline" => 1), "scope" => array("preColumn" => "{$preColumnn}")));
     $criteria = array();
     if (is_array($commandResult) && array_key_exists('ok', $commandResult) && $commandResult['ok'] == 1) {
         foreach ($commandResult['results'] as $filter) {
             $criteria[] = array($filter['_id'] => $value);
         }
     }
     return $criteria;
 }
 public function up(Database $db)
 {
     $dm = $this->container->get('doctrine_mongodb')->getManager();
     $deleted = $db->selectCollection('Node')->createQueryBuilder()->field('deleted')->equals(true)->getQuery()->execute();
     $facetRepo = $dm->getRepository('BpiApiBundle:Entity\\Facet');
     foreach ($deleted as $node) {
         if (isset($node['_id'])) {
             $facet = $facetRepo->findOneByNodeId((string) $node['_id']);
             if ($facet) {
                 $dm->remove($facet);
                 $dm->flush();
             }
         }
     }
 }
    /**
     * @param Database $db
     */
    public function down(Database $db)
    {
        $db->execute('
            var deleteBlockPrivateInArea = function(area, node) {
                if (typeof area.blocks != \'undefined\') {
                    var blocks = area.blocks;
                    for (var i in blocks) {
                        var block = blocks[i];
                        delete block.blockPrivate;
                    }
                }
                if (typeof area.subAreas != \'undefined\') {
                    var areas = area.subAreas;
                    for (var i in areas) {
                        var subAreas = areas[i];
                        deleteBlockPrivateInArea(subAreas, node);
                    }
                }
            }

            db.node.find().forEach(function(item) {
                 var rootArea = item.rootArea;
                 if (typeof rootArea != \'undefined\') {
                     deleteBlockPrivateInArea(rootArea, item);
                 }

                 db.node.update({ _id: item._id }, item);
            });
        ');
    }
Beispiel #16
0
 /**
  * Execute the mapReduce command.
  *
  * @see Collection::mapReduce()
  * @param string|\MongoCode $map
  * @param string|\MongoCode $reduce
  * @param array|string      $out
  * @param array             $query
  * @param array             $options
  * @return ArrayIterator
  * @throws ResultException if the command fails
  */
 protected function doMapReduce($map, $reduce, $out, array $query, array $options)
 {
     $options = isset($options['timeout']) ? $this->convertSocketTimeout($options) : $options;
     $command = array();
     $command['mapreduce'] = $this->mongoCollection->getName();
     $command['map'] = $map;
     $command['reduce'] = $reduce;
     $command['query'] = (object) $query;
     $command['out'] = $out;
     $command = array_merge($command, $options);
     foreach (array('map', 'reduce', 'finalize') as $key) {
         if (isset($command[$key]) && is_string($command[$key])) {
             $command[$key] = new \MongoCode($command[$key]);
         }
     }
     $result = $this->database->command($command);
     if (empty($result['ok'])) {
         throw new ResultException($result);
     }
     if (isset($result['result']) && is_string($result['result'])) {
         return $this->database->selectCollection($result['result'])->find();
     }
     if (isset($result['result']) && is_array($result['result']) && isset($result['result']['db'], $result['result']['collection'])) {
         return $this->database->getConnection()->selectCollection($result['result']['db'], $result['result']['collection'])->find();
     }
     $arrayIterator = new ArrayIterator(isset($result['results']) ? $result['results'] : array());
     $arrayIterator->setCommandResult($result);
     return $arrayIterator;
 }
 /**
  * @param Database $db
  */
 public function down(Database $db)
 {
     $db->execute('
         db.users_group.find({ \'modelRoles\' : { $exists:1 }}).forEach(function(item) {
             for (i = 0; i != item.modelRoles.length; ++i) {
                 if (item.modelRoles[i].type == \'node\') {
                     delete item.modelRoles[i].type;
                     item.modelRoles[i].nodeId = item.modelRoles[i].id;
                     delete item.modelRoles[i].id;
                 }
             }
             db.users_group.update({ _id: item._id }, item);
         });
     ');
     $db->execute('db.users_group.update({}, { $rename : { \'modelRoles\':\'nodeRoles\' }}, { multi: true });');
 }
    /**
     * @param Database $db
     */
    public function down(Database $db)
    {
        $db->execute($this->getCleanRowAreasFunction() . '

            db.template.find().forEach(function(item) {
                 if (typeof item.rootArea != \'undefined\') {
                     var rowAreas = item.rootArea.subAreas;
                     areas = []
                     areas = cleanRowAreas(rowAreas);
                     item.areas = areas;
                     delete item.rootArea;
                     db.template.update({ _id: item._id }, item);
                 }
            });
        ');
    }
Beispiel #19
0
 public function up(Database $db)
 {
     $collection = $db->selectCollection('Template');
     $templates = $collection->find([], ['files' => 1, 'id' => 1]);
     foreach ($templates as $template) {
         foreach ($template['files'] as $ii => $file) {
             $isImage = preg_match('/\\.(jpg|png|gif)$/', $file['clientPath']) === 1;
             if (!$isImage) {
                 continue;
             }
             if (isset($file['attributes']['width']) && isset($file['attributes']['height'])) {
                 continue;
             }
             list($width, $height) = getimagesize($file['fileSystemUrl']);
             $collection->update(['id' => $template['id'], 'files.clientPath' => $file['clientPath']], ['$set' => ['files.' . $ii . '.attributes.width' => $width, 'files.' . $ii . '.attributes.height' => $height]]);
         }
     }
 }
 private function setupClaimsIndexes()
 {
     foreach (MongoDBDocumentBuilder::$SUPPORTED_ENTITY_TYPES as $entityType) {
         $collection = $this->database->selectCollection($entityType);
         foreach (MongoDBDocumentBuilder::$SUPPORTED_DATAVALUE_TYPES as $dataValueType) {
             $key = 'sclaims.' . $dataValueType;
             $collection->ensureIndex([$key => 1], ['sparse' => true, 'socketTimeoutMS' => -1]);
         }
     }
 }
    /**
     * @param Database $db
     * @param string $collection
     * @param string $property
     */
    protected function downStorageTranslatedValue(Database $db, $collection, $property)
    {
        $db->execute('
            db.' . $collection . '.find({"' . $property . '":{$exists:1}}).forEach(function(item) {
                 var property = item.' . $property . ';
                 var newProperty = {};
                 for (var language in property) {
                    var value = property[language];

                    var element = {};
                    element.language = language;
                    element.value = value;
                    newProperty[language] = element;
                 }
                 item.' . $property . ' = newProperty;

                 db.' . $collection . '.update({_id: item._id}, item);
            });
        ');
    }
    /**
     * @param Database $db
     */
    public function down(Database $db)
    {
        $db->execute('
            db.workflow_function.find({"names":{$exists:1}}).forEach(function(item) {
                 var property = item.names;
                 var newProperty = {};
                 for (var language in property) {
                    var value = property[language];

                    var element = {};
                    element.language = language;
                    element.value = value;
                    newProperty[language] = element;
                 }
                 item.names = newProperty;

                 db.workflow_function.update({_id: item._id}, item);
            });
        ');
    }
 protected function tearDown()
 {
     $this->oid = array();
     if ($this->connection) {
         $this->connection->close();
         $this->connection = null;
     }
     if ($this->con) {
         $this->con->drop();
         $this->con = null;
     }
 }
Beispiel #24
0
 public function down(Database $db)
 {
     $collection = $db->selectCollection('Company');
     $companies = $collection->find();
     foreach ($companies as $company) {
         if (!isset($company['address']) || count($company['address']) === 0) {
             continue;
         }
         $downNeeded = false;
         foreach ($this->arrayIndexesThatIndicateMigrated as $index) {
             if (isset($company['address'][$index])) {
                 $downNeeded = true;
                 break;
             }
         }
         if (!$downNeeded) {
             continue;
         }
         // Convert back to array of objects.
         $collection->update(['_id' => $company['_id']], ['$set' => ['address' => [$company['address']]]]);
     }
 }
Beispiel #25
0
 protected function doMapReduce($map, $reduce, array $query, array $options)
 {
     if (is_string($map)) {
         $map = new \MongoCode($map);
     }
     if (is_string($reduce)) {
         $reduce = new \MongoCode($reduce);
     }
     $command = array();
     $command['mapreduce'] = $this->mongoCollection->getName();
     $command['map'] = $map;
     $command['reduce'] = $reduce;
     $command['query'] = $query;
     $command = array_merge($command, $options);
     $result = $this->database->command($command);
     if (!$result['ok']) {
         throw new \RuntimeException($result['errmsg']);
     }
     return $this->database->selectCollection($result['result'])->find();
 }
Beispiel #26
0
 protected function doMapReduce($map, $reduce, array $out, array $query, array $options)
 {
     if (is_string($map)) {
         $map = new \MongoCode($map);
     }
     if (is_string($reduce)) {
         $reduce = new \MongoCode($reduce);
     }
     $command = array();
     $command['mapreduce'] = $this->getMongoCollection()->getName();
     $command['map'] = $map;
     $command['reduce'] = $reduce;
     $command['query'] = (object) $query;
     $command['out'] = $out;
     $command = array_merge($command, $options);
     $result = $this->database->command($command);
     if (!$result['ok']) {
         throw new \RuntimeException($result['errmsg']);
     }
     if (isset($out['inline']) && $out['inline'] === true) {
         return new ArrayIterator($result['results']);
     }
     return $this->database->selectCollection($result['result'])->find();
 }
 /**
  * @param Database $db
  */
 public function down(Database $db)
 {
     $languages = $this->container->getParameter('open_orchestra_backoffice.orchestra_choice.front_language');
     $languages = array_keys($languages);
     $language = $languages[0];
     $revertRequest = '
         db.site.find().forEach(function(item) {
             var itemHasChanged = false;
             if ("object" == typeof item.metaKeywords) {
                 item.metaKeywords = item.metaKeywords.' . $language . ';
                 itemHasChanged = true;
             }
             if ("object" == typeof item.metaDescriptions) {
                 item.metaDescription = item.metaDescriptions.' . $language . ';
                 delete item.metaDescriptions;
                 itemHasChanged = true;
             }
             if (itemHasChanged) {
                 db.site.update({_id: item._id}, item);
             }
         });
     ';
     $db->execute($revertRequest);
 }
Beispiel #28
0
 public function testSocketTimeoutOptionIsNotConvertedForOlderDrivers()
 {
     if (version_compare(phpversion('mongo'), '1.5.0', '>=')) {
         $this->markTestSkipped('This test is not applicable to driver versions >= 1.5.0');
     }
     $mongoDB = $this->getMockMongoDB();
     $mongoDB->expects($this->any())->method('command')->with(array('count' => 'foo'), array('timeout' => 1000));
     $database = new Database($this->getMockConnection(), $mongoDB, $this->getMockEventManager());
     $database->command(array('count' => 'foo'), array('timeout' => 1000));
 }
Beispiel #29
0
 /**
  * Executes a closure with a temporary read preference on a database or
  * collection.
  *
  * @param Database|Collection $object
  * @param \Closure            $closure
  * @return mixed
  */
 private function withReadPreference($object, \Closure $closure)
 {
     if (!isset($this->query['readPreference'])) {
         return $closure();
     }
     $prevReadPref = $object->getReadPreference();
     $object->setReadPreference($this->query['readPreference'], $this->query['readPreferenceTags']);
     try {
         $result = $closure();
     } catch (\Exception $e) {
     }
     $prevTags = !empty($prevReadPref['tagsets']) ? $prevReadPref['tagsets'] : null;
     $object->setReadPreference($prevReadPref['type'], $prevTags);
     if (isset($e)) {
         throw $e;
     }
     return $result;
 }
Beispiel #30
0
 /**
  * @see Database::getDBRef()
  */
 public function getDBRef(array $ref)
 {
     $this->log(['getDBRef' => true, 'reference' => $ref]);
     return parent::getDBRef($ref);
 }