Skip to content

Commit

Permalink
Merge pull request #53 from PostgreSQL-For-Wordpress/mb-fix-install
Browse files Browse the repository at this point in the history
Migrate install rewriting to new rewriters
  • Loading branch information
mattbucci authored Nov 7, 2023
2 parents 3754ae3 + ed00779 commit 8a19f4c
Show file tree
Hide file tree
Showing 14 changed files with 569 additions and 388 deletions.
35 changes: 14 additions & 21 deletions pg4wp/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

// This is required by class-wpdb so we must load it first
require_once ABSPATH . '/wp-includes/version.php';
require_once ABSPATH . '/wp-includes/cache.php';
require_once ABSPATH . '/wp-includes/l10n.php';

Expand All @@ -20,39 +21,31 @@ function wpsql_is_resource($object)
}
}

// Logs are put in the pg4wp directory
define( 'PG4WP_LOG', PG4WP_ROOT.'/logs/');
// Check if the logs directory is needed and exists or create it if possible
if( (PG4WP_DEBUG || PG4WP_LOG_ERRORS) &&
!file_exists( PG4WP_LOG) &&
is_writable(dirname( PG4WP_LOG)))
mkdir( PG4WP_LOG);

// Load the driver defined in 'db.php'
require_once( PG4WP_ROOT.'/driver_'.DB_DRIVER.'.php');
require_once(PG4WP_ROOT . '/driver_' . DB_DRIVER . '.php');

// This loads up the wpdb class applying appropriate changes to it
$replaces = array(
'define( ' => '// define( ',
'class wpdb' => 'class wpdb2',
'new wpdb' => 'new wpdb2',
'mysql_' => 'wpsql_',
'is_resource' => 'wpsql_is_resource',
'<?php' => '',
'?>' => '',
'define( ' => '// define( ',
'class wpdb' => 'class wpdb2',
'new wpdb' => 'new wpdb2',
'mysql_' => 'wpsql_',
'is_resource' => 'wpsql_is_resource',
'<?php' => '',
'?>' => '',
);

// Ensure class uses the replaced mysql_ functions rather than mysqli_
if (!defined('WP_USE_EXT_MYSQL')) {
define('WP_USE_EXT_MYSQL', true);
define('WP_USE_EXT_MYSQL', true);
}
if (WP_USE_EXT_MYSQL != true) {
throw new \Exception("PG4SQL CANNOT BE ENABLED WITH MYSQLI, REMOVE ANY WP_USE_EXT_MYSQL configuration");
throw new \Exception("PG4SQL CANNOT BE ENABLED WITH MYSQLI, REMOVE ANY WP_USE_EXT_MYSQL configuration");
}

eval(str_replace(array_keys($replaces), array_values($replaces), file_get_contents(ABSPATH . '/wp-includes/class-wpdb.php')));

// Create wpdb object if not already done
if (! isset($wpdb) && defined('DB_USER')) {
$wpdb = new wpdb2( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST );
}
if (!isset($wpdb) && defined('DB_USER')) {
$wpdb = new wpdb2(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
}
21 changes: 15 additions & 6 deletions pg4wp/db.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,25 @@

// This defines the directory where PG4WP files are loaded from
// 3 places checked : wp-content, wp-content/plugins and the base directory
if(file_exists(ABSPATH . '/wp-content/pg4wp')) {
define('PG4WP_ROOT', ABSPATH . '/wp-content/pg4wp');
} elseif(file_exists(ABSPATH . '/wp-content/plugins/pg4wp')) {
define('PG4WP_ROOT', ABSPATH . '/wp-content/plugins/pg4wp');
} elseif(file_exists(ABSPATH . '/pg4wp')) {
define('PG4WP_ROOT', ABSPATH . '/pg4wp');
if(file_exists(ABSPATH . 'wp-content/pg4wp')) {
define('PG4WP_ROOT', ABSPATH . 'wp-content/pg4wp');
} elseif(file_exists(ABSPATH . 'wp-content/plugins/pg4wp')) {
define('PG4WP_ROOT', ABSPATH . 'wp-content/plugins/pg4wp');
} elseif(file_exists(ABSPATH . 'pg4wp')) {
define('PG4WP_ROOT', ABSPATH . 'pg4wp');
} else {
die('PG4WP file directory not found');
}

// Logs are put in the pg4wp directory
if (!defined('PG4WP_LOG')) {
define('PG4WP_LOG', PG4WP_ROOT . '/logs/');
}
// Check if the logs directory is needed and exists or create it if possible
if((PG4WP_DEBUG || PG4WP_LOG_ERRORS) && !file_exists(PG4WP_LOG) && is_writable(dirname(PG4WP_LOG))) {
mkdir(PG4WP_LOG);
}

// Here happens all the magic
require_once(PG4WP_ROOT . '/core.php');
} // Protection against multiple loading
120 changes: 80 additions & 40 deletions pg4wp/driver_mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,83 @@
* Provides a driver for MySQL
* This file remaps all wpsql_* calls to mysql_* original name
*/
function wpsql_num_rows($result)
{ return mysql_num_rows($result); }
function wpsql_numrows($result)
{ return mysql_num_rows($result); }
function wpsql_num_fields($result)
{ return mysql_num_fields($result); }
function wpsql_fetch_field($result)
{ return mysql_fetch_field($result); }
function wpsql_fetch_object($result)
{ return mysql_fetch_object($result); }
function wpsql_free_result($result)
{ return mysql_free_result($result); }
function wpsql_affected_rows()
{ return mysql_affected_rows(); }
function wpsql_fetch_row($result)
{ return mysql_fetch_row($result); }
function wpsql_data_seek($result, $offset)
{ return mysql_data_seek( $result, $offset ); }
function wpsql_error()
{ return mysql_error();}
function wpsql_fetch_assoc($result)
{ return mysql_fetch_assoc($result); }
function wpsql_escape_string($s)
{ return mysql_real_escape_string($s); }
function wpsql_real_escape_string($s,$c=NULL)
{ return mysql_real_escape_string($s,$c); }
function wpsql_get_server_info()
{ return mysql_get_server_info(); }
function wpsql_result($result, $i, $fieldname)
{ return mysql_result($result, $i, $fieldname); }
function wpsql_connect($dbserver, $dbuser, $dbpass)
{ return mysql_connect($dbserver, $dbuser, $dbpass); }
function wpsql_fetch_array($result)
{ return mysql_fetch_array($result); }
function wpsql_select_db($dbname, $connection_id)
{ return mysql_select_db($dbname, $connection_id); }
function wpsql_query($sql)
{ return mysql_query($sql); }
function wpsql_insert_id($table)
{ return mysql_insert_id($table); }
function wpsql_num_rows($result)
{
return mysql_num_rows($result);
}
function wpsql_numrows($result)
{
return mysql_num_rows($result);
}
function wpsql_num_fields($result)
{
return mysql_num_fields($result);
}
function wpsql_fetch_field($result)
{
return mysql_fetch_field($result);
}
function wpsql_fetch_object($result)
{
return mysql_fetch_object($result);
}
function wpsql_free_result($result)
{
return mysql_free_result($result);
}
function wpsql_affected_rows()
{
return mysql_affected_rows();
}
function wpsql_fetch_row($result)
{
return mysql_fetch_row($result);
}
function wpsql_data_seek($result, $offset)
{
return mysql_data_seek($result, $offset);
}
function wpsql_error()
{
return mysql_error();
}
function wpsql_fetch_assoc($result)
{
return mysql_fetch_assoc($result);
}
function wpsql_escape_string($s)
{
return mysql_real_escape_string($s);
}
function wpsql_real_escape_string($s, $c = null)
{
return mysql_real_escape_string($s, $c);
}
function wpsql_get_server_info()
{
return mysql_get_server_info();
}
function wpsql_result($result, $i, $fieldname)
{
return mysql_result($result, $i, $fieldname);
}
function wpsql_connect($dbserver, $dbuser, $dbpass)
{
return mysql_connect($dbserver, $dbuser, $dbpass);
}
function wpsql_fetch_array($result)
{
return mysql_fetch_array($result);
}
function wpsql_select_db($dbname, $connection_id)
{
return mysql_select_db($dbname, $connection_id);
}
function wpsql_query($sql)
{
return mysql_query($sql);
}
function wpsql_insert_id($table)
{
return mysql_insert_id($table);
}
4 changes: 2 additions & 2 deletions pg4wp/driver_pgsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ function pg4wp_init()
$result = pg_query($connection, $sql);

if ((PG4WP_DEBUG || PG4WP_LOG_ERRORS) && $result === false) {
$error = pg_last_error();
$error = pg_last_error($connection);
error_log("[" . microtime(true) . "] Error creating MySQL-compatible field function: $error\n", 3, PG4WP_LOG . 'pg4wp_errors.log');
}
}
Expand Down Expand Up @@ -558,4 +558,4 @@ function wpsql_get_server_info()
function wpsql_get_client_info()
{
return '8.0.35'; // Just want to fool wordpress ...
}
}
Loading

0 comments on commit 8a19f4c

Please sign in to comment.