hosting_client.install

Define database schema, install and update functions for the hosting_client module.

Functions

Namesort descending Description
hosting_client_install Implements hook_install().
hosting_client_schema Implements hook_schema().
hosting_client_update_1 Implements hook_update_N().
hosting_client_update_2 Implements hook_update_N().
hosting_client_update_3 Implements hook_update_N().
hosting_client_update_4 Implements hook_update_N().
hosting_client_update_5 Implements hook_update_N().
hosting_client_update_6 Implements hook_update_N().
hosting_client_update_6000 Implements hook_update_N().
hosting_client_update_6001 Implements hook_update_N().
hosting_client_update_6002 Implements hook_update_N().
hosting_client_update_6003 Implements hook_update_N().
hosting_client_update_6004 Implements hook_update_N().
hosting_client_update_7 Implements hook_update_N().
hosting_client_update_8 Implements hook_update_N().
hosting_client_update_9 Implements hook_update_N().

File

client/hosting_client.install
View source
  1. <?php
  2. /**
  3. * @file
  4. * Define database schema, install and update functions for the hosting_client module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function hosting_client_schema() {
  10. $schema['hosting_client'] = array(
  11. 'fields' => array(
  12. 'vid' => array(
  13. 'type' => 'int',
  14. 'unsigned' => TRUE,
  15. 'not null' => TRUE,
  16. 'default' => 0,
  17. ),
  18. 'nid' => array(
  19. 'type' => 'int',
  20. 'unsigned' => TRUE,
  21. 'not null' => TRUE,
  22. 'default' => 0,
  23. ),
  24. 'uname' => array(
  25. 'type' => 'varchar',
  26. 'length' => 255,
  27. 'not null' => TRUE,
  28. 'default' => '',
  29. 'description' => 'Machine-usable name of this client (without shell metacharacters, usable for UNIX groups)',
  30. ),
  31. ),
  32. 'primary key' => array('vid'),
  33. 'unique keys' => array('uname_unq' => array('uname')),
  34. );
  35. $schema['hosting_client_user'] = array(
  36. 'fields' => array(
  37. 'user' => array(
  38. 'type' => 'int',
  39. 'unsigned' => TRUE,
  40. 'not null' => TRUE,
  41. 'default' => 0,
  42. ),
  43. 'client' => array(
  44. 'type' => 'int',
  45. 'unsigned' => TRUE,
  46. 'not null' => TRUE,
  47. 'default' => 0,
  48. ),
  49. 'contact_type' => array(
  50. 'type' => 'varchar',
  51. 'length' => 255,
  52. 'not null' => TRUE,
  53. 'default' => '',
  54. ),
  55. ),
  56. 'primary key' => array('user', 'client'),
  57. );
  58. $schema['hosting_platform_client_access'] = array(
  59. 'fields' => array(
  60. 'pid' => array(
  61. 'type' => 'int',
  62. 'unsigned' => TRUE,
  63. 'not null' => TRUE,
  64. 'default' => 0,
  65. ),
  66. 'cid' => array(
  67. 'type' => 'int',
  68. 'unsigned' => TRUE,
  69. 'not null' => TRUE,
  70. 'default' => 0,
  71. ),
  72. ),
  73. );
  74. return $schema;
  75. }
  76. /**
  77. * Implements hook_install().
  78. */
  79. function hosting_client_install() {
  80. // Add default 'admin' client.
  81. $insert = db_insert('hosting_client_user')
  82. ->fields(array(
  83. 'user' => 1,
  84. 'client' => 1,
  85. ))
  86. ->execute();
  87. }
  88. /**
  89. * Implements hook_update_N().
  90. *
  91. * Add the unique index to the client table
  92. *
  93. * This will also run through all existing clients and merge / delete the ones that
  94. * don't belong.
  95. */
  96. function hosting_client_update_1() {
  97. switch ($GLOBALS['db_type']) {
  98. case 'mysql' :
  99. case 'mysqli':
  100. $ret = array();
  101. $result = db_query("SELECT email, count(distinct nid) as count FROM {hosting_client} GROUP BY email");
  102. while ($distinct = db_fetch_object($result)) {
  103. if ($distinct->count > 1) {
  104. # we have found duplicates.
  105. $result2 = db_query("SELECT nid FROM {hosting_client} WHERE email = '%s' ORDER BY nid", $distinct->email);
  106. $first = FALSE;
  107. while ($client = db_fetch_object($result2)) {
  108. if (!$first) {
  109. // this is the key all the others will be assigned to.
  110. $first = $client->nid;
  111. }
  112. else {
  113. // reset nodes to the first occurrence, and delete the duplicate
  114. db_query("UPDATE {hosting_site} SET client=%d WHERE client=%d", $first, $client->nid);
  115. node_delete($client->nid);
  116. }
  117. }
  118. }
  119. }
  120. $ret[] = update_sql("CREATE UNIQUE INDEX hosting_client_email_idx ON hosting_client (email)");
  121. break;
  122. }
  123. return $ret;
  124. }
  125. /**
  126. * Implements hook_update_N().
  127. *
  128. * Create the hosting_client_user relationship table
  129. */
  130. function hosting_client_update_2() {
  131. $ret = array();
  132. $ret[] = update_sql("CREATE TABLE {hosting_client_user} (
  133. user int(10) unsigned NOT NULL default '0',
  134. client int(10) unsigned NOT NULL default '0',
  135. PRIMARY KEY (user)
  136. ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
  137. // Insert the uid 1 user into the admin client record.
  138. $ret[] = update_sql("INSERT INTO {hosting_client_user} VALUES (1, 1)");
  139. node_access_rebuild();
  140. return $ret;
  141. }
  142. /**
  143. * Implements hook_update_N().
  144. *
  145. * Rebuild node access table
  146. */
  147. function hosting_client_update_3() {
  148. $ret = array();
  149. node_access_rebuild();
  150. return $ret;
  151. }
  152. /**
  153. * Implements hook_update_N().
  154. *
  155. * Rebuild node access table
  156. */
  157. function hosting_client_update_4() {
  158. $ret = array();
  159. node_access_rebuild();
  160. return $ret;
  161. }
  162. /**
  163. * Implements hook_update_N().
  164. *
  165. * Rebuild node access table
  166. */
  167. function hosting_client_update_5() {
  168. $ret = array();
  169. node_access_rebuild();
  170. return $ret;
  171. }
  172. /**
  173. * Implements hook_update_N().
  174. *
  175. * Make it possible to have many clients per user and keep track of the contact type (admin/tech/billing/etc.) between users and clients
  176. */
  177. function hosting_client_update_6() {
  178. $ret = array();
  179. $ret[] = update_sql("ALTER TABLE {hosting_client_user} DROP PRIMARY KEY, ADD PRIMARY KEY (user, client)");
  180. $ret[] = update_sql("ALTER TABLE {hosting_client_user} ADD contact_type LONGTEXT NOT NULL");
  181. node_access_rebuild();
  182. return $ret;
  183. }
  184. /**
  185. * Implements hook_update_N().
  186. *
  187. * Rebuild the node access table now that we fixed the hook_access properly
  188. */
  189. function hosting_client_update_7() {
  190. node_access_rebuild();
  191. return array();
  192. }
  193. /**
  194. * Implements hook_update_N().
  195. *
  196. * Add the hosting_client_platforms table and default all clients
  197. * to have access to all platforms
  198. */
  199. function hosting_client_update_8() {
  200. $ret = array();
  201. $ret[] = update_sql("CREATE TABLE {hosting_platform_client_access}
  202. (pid int(10) unsigned NOT NULL default '0',
  203. cid int(10) unsigned NOT NULL default '0')");
  204. return $ret;
  205. }
  206. /**
  207. * Implements hook_update_N().
  208. *
  209. * Add uid 1 with client 1 to the hosting_client_user table if it wasn't there already.
  210. * Was not happening on fresh installs via hook_install()
  211. */
  212. function hosting_client_update_9() {
  213. $ret = array();
  214. $result = db_query("SELECT user, client FROM {hosting_client_user} WHERE user = 1 AND client = 1");
  215. if (!db_affected_rows($result)) {
  216. $ret[] = update_sql("INSERT INTO {hosting_client_user} (user, client) VALUES (1, 1)");
  217. }
  218. return $ret;
  219. }
  220. /**
  221. * Implements hook_update_N().
  222. *
  223. * Cleanup the hosting_client fields
  224. *
  225. * This rewrites the client node title based on the organization, name
  226. * or email field (in that order).
  227. *
  228. * It also populates the new, unique, uname field in the
  229. * hosting_client table.
  230. *
  231. * Note that this request will fail if you have more than 100 clients
  232. * with the same title.
  233. *
  234. * https://drupal.org/node/962330
  235. * https://drupal.org/node/461840
  236. */
  237. function hosting_client_update_6000() {
  238. $ret = array();
  239. db_add_field($ret, 'hosting_client', 'uname', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
  240. // populate the unique uname field
  241. $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'");
  242. while ($client = db_fetch_object($result)) {
  243. // try to find a proper name for the client
  244. if ($client->organization) {
  245. // we arbitrarily choose the organisation as the base parameter
  246. $name = $client->organization;
  247. }
  248. elseif ($client->name) {
  249. // fallback on the name of the "contact"
  250. $name = $client->name;
  251. }
  252. else {
  253. // fallback - use the email
  254. $name = $client->email;
  255. }
  256. $unique = preg_replace("/[!\W\.\-]/", "", $name);
  257. $failed_client = FALSE;
  258. $newname = $name;
  259. for ($i = ''; $i < 100; $i++) {
  260. if ($nid = db_result(db_query("SELECT nid FROM {hosting_client} WHERE uname = '%s'", $unique))) {
  261. // @ignore security_3
  262. drupal_set_message(t('nid !nid dupes this client (!client_nid) unique: !unique', array('!nid' => $nid, '!client_nid' => $client->nid, '!unique' => $unique)));
  263. }
  264. else {
  265. // @ignore security_3
  266. drupal_set_message(t('setting client !name to !newname (!unique)', array('!name' => $name, '!newname' => $newname, '!unique' => $unique)));
  267. db_query("UPDATE {hosting_client} SET uname = '%s' WHERE nid = %d AND vid = %d", $unique, $client->nid, $client->vid);
  268. db_query("UPDATE {node} SET title = '%s' WHERE nid = %d", $newname, $client->nid);
  269. db_query("UPDATE {node_revisions} SET title = '%s' WHERE nid = %d AND vid = %d", $name, $client->nid, $client->vid);
  270. $failed_client = FALSE;
  271. break;
  272. }
  273. $failed_client = $name;
  274. $unique = hosting_client_sanitize($name) . $i;
  275. $newname = $name . $i;
  276. }
  277. if ($failed_client) {
  278. $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)));
  279. break;
  280. }
  281. }
  282. db_add_unique_key($ret, 'hosting_client', 'uname_unq', array('uname'));
  283. return $ret;
  284. }
  285. /**
  286. * Implements hook_update_N().
  287. *
  288. * Use VARCHAR on contact_type field so that we support MariaDB correctly.
  289. *
  290. * See #1093436
  291. */
  292. function hosting_client_update_6001() {
  293. $ret = array();
  294. db_change_field($ret, 'hosting_client_user', 'contact_type', 'contact_type',
  295. array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
  296. return $ret;
  297. }
  298. /**
  299. * Implements hook_update_N().
  300. *
  301. * Remove old primary keys from hosting_client table
  302. *
  303. * This should never fail as the index is present only in really old installs
  304. * and this update may run on installs that have been done after the index was
  305. * removed from the schema.
  306. */
  307. function hosting_client_update_6002() {
  308. $ret = array();
  309. @db_drop_unique_key($ret, 'hosting_client', 'hosting_client_email_idx');
  310. $ret[0]['success'] = TRUE; // never fail
  311. return $ret;
  312. }
  313. /**
  314. * Implements hook_update_N().
  315. *
  316. * Remove duplicate client nodes.
  317. *
  318. * Those may have been created in development versions of 2.x code, see
  319. * https://drupal.org/node/1146014
  320. */
  321. function hosting_client_update_6003() {
  322. $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;");
  323. while ($nid = db_result($q)) {
  324. node_delete($nid);
  325. }
  326. return array(); // never fail
  327. }
  328. /**
  329. * Implements hook_update_N().
  330. *
  331. * Make all sites with client relationship lost owned by the default client.
  332. */
  333. function hosting_client_update_6004() {
  334. $ret = array();
  335. $ret[] = update_sql("UPDATE {hosting_site} SET client = %d WHERE client = 0", HOSTING_DEFAULT_CLIENT);
  336. return $ret;
  337. }