<commit_before><?php

/**
 * This file contains a MySQLnd Userspace Handler for gathering
 * and storing MySQL SQL query statistics.
 *
 * PHP Version 5.3
 *
 * @category   Libraries
 * @package    Database
 * @subpackage Libraries
 * @author     <NAME> <<EMAIL>>
 * @author     M2Mobi <<EMAIL>>
 */

namespace Lunr\Libraries\Database;

/**
 * MySQLnd Userspace Handler to log Query Statistics.
 * Stats are logged into a sqlite database.
 *
 * @category   Libraries
 * @package    Database
 * @subpackage Libraries
 * @author     <NAME> <<EMAIL>>
 * @author     M2Mobi <<EMAIL>>
 */
class MySQLndQueryLogger extends MySQLndUhConnection {

    /**
     * Intercept MySQL queries.
     *
     * @param Resource $connection MySQL database connection
     * @param String   $query      SQL query to run
     *
     * @return mixed $return Return value of mysqli_query
     */
    public function query($connection, $query)
    {
        $id = $this->get_function_call_hierarchy(xdebug_get_function_stack());

        $start = microtime(TRUE);
        $return = parent::query($connection, $query);
        $time = microtime(TRUE) - $start;

        $this->record_query_stats($id, $time);

        return $return;
    }

    /**
     * Creates a function call string.
     *
     * This takes the function stack as input and transforms it
     * into a unique identifier for a query.
     *
     * @param array $stack The Function Stack
     *
     * @return String $hierarchy The Function call string
     */
    private function get_function_call_hierarchy($stack)
    {
        $hierarchy = '';
        foreach ($stack as &$value)
        {
            if (isset($value['class']))
            {
                $hierarchy .= $value['class'] . '->' . $value['function'] . '()=>';
            }
            else
            {
                $hierarchy .= $value['function'] . '()=>';
            }
        }
        unset($value);

        $hierarchy = trim($hierarchy, '=>');
        return $hierarchy;
    }

    /**
     * Record SQL Query statistics.
     *
     * @param String $identifier SQL Query identifier
     * @param Float  $time       Measured runtime of the query
     *
     * @return void
     */
    private function record_query_stats($identifier, $time)
    {
        global $stats_db;
        $sqlite = DBMan::get_db_connection($stats_db, FALSE);

        $data = array(
                    'queryIdentifier' => $identifier,
                    'execTime' => $time,
                    'execDate' => M2DateTime::get_datetime()
                );
        $sqlite->insert('query_stats', $data);
    }

}

?><commit_msg>Database: Fix MySQLndQueryLogger for new DateTime API.<commit_after><?php

/**
 * This file contains a MySQLnd Userspace Handler for gathering
 * and storing MySQL SQL query statistics.
 *
 * PHP Version 5.3
 *
 * @category   Libraries
 * @package    Database
 * @subpackage Libraries
 * @author     <NAME> <<EMAIL>>
 * @author     M2Mobi <<EMAIL>>
 */

namespace Lunr\Libraries\Database;
use Lunr\Libraries\Core\DateTime;

/**
 * MySQLnd Userspace Handler to log Query Statistics.
 * Stats are logged into a sqlite database.
 *
 * @category   Libraries
 * @package    Database
 * @subpackage Libraries
 * @author     <NAME> <<EMAIL>>
 * @author     M2Mobi <<EMAIL>>
 */
class MySQLndQueryLogger extends MySQLndUhConnection
{

    /**
     * Instance of the DateTime class.
     * @var DateTime
     */
    private $datetime;

    /**
     * Constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->datetime = new DateTime();
    }

    /**
     * Destructor.
     */
    public function __destruct()
    {
        unset($this->datetime);
        parent::__destruct();
    }

    /**
     * Intercept MySQL queries.
     *
     * @param Resource $connection MySQL database connection
     * @param String   $query      SQL query to run
     *
     * @return mixed $return Return value of mysqli_query
     */
    public function query($connection, $query)
    {
        $id = $this->get_function_call_hierarchy(xdebug_get_function_stack());

        $start = microtime(TRUE);
        $return = parent::query($connection, $query);
        $time = microtime(TRUE) - $start;

        $this->record_query_stats($id, $time);

        return $return;
    }

    /**
     * Creates a function call string.
     *
     * This takes the function stack as input and transforms it
     * into a unique identifier for a query.
     *
     * @param array $stack The Function Stack
     *
     * @return String $hierarchy The Function call string
     */
    private function get_function_call_hierarchy($stack)
    {
        $hierarchy = '';
        foreach ($stack as &$value)
        {
            if (isset($value['class']))
            {
                $hierarchy .= $value['class'] . '->' . $value['function'] . '()=>';
            }
            else
            {
                $hierarchy .= $value['function'] . '()=>';
            }
        }
        unset($value);

        $hierarchy = trim($hierarchy, '=>');
        return $hierarchy;
    }

    /**
     * Record SQL Query statistics.
     *
     * @param String $identifier SQL Query identifier
     * @param Float  $time       Measured runtime of the query
     *
     * @return void
     */
    private function record_query_stats($identifier, $time)
    {
        global $stats_db;
        $sqlite = DBMan::get_db_connection($stats_db, FALSE);

        $data = array(
            'queryIdentifier' => $identifier,
            'execTime' => $time,
            'execDate' => $this->datetime->set_datetime_format('%Y-%m-%d %H-%M-%S')->get_datetime()
        );
        $sqlite->insert('query_stats', $data);
    }

}

?>
{'fraction_non_alphanumeric': 0.12554262892863344, 'fraction_numerical': 0.0015627713144643167, 'mean_word_length': 2.0573248407643314, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 50, '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': '4227766', 'n_tokens_mistral': 1701, 'n_tokens_neox': 1637, 'n_words': 430}