Compatibility with PHP < 5.3

This commit is contained in:
Jakub Vrana 2018-01-22 17:30:12 +01:00
parent 83e16e059f
commit 0d2b232bd7

View file

@ -231,7 +231,8 @@ if (isset($_GET["mongo"])) {
$options["db"] = $db; $options["db"] = $db;
} }
try { try {
$this->_link = new MongoDB\Driver\Manager("mongodb://$server", $options); $class = 'MongoDB\Driver\Manager';
$this->_link = new $class("mongodb://$server", $options);
return true; return true;
} catch (Exception $ex) { } catch (Exception $ex) {
$this->error = $ex->getMessage(); $this->error = $ex->getMessage();
@ -344,7 +345,8 @@ if (isset($_GET["mongo"])) {
} }
$limit = min(200, max(1, (int) $limit)); $limit = min(200, max(1, (int) $limit));
$skip = $page * $limit; $skip = $page * $limit;
$query = new MongoDB\Driver\Query($where, array('projection' => $select, 'limit' => $limit, 'skip' => $skip, 'sort' => $sort)); $class = 'MongoDB\Driver\Query';
$query = new $class($where, array('projection' => $select, 'limit' => $limit, 'skip' => $skip, 'sort' => $sort));
$results = $connection->_link->executeQuery("$connection->_db_name.$table", $query); $results = $connection->_link->executeQuery("$connection->_db_name.$table", $query);
return new Min_Result($results); return new Min_Result($results);
} }
@ -353,7 +355,8 @@ if (isset($_GET["mongo"])) {
global $connection; global $connection;
$db = $connection->_db_name; $db = $connection->_db_name;
$where = sql_query_where_parser($queryWhere); $where = sql_query_where_parser($queryWhere);
$bulk = new MongoDB\Driver\BulkWrite(array()); $class = 'MongoDB\Driver\BulkWrite';
$bulk = new $class(array());
if (isset($set['_id'])) { if (isset($set['_id'])) {
unset($set['_id']); unset($set['_id']);
} }
@ -378,7 +381,8 @@ if (isset($_GET["mongo"])) {
global $connection; global $connection;
$db = $connection->_db_name; $db = $connection->_db_name;
$where = sql_query_where_parser($queryWhere); $where = sql_query_where_parser($queryWhere);
$bulk = new MongoDB\Driver\BulkWrite(array()); $class = 'MongoDB\Driver\BulkWrite';
$bulk = new $class(array());
$bulk->delete($where, array('limit' => $limit)); $bulk->delete($where, array('limit' => $limit));
$results = $connection->_link->executeBulkWrite("$db.$table", $bulk); $results = $connection->_link->executeBulkWrite("$db.$table", $bulk);
$connection->affected_rows = $results->getDeletedCount(); $connection->affected_rows = $results->getDeletedCount();
@ -388,7 +392,8 @@ if (isset($_GET["mongo"])) {
function insert($table, $set) { function insert($table, $set) {
global $connection; global $connection;
$db = $connection->_db_name; $db = $connection->_db_name;
$bulk = new MongoDB\Driver\BulkWrite(array()); $class = 'MongoDB\Driver\BulkWrite';
$bulk = new $class(array());
if (isset($set['_id']) && empty($set['_id'])) { if (isset($set['_id']) && empty($set['_id'])) {
unset($set['_id']); unset($set['_id']);
} }
@ -403,7 +408,8 @@ if (isset($_GET["mongo"])) {
/** @var $connection Min_DB */ /** @var $connection Min_DB */
global $connection; global $connection;
$return = array(); $return = array();
$command = new MongoDB\Driver\Command(array('listDatabases' => 1)); $class = 'MongoDB\Driver\Command';
$command = new $class(array('listDatabases' => 1));
$results = $connection->_link->executeCommand('admin', $command); $results = $connection->_link->executeCommand('admin', $command);
foreach ($results as $dbs) { foreach ($results as $dbs) {
foreach ($dbs->databases as $db) { foreach ($dbs->databases as $db) {
@ -420,7 +426,8 @@ if (isset($_GET["mongo"])) {
function tables_list() { function tables_list() {
global $connection; global $connection;
$command = new MongoDB\Driver\Command(array('listCollections' => 1)); $class = 'MongoDB\Driver\Command';
$command = new $class(array('listCollections' => 1));
$results = $connection->_link->executeCommand($connection->_db_name, $command); $results = $connection->_link->executeCommand($connection->_db_name, $command);
$collections = array(); $collections = array();
foreach ($results as $result) { foreach ($results as $result) {
@ -436,7 +443,8 @@ if (isset($_GET["mongo"])) {
function indexes($table, $connection2 = null) { function indexes($table, $connection2 = null) {
global $connection; global $connection;
$return = array(); $return = array();
$command = new MongoDB\Driver\Command(array('listIndexes' => $table)); $class = 'MongoDB\Driver\Command';
$command = new $class(array('listIndexes' => $table));
$results = $connection->_link->executeCommand($connection->_db_name, $command); $results = $connection->_link->executeCommand($connection->_db_name, $command);
foreach ($results as $index) { foreach ($results as $index) {
$descs = array(); $descs = array();
@ -483,7 +491,8 @@ if (isset($_GET["mongo"])) {
function found_rows($table_status, $where) { function found_rows($table_status, $where) {
global $connection; global $connection;
$where = where_to_query($where); $where = where_to_query($where);
$command = new MongoDB\Driver\Command(array('count' => $table_status['Name'], 'query' => $where)); $class = 'MongoDB\Driver\Command';
$command = new $class(array('count' => $table_status['Name'], 'query' => $where));
$results = $connection->_link->executeCommand($connection->_db_name, $command); $results = $connection->_link->executeCommand($connection->_db_name, $command);
$toArray = $results->toArray(); $toArray = $results->toArray();
return $toArray[0]->n; return $toArray[0]->n;
@ -516,7 +525,8 @@ if (isset($_GET["mongo"])) {
if ($col == "_id") { if ($col == "_id") {
$val = str_replace('MongoDB\BSON\ObjectID("', "", $val); $val = str_replace('MongoDB\BSON\ObjectID("', "", $val);
$val = str_replace('")', "", $val); $val = str_replace('")', "", $val);
$val = new MongoDB\BSON\ObjectID($val); $class = 'MongoDB\BSON\ObjectID';
$val = new $class($val);
} }
if (!in_array($op, $operators)) { if (!in_array($op, $operators)) {
continue; continue;
@ -525,8 +535,9 @@ if (isset($_GET["mongo"])) {
$val = (float) $val; $val = (float) $val;
$op = $match[1]; $op = $match[1];
} elseif (preg_match('~^\(date\)(.+)~', $op, $match)) { } elseif (preg_match('~^\(date\)(.+)~', $op, $match)) {
$dateTime = new \DateTime($val); $dateTime = new DateTime($val);
$val = new MongoDB\BSON\UTCDatetime($dateTime->getTimestamp() * 1000); $class = 'MongoDB\BSON\UTCDatetime';
$val = new $class($dateTime->getTimestamp() * 1000);
$op = $match[1]; $op = $match[1];
} }
switch ($op) { switch ($op) {