hosting_client.install in Hosting 7.3
Same filename and directory in other branches
Define database schema, install and update functions for the hosting_client module.
File
client/hosting_client.installView source
<?php
/**
* @file
* Define database schema, install and update functions for the hosting_client module.
*/
/**
* Implements 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;
}
/**
* Implements hook_install().
*/
function hosting_client_install() {
// Add default 'admin' client.
$insert = db_insert('hosting_client_user')
->fields(array(
'user' => 1,
'client' => 1,
))
->execute();
}
/**
* Implements hook_update_N().
*
* 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;
}
/**
* Implements hook_update_N().
*
* 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;
}
/**
* Implements hook_update_N().
*
* Rebuild node access table
*/
function hosting_client_update_3() {
$ret = array();
node_access_rebuild();
return $ret;
}
/**
* Implements hook_update_N().
*
* Rebuild node access table
*/
function hosting_client_update_4() {
$ret = array();
node_access_rebuild();
return $ret;
}
/**
* Implements hook_update_N().
*
* Rebuild node access table
*/
function hosting_client_update_5() {
$ret = array();
node_access_rebuild();
return $ret;
}
/**
* Implements hook_update_N().
*
* 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;
}
/**
* Implements hook_update_N().
*
* Rebuild the node access table now that we fixed the hook_access properly
*/
function hosting_client_update_7() {
node_access_rebuild();
return array();
}
/**
* Implements hook_update_N().
*
* 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;
}
/**
* Implements hook_update_N().
*
* 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;
}
/**
* Implements hook_update_N().
*
* 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))) {
// @ignore security_3
drupal_set_message(t('nid !nid dupes this client (!client_nid) unique: !unique', array(
'!nid' => $nid,
'!client_nid' => $client->nid,
'!unique' => $unique,
)));
}
else {
// @ignore security_3
drupal_set_message(t('setting client !name to !newname (!unique)', array(
'!name' => $name,
'!newname' => $newname,
'!unique' => $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;
}
/**
* Implements hook_update_N().
*
* 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;
}
/**
* Implements hook_update_N().
*
* 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;
}
/**
* Implements hook_update_N().
*
* Remove duplicate client nodes.
*
* Those may have been created in development versions of 2.x code, see
* https://drupal.org/node/1146014
*/
function hosting_client_update_6003() {
$q = db_query("SELECT n.nid FROM {node} n LEFT JOIN {hosting_client} hc ON n.nid = hc.nid WHERE n.type = 'client' AND hc.nid IS NULL;");
while ($nid = db_result($q)) {
node_delete($nid);
}
return array();
// never fail
}
/**
* Implements hook_update_N().
*
* Make all sites with client relationship lost owned by the default client.
*/
function hosting_client_update_6004() {
$ret = array();
$ret[] = update_sql("UPDATE {hosting_site} SET client = %d WHERE client = 0", HOSTING_DEFAULT_CLIENT);
return $ret;
}
/**
* Implements hook_update_N().
*
* Re-build node access.
*/
function hosting_client_update_7001(&$sandbox) {
// node_access_rebuild from the the update hook fails without the following defintion.
if (!defined('HOSTING_ADMIN_CLIENT')) {
define('HOSTING_ADMIN_CLIENT', variable_get('hosting_admin_client', 1));
}
// @see https://gist.github.com/johnennewdeeson/6d4a6d667538e6149f56
_node_access_rebuild_batch_operation($sandbox);
$sandbox['#finished'] = $sandbox['sandbox']['progress'] >= $sandbox['sandbox']['max'];
if ($sandbox['#finished']) {
node_access_needs_rebuild(FALSE);
}
}
/**
* Enable the setting to require users to have a client before creating a site.
* The new default is "disabled" but the existing behavior should be preserved.
*/
function hosting_client_update_7002() {
variable_set('hosting_client_require_client_to_create_site', 1);
}