Commit b222068f authored by Steven Jones's avatar Steven Jones

More performance fixes.

parent 831d7eb1
......@@ -925,25 +925,45 @@ function hosting_client_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
* Small helper function to get platforms that haven't been deleted or locked,
* and are accessible by this user.
*
* We can get called a few times during a page request, so we implement static
* caching for speed, and because the platforms that are available are unlikely
* to change during a single page request.
*
* @param $uid
* The user ID to retrieve the allowed platforms for. If none is specified the
* currently logged in user will be used.
* @param $reset
* Whether to reset the internal static cache or not.
*
* @todo this is not the right way. we need to implement node-level
* access permissions here, the same way we do for sites. see
* http://drupal.org/node/725952
*/
function _hosting_get_allowed_platforms($uid = NULL) {
function _hosting_get_allowed_platforms($uid = NULL, $reset = FALSE) {
static $platforms = array();
if ($reset) {
$platforms = array();
}
if (is_null($uid)) {
global $user;
$uid = $user->uid;
}
$return = array();
$result = db_query("SELECT n.nid, n.title FROM {node} n LEFT JOIN {hosting_platform} h ON n.nid = h.nid
LEFT JOIN {hosting_platform_client_access} p ON n.nid = p.pid
if (!isset($platforms[$uid])) {
$platforms[$uid] = array();
$result = db_query("SELECT n.nid, n.title FROM {node} n LEFT JOIN {hosting_platform} h ON n.nid = h.nid
LEFT JOIN {hosting_platform_client_access} p ON n.nid = p.pid
LEFT JOIN {hosting_client_user} c ON c.client = p.cid
WHERE n.type='platform' AND n.status=1 AND h.status > '%d'
WHERE n.type='platform' AND n.status=1 AND h.status > '%d'
AND (c.user= %d OR p.pid IS NULL)", HOSTING_PLATFORM_LOCKED, $uid);
while($server = db_fetch_object($result)) {
$return[$server->nid] = $server->title;
while($server = db_fetch_object($result)) {
$platforms[$uid][$server->nid] = $server->title;
}
}
return $return;
return $platforms[$uid];
}
/**
......
......@@ -62,6 +62,9 @@ function _hosting_package_instance_languages($instance) {
db_query("INSERT INTO {hosting_package_languages} (iid, language) VALUES (%d, '%s')", $instance->iid, $language);
}
}
// Clear the static cache for languages of package instances.
_hosting_package_instances_load_languages(NULL, TRUE);
}
function hosting_package_instance_update(&$instance) {
......@@ -126,20 +129,40 @@ function _hosting_package_instances_load($param, $multiple = FALSE) {
WHERE " . $cond, $arguments);
while ($instance = db_fetch_object($result)) {
$instance->languages = _hosting_package_instances_load_languages($instance->iid);
if (!$multiple) {
return $instance;
}
$instances[$instance->iid] = $instance;
}
return $instances;
}
/**
* Get the languages associated with an instance of a package.
*/
function _hosting_package_instances_load_languages($instance_id = NULL, $reset = FALSE) {
static $instances = array();
if (!empty($reset)) {
$instances = array();
}
// Build the array of languages if requested and we need to.
if (!is_null($instance_id) && !isset($instances[$instance_id])) {
$languages = array('en' => 'en');
// load language options: if argument is null, load all language options
$lang_result = db_query("SELECT DISTINCT(language) FROM {hosting_package_languages} WHERE iid = %d", $instance->iid);
$lang_result = db_query("SELECT DISTINCT(language) FROM {hosting_package_languages} WHERE iid = %d", $instance_id);
while ($language = db_fetch_object($lang_result)) {
$languages[$language->language] = _hosting_language_name($language->language);
}
$instance->languages = $languages;
if (!$multiple) {
return $instance;
}
$instances[$instance->iid] = $instance;
$instances[$instance_id] = $languages;
}
return $instances;
// Return the array of languages if requested.
return !is_null($instance_id) ? $instances[$instance_id] : NULL;
}
......@@ -210,37 +233,45 @@ function hosting_package_instance_version_code($version) {
function hosting_package_comparison($current, $target) {
_hosting_package_temporary_table("current", $current);
_hosting_package_temporary_table("target", $target);
$current_tbl = _hosting_package_temporary_table($current);
$target_tbl = _hosting_package_temporary_table($target);
$status = array();
$result = db_query("SELECT count(c.nid) AS error FROM current c LEFT JOIN target t ON c.nid=t.nid WHERE (t.schema_version > 0) && (c.schema_version > t.schema_version) AND c.status=1");
$result = db_query("SELECT count(c.nid) AS error FROM %s c LEFT JOIN %s t ON c.nid=t.nid WHERE (t.schema_version > 0) && (c.schema_version > t.schema_version) AND c.status=1", $current_tbl, $target_tbl);
while ($obj = db_fetch_object($result)) {
$status['error'] = $obj->error;
}
$result = db_query("SELECT COUNT(c.nid) as missing FROM current c LEFT JOIN target t ON c.nid=t.nid WHERE t.nid IS null AND c.status=1");
$result = db_query("SELECT COUNT(c.nid) as missing FROM %s c LEFT JOIN %s t ON c.nid=t.nid WHERE t.nid IS null AND c.status=1", $current_tbl, $target_tbl);
while ($obj = db_fetch_object($result)) {
$status['missing'] = $obj->missing;
}
$result = db_query("SELECT COUNT(c.nid) as upgrade FROM current c LEFT JOIN target t ON c.nid=t.nid WHERE (c.version_code < t.version_code) OR (c.schema_version < t.schema_version) AND c.status=1");
$result = db_query("SELECT COUNT(c.nid) as upgrade FROM %s c LEFT JOIN %s t ON c.nid=t.nid WHERE (c.version_code < t.version_code) OR (c.schema_version < t.schema_version) AND c.status=1", $current_tbl, $target_tbl);
while ($obj = db_fetch_object($result)) {
$status['upgrade'] = $obj->upgrade;
}
$result = db_query("SELECT count(c.nid) AS downgrade FROM current c LEFT JOIN target t ON c.nid=t.nid WHERE (c.version_code > t.version_code)");
$result = db_query("SELECT count(c.nid) AS downgrade FROM %s c LEFT JOIN %s t ON c.nid=t.nid WHERE (c.version_code > t.version_code)", $current_tbl, $target_tbl);
while ($obj = db_fetch_object($result)) {
$status['downgrade'] = $obj->downgrade;
}
db_query("DROP TEMPORARY TABLE target");
db_query("DROP TEMPORARY TABLE current");
return $status;
}
function _hosting_package_temporary_table($name, $ref) {
db_query("CREATE TEMPORARY TABLE %s AS SELECT p.nid, p.short_name, p.old_short_name, i.version_code, i.schema_version, i.version, i.status FROM {hosting_package} p LEFT JOIN {hosting_package_instance} i on p.nid = i.package_id WHERE i.rid = %d", $name, $ref);
function _hosting_package_temporary_table($ref) {
static $names = array();
if (!isset($names[$ref])) {
// Generate a unique table name for this $ref.
$names[$ref] = '_hosting_comparision_' . trim($ref);
// Create the temporary table.
db_query("CREATE TEMPORARY TABLE %s AS SELECT p.nid, p.short_name, p.old_short_name, i.version_code, i.schema_version, i.version, i.status FROM {hosting_package} p LEFT JOIN {hosting_package_instance} i on p.nid = i.package_id WHERE i.rid = %d", $names[$ref], $ref);
}
return $names[$ref];
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment