<?php


// Return a list of all blog IDs.
// (Does not include deleted and archived blogs).
function noblogs_get_blogs() {
  global $wpdb;
  $sql = "SELECT blog_id FROM $wpdb->blogs WHERE deleted = 0 AND archived = '0'";
  $result = $wpdb->get_results($sql);
  return ($result);
}

// Find a blog by its name or ID.
function noblogs_get_blog($blogname) {
  global $wpdb;
  if (preg_match('/^\d+$/', $blogname)) {
    $sql = "SELECT * FROM $wpdb->blogs WHERE blog_id='" . $blogname . "'";
  } else {
    if (!preg_match('/\.noblogs\.org$/', $blogname)) {
      $blogname = $blogname . '.noblogs.org';
    }
    $sql = "SELECT * FROM $wpdb->blogs WHERE domain = '" . $blogname . "'";
  }
  $result = $wpdb->get_results($sql);
  return $result[0];
}

// Return the database connection information associated with a blog.
function noblogs_get_backend_for_blog($blog_id) {
  global $wpdb;

  // Create a temporary hash object just to load the backends
  // (we cannot use the map in $wpdb because it doesn't have the
  // backend array anymore).
  $hashmap = new Flexihash(null, R2DB_FLEXIHASH_REPLICAS);
  $reversemap = noblogs_load_backends(NOBLOGS_BACKEND_CONFIG, $hashmap);

  // 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);
  $backend = $reversemap[$lookup];

  // Be nice and split the database host into 'host' and 'port' elements.
  $result = array();
  if (preg_match('/^(.*):([0-9]*)$/', $backend['host'], $matches)) {
    $result['host'] = $matches[1];
    $result['port'] = $matches[2];
  } else {
    $result['host'] = $backend['host'];
  }
  $result['user'] = $backend['user'];
  $result['password'] = $backend['password'];
  $result['db'] = $backend['name'];
  return $result;
}

// Return the full backend -> blogs map.
function noblogs_get_backend_map() {
  global $wpdb;
  $wpdb_hash = &$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);
    if (!array_key_exists($backend_id, $backend_map)) {
      $backend_map[$backend_id] = array();
    }
    array_push($backend_map[$backend_id], (int)$blog_id);
  }

  return $backend_map;
}

// Return the backend ID for the local host.
function noblogs_get_local_backend_id() {
  $id = trim(file_get_contents("/etc/ai/public_id"));
  return "backend_{$id}";
}

// Return a list of blogs that are local to this server.
function noblogs_get_local_blogs() {
  $backend_map = noblogs_get_backend_map();
  $local_id = noblogs_get_local_backend_id();
  return $backend_map[$local_id];
}

// Return true if the blog has not been updated in some time.
function noblogs_is_stale($days = 180) {
    global $wpdb;
    $recent = new WP_Query("showposts=1&orderby=modified&post_status=publish");
    if ($recent->have_posts()) {
        $recent->the_post();
        $last_update = get_the_modified_time('U');
        $now = time();
        if ( ($now - $last_update) > 86400*$days )
            return true;
    }
    return false;
}