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
This commit is contained in:
kjmtsh 2014-07-30 23:44:12 +00:00
parent d4b86db22e
commit 5e95bc00c4
3 changed files with 73 additions and 28 deletions

View File

@ -43,7 +43,7 @@ class PDODB extends wpdb {
* *
* @see wpdb::__construct() * @see wpdb::__construct()
*/ */
function __construct() { public function __construct() {
register_shutdown_function(array($this, '__destruct')); register_shutdown_function(array($this, '__destruct'));
if (WP_DEBUG) if (WP_DEBUG)
@ -60,7 +60,7 @@ class PDODB extends wpdb {
* *
* @see wpdb::__destruct() * @see wpdb::__destruct()
*/ */
function __destruct() { public function __destruct() {
return true; return true;
} }
@ -71,7 +71,7 @@ class PDODB extends wpdb {
* *
* @see wpdb::set_charset() * @see wpdb::set_charset()
*/ */
function set_charset($dbh, $charset = null, $collate = null) { public function set_charset($dbh, $charset = null, $collate = null) {
if ( ! isset( $charset ) ) if ( ! isset( $charset ) )
$charset = $this->charset; $charset = $this->charset;
if ( ! isset( $collate ) ) if ( ! isset( $collate ) )
@ -82,7 +82,7 @@ class PDODB extends wpdb {
* *
* @see wpdb::set_sql_mode() * @see wpdb::set_sql_mode()
*/ */
function set_sql_mode($modes = array()) { public function set_sql_mode($modes = array()) {
unset($modes); unset($modes);
return; return;
} }
@ -93,7 +93,7 @@ class PDODB extends wpdb {
* *
* @see wpdb::select() * @see wpdb::select()
*/ */
function select($db, $dbh = null) { public function select($db, $dbh = null) {
if (is_null($dbh)) if (is_null($dbh))
$dbh = $this->dbh; $dbh = $this->dbh;
$this->ready = true; $this->ready = true;
@ -116,7 +116,18 @@ class PDODB extends wpdb {
function _real_escape($string) { function _real_escape($string) {
return addslashes($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. * Method to put out the error message.
* *
@ -124,7 +135,7 @@ class PDODB extends wpdb {
* *
* @see wpdb::print_error() * @see wpdb::print_error()
*/ */
function print_error($str = '') { public function print_error($str = '') {
global $EZSQL_ERROR; global $EZSQL_ERROR;
if (!$str) { if (!$str) {
@ -172,7 +183,7 @@ class PDODB extends wpdb {
* *
* @see wpdb::flush * @see wpdb::flush
*/ */
function flush() { public function flush() {
$this->last_result = array(); $this->last_result = array();
$this->col_info = null; $this->col_info = null;
$this->last_query = null; $this->last_query = null;
@ -187,7 +198,7 @@ class PDODB extends wpdb {
* *
* @see wpdb::db_connect() * @see wpdb::db_connect()
*/ */
function db_connect($allow_bail=true) { public function db_connect($allow_bail=true) {
if (WP_DEBUG) { if (WP_DEBUG) {
$this->dbh = new PDOEngine(); $this->dbh = new PDOEngine();
} else { } else {
@ -202,13 +213,14 @@ class PDODB extends wpdb {
} }
$is_enabled_foreign_keys = @$this->get_var('PRAGMA foreign_keys'); $is_enabled_foreign_keys = @$this->get_var('PRAGMA foreign_keys');
if ($is_enabled_foreign_keys == '0') @$this->query('PRAGMA foreign_keys = ON'); if ($is_enabled_foreign_keys == '0') @$this->query('PRAGMA foreign_keys = ON');
$this->has_connected = true;
$this->ready = true; $this->ready = true;
} }
/** /**
* Method to dummy out wpdb::check_connection() * Method to dummy out wpdb::check_connection()
* *
*/ */
function check_connection($allow_bail=true) { public function check_connection($allow_bail=true) {
return true; return true;
} }
/** /**
@ -219,7 +231,7 @@ class PDODB extends wpdb {
* *
* @see wpdb::query() * @see wpdb::query()
*/ */
function query($query) { public function query($query) {
if (!$this->ready) if (!$this->ready)
return false; return false;
@ -267,7 +279,14 @@ class PDODB extends wpdb {
return $return_val; 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) { private function _do_query($query) {
if (defined('SAVEQUERIES') && SAVEQUERIES) { if (defined('SAVEQUERIES') && SAVEQUERIES) {
@ -301,7 +320,7 @@ class PDODB extends wpdb {
* *
* @see wpdb::has_cap() * @see wpdb::has_cap()
*/ */
function has_cap($db_cap) { public function has_cap($db_cap) {
switch(strtolower($db_cap)) { switch(strtolower($db_cap)) {
case 'collation': case 'collation':
case 'group_concat': case 'group_concat':
@ -322,7 +341,7 @@ class PDODB extends wpdb {
* *
* @see wpdb::db_version() * @see wpdb::db_version()
*/ */
function db_version() { public function db_version() {
//global $required_mysql_version; //global $required_mysql_version;
//return $required_mysql_version; //return $required_mysql_version;
return '5.5'; return '5.5';

View File

@ -163,15 +163,36 @@ class PDOEngine extends PDO {
* @param none * @param none
*/ */
function __construct() { function __construct() {
register_shutdown_function(array($this, '__destruct'));
$this->init(); $this->init();
} }
/** /**
* Destructor * Destructor
* *
* If SQLITE_MEM_DEBUG constant is defined, append information about
* memory usage into database/mem_debug.txt.
*
* @return boolean * @return boolean
*/ */
function __destruct() { 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; return true;
} }
@ -726,20 +747,25 @@ class PDOEngine extends PDO {
//long queries can really kill this //long queries can really kill this
$pattern = '/(?<!\\\\)([\'"])(.*?)(?<!\\\\)\\1/imsx'; $pattern = '/(?<!\\\\)([\'"])(.*?)(?<!\\\\)\\1/imsx';
$_limit = $limit = ini_get('pcre.backtrack_limit'); $_limit = $limit = ini_get('pcre.backtrack_limit');
do { // if user's setting is more than default * 10, make PHP do the job.
if ($limit > 10000000) { if ($limit > 10000000) {
$message = 'The query is too big to parse properly'; $query = preg_replace_callback($pattern, array($this, 'replace_variables_with_placeholders'), $this->rewritten_query);
$this->set_error(__LINE__, __FUNCTION__, $message); } else {
break; //no point in continuing execution, would get into a loop do {
} else { if ($limit > 10000000) {
ini_set('pcre.backtrack_limit', $limit); $message = 'The query is too big to parse properly';
$query = preg_replace_callback($pattern, array($this,'replace_variables_with_placeholders'), $this->rewritten_query); $this->set_error(__LINE__, __FUNCTION__, $message);
} break; //no point in continuing execution, would get into a loop
$limit = $limit * 10; } else {
} while (empty($query)); 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 //reset the pcre.backtrack_limist
ini_set('pcre.backtrack_limit', $_limit); ini_set('pcre.backtrack_limit', $_limit);
}
$this->queries[] = "With Placeholders:\n" . $query; $this->queries[] = "With Placeholders:\n" . $query;
$this->prepared_query = $query; $this->prepared_query = $query;
} }

View File

@ -130,7 +130,7 @@ class CreateQuery{
'longblob' => 'blob', 'longtext' => 'text' 'longblob' => 'blob', 'longtext' => 'text'
); );
foreach ($array_types as $o=>$r){ foreach ($array_types as $o=>$r){
$pattern = "/\\b(?<!`)$o\\b\\s*(\([^\)]*\)*)?\\s*/ims"; $pattern = "/\\b(?<!')$o\\b\\s*(\([^\)]*\)*)?\\s*/ims";
if (preg_match("/^\\s*.*?\\s*\(.*?$o.*?\)/im", $this->_query)) { if (preg_match("/^\\s*.*?\\s*\(.*?$o.*?\)/im", $this->_query)) {
; ;
} else { } else {