From 0231e8c83541ae8be336e10e89851402fea372c7 Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Fri, 19 Feb 2021 15:35:46 +0000
Subject: [PATCH] Update code for new $r2db_hash_map global

---
 bin/generate-noblogsmv-map |  6 +++++-
 lib/blogs.php              | 19 ++++++++++++++++---
 2 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/bin/generate-noblogsmv-map b/bin/generate-noblogsmv-map
index 771bb33..a462dfe 100755
--- a/bin/generate-noblogsmv-map
+++ b/bin/generate-noblogsmv-map
@@ -134,7 +134,11 @@ function backend_to_http_endpoint($backend) {
 // Print the blog -> backend map.
 function generate_map() {
   global $wpdb;
-  $wpdb_hash = &$wpdb->hash_map;
+  global $r2db_hash_map;
+  $wpdb_hash = $r2db_hash_map;
+  if (!$wpdb_hash) {
+    $wpdb_hash = &$wpdb->hash_map;
+  }
 
   $blogs = get_blogs();
 
diff --git a/lib/blogs.php b/lib/blogs.php
index 257827b..6978775 100644
--- a/lib/blogs.php
+++ b/lib/blogs.php
@@ -28,6 +28,13 @@ function noblogs_get_blog($blogname) {
 // Return the database connection information associated with a blog.
 function noblogs_get_backend_for_blog($blog_id) {
   global $wpdb;
+  global $r2db_hash_map;
+
+  // Compatibility shim between old/new r2db.
+  $hash_map = $r2db_hash_map;
+  if (!$hash_map) {
+    $hash_map = $wpdb->hash_map;
+  }
 
   // Create a temporary hash object just to load the backends
   // (we cannot use the map in $wpdb because it doesn't have the
@@ -37,7 +44,7 @@ function noblogs_get_backend_for_blog($blog_id) {
 
   // Lookup the blog ID using the $wpdb hash just to be safe (though
   // we should get an identical result using $hashmap).
-  $lookup = $wpdb->hash_map->lookup($blog_id);
+  $lookup = $hash_map->lookup($blog_id);
   $backend = $reversemap[$lookup];
 
   // Be nice and split the database host into 'host' and 'port' elements.
@@ -57,14 +64,20 @@ function noblogs_get_backend_for_blog($blog_id) {
 // Return the full backend -> blogs map.
 function noblogs_get_backend_map() {
   global $wpdb;
-  $wpdb_hash = &$wpdb->hash_map;
+  global $r2db_hash_map;
+
+  // Compatibility shim between old/new r2db.
+  $hash_map = $r2db_hash_map;
+  if (!$hash_map) {
+    $hash_map = $wpdb->hash_map;
+  }
 
   $blogs = noblogs_get_blogs();
   $backend_map = array();
 
   foreach ($blogs as $blog) {
     $blog_id = $blog->blog_id;
-    $backend_id = $wpdb_hash->lookup($blog_id);
+    $backend_id = $hash_map->lookup($blog_id);
     if (!array_key_exists($backend_id, $backend_map)) {
       $backend_map[$backend_id] = array();
     }
-- 
GitLab