hosting_client.install in Hostmaster (Aegir) 6
Install, update and uninstall for the clients module.
File
modules/hosting/client/hosting_client.installView source
<?php
/**
* @file
* Install, update and uninstall for the clients module.
*/
/**
* Implementation of hook_schema().
*/
function hosting_client_schema() {
$schema['hosting_client'] = array(
'fields' => array(
'vid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'nid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'uname' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'Machine-usable name of this client (without shell metacharacters, usable for UNIX groups)',
),
),
'primary key' => array(
'vid',
),
'unique keys' => array(
'uname_unq' => array(
'uname',
),
),
);
$schema['hosting_client_user'] = array(
'fields' => array(
'user' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'client' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'contact_type' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
),
'primary key' => array(
'user',
'client',
),
);
$schema['hosting_platform_client_access'] = array(
'fields' => array(
'pid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'cid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
),
);
return $schema;
}
/**
* Implementation of hook_install().
*/
function hosting_client_install() {
// Create tables.
drupal_install_schema('hosting_client');
$ret = array();
$ret[] = update_sql("INSERT INTO {hosting_client_user} (user, client) VALUES (1, 1)");
return $ret;
}
/**
* Add the unique index to the client table
*
* This will also run through all existing clients and merge / delete the ones that
* don't belong.
*/
function hosting_client_update_1() {
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
$ret = array();
$result = db_query("SELECT email, count(distinct nid) as count FROM {hosting_client} GROUP BY email");
while ($distinct = db_fetch_object($result)) {
if ($distinct->count > 1) {
# we have found duplicates.
$result2 = db_query("SELECT nid FROM {hosting_client} WHERE email = '%s' ORDER BY nid", $distinct->email);
$first = false;
while ($client = db_fetch_object($result2)) {
if (!$first) {
// this is the key all the others will be assigned to.
$first = $client->nid;
}
else {
// reset nodes to the first occurrence, and delete the duplicate
db_query("UPDATE {hosting_site} SET client=%d WHERE client=%d", $first, $client->nid);
node_delete($client->nid);
}
}
}
}
$ret[] = update_sql("CREATE UNIQUE INDEX hosting_client_email_idx ON hosting_client (email)");
break;
}
return $ret;
}
/**
* Create the hosting_client_user relationship table
*/
function hosting_client_update_2() {
$ret = array();
$ret[] = update_sql("CREATE TABLE {hosting_client_user} (\n user int(10) unsigned NOT NULL default '0',\n client int(10) unsigned NOT NULL default '0',\n PRIMARY KEY (user)\n ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
// Insert the uid 1 user into the admin client record.
$ret[] = update_sql("INSERT INTO {hosting_client_user} VALUES (1, 1)");
node_access_rebuild();
return $ret;
}
/**
* Rebuild node access table
*/
function hosting_client_update_3() {
$ret = array();
node_access_rebuild();
return $ret;
}
/**
* Rebuild node access table
*/
function hosting_client_update_4() {
$ret = array();
node_access_rebuild();
return $ret;
}
/**
* Rebuild node access table
*/
function hosting_client_update_5() {
$ret = array();
node_access_rebuild();
return $ret;
}
/**
* Make it possible to have many clients per user and keep track of the contact type (admin/tech/billing/etc.) between users and clients
*/
function hosting_client_update_6() {
$ret = array();
$ret[] = update_sql("ALTER TABLE {hosting_client_user} DROP PRIMARY KEY, ADD PRIMARY KEY (user, client)");
$ret[] = update_sql("ALTER TABLE {hosting_client_user} ADD contact_type LONGTEXT NOT NULL");
node_access_rebuild();
return $ret;
}
/**
* Rebuild the node access table now that we fixed the hook_access properly
*/
function hosting_client_update_7() {
node_access_rebuild();
return array();
}
/**
* Add the hosting_client_platforms table and default all clients
* to have access to all platforms
*/
function hosting_client_update_8() {
$ret = array();
$ret[] = update_sql("CREATE TABLE {hosting_platform_client_access} \n (pid int(10) unsigned NOT NULL default '0', \n cid int(10) unsigned NOT NULL default '0')");
return $ret;
}
/**
* Add uid 1 with client 1 to the hosting_client_user table if it wasn't there already.
* Was not happening on fresh installs via hook_install()
*/
function hosting_client_update_9() {
$ret = array();
$result = db_query("SELECT user, client FROM {hosting_client_user} WHERE user = 1 AND client = 1");
if (!db_affected_rows($result)) {
$ret[] = update_sql("INSERT INTO {hosting_client_user} (user, client) VALUES (1, 1)");
}
return $ret;
}
/**
* Cleanup the hosting_client fields
*
* This rewrites the client node title based on the organization, name
* or email field (in that order).
*
* It also populates the new, unique, uname field in the
* hosting_client table.
*
* Note that this request will fail if you have more than 100 clients
* with the same title.
*
* https://drupal.org/node/962330
* https://drupal.org/node/461840
*/
function hosting_client_update_6000() {
$ret = array();
db_add_field($ret, 'hosting_client', 'uname', array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
));
// populate the unique uname field
$result = db_query("SELECT n.nid,n.vid,n.title,hc.organization,hc.name,hc.email FROM {node} n JOIN {hosting_client} hc ON hc.nid = n.nid WHERE n.type = 'client'");
while ($client = db_fetch_object($result)) {
// try to find a proper name for the client
if ($client->organization) {
// we arbitrarily choose the organisation as the base parameter
$name = $client->organization;
}
elseif ($client->name) {
// fallback on the name of the "contact"
$name = $client->name;
}
else {
// fallback - use the email
$name = $client->email;
}
$unique = preg_replace("/[!\\W\\.\\-]/", "", $name);
$failed_client = FALSE;
$newname = $name;
for ($i = ''; $i < 100; $i++) {
if ($nid = db_result(db_query("SELECT nid FROM {hosting_client} WHERE uname = '%s'", $unique))) {
drupal_set_message("nid {$nid} dupes this client ({$client->nid}) unique: {$unique}");
}
else {
drupal_set_message("setting client {$name} to " . $newname . " ({$unique})");
db_query("UPDATE {hosting_client} SET uname = '%s' WHERE nid = %d AND vid = %d", $unique, $client->nid, $client->vid);
db_query("UPDATE {node} SET title = '%s' WHERE nid = %d", $newname, $client->nid);
db_query("UPDATE {node_revisions} SET title = '%s' WHERE nid = %d AND vid = %d", $name, $client->nid, $client->vid);
$failed_client = FALSE;
break;
}
$failed_client = $name;
$unique = hosting_client_sanitize($name) . $i;
$newname = $name . $i;
}
if ($failed_client) {
$ret['#abort'] = array(
'success' => FALSE,
'query' => t('Could not find a unique client name for client @client (nid: @nid)', array(
'@client' => $failed_client,
'@nid' => $client->nid,
)),
);
break;
}
}
db_add_unique_key($ret, 'hosting_client', 'uname_unq', array(
'uname',
));
return $ret;
}
/**
* Use VARCHAR on contact_type field so that we support MariaDB correctly.
*
* See #1093436
*/
function hosting_client_update_6001() {
$ret = array();
db_change_field($ret, 'hosting_client_user', 'contact_type', 'contact_type', array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
));
return $ret;
}
/**
* Remove old primary keys from hosting_client table
*
* This should never fail as the index is present only in really old installs
* and this update may run on installs that have been done after the index was
* removed from the schema.
*/
function hosting_client_update_6002() {
$ret = array();
@db_drop_unique_key($ret, 'hosting_client', 'hosting_client_email_idx');
$ret[0]['success'] = TRUE;
// never fail
return $ret;
}
Functions
Name | Description |
---|---|
hosting_client_install | Implementation of hook_install(). |
hosting_client_schema | Implementation of hook_schema(). |
hosting_client_update_1 | Add the unique index to the client table |
hosting_client_update_2 | Create the hosting_client_user relationship table |
hosting_client_update_3 | Rebuild node access table |
hosting_client_update_4 | Rebuild node access table |
hosting_client_update_5 | Rebuild node access table |
hosting_client_update_6 | Make it possible to have many clients per user and keep track of the contact type (admin/tech/billing/etc.) between users and clients |
hosting_client_update_6000 | Cleanup the hosting_client fields |
hosting_client_update_6001 | Use VARCHAR on contact_type field so that we support MariaDB correctly. |
hosting_client_update_6002 | Remove old primary keys from hosting_client table |
hosting_client_update_7 | Rebuild the node access table now that we fixed the hook_access properly |
hosting_client_update_8 | Add the hosting_client_platforms table and default all clients to have access to all platforms |
hosting_client_update_9 | Add uid 1 with client 1 to the hosting_client_user table if it wasn't there already. Was not happening on fresh installs via hook_install() |