Conflict with db_set_active and variable_get I am running a drupal multi-site system, with shared code and seperated DB. Each individual multi-site represents one continent and can have different language versions.

What i want:
I am working on a module, that gathers all available languages on all multi-sites into one big continent/language map.

To query cross-multi-site data i use db_set_active([multisite]) and manually fetch some data via db_query().

foreach ($multisites as $multisite) { 
  db_set_active($multisite['db']); //switch context
  $links = db_query(Select language from {languages} etc...)) // run query
  do_somthing_with_data($links) //work with data
  db_set_active(); //switch to default
}


This works pretty fine, 
but there are already drupal-core functions in language.inc i want to use (e.g. language_negotiation_get_switch_links()).

foreach ($multisites as $multisite) { 
  db_set_active($multisite['db']); //switch context
  $links = language_negotiation_get_switch_links(...,...) // run core-function
  db_set_active(); //switch to default
}


Here is my problem:
Several core functions use variable_set() and variable_get(). They are persistent and initialized on bootstrap. 

When changing the active database with db_set_active(), incoming queries have the new context, but values fetched with variable_get() are inconsistent, because they store data from default database, filled on bootstrap.

In a nutshell:
I want to run core-functions on one multi-site with data from another multi-site. db_set_active() works fine, but data is inconsistent because variable_get() uses data, which is initialized on bootstrap.

Has anyone ever worked with this issue?

Kind regards
larry

Addition:

As I got deeper into development it became more and more complicated. I don't recommend the procedure of cross-multi-site-core-function-calling, because the initialization of core-functionality is triggered during bootstrap.

Here an example:

The multi-site you are browsing to does not need to initialize language.inc, because it has just one language-version. Further in code-progress you switch context to a multi-site with two or more languages but language.inc functionality is not available, because it was not initialized.
I've been reading around, and on db_set_active  API page on d.o the 2 comments on that page mention the problem with caches and multi dbs. 

So I went on to look at variable_get API page and it uses global $conf variable. I did a quick search on google on how to "refresh global variables conf" and got this page on d.o DrupalWebTestCase::refreshVariables(). Basically this function refreshes the global variables like so:

protected function refreshVariables() {
  global $conf;
  cache_clear_all('variables', 'cache_bootstrap');
  $conf = variable_initialize();
}


So I'm guessing (I haven't tried it and I don't have a multisite installation at hand to test it) you just need to utilise the code from the above function and clear the variables cache and re-initialize the variables in your foreach loop. I'm not sure how efficient it is so that's another issue if it's not efficient and slows down your page.

global $conf;
foreach ($multisites as $multisite) { 
  // Not sure if cache_clear_all should be called before or after switching the database. You should test and figure out what's best.
  cache_clear_all('variables', 'cache_bootstrap');
  db_set_active($multisite['db']); //switch context
  // Re-set $conf global variable so when variable_get gets called it uses the updated $conf.
  $conf = variable_initialize();
  $links = language_negotiation_get_switch_links(...,...) // run core-function
  db_set_active(); //switch to default
}


Hope this helps steer you in the right direction. Good luck