From 5e95bc00c4b3591a3820e0954cba377d429b8864 Mon Sep 17 00:00:00 2001 From: kjmtsh Date: Wed, 30 Jul 2014 23:44:12 +0000 Subject: [PATCH] Added memory usage check code and changed for the usage of user defined pcre.backtrack_limit value git-svn-id: https://plugins.svn.wordpress.org/sqlite-integration/trunk@957733 b8457f37-d9ea-0310-8a92-e5e31aec5664 --- pdodb.class.php | 45 +++++++++++++++++++++++++---------- pdoengine.class.php | 54 +++++++++++++++++++++++++++++++----------- query_create.class.php | 2 +- 3 files changed, 73 insertions(+), 28 deletions(-) diff --git a/pdodb.class.php b/pdodb.class.php index ad1e269..3765197 100644 --- a/pdodb.class.php +++ b/pdodb.class.php @@ -43,7 +43,7 @@ class PDODB extends wpdb { * * @see wpdb::__construct() */ - function __construct() { + public function __construct() { register_shutdown_function(array($this, '__destruct')); if (WP_DEBUG) @@ -60,7 +60,7 @@ class PDODB extends wpdb { * * @see wpdb::__destruct() */ - function __destruct() { + public function __destruct() { return true; } @@ -71,7 +71,7 @@ class PDODB extends wpdb { * * @see wpdb::set_charset() */ - function set_charset($dbh, $charset = null, $collate = null) { + public function set_charset($dbh, $charset = null, $collate = null) { if ( ! isset( $charset ) ) $charset = $this->charset; if ( ! isset( $collate ) ) @@ -82,7 +82,7 @@ class PDODB extends wpdb { * * @see wpdb::set_sql_mode() */ - function set_sql_mode($modes = array()) { + public function set_sql_mode($modes = array()) { unset($modes); return; } @@ -93,7 +93,7 @@ class PDODB extends wpdb { * * @see wpdb::select() */ - function select($db, $dbh = null) { + public function select($db, $dbh = null) { if (is_null($dbh)) $dbh = $this->dbh; $this->ready = true; @@ -116,7 +116,18 @@ class PDODB extends wpdb { function _real_escape($string) { return addslashes($string); } - + /** + * Method to dummy out wpdb::esc_like() function. + * + * WordPress 4.0.0 introduced esc_like() function that adds backslashes to %, + * underscore and backslash, which is not interpreted as escape character + * by SQLite. So we override it and dummy out this function. + * + * @see wpdb::esc_like() + */ + public function esc_like($text) { + return $text; + } /** * Method to put out the error message. * @@ -124,7 +135,7 @@ class PDODB extends wpdb { * * @see wpdb::print_error() */ - function print_error($str = '') { + public function print_error($str = '') { global $EZSQL_ERROR; if (!$str) { @@ -172,7 +183,7 @@ class PDODB extends wpdb { * * @see wpdb::flush */ - function flush() { + public function flush() { $this->last_result = array(); $this->col_info = null; $this->last_query = null; @@ -187,7 +198,7 @@ class PDODB extends wpdb { * * @see wpdb::db_connect() */ - function db_connect($allow_bail=true) { + public function db_connect($allow_bail=true) { if (WP_DEBUG) { $this->dbh = new PDOEngine(); } else { @@ -202,13 +213,14 @@ class PDODB extends wpdb { } $is_enabled_foreign_keys = @$this->get_var('PRAGMA foreign_keys'); if ($is_enabled_foreign_keys == '0') @$this->query('PRAGMA foreign_keys = ON'); + $this->has_connected = true; $this->ready = true; } /** * Method to dummy out wpdb::check_connection() * */ - function check_connection($allow_bail=true) { + public function check_connection($allow_bail=true) { return true; } /** @@ -219,7 +231,7 @@ class PDODB extends wpdb { * * @see wpdb::query() */ - function query($query) { + public function query($query) { if (!$this->ready) return false; @@ -267,7 +279,14 @@ class PDODB extends wpdb { return $return_val; } /** + * Method for future use? * + * WordPress 3.9 separated the method to execute real query from query() function. + * This is for the restoration from the case that nothing returns from database. + * But this is necessary because we aleady did error manipulations in + * pdoengine.class.php. So we don't use this function. + * + * @access private */ private function _do_query($query) { if (defined('SAVEQUERIES') && SAVEQUERIES) { @@ -301,7 +320,7 @@ class PDODB extends wpdb { * * @see wpdb::has_cap() */ - function has_cap($db_cap) { + public function has_cap($db_cap) { switch(strtolower($db_cap)) { case 'collation': case 'group_concat': @@ -322,7 +341,7 @@ class PDODB extends wpdb { * * @see wpdb::db_version() */ - function db_version() { + public function db_version() { //global $required_mysql_version; //return $required_mysql_version; return '5.5'; diff --git a/pdoengine.class.php b/pdoengine.class.php index a3401c5..84a9d28 100644 --- a/pdoengine.class.php +++ b/pdoengine.class.php @@ -163,15 +163,36 @@ class PDOEngine extends PDO { * @param none */ function __construct() { + register_shutdown_function(array($this, '__destruct')); $this->init(); } /** * Destructor * + * If SQLITE_MEM_DEBUG constant is defined, append information about + * memory usage into database/mem_debug.txt. + * * @return boolean */ function __destruct() { - $this->pdo = null; + if (defined('SQLITE_MEM_DEBUG') && SQLITE_MEM_DEBUG) { + $max = ini_get('memory_limit'); + if (is_null($max)) { + $message = sprintf("[%s] Memory_limit is not set in php.ini file.", date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME'])); + file_put_contents(FQDBDIR . 'mem_debug.txt', $message, FILE_APPEND); + return true; + } + if (stripos($max, 'M') !== false) { + $max = (int) $max * 1024 * 1024; + } + $peak = memory_get_peak_usage(true); + $used = round((int) $peak / (int) $max * 100, 2); + if ($used > 90) { + $message = sprintf("[%s] Memory peak usage warning: %s %% used. (max: %sM, now: %sM)\n", date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']), $used, $max, $peak); + file_put_contents(FQDBDIR . 'mem_debug.txt', $message, FILE_APPEND); + } + } + //$this->pdo = null; return true; } @@ -726,20 +747,25 @@ class PDOEngine extends PDO { //long queries can really kill this $pattern = '/(? 10000000) { - $message = 'The query is too big to parse properly'; - $this->set_error(__LINE__, __FUNCTION__, $message); - break; //no point in continuing execution, would get into a loop - } else { - ini_set('pcre.backtrack_limit', $limit); - $query = preg_replace_callback($pattern, array($this,'replace_variables_with_placeholders'), $this->rewritten_query); - } - $limit = $limit * 10; - } while (empty($query)); + // if user's setting is more than default * 10, make PHP do the job. + if ($limit > 10000000) { + $query = preg_replace_callback($pattern, array($this, 'replace_variables_with_placeholders'), $this->rewritten_query); + } else { + do { + if ($limit > 10000000) { + $message = 'The query is too big to parse properly'; + $this->set_error(__LINE__, __FUNCTION__, $message); + break; //no point in continuing execution, would get into a loop + } else { + ini_set('pcre.backtrack_limit', $limit); + $query = preg_replace_callback($pattern, array($this,'replace_variables_with_placeholders'), $this->rewritten_query); + } + $limit = $limit * 10; + } while (is_null($query)); - //reset the pcre.backtrack_limist - ini_set('pcre.backtrack_limit', $_limit); + //reset the pcre.backtrack_limist + ini_set('pcre.backtrack_limit', $_limit); + } $this->queries[] = "With Placeholders:\n" . $query; $this->prepared_query = $query; } diff --git a/query_create.class.php b/query_create.class.php index e8ff1b0..6cf9ece 100644 --- a/query_create.class.php +++ b/query_create.class.php @@ -130,7 +130,7 @@ class CreateQuery{ 'longblob' => 'blob', 'longtext' => 'text' ); foreach ($array_types as $o=>$r){ - $pattern = "/\\b(?_query)) { ; } else {