<commit_before>
<?php

/**
 * This Class is for MongoDB related operations like connect , create,update,read and remove/delete
 *
 * In this class we have the functions @todo Long Desc
 *
 * @example <br/>
 * $obj=new Obj(); //This is automatically called from cDatabase,Singleton obj so it will connect if the connection is not available <br>
 * $obj->read();//Will by default perform sort,limit,offset if the attribs are present<br>
 * $obj->update();<br>
 * $obj->create();<br>
 * $obj->remove();<br>
 *
 *
 */
//include AppLoggerModule . 'cMLogger.php';


class cMongo {

    public $connection;
    //collection
    public $table;
    public $condition;
    //fields
    public $column;
    //DB
    public $db;
    public $orderby;
    public $limit;
    public $offset;
    public $group_by;
    public $result;
    private $cursor;
    public $returnType = "json";
    private $mLogger = '';

    public function __construct($newDatabaseInfo) {
        $this->getConnection($newDatabaseInfo);

    }

    public function getConnection($newDatabaseInfo) {
        if (!$this->db) {
            $dbCredencials = '';
            if ($newDatabaseInfo['user'] && $newDatabaseInfo['pass']) {
                $dbCredencials = $newDatabaseInfo['user'] . ':' . $newDatabaseInfo['pass'] . '@';
            }
            $this->connection = new MongoDB\Client('mongodb://' . $dbCredencials . $newDatabaseInfo['host'] . ':' . $newDatabaseInfo['port'],
                    [],
                    ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']]);
            $this->db = $this->connection->{$newDatabaseInfo['name']};
//            $this->mLogger = new cMLogger();
        }

    }

    private function logger($functionname, $type = 'debug', $exception = null) {
        $message = array("Function" => $functionname,
            " Table " => $this->table,
            "Columns" => json_encode($this->column),
            "Condition" => json_encode($this->condition),
            "Orderby" => json_encode($this->orderby),
            "Limit" => json_encode($this->limit),
            "Offset" => json_encode($this->offset)
        );
        if ($type == 'error') {
            $exception = array("Error Message" => $exception->getMessage(),
                "Error Code" => $exception->getCode(),
                "Error File" => $exception->getFile(),
                "Error Trace" => $exception->getTraceAsString(),
            );
            $this->mLogger->error(array_merge($message, $exception));
        } else {
            $this->mLogger->debug($message);
        }

    }

    public function create() {
        try {
            $seq = $this->getNextSequence();
            if ($seq != '') {
                $this->column['_id'] = $this->getNextSequence();
            }
            $this->replaceMetaFields();
            $this->result = $this->db->{$this->table}->insertOne($this->column);

//            $this->logger(__FUNCTION__,"debug");
            $this->resetDefaults();
            return $this->result->getInsertedId();
        } catch (Exception $e) {
//            $this->logger(__FUNCTION__,"error",$e);
            return $e->getMessage();
        }

    }

    public function delete() {
        try {
            //$this->resetDefaults();
//            $this->logger(__FUNCTION__,"debug");
            $this->result = $this->db->{$this->table}->deleteMany($this->condition);
            return $this->result->getDeletedCount();
        } catch (Exception $e) {
//            $this->logger(__FUNCTION__,"error",$e);
            return $e->getMessage();
        }

    }

    public function aggregate() {

//        $adminDB = $this->connection->admin; //require admin priviledge
//
//        $mongodb_info = $adminDB->command(array('buildinfo' => true));
//        $mongodb_version = $mongodb_info['version'];
//
        $this->cursor = $this->db->{$this->table}->aggregateCursor(array('$group' => $this->group_by,
            '$sort' => $this->order_by, '$limit' => $this->limit));
        foreach ($this->cursor as $doc) {
            $this->result[] = $doc;
        }


        $this->resetDefaults();
        return $this->result;

    }

    public function drop() {
        try {
            //$this->resetDefaults();
            return $this->db->{$this->table}->drop();
        } catch (Exception $e) {
//        $this->logger(__FUNCTION__,"error",$e);
            return $e->getMessage();
        }

    }

    public function read() {
        try {

            $this->result = array();

            if ($this->group_by) {

                if (count($this->condition) > 0) {
                    $options['condition'] = $this->condition;
                }
                //$this->group_by[0] == Columns to be grouped
                //$this->group_by[1] == Initial values to be returned
                //$this->group_by[2] == Javascript function to reduce the array

                $this->cursor = $this->db->{$this->table}->group($this->group_by[0],
                        $this->group_by[1], $this->group_by[2], $options);
                $this->result = $this->cursor['retval'];
            } else {
                if (!is_array($this->condition)) {
                    $this->condition = array();
                }
                if (is_array($this->column)) {
                    $findOptions['projection'] = $this->column;
                }

                if ($this->orderby) {

                    $findOptions['sort'] = $this->orderby;
                }
                if ($this->limit) {

                    $findOptions['limit'] = $this->limit;
                }
                if ($this->offset) {
                    $findOptions['skip'] = $this->offset;
                }
                if ($this->columns) {
                    
                }
                $findOptions['typeMap'] = array('root' => 'array', 'document' => 'array',
                    'array' => 'array');
                $this->cursor = $this->db->{$this->table}->find($this->condition,
                        $findOptions);

                foreach ($this->cursor as $doc) {
                    foreach ($doc as $columnName => $columnData) {
                        if (is_object($columnData)) {
                            $doc[$columnName] = (string) $columnData;
                        }
                    }
                    $this->result[] = $doc;
                }
//                $this->result=(MongoDB\BSON\toJSON($this->result));
//                $this->cursor->unserialize();
//                print_r(json_encode($this->result,JSON_FORCE_OBJECT));
            }
//            $this->logger(__FUNCTION__,"debug");
            $this->resetDefaults();
            return $this->result;
        } catch (Exception $e) {
//            $this->logger(__FUNCTION__,"error",$e);
            return $e->getMessage();
        }

    }

    function objectToArray($data) {
        if (is_object($data)) {<commit_msg>Local changes<commit_after>
<?php

/**
 * This Class is for MongoDB related operations like connect , create,update,read and remove/delete
 *
 * In this class we have the functions @todo Long Desc
 *
 * @example <br/>
 * $obj=new Obj(); //This is automatically called from cDatabase,Singleton obj so it will connect if the connection is not available <br>
 * $obj->read();//Will by default perform sort,limit,offset if the attribs are present<br>
 * $obj->update();<br>
 * $obj->create();<br>
 * $obj->remove();<br>
 *
 *
 */
//include AppLoggerModule . 'cMLogger.php';


class cMongo {

    public $connection;
    //collection
    public $table;
    public $condition;
    //fields
    public $column;
    //DB
    public $db;
    public $orderby;
    public $limit;
    public $offset;
    public $group_by;
    public $result;
    private $cursor;
    public $returnType = "json";
    private $mLogger = '';

    public function __construct($newDatabaseInfo) {
        $this->getConnection($newDatabaseInfo);

    }

    public function getConnection($newDatabaseInfo) {
        if (!$this->db) {
            $dbCredencials = '';
            if ($newDatabaseInfo['user'] && $newDatabaseInfo['pass']) {
                $dbCredencials = $newDatabaseInfo['user'] . ':' . $newDatabaseInfo['pass'] . '@';
            }
            $this->connection = new MongoDB\Client('mongodb://' . $dbCredencials . $newDatabaseInfo['host'] . ':' . $newDatabaseInfo['port'],
                    [],
                    ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']]);
            $this->db = $this->connection->{$newDatabaseInfo['name']};
//            $this->mLogger = new cMLogger();
        }

    }

    private function logger($functionname, $type = 'debug', $exception = null) {
        $message = array("Function" => $functionname,
            " Table " => $this->table,
            "Columns" => json_encode($this->column),
            "Condition" => json_encode($this->condition),
            "Orderby" => json_encode($this->orderby),
            "Limit" => json_encode($this->limit),
            "Offset" => json_encode($this->offset)
        );
        if ($type == 'error') {
            $exception = array("Error Message" => $exception->getMessage(),
                "Error Code" => $exception->getCode(),
                "Error File" => $exception->getFile(),
                "Error Trace" => $exception->getTraceAsString(),
            );
            $this->mLogger->error(array_merge($message, $exception));
        } else {
            $this->mLogger->debug($message);
        }

    }

    public function create() {
        try {
            $seq = $this->getNextSequence();
            if ($seq != '') {
                $this->column['_id'] = $this->getNextSequence();
            }
            $this->replaceMetaFields();
            $this->result = $this->db->{$this->table}->insertOne($this->column);

//            $this->logger(__FUNCTION__,"debug");
            $this->resetDefaults();
            return $this->result->getInsertedId();
        } catch (Exception $e) {
//            $this->logger(__FUNCTION__,"error",$e);
            return $e->getMessage();
        }

    }

    public function delete() {
        try {
            //$this->resetDefaults();
//            $this->logger(__FUNCTION__,"debug");
            $this->result = $this->db->{$this->table}->deleteMany($this->condition);
            return $this->result->getDeletedCount();
        } catch (Exception $e) {
//            $this->logger(__FUNCTION__,"error",$e);
            return $e->getMessage();
        }

    }

    public function aggregate() {

//        $adminDB = $this->connection->admin; //require admin priviledge
//
//        $mongodb_info = $adminDB->command(array('buildinfo' => true));
//        $mongodb_version = $mongodb_info['version'];
//
        $this->cursor = $this->db->{$this->table}->aggregateCursor(array('$group' => $this->group_by,
            '$sort' => $this->order_by, '$limit' => $this->limit));
        foreach ($this->cursor as $doc) {
            $this->result[] = $doc;
        }


        $this->resetDefaults();
        return $this->result;

    }

    public function drop() {
        try {
            //$this->resetDefaults();
            return $this->db->{$this->table}->drop();
        } catch (Exception $e) {
//        $this->logger(__FUNCTION__,"error",$e);
            return $e->getMessage();
        }

    }

    public function read() {
        try {

            $this->result = array();

            if ($this->group_by) {

                if (count($this->condition) > 0) {
                    $options['condition'] = $this->condition;
                }
                //$this->group_by[0] == Columns to be grouped
                //$this->group_by[1] == Initial values to be returned
                //$this->group_by[2] == Javascript function to reduce the array

                $this->cursor = $this->db->{$this->table}->group($this->group_by[0],
                        $this->group_by[1], $this->group_by[2], $options);
                $this->result = $this->cursor['retval'];
            } else {
                if (!is_array($this->condition)) {
                    $this->condition = array();
                }
                if (is_array($this->column)) {
                    $findOptions['projection'] = $this->column;
                }

                if ($this->orderby) {

                    $findOptions['sort'] = $this->orderby;
                }
                if ($this->limit) {

                    $findOptions['limit'] = $this->limit;
                }
                if ($this->offset) {
                    $findOptions['skip'] = $this->offset;
                }
                if ($this->column) {
                    $findOptions['projection']=$this->column;
                }
                $findOptions['typeMap'] = array('root' => 'array', 'document' => 'array',
                    'array' => 'array');
                $this->cursor = $this->db->{$this->table}->find($this->condition,
                        $findOptions);

                foreach ($this->cursor as $doc) {
                    foreach ($doc as $columnName => $columnData) {
                        if (is_object($columnData)) {
                            $doc[$columnName] = (string) $columnData;
                        }
                    }
                    $this->result[] = $doc;
                }
//                $this->result=(MongoDB\BSON\toJSON($this->result));
//                $this->cursor->unserialize();
//                print_r(json_encode($this->result,JSON_FORCE_OBJECT));
            }
//            $this->logger(__FUNCTION__,"debug");
            $this->resetDefaults();
            return $this->result;
        } catch (Exception $e) {
//            $this->logger(__FUNCTION__,"error",$e);
            return $e->getMessage();
        }

    }

    function objectToArray($data) {
        if (is_object($data)) {
{'fraction_non_alphanumeric': 0.1724482512491078, 'fraction_numerical': 0.0009992862241256246, 'mean_word_length': 1.7242854365156524, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 306, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1249962', 'n_tokens_mistral': 3956, 'n_tokens_neox': 3791, 'n_words': 712}