Skip to content
Snippets Groups Projects
Commit f7c13600 authored by lucha's avatar lucha
Browse files

added hyperdb, to show how a drop-in plugin would work

parent 98abb3a2
No related branches found
No related tags found
No related merge requests found
...@@ -5,3 +5,8 @@ Some rough ideas on how the situation could be improved: ...@@ -5,3 +5,8 @@ Some rough ideas on how the situation could be improved:
- the ```upstream``` branch should disappear. We should not have to care ourselves on keeping a sync of Wordpress source code - the ```upstream``` branch should disappear. We should not have to care ourselves on keeping a sync of Wordpress source code
- we should really just keep the patches and our contributions - we should really just keep the patches and our contributions
- we need to be able to build a docker container in the end - we need to be able to build a docker container in the end
## About drop-in plugins
Drop-in plugins, such as ```Hyperdb```, do not have to be installed in the plugins directory, but we have instead to place files directly into wp-content.
We have to thus manually specify where they have to go.
...@@ -23,11 +23,14 @@ ...@@ -23,11 +23,14 @@
"johnpbloch/wordpress": "^5.4", "johnpbloch/wordpress": "^5.4",
"bjornjohansen/wplang": "^0.1.1", "bjornjohansen/wplang": "^0.1.1",
"lkwdwrd/wp-muplugin-loader": "^1.0", "lkwdwrd/wp-muplugin-loader": "^1.0",
"wpackagist-theme/minimalism": "1.0.3" "wpackagist-theme/minimalism": "1.0.3",
"koodimonni/composer-dropin-installer": "^1.2",
"wpackagist-plugin/hyperdb": "^1.6"
}, },
"extra": { "extra": {
"wordpress-install-dir": "web/wp", "wordpress-install-dir": "web/wp",
"installer-paths": { "installer-paths": {
"vendor/{$name}": ["wpackagist-plugin/hyperdb"],
"web/app/mu-plugins/{$name}/": ["type:wordpress-muplugin"], "web/app/mu-plugins/{$name}/": ["type:wordpress-muplugin"],
"web/app/plugins/{$name}/": ["type:wordpress-plugin"], "web/app/plugins/{$name}/": ["type:wordpress-plugin"],
"web/app/themes/{$name}/": ["type:wordpress-theme"] "web/app/themes/{$name}/": ["type:wordpress-theme"]
...@@ -37,6 +40,9 @@ ...@@ -37,6 +40,9 @@
"patches-file": "composer.patches.json", "patches-file": "composer.patches.json",
"force-mu": [ "force-mu": [
"disable-wordpress-updates" "disable-wordpress-updates"
] ],
"dropin-paths": {
"web/app": ["package:wpackagist-plugin/hyperdb:db.php"]
}
} }
} }
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "6f5c67e11208010e0856439723cf37c1", "content-hash": "7857809a1c2b51ea4d9581f6e2bcc2c3",
"packages": [ "packages": [
{ {
"name": "bjornjohansen/wplang", "name": "bjornjohansen/wplang",
...@@ -344,6 +344,49 @@ ...@@ -344,6 +344,49 @@
], ],
"time": "2018-11-09T20:10:38+00:00" "time": "2018-11-09T20:10:38+00:00"
}, },
{
"name": "koodimonni/composer-dropin-installer",
"version": "1.2",
"source": {
"type": "git",
"url": "https://github.com/Koodimonni/Composer-Dropin-Installer.git",
"reference": "a749f19e3a3bc05529190961ed7529592b20138e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Koodimonni/Composer-Dropin-Installer/zipball/a749f19e3a3bc05529190961ed7529592b20138e",
"reference": "a749f19e3a3bc05529190961ed7529592b20138e",
"shasum": ""
},
"require": {
"composer-plugin-api": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "3.7.*-dev"
},
"type": "composer-plugin",
"extra": {
"class": "Koodimonni\\Composer\\Dropin"
},
"autoload": {
"psr-4": {
"Koodimonni\\Composer\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"WTFPL"
],
"authors": [
{
"name": "Onni Hakala",
"email": "onni@koodimonni.fi",
"role": "Developer"
}
],
"description": "Install packages or a few files from packages into custom paths without overwriting existing stuff.",
"time": "2018-02-04T10:52:01+00:00"
},
{ {
"name": "lkwdwrd/wp-muplugin-loader", "name": "lkwdwrd/wp-muplugin-loader",
"version": "1.0.5", "version": "1.0.5",
...@@ -393,6 +436,31 @@ ...@@ -393,6 +436,31 @@
], ],
"time": "2018-04-20T22:09:51+00:00" "time": "2018-04-20T22:09:51+00:00"
}, },
{
"name": "wpackagist-plugin/hyperdb",
"version": "1.6",
"source": {
"type": "svn",
"url": "https://plugins.svn.wordpress.org/hyperdb/",
"reference": "tags/1.6"
},
"dist": {
"type": "zip",
"url": "https://downloads.wordpress.org/plugin/hyperdb.1.6.zip",
"reference": null,
"shasum": null
},
"require": {
"composer/installers": "~1.0"
},
"type": "wordpress-plugin",
"extra": {
"patches_applied": {
"hyperdb logging": "patches/hyperdb.patch"
}
},
"homepage": "https://wordpress.org/plugins/hyperdb/"
},
{ {
"name": "wpackagist-theme/minimalism", "name": "wpackagist-theme/minimalism",
"version": "1.0.3", "version": "1.0.3",
......
{ {
"patches": { "patches": {
"wpackagist-plugin/disable-wordpress-updates": { "wpackagist-plugin/disable-wordpress-updates": {
"Patch title": "patches/0001-Patched-Disable-Updates-to-run-checks-during-cron-jo.patch" "Disable automatic updates": "patches/0001-Patched-Disable-Updates-to-run-checks-during-cron-jo.patch"
}, },
"wpackagist-theme/minimalism": { "wpackagist-theme/minimalism": {
"Minimalism theme footer": "patches/0001-Patch-theme-minimalism.patch" "Minimalism theme footer": "patches/0001-Patch-theme-minimalism.patch"
} },
} "wpackagist-plugin/hyperdb": {
"hyperdb logging": "patches/hyperdb.patch"
}
}
} }
diff --git a/db.php b/db.php
index f900d305..8829d102 100644
--- a/db.php
+++ b/db.php
@@ -51,6 +50,13 @@ define( 'HYPERDB_CONN_HOST_ERROR', 2003 ); // Can't connect to MySQL server on
define( 'HYPERDB_SERVER_GONE_ERROR', 2006 ); // MySQL server has gone away
class hyperdb extends wpdb {
+
+ /**
+ * A/I patch!
+ * store a FlexiHash() instance here.
+ */
+ var $hash_map;
+
/**
* The last table that was queried
* @var string
@@ -306,6 +312,12 @@ class hyperdb extends wpdb {
. ')\W([\w-]+)\W/is', $q, $maybe) )
return $maybe[1];
+ // SHOW TABLES LIKE (used in some plugins)
+ if ( preg_match('/^\s*'
+ . 'SHOW\s+TABLES\s+LIKE\s+'
+ . '\W(\w+)\W/is', $q, $maybe) )
+ return $maybe[1];
+
// Big pattern for the rest of the table-related queries in MySQL 5.0
if ( preg_match('/^\s*(?:'
. '(?:EXPLAIN\s+(?:EXTENDED\s+)?)?SELECT.*?\s+FROM'
@@ -401,6 +413,11 @@ class hyperdb extends wpdb {
$this->run_callbacks( 'dataset_found', $dataset );
+ if (WP_DEBUG === true) {
+ $_ai_msg = sprintf("We choose dataset %s for query '%s'", $this->dataset, $query);
+ $this->print_error($_ai_msg);
+ }
+
if ( empty( $this->hyper_servers ) ) {
if ( $this->is_mysql_connection( $this->dbh ) )
return $this->dbh;
@@ -665,16 +682,19 @@ class hyperdb extends wpdb {
$success = false;
$this->last_connection = compact('dbhname', 'host', 'port', 'user', 'name', 'tcp', 'elapsed', 'success');
$this->db_connections[] = $this->last_connection;
- $msg = date( "Y-m-d H:i:s" ) . " Can't select $dbhname - \n";
- $msg .= "'referrer' => '{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}',\n";
- $msg .= "'server' => {$server},\n";
- $msg .= "'host' => {$host},\n";
- $msg .= "'error' => " . $this->ex_mysql_error() . ",\n";
- $msg .= "'errno' => " . $this->ex_mysql_errno() . ",\n";
- $msg .= "'server_state' => $server_state\n";
- $msg .= "'lagged_status' => " . ( isset( $lagged_status ) ? $lagged_status : HYPERDB_LAG_UNKNOWN );
-
- $this->print_error( $msg );
+ /* Workaround to stop logging a/i */
+ if ( AI_LOG_HYPERDB == true ) {
+ $msg = date( "Y-m-d H:i:s" ) . " Can't select $dbhname - \n";
+ $msg .= "'referrer' => '{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}',\n";
+ $msg .= "'server' => {$server},\n";
+ $msg .= "'host' => {$host},\n";
+ $msg .= "'error' => " . $this->ex_mysql_error() . ",\n";
+ $msg .= "'errno' => " . $this->ex_mysql_errno() . ",\n";
+ $msg .= "'server_state' => $server_state\n";
+ $msg .= "'lagged_status' => " . ( isset( $lagged_status ) ? $lagged_status : HYPERDB_LAG_UNKNOWN );
+
+ $this->print_error( $msg );
+ }
}
if ( ! $success || ! isset( $this->dbhs[$dbhname] ) || ! $this->is_mysql_connection( $this->dbhs[$dbhname] ) ) {
@@ -709,6 +729,15 @@ class hyperdb extends wpdb {
$this->set_charset($this->dbhs[$dbhname], $charset, $collate);
+ if ( !isset( $charset ) )
+ $charset = null;
+
+ if ( !isset( $collate ) )
+ $collate = null;
+
+ // AI workaround: set all queries to latin1
+ $this->set_charset($this->dbhs[$dbhname], 'latin1');
+
$this->dbh = $this->dbhs[$dbhname]; // needed by $wpdb->_real_escape()
$this->last_used_server = compact('host', 'user', 'name', 'read', 'write');
@@ -857,6 +886,11 @@ class hyperdb extends wpdb {
$statement_after_query = $this->run_callbacks( 'statement_after_query' );
$query_for_log = $query;
+ // A/I: prevent mysql-proxy to cache the admin section
+ if (strpos($_SERVER['REQUEST_URI'], '/wp-admin/') !== false) {
+ $query .= ' /* NO CACHE */';
+ }
+
$this->timer_start();
if ( $statement_before_query ) {
$query_for_log = "$statement_before_query; $query_for_log";
@@ -1428,6 +1462,23 @@ class hyperdb extends wpdb {
} // class hyperdb
-$wpdb = new hyperdb();
+/**
+ * AI custom: Logs all queries for debugging purposes
+ */
+function ai_log_db_queries($query, $time, $backtrace=null, hyperdb $obj ) {
+ $fh = fopen( ABSPATH . '/profiling/noblogs_queries_'. date('Ymd') . '.log', 'a');
+ if (!$fh) {
+ return array($query, $time, $backtrace);
+ }
+ fwrite($fh, sprintf("##\n#Date: %s\n#Query time: %s\n%s\n", date('r'), $time, $query));
+ fclose($fh);
+ return array($query, $time, $backtrace);
+}
+
+$wpdb = new hyperdb();
+if ( AI_DB_PROFILER === true ) {
+ $wpdb->save_queries = true;
+ $wpdb->save_query_callback = 'ai_log_db_queries';
+}
require( DB_CONFIG_FILE );
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment