Author: Johannes Skov Frandsen (localgod@heaven.dk)
 public function testBasic()
 {
     $r1 = new MongoRegex("//");
     $this->assertEquals($r1->regex, "");
     $this->assertEquals($r1->flags, "");
     $r2 = new MongoRegex("/foo/bar");
     $this->assertEquals($r2->regex, "foo");
     $this->assertEquals($r2->flags, "bar");
     $r3 = new MongoRegex($r2);
     $this->assertEquals($r3->regex, "foo");
     $this->assertEquals($r3->flags, "bar");
     $stupid_str = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";
     $rstupid = new MongoRegex("/{$stupid_str}/flagflagflagflagflag");
     $this->assertEquals($rstupid->regex, $stupid_str);
     $this->assertEquals($rstupid->flags, "flagflagflagflagflag");
     $m = new Mongo();
     $c = $m->selectCollection('phpunit', 'regex');
     $c->drop();
     $c->insert(array('x' => 0, 'r1' => $r1));
     $c->insert(array('x' => 1, 'r2' => $r2));
     $c->insert(array('x' => 2, 'stupid' => $rstupid));
     $obj = $c->findOne(array('x' => 0));
     $this->assertEquals($obj['r1']->regex, "");
     $this->assertEquals($obj['r1']->flags, "");
     $obj = $c->findOne(array('x' => 1));
     $this->assertEquals($obj['r2']->regex, "foo");
     $this->assertEquals($obj['r2']->flags, "bar");
     $obj = $c->findOne(array('x' => 2));
     $this->assertEquals($obj['stupid']->regex, $stupid_str);
     $this->assertEquals($obj['stupid']->flags, "flagflagflagflagflag");
 }
Beispiel #2
0
 function __construct($db = null, $host = null)
 {
     $mongo = new Mongo($host);
     $database = $mongo->selectDB($db);
     $this->mongo = $mongo;
     $this->database = $database;
 }
 private function __construct()
 {
     $con = new Mongo("mongodb://" . MONGO_HOST . ":27017");
     $db = $con->selectDB(MONGO_DATABASE);
     // Connect to Database
     $this->mGrid = $db->getGridFS();
 }
Beispiel #4
0
 private static function _UpdateReport($url, $reportName)
 {
     $_host = parse_url($url, PHP_URL_HOST);
     $_today = new MongoDate(strtotime('today'));
     $connected = false;
     while (!$connected) {
         try {
             $_mdb = new Mongo();
             $connected = true;
         } catch (MongoConnectionException $e) {
             //echo "\nMongoConnectionException: ".$e->getMessage();
             sleep(1);
         }
     }
     $_reports = $_mdb->selectDB("googleproxy")->reports;
     $_rowFilter = array('host' => $_host, 'date' => $_today);
     $success = false;
     while (!$success) {
         try {
             if (!$_reports->findOne($_rowFilter)) {
                 $_reports->insert(array_merge($_rowFilter, array(ReportType::CacheHitCount => array('count' => 0), ReportType::RequestCount => array('count' => 0))));
             }
             $success = true;
         } catch (MongoCursorException $e) {
             //echo "\nMongoCursorException: ".$e->getMessage();
             sleep(1);
         }
     }
     $_reports->ensureIndex(array('report' => 1, 'date' => 1), array('background' => true));
     $_update = array('$inc' => array($reportName . '.count' => 1));
     $_options['multiple'] = false;
     $_reports->update($_rowFilter, $_update, $_options);
 }
Beispiel #5
0
 public function __construct()
 {
     $mongo = new Mongo();
     $mongoDb = $mongo->selectDB('ContactsManager');
     $this->contactsManagerCollection = new MongoCollection($mongoDb, 'Contact');
     $this->contactsManagerCollection->ensureIndex(array('email' => 1), array('unique' => true, 'dropDups' => true));
 }
Beispiel #6
0
 /**
  * Creates a mongo connection and connects.
  *
  * @throws MongoConnectionException, Exception
  * @return MongoDB database object
  */
 private function createInstance()
 {
     //Pull these from a config file
     jimport('sml.smlconfig');
     $conf = new SMLConfig();
     $serverList = $conf->mongo_hosts;
     $database = $conf->mongo_db;
     $persistent = $conf->mongo_persistent;
     $paired = $conf->mongo_paired;
     //End config entries
     if (count($serverList) > 2) {
         throw new Exception("Connection can be established to 2 or fewer instances only.");
     }
     if (count($serverList) == 2) {
         $paired = true;
         $servers = implode(',', $serverList);
     } else {
         $servers = $serverList[0];
     }
     try {
         $m = new Mongo($servers, true, $persistent, $paired);
         $db = $m->selectDB($database);
     } catch (Exception $e) {
         //we should swallow this, and likely put a site down message up..
         die('<pre>' . $e->getTraceAsString());
     }
     return $db;
 }
Beispiel #7
0
 protected function conn()
 {
     if (!isset(self::$_db_handle[$this->db_config_name])) {
         if (false == ($iconfig = C($this->db_config_name, 'db'))) {
             show_error('数据库配制文件db.config.php中 ' . $this->db_config_name . '  未设置。');
         }
         $DSN = 'mongodb://';
         // exp: mongodb://192.168.1.222
         if ($iconfig['user']) {
             $DSN .= $iconfig['user'];
         }
         if ($iconfig['password']) {
             $DSN .= ':' . $iconfig['password'];
         }
         if ($DSN != 'mongodb://') {
             $DSN .= '@';
         }
         if ($iconfig['host']) {
             $DSN .= $iconfig['host'];
         }
         if (!empty($iconfig['port'])) {
             $DSN .= ':' . $iconfig['port'];
         }
         $mongoDB = new Mongo($DSN);
         self::$_db_handle[$this->db_config_name] = $this->db = $mongoDB->selectDB($iconfig['dbname']);
     } else {
         $this->db = self::$_db_handle[$this->db_config_name];
     }
     return $this->db;
 }
Beispiel #8
0
 /**
  * $dsn has to contain db_name after the host. E.g. "mongodb://localhost:27017/mongo_test_db"
  *
  * @static
  * @param $dsn
  * @param $user
  * @param $password
  * @return \Mongo
  * @throws \Exception
  */
 public function __construct($dsn, $user, $password)
 {
     /* defining DB name */
     $this->dbName = substr($dsn, strrpos($dsn, '/') + 1);
     if (strlen($this->dbName) == 0) {
         throw new \Exception('Please specify valid $dsn with DB name after the host:port');
     }
     /* defining host */
     if (false !== strpos($dsn, 'mongodb://')) {
         $this->host = str_replace('mongodb://', '', $dsn);
     } else {
         $this->host = $dsn;
     }
     $this->host = rtrim(str_replace($this->dbName, '', $this->host), '/');
     $options = array('connect' => TRUE);
     if ($user && $password) {
         $options += array('username' => $user, 'password' => $password);
     }
     try {
         $m = new \Mongo($dsn, $options);
         $this->dbh = $m->selectDB($this->dbName);
     } catch (\MongoConnectionException $e) {
         throw new \Exception(sprintf('Failed to open Mongo connection: %s', $e->getMessage()));
     }
     $this->dsn = $dsn;
     $this->user = $user;
     $this->password = $password;
 }
Beispiel #9
0
 function __construct()
 {
     $this->conf = new Conf();
     $m = new Mongo();
     $db = $m->selectDB($this->conf->db());
     $this->selfColl = $db->zone;
 }
 public function __construct()
 {
     $debugOutput = false;
     $mongoDir = "/usr";
     if (!file_exists($mongoDir . '/bin/mongod')) {
         throw new PHPUnit_Framework_SkippedTestError('mongo daemon not found');
     }
     static $m;
     if (!isset($m)) {
         $port = Kwf_Util_Tcp::getFreePort(rand(27020, 30000));
         $cmd = "php " . KWF_PATH . "/bootstrap.php test forward --controller=kwf_model_mongo_run-temp-mongo --port={$port}";
         if ($debugOutput) {
             echo $cmd . "\n";
             $descriptorspec = array(1 => STDOUT, 2 => STDOUT);
         } else {
             $cmd .= " 2>&1 >/dev/null";
             $descriptorspec = array(1 => array('pipe', 'w'), 2 => STDOUT);
         }
         self::$_proc = new Kwf_Util_Proc($cmd, $descriptorspec);
         sleep(3);
         $m = new Mongo("mongodb://localhost:{$port}");
         register_shutdown_function(array('Kwf_Model_Mongo_TestModel', 'shutDown'));
     }
     $db = $m->selectDB("modeltest");
     $db->selectCollection($this->_collection)->drop();
     $config = array('db' => $db);
     parent::__construct($config);
 }
Beispiel #11
0
 protected static function createConnection()
 {
     $server = isset($GLOBALS['mongo_server']) ? $GLOBALS['mongo_server'] : 'mongodb://localhost:27017';
     $dbName = isset($GLOBALS['mongo_db_name']) ? $GLOBALS['mongo_db_name'] : 'task_queue_tests';
     $mongo = new \Mongo($server);
     return $mongo->selectDb($dbName);
 }
Beispiel #12
0
 /**
  * Registers the module-only services
  *
  * @param Phalcon\DI $di
  */
 public function registerServices($di)
 {
     /**
      * Read configuration
      */
     $config = (include __DIR__ . "/config/config.php");
     $di['view']->setViewsDir(__DIR__ . '/views/');
     /**
      * The database connection is created based on the parameters defined in the configuration file
      */
     $di['db'] = function () use($config) {
         return new DbAdapter(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname));
     };
     /**
      * The mongo connection is created
      */
     $di['mongo'] = function () {
         $mongo = new \Mongo();
         return $mongo->selectDb("biko");
     };
     /**
      * ODM Collection Manager
      */
     $di['collectionManager'] = function () {
         return new CollectionManager();
     };
     /**
      * Unique Cart ID generator
      */
     $di['unique'] = function () {
         return new Generator();
     };
 }
 public static function add_point($lat, $lon, $point_name)
 {
     $m = new Mongo();
     $db = $m->selectDB("your_db");
     $collection = $db->your_collection;
     $collection->insert(array("point_name" => $point_name, "loc" => array("lon" => $lon, "lat" => $lat)));
 }
 /**
  * 构造查询
  *
  * @param Mongo $mongo MongoDB连接
  * @param string $db 数据库
  * @param string $collection 集合
  */
 function __construct(Mongo $mongo, $db, $collection)
 {
     $this->_dbName = $db;
     $this->_collectionName = $collection;
     $this->_db = $mongo->selectDB($this->_dbName);
     $this->_collection = $mongo->selectCollection($this->_dbName, $this->_collectionName);
 }
 public function connect()
 {
     // Connect to the database and return the collection
     $mongo = new Mongo('mongodb://localhost');
     $db = $mongo->selectDB('test');
     return $db->selectCollection('locations');
 }
Beispiel #16
0
 function __construct()
 {
     $this->conf = new Conf();
     $m = new Mongo();
     $db = $m->selectDB($this->conf->db());
     $this->placeColl = $db->place;
     $this->yakCatColl = $db->yakcat;
     $this->filesourceColl = $db->filesource;
     $this->title = '';
     $this->content = '';
     $this->thumb = '';
     $this->origin = '';
     $this->filesourceId = '';
     $this->filesourceTitle = '';
     $this->access = 1;
     $this->licence = '';
     $this->outGoingLink = '';
     $this->yakTag = array();
     $this->yakCat = array();
     $this->humanCat = array();
     $this->freeTag = array();
     $this->creationDate = time();
     $this->lastModifDate = time();
     $this->location = new Location();
     $this->address = new Address();
     $this->formatted_address = "";
     $this->contact = new Contact();
     $this->status = 0;
     $this->user = 0;
     $this->zone = 0;
 }
function insert_from_cursor()
{
    $m = new Mongo("localhost");
    $m->connect();
    $db = $m->selectDB("test-database");
    $collection = $db->successfulTweets;
    $successfulTweets = $collection->find();
    // echo "<pre>";
    foreach ($successfulTweets as $tweet) {
        // var_dump($tweet);
        $text = $tweet["text"];
        $author = $tweet["user_name"];
        $tweet_url = "https://twitter.com/statuses/" . $tweet["tweet_id"];
        $handle_url = "https://twitter.com/" . $tweet["user_name"];
        if ($tweet["tweet_id"] == Null) {
            $title = "<span>--manual_post<span>: " . $text;
            $content = "This post was manually submitted";
            $author = "--manual_post";
        } else {
            $title = "<span>" . $author . "</span>: " . $text . "<p> - [<a href=\"" . $tweet_url . "\" class=\"header-tweet-link\">link</a> | <a href=\"" . $handle_url . "\" class=\"header-handle-link\">@" . $author . "</a>]</p>";
            $content = "<a href=\"" . $tweet_url . "\">Check out the original tweet here: https://twitter.com/statuses/" . $tweet["tweet_id"];
            $author = $tweet["user_name"];
        }
        if ($tweet["tweeted_yet"] == True) {
            // echo "Already posted";
        } else {
            // echo "Not yet posted";
            $myPost = array('post_title' => $title, 'post_content' => $content, 'post_status' => 'publish', 'post_author' => $author);
            // var_dump($myPost);
            $collection->update(array('_id' => $tweet['_id']), array('$set' => array('tweeted_yet' => True)));
            $newVar = wp_insert_post($myPost);
        }
    }
    // echo "</pre>";
}
 /**
  * Sets up the fixture, for example, opens a network connection.
  * This method is called before a test is executed.
  *
  * @access protected
  */
 protected function setUp()
 {
     $m = new Mongo();
     $this->object = $m->selectCollection('phpunit', 'c');
     $this->object->drop();
     //        $this->object->start = memory_get_usage(true);
 }
Beispiel #19
0
 /**
  * 构造函数,创建Mongo操作对象,并选择库,选择集合
  * @param $collection 集合名称
  */
 public function __construct($collection = 'guess_you_like_document')
 {
     $conn = new Mongo('mongodb://othermongodb:30000');
     $db = $conn->selectDB('cms');
     $this->myCollection = $db->selectCollection($collection);
     return $this->myCollection;
 }
Beispiel #20
0
 /**
  * Constructs a new memcache cache driver and sets its configuration.
  * 
  * @param array $config The Memcache configuration.
  * 
  * @return Mongo
  */
 public function __construct(array $config = [])
 {
     $this->config = array_merge($this->config, $config);
     $mongo = new \Mongo($this->config['dsn'], $this->config['options']);
     $mongodb = $mongo->selectDB($this->config['db']);
     $this->collection = $mongodb->selectCollection($this->config['collection']);
     $this->collection->ensureIndex(['_id' => 1, 'expires' => 1], ['background' => true]);
 }
Beispiel #21
0
 static function db()
 {
     if (is_null(self::$_db)) {
         $conn = new Mongo();
         self::$_db = $conn->selectDB(self::get_config('db_name'));
     }
     return self::$_db;
 }
Beispiel #22
0
 /**
  * Simple connect method to get the database queries up and running.
  *
  * @return  void
  */
 public function connect()
 {
     if (!is_object($this->link)) {
         $mc = new Mongo($this->dsn);
         // Select the DB
         $this->link = $mc->selectDB($this->name);
     }
 }
 /**
  * @param string $databaseName
  * @return bool
  */
 public static function hasDB($databaseName)
 {
     $databases = static::$_mongo->listDBs();
     $result = array_filter($databases['databases'], function ($item) use($databaseName) {
         return $item['name'] == $databaseName;
     });
     return count($result) != 0;
 }
Beispiel #24
0
 protected function tearDown()
 {
     if (!empty($this->_connectionString) && extension_loaded('mongo')) {
         $this->_model = null;
         $connection = new \Mongo($this->_connectionString);
         $connection->dropDB($this->_dbName);
     }
 }
Beispiel #25
0
 /**
  * @return \MongoCollection
  */
 public function getCollection()
 {
     if (!$this->collection) {
         $mongo = new \Mongo(sprintf('mongodb://%s', implode(',', $this->settings['servers'])));
         $this->collection = $mongo->selectDB($this->settings['database'])->selectCollection($this->settings['collection']);
     }
     return $this->collection;
 }
 /**
  * Internal convenience method that returns the instance of the MongoDB Collection
  *
  * @return \MongoCollection
  */
 protected function getMongo()
 {
     if ($this->mongo === null) {
         $mongo = new \Mongo($this->dsn);
         list($database, $collection, ) = explode('/', substr(parse_url($this->dsn, PHP_URL_PATH), 1));
         $this->mongo = $mongo->selectCollection($database, $collection);
     }
     return $this->mongo;
 }
 public function __construct()
 {
     $m = new Mongo();
     $db = $m->selectDB("tribunal");
     $this->Incidents = $db->incidents;
     $this->Cases = $db->cases;
     $this->ReportedChampions = $db->reported_champions;
     $this->ReportReasons = $db->report_reasons;
 }
Beispiel #28
0
 /**
  * Get collection
  *
  * @return \MongoCollection
  */
 protected function _getCollection()
 {
     if (null === $this->_collection) {
         $connection = new \Mongo($this->_options['connection_string'], $this->_options['mongo_options']);
         $database = $connection->selectDB($this->_options['db']);
         $this->_collection = $database->selectCollection($this->_options['collection']);
     }
     return $this->_collection;
 }
 /**
  * Sets up the fixture, for example, opens a network connection.
  * This method is called before a test is executed.
  *
  * @access protected
  */
 protected function setUp()
 {
     $m = new Mongo();
     $db = $m->selectDB('phpunit');
     $grid = $db->getGridFS();
     $grid->drop();
     $grid->storeFile('tests/somefile');
     $this->object = $grid->find();
     $this->object->start = memory_get_usage(true);
 }
 public function testRef()
 {
     ini_set("mongo.cmd", ":");
     $this->object->insert(array("_id" => 123, "hello" => "world"));
     $this->object->insert(array("_id" => 456, "ref" => array(":ref" => "bar", ":id" => 123)));
     $ref = $this->object->findOne(array("_id" => 456));
     $obj = MongoDBRef::get($this->object->db, $ref["ref"]);
     $this->assertNotNull($obj);
     $this->assertEquals("world", $obj["hello"], json_encode($obj));
 }