domain_prefix.module

Tracking 5.x-1.x branch
  1. drupal
    1. 5 contributions/domain/domain_prefix/domain_prefix.module
    2. 6 contributions/domain/domain_prefix/domain_prefix.module

Interface for selective table prefixing for use with Domain Access. For this module to work correctly, you will need to follow the INSTALL.txt instructions for editing your settings.php file.

Functions & methods

NameDescription
domain_prefix_configure_formFormsAPI for generating the configuration form
domain_prefix_configure_form_submitFormsAPI for the domain_prefix_configure_form.
domain_prefix_disallowNames of tables explicitly not allowed to be copied
domain_prefix_domaininstallImplement hook_domaininstall()
domain_prefix_domainlinksImplement hook_domainlinks()
domain_prefix_domainupdateImplement hook_domainupdate().
domain_prefix_drop_recordsDrop tables created by this module.
domain_prefix_formThe table prefixing page for a domain.
domain_prefix_form_submitFormsAPI for domain_prefix_form.
domain_prefix_get_prefixCheck for existing table prefixing.
domain_prefix_get_tablesGet the tables with the active prefix
domain_prefix_lookupLookup stored table information for a domain.
domain_prefix_menuImplement hook_menu()
domain_prefix_module_lookupDerive a module name from the table name
domain_prefix_permImplement hook_perm
domain_prefix_stringUniform prefix string
domain_prefix_table_existsDoes a table exist -- we use this to bypass Drupal's default table prefixing check.
domain_prefix_update_sequencesCorrect the {sequences} table if doing an update.
theme_domain_prefix_configure_formFormsAPI theming for domain_prefix_configure_form.
_domain_prefix_sortHelper sort function

Constants

NameDescription
DOMAIN_PREFIX_COPY
DOMAIN_PREFIX_CREATE
DOMAIN_PREFIX_DROP
DOMAIN_PREFIX_IGNOREConstant definitions for the various actions.
DOMAIN_PREFIX_UPDATE

File

View source
  1. <?php
  2. /**
  3. * @defgroup domain_prefix Domain Prefix: dynamic table prefixing
  4. *
  5. * Allows for the dynamic table prefixing of selected database tables.
  6. */
  7. /**
  8. * @file
  9. * Interface for selective table prefixing for use with Domain Access.
  10. * For this module to work correctly, you will need to follow the INSTALL.txt
  11. * instructions for editing your settings.php file.
  12. *
  13. * @ingroup domain_prefix
  14. */
  15. /**
  16. * Constant definitions for the various actions.
  17. */
  18. define('DOMAIN_PREFIX_IGNORE', 1);
  19. define('DOMAIN_PREFIX_CREATE', 2);
  20. define('DOMAIN_PREFIX_COPY', 4);
  21. define('DOMAIN_PREFIX_DROP', 8);
  22. define('DOMAIN_PREFIX_UPDATE', 16);
  23. /**
  24. * Implement hook_menu()
  25. */
  26. function domain_prefix_menu($may_cache) {
  27. $items = array();
  28. if ($may_cache) {
  29. $items[] = array(
  30. 'title' => t('Table prefixing'),
  31. 'path' => 'admin/build/domain/prefix',
  32. 'access' => user_access('administer table prefixing'),
  33. 'type' => MENU_LOCAL_TASK,
  34. 'callback' => 'drupal_get_form',
  35. 'callback arguments' => array('domain_prefix_configure_form')
  36. );
  37. }
  38. else {
  39. $items[] = array(
  40. 'title' => t('Domain prefix settings'),
  41. 'path' => 'admin/build/domain/prefix/'. arg(4),
  42. 'access' => user_access('administer table prefixing'),
  43. 'type' => MENU_CALLBACK,
  44. 'callback' => 'drupal_get_form',
  45. 'callback arguments' => array('domain_prefix_form', arg(4))
  46. );
  47. $items[] = array(
  48. 'title' => t('Domain prefix update'),
  49. 'path' => 'domain_prefix_update',
  50. 'access' => user_access('administer table prefixing'),
  51. 'type' => MENU_CALLBACK,
  52. 'callback' => 'domain_prefix_update',
  53. );
  54. }
  55. return $items;
  56. }
  57. /**
  58. * Implement hook_perm
  59. */
  60. function domain_prefix_perm() {
  61. return array('administer table prefixing');
  62. }
  63. /**
  64. * Check for existing table prefixing.
  65. */
  66. function domain_prefix_get_prefix() {
  67. global $db_prefix;
  68. // Check for existing table prefixing.
  69. $prefix = NULL;
  70. if (!empty($db_prefix)) {
  71. if (is_array($db_prefix)) {
  72. $prefix = $db_prefix['default'];
  73. }
  74. else {
  75. $prefix = $db_prefix;
  76. }
  77. }
  78. return $prefix;
  79. }
  80. /**
  81. * Get the tables with the active prefix
  82. *
  83. * @param $prefix
  84. * The table prefix used with this domain. Optional.
  85. */
  86. function domain_prefix_get_tables($prefix = NULL) {
  87. // Check for default prefix settings.
  88. if (empty($prefix)) {
  89. $prefix = domain_prefix_get_prefix();
  90. }
  91. switch ($GLOBALS['db_type']) {
  92. case 'pgsql':
  93. if (empty($prefix)) {
  94. $result = db_query("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'");
  95. }
  96. else {
  97. $result = db_query("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_name LIKE '{$prefix}%%'");
  98. }
  99. break;
  100. default:
  101. // MySQL and MySQLi implementation.
  102. if (empty($prefix)) {
  103. $result = db_query("SHOW TABLES");
  104. }
  105. else {
  106. $result = db_query("SHOW TABLES LIKE '{$prefix}%%'");
  107. }
  108. break;
  109. }
  110. $tables = array();
  111. $disallow = domain_prefix_disallow();
  112. while ($data = db_fetch_array($result)) {
  113. // Chop table prefixes.
  114. $str = current($data);
  115. if (!empty($prefix)) {
  116. $str = preg_replace('/'. $prefix .'/', '', $str, 1);
  117. }
  118. if (!in_array($str, $disallow) && substr($str, 0, 7) != 'domain_') {
  119. $tables[$str]['tablename'] = $str;
  120. $tables[$str]['module'] = domain_prefix_module_lookup($str);
  121. }
  122. }
  123. // Sort them by module
  124. uasort($tables, '_domain_prefix_sort');
  125. return $tables;
  126. }
  127. /**
  128. * Helper sort function
  129. */
  130. function _domain_prefix_sort($a, $b) {
  131. $_a = str_replace('_', '', $a['tablename']);
  132. $_b = str_replace('_', '', $b['tablename']);
  133. if ($a['module'] == $b['module']) {
  134. return strcmp($_a, $_b);
  135. }
  136. return strcmp($a['module'], $b['module']);
  137. }
  138. /**
  139. * FormsAPI for generating the configuration form
  140. */
  141. function domain_prefix_configure_form() {
  142. // We must be on the root domain!
  143. $default = domain_default();
  144. domain_goto($default);
  145. // Get the tables for the root installation.
  146. $tables = domain_prefix_get_tables($prefix);
  147. // Remove the disallowed tables.
  148. $disallow = domain_prefix_disallow();
  149. // Get the current settings.
  150. $info = unserialize(variable_get('domain_prefix', NULL));
  151. $settings = $info['settings'];
  152. $source_defaults = $info['sources'];
  153. $form = array();
  154. $form['domain'] = array(
  155. '#type' => 'fieldset',
  156. '#title' => t('Domain creation rules'),
  157. '#collapsible' => TRUE,
  158. '#collapsed' => FALSE
  159. );
  160. $form['domain']['domain_prefix_options'] = array(
  161. '#type' => 'radios',
  162. '#title' => t('Domain creation options'),
  163. '#description' => t('Determines what actions to take when creating new domain records.'),
  164. '#options' => array(1 => t('Generate tables as defined below'), 0 => t('Do not generate any tables')),
  165. '#default_value' => variable_get('domain_prefix_options', 1),
  166. '#required' => TRUE
  167. );
  168. $last = ''; // Flag for module grouping.
  169. // Get the source table data.
  170. $root = domain_default();
  171. foreach ($tables as $table => $info) {
  172. if (!in_array($table, $disallow) && substr($table, 0, 7) != 'domain_') {
  173. if (empty($settings[$table])) {
  174. $settings[$table] = DOMAIN_PREFIX_IGNORE;
  175. $source_defaults['_source_'. $table] = 0;
  176. }
  177. $module = $info['module'];
  178. if ($last != $module) {
  179. $last = $module;
  180. }
  181. else {
  182. $module = '';
  183. }
  184. $options = array();
  185. $options[DOMAIN_PREFIX_IGNORE] = t('ignore');
  186. $options[DOMAIN_PREFIX_CREATE] = t('create');
  187. $options[DOMAIN_PREFIX_COPY] = t('copy');
  188. $form['domain_prefix'][$table] = array(
  189. '#type' => 'radios',
  190. '#title' => $table,
  191. '#options' => $options,
  192. '#default_value' => $settings[$table],
  193. '#description' => $module
  194. );
  195. // Get the table copying options for this entry.
  196. // Can't pass a zero through FormAPI select.
  197. $sources = array();
  198. $sources[0] = $root['sitename'];
  199. // Check to see if other table prefixes have been created.
  200. $result = db_query("SELECT dp.domain_id, d.sitename FROM {domain_prefix} dp INNER JOIN {domain} d ON dp.domain_id = d.domain_id WHERE dp.tablename = '%s' AND dp.status > 1", $table);
  201. while ($data = db_fetch_array($result)) {
  202. $sources[$data['domain_id']] = $data['sitename'];
  203. }
  204. $form['domain_source']['_source_'. $table] = array(
  205. '#type' => 'select',
  206. '#title' => '',
  207. '#options' => $sources,
  208. '#default_value' => $source_defaults['_source_'. $table],
  209. );
  210. }
  211. }
  212. $form['submit'] = array(
  213. '#type' => 'submit',
  214. '#value' => t('Save prefix settings'),
  215. );
  216. $form['restore'] = array(
  217. '#type' => 'submit',
  218. '#value' => t('Restore defaults'),
  219. );
  220. return $form;
  221. }
  222. /**
  223. * Implement hook_domainlinks()
  224. *
  225. * @param $domain
  226. * The currently active $domain array, provided by domain_lookup().
  227. */
  228. function domain_prefix_domainlinks($domain) {
  229. if (user_access('administer table prefixing')) {
  230. $links[] = array(
  231. 'title' => t('tables'),
  232. 'path' => 'admin/build/domain/prefix/'. $domain['domain_id']
  233. );
  234. return $links;
  235. }
  236. }
  237. /**
  238. * FormsAPI for the domain_prefix_configure_form.
  239. */
  240. function domain_prefix_configure_form_submit($form_id, $form_values) {
  241. // Throw away what we don't need.
  242. $options = $form_values['domain_prefix_options'];
  243. $unset = array('op', 'submit', 'restore', 'form_token', 'form_id', 'domain_prefix_options');
  244. $data = $form_values;
  245. foreach ($unset as $key) {
  246. unset($data[$key]);
  247. }
  248. if ($form_values['op'] == $form_values['restore']) {
  249. variable_del('domain_prefix');
  250. drupal_set_message(t('Default prefix settings reset.'));
  251. }
  252. else {
  253. // Process the source data.
  254. foreach ($data as $key => $value) {
  255. if (substr($key, 0, 8) == '_source_') {
  256. $info['sources'][$key] = $value;
  257. }
  258. else {
  259. $info['settings'][$key] = $value;
  260. }
  261. }
  262. $settings = serialize($info);
  263. variable_set('domain_prefix', $settings);
  264. drupal_set_message(t('Default prefix settings changed.'));
  265. }
  266. variable_set('domain_prefix_options', $options);
  267. }
  268. /**
  269. * FormsAPI theming for domain_prefix_configure_form.
  270. */
  271. function theme_domain_prefix_configure_form($form) {
  272. $output = t('<p>These settings control advanced functions. Please read the documentation carefully.</p>');
  273. $header = array(t('Table'), t('Source'), t('Ignore'), t('Create'), t('Copy'));
  274. if ($form['prefix_theme']['#value'] > 0) {
  275. $header[] = t('Update');
  276. $header[] = t('Drop');
  277. unset($form['prefix_theme']);
  278. }
  279. $output = drupal_render($form['domain']);
  280. foreach (element_children($form['domain_prefix']) as $key) {
  281. if ($form['domain_prefix'][$key]['#description']) {
  282. $rows[] = array('<b>'. $form['domain_prefix'][$key]['#description'] .'</b>', array('', 'colspan' => 4));
  283. }
  284. $row = array();
  285. $row[] = ' - '. $form['domain_prefix'][$key]['#title'];
  286. // Insert the source selection element.
  287. $row[] = drupal_render($form['domain_source']['_source_'. $key]);
  288. foreach (element_children($form['domain_prefix'][$key]) as $option) {
  289. $row[] = drupal_render($form['domain_prefix'][$key][$option]);
  290. }
  291. // Throw the rest away, since we already rendered what we want from it.
  292. $render = drupal_render($form['domain_prefix'][$key]);
  293. $rows[] = $row;
  294. }
  295. $output .= theme('table', $header, $rows);
  296. $output .= drupal_render($form);
  297. return $output;
  298. }
  299. /**
  300. * The table prefixing page for a domain.
  301. *
  302. * @param $domain_id
  303. * The domain_id taken from {domain}.
  304. * @param $arguments
  305. * An array of additional hidden key/value pairs to pass to the form.
  306. * Used by child modules to control behaviors.
  307. */
  308. function domain_prefix_form($domain_id, $arguments = array()) {
  309. // We must be on the root domain!
  310. $default = domain_default();
  311. domain_goto($default);
  312. $domain = domain_lookup($domain_id);
  313. drupal_set_title(t('Table prefixing for @domain', array('@domain' => $domain['sitename'])));
  314. // Remove the disallowed tables.
  315. $disallow = domain_prefix_disallow();
  316. // Get the default table set and settings.
  317. $default = domain_prefix_get_tables();
  318. // Get the current settings.
  319. $info = unserialize(variable_get('domain_prefix', NULL));
  320. $settings = $info['settings'];
  321. $source_defaults = $info['sources'];
  322. // Check the defaults against those saved for this domain.
  323. $result = db_query("SELECT tablename, source FROM {domain_prefix} WHERE domain_id = %d", $domain['domain_id']);
  324. while ($sourcedata = db_fetch_array($result)) {
  325. $source_defaults['_source_'. $sourcedata['tablename']] = $sourcedata['source'];
  326. }
  327. // Get the root source table data.
  328. $root = domain_default();
  329. if (empty($settings) && !$_POST) {
  330. drupal_set_message(t('There are no default settings configured.'));
  331. }
  332. // Get the stored table data for this domain.
  333. $tables = domain_prefix_lookup($domain_id);
  334. $submit = t('Update domain tables');
  335. if (empty($tables)) {
  336. if (!$_POST && !$form_values['execute'] && !$arguments['user_submitted']) {
  337. drupal_set_message(t('The table creation sequence has not run for this domain.'));
  338. }
  339. $submit = t('Generate domain tables');
  340. $table_options = $default;
  341. }
  342. else {
  343. $table_options = array();
  344. $settings = array();
  345. foreach ($tables as $name => $table) {
  346. if (array_key_exists($name, $default)) {
  347. $table_options[$table['tablename']] = $table;
  348. $settings[$table['tablename']] = $table['status'];
  349. }
  350. }
  351. }
  352. // Sort them by module
  353. uasort($table_options, '_domain_prefix_sort');
  354. // All tables are prefixed as 'domain_#_'
  355. $prefix = domain_prefix_string($domain_id);
  356. // Generate the form.
  357. $form = array();
  358. // The $arguments arrray allows other modules to pass values to change the bahavior
  359. // of submit and validate functions.
  360. if (!empty($arguments)) {
  361. $form['domain_arguments'] = array('#type' => 'value', '#value' => $arguments);
  362. }
  363. $delete_flag = 0; // Flag for the theme function delete column
  364. $last = ''; // Flag for module groupings.
  365. foreach ($table_options as $table => $info) {
  366. if (!in_array($table, $disallow)) {
  367. if (empty($settings[$table])) {
  368. $settings[$table] = DOMAIN_PREFIX_IGNORE;
  369. }
  370. $options = array();
  371. $options[DOMAIN_PREFIX_IGNORE] = t('ignore');
  372. $options[DOMAIN_PREFIX_CREATE] = t('create');
  373. $options[DOMAIN_PREFIX_COPY] = t('copy');
  374. if ($settings[$table] > 0) {
  375. $exists = domain_prefix_table_exists($prefix, $table);
  376. if ($exists > 0) {
  377. $options[DOMAIN_PREFIX_UPDATE] = t('update');
  378. $options[DOMAIN_PREFIX_DROP] = t('drop');
  379. $delete_flag++;
  380. }
  381. }
  382. $module = $info['module'];
  383. if ($last != $module) {
  384. $last = $module;
  385. }
  386. else {
  387. $module = '';
  388. }
  389. $form['domain_prefix'][$table] = array(
  390. '#type' => 'radios',
  391. '#title' => $table,
  392. '#options' => $options,
  393. '#default_value' => $settings[$table],
  394. '#description' => $module
  395. );
  396. // Get the table copying options for this entry.
  397. // Can't pass a zero through FormAPI select.
  398. $sources = array();
  399. $sources[0] = $root['sitename'];
  400. // Check to see if other table prefixes have been created.
  401. $result = db_query("SELECT dp.domain_id, d.sitename FROM {domain_prefix} dp INNER JOIN {domain} d ON dp.domain_id = d.domain_id WHERE dp.tablename = '%s' AND dp.status > 1", $table);
  402. while ($data = db_fetch_array($result)) {
  403. // Cannot copy onto itself.
  404. if ($data['domain_id'] != $domain['domain_id']) {
  405. $sources[$data['domain_id']] = $data['sitename'];
  406. }
  407. }
  408. $form['domain_source']['_source_'. $table] = array(
  409. '#type' => 'select',
  410. '#title' => '',
  411. '#options' => $sources,
  412. '#default_value' => $source_defaults['_source_'. $table],
  413. );
  414. }
  415. }
  416. $form['#theme'] = 'domain_prefix_configure_form';
  417. $form['prefix_theme'] = array('#type' => 'value', '#value' => $delete_flag);
  418. $form['domain_id'] = array('#type' => 'value', '#value' => $domain_id);
  419. $form['submit'] = array(
  420. '#type' => 'submit',
  421. '#value' => $submit
  422. );
  423. return $form;
  424. }
  425. /**
  426. * Does a table exist -- we use this to bypass Drupal's default table prefixing check.
  427. *
  428. * @param $prefix
  429. * The table prefix used with this domain. Optional.
  430. * @param $table
  431. * The name of the table being acted upon.
  432. */
  433. function domain_prefix_table_exists($prefix, $table) {
  434. global $db_type;
  435. $string = db_escape_table($prefix . $table);
  436. switch ($db_type) {
  437. case 'pgsql':
  438. $query = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_name = '{%s}'";
  439. break;
  440. default:
  441. $query = "SHOW TABLES LIKE '{%s}'";
  442. break;
  443. }
  444. return db_num_rows(db_query($query, $string));
  445. }
  446. /**
  447. * FormsAPI for domain_prefix_form.
  448. */
  449. function domain_prefix_form_submit($form_id, $form_values) {
  450. // Flag messages for the administrative user only.
  451. $msg = TRUE;
  452. $create = TRUE;
  453. if ($form_values['domain_arguments']['user_submitted']) {
  454. $msg = FALSE;
  455. // Should we create tables for user domains?
  456. $create = variable_get('domain_user_prefixing', 0);
  457. }
  458. if ($create) {
  459. // Throw away what we don't need.
  460. $prefix = domain_prefix_string($form_values['domain_id']);
  461. $unset = array('prefix_theme', 'domain_id', 'op', 'submit', 'restore', 'form_token', 'form_id', 'execute');
  462. $data = $form_values;
  463. foreach ($unset as $key) {
  464. unset($data[$key]);
  465. }
  466. // Delete existing records, but get the existing values first.
  467. $current = domain_prefix_lookup($form_values['domain_id']);
  468. db_query("DELETE FROM {domain_prefix} WHERE domain_id = %d", $form_values['domain_id']);
  469. // Get the database type.
  470. $db_type = $GLOBALS['db_type'];
  471. foreach ($data as $key => $value) {
  472. // Do not process tables for the source elements.
  473. // But be sure to set the proper source table prefix for copying data.
  474. if (substr($key, 0, 8) != '_source_') {
  475. $source = $data['_source_'. $key];
  476. if ($source > 0) {
  477. $source_prefix = domain_prefix_string($source);
  478. }
  479. else {
  480. $source_prefix = '';
  481. }
  482. $update = FALSE;
  483. if (empty($value)) {
  484. $value = DOMAIN_PREFIX_IGNORE;
  485. $update = TRUE;
  486. }
  487. $newtable = db_escape_table($prefix . $key);
  488. $module = domain_prefix_module_lookup($key);
  489. $exists = domain_prefix_table_exists($prefix, $key);
  490. $oldtable = db_escape_table($key);
  491. $sourcetable = db_escape_table($source_prefix . $key);
  492. if ($value == DOMAIN_PREFIX_CREATE) {
  493. if (!$exists) {
  494. if ($db_type == 'pgsql') {
  495. db_query("CREATE TABLE {%s} AS SELECT * FROM {%s}", $newtable, $sourcetable);
  496. db_query("TRUNCATE TABLE {%s}", $newtable);
  497. }
  498. else {
  499. db_query("CREATE TABLE {%s} LIKE {%s}", $newtable, $sourcetable);
  500. }
  501. if ($msg) {
  502. drupal_set_message(t('!string table created.', array('!string' => $newtable)));
  503. }
  504. $update = TRUE;
  505. }
  506. else if ($current[$oldtable]['status'] == DOMAIN_PREFIX_COPY) {
  507. drupal_set_message(t('!string table cannot be created, since it already exists.', array('!string' => $newtable)));
  508. }
  509. }
  510. else if ($value == DOMAIN_PREFIX_COPY) {
  511. if (!$exists) {
  512. if ($db_type == 'pgsql') {
  513. db_query("CREATE TABLE {%s} AS SELECT * FROM {%s}", $newtable, $sourcetable);
  514. }
  515. else {
  516. db_query("CREATE TABLE {%s} LIKE {%s}", $newtable, $sourcetable);
  517. db_query("INSERT INTO {%s} SELECT * FROM {%s}", $newtable, $sourcetable);
  518. }
  519. // Update {sequences} table.
  520. domain_prefix_update_sequences('update', $newtable, $sourcetable);
  521. if ($msg) {
  522. drupal_set_message(t('!string table copied.', array('!string' => $newtable)));
  523. }
  524. $update = TRUE;
  525. }
  526. else if ($current[$oldtable]['status'] == DOMAIN_PREFIX_CREATE) {
  527. drupal_set_message(t('!string table cannot be copied, since it already exists.', array('!string' => $newtable)));
  528. }
  529. }
  530. else if ($value == DOMAIN_PREFIX_UPDATE) {
  531. if ($exists > 0) {
  532. db_query("TRUNCATE TABLE {%s}", $newtable);
  533. db_query("INSERT INTO {%s} SELECT * FROM {%s}", $newtable, $sourcetable);
  534. // Update {sequences} table.
  535. domain_prefix_update_sequences('update', $newtable, $sourcetable);
  536. if ($msg) {
  537. drupal_set_message(t('!string table updated from source.', array('!string' => $newtable)));
  538. }
  539. $update = TRUE;
  540. // Set the stored value to "copy" for record keeping.
  541. $value = DOMAIN_PREFIX_COPY;
  542. }
  543. }
  544. else if ($value == DOMAIN_PREFIX_DROP) {
  545. if ($exists > 0) {
  546. db_query("DROP TABLE {%s}", $newtable);
  547. $value = DOMAIN_PREFIX_IGNORE;
  548. // Update {sequences} table.
  549. domain_prefix_update_sequences('drop', $newtable, $sourcetable);
  550. if ($msg) {
  551. drupal_set_message(t('!string table dropped.', array('!string' => $newtable)));
  552. }
  553. $update = TRUE;
  554. }
  555. else {
  556. drupal_set_message(t('!string table does not exist.', array('!string' => $newtable)));
  557. }
  558. }
  559. // Update our records.
  560. if (!$update && $value != 1) {
  561. $value = $current[$oldtable]['status'];
  562. }
  563. db_query("INSERT INTO {domain_prefix} (domain_id, status, tablename, module, source) VALUES (%d, %d, '%s', '%s', %d)", $form_values['domain_id'], $value, $key, $module, $form_values['_source_'. $key]);
  564. }
  565. }
  566. }
  567. // Clear the cache.
  568. cache_clear_all();
  569. }
  570. /**
  571. * Correct the {sequences} table if doing an update.
  572. *
  573. * @param $op
  574. * The operation to perform, either 'update' or 'drop.'
  575. * @param $newtable
  576. * The name of the table being updated.
  577. * @param $sourcetable
  578. * The name of the table providing the tempalte for the update
  579. */
  580. function domain_prefix_update_sequences($op, $newtable, $sourcetable) {
  581. $result = db_query("SELECT name, id FROM {sequences} WHERE name LIKE '{%s%}'", $sourcetable);
  582. $dbprefix = domain_prefix_get_prefix();
  583. $source = explode('_', $dbprefix . $sourcetable);
  584. while ($variable = db_fetch_array($result)) {
  585. // We have to match the variable source name, to prevent duplicates.
  586. $var = explode('_', $variable['name']);
  587. array_pop($var); // Toss out the last element.
  588. $diff = array_diff($var, $source);
  589. if (empty($diff)) {
  590. $newvariable = $newtable . substr($variable['name'], strlen($dbprefix . $sourcetable));
  591. $target = db_result(db_query("SELECT id FROM {sequences} WHERE name = '{%s}'", $newvariable));
  592. if (!$target) {
  593. if ($op == 'update') {
  594. db_query("INSERT INTO {sequences} (name, id) VALUES ('{%s}', %d)", $newvariable, $variable['id']);
  595. }
  596. }
  597. else {
  598. if ($op == 'update') {
  599. db_query("UPDATE {sequences} SET id = %d WHERE name = '{%s}'", $variable['id'], $newvariable);
  600. }
  601. else {
  602. db_query("DELETE FROM {sequences} WHERE name = '{%s}'", $newvariable);
  603. }
  604. }
  605. }
  606. }
  607. }
  608. /**
  609. * Derive a module name from the table name
  610. *
  611. * In future, SchemaAPI will do this for us.
  612. * For now, we will have to map core tables.
  613. *
  614. * @param $table
  615. * The name of the table being acted upon.
  616. */
  617. function domain_prefix_module_lookup($table) {
  618. $match = explode('_', $table);
  619. $module = $match[0];
  620. switch ($table) {
  621. default:
  622. break;
  623. case 'boxes':
  624. $module = 'blocks';
  625. break;
  626. case 'file_revisions':
  627. $module = 'files';
  628. break;
  629. case 'cache_menu':
  630. $module = 'menu';
  631. break;
  632. case 'filter_formats':
  633. $module = 'filters';
  634. break;
  635. case 'access':
  636. case 'permission':
  637. case 'role':
  638. case 'users':
  639. case 'users_roles':
  640. $module = 'users';
  641. break;
  642. case 'url_alias':
  643. $module = 'path';
  644. break;
  645. case 'variable':
  646. $module = 'cache';
  647. break;
  648. case 'client':
  649. case 'client_system':
  650. $module = 'drupal';
  651. break;
  652. case 'accesslog':
  653. $module = 'statistics';
  654. break;
  655. case 'term_data':
  656. case 'term_hierarchy':
  657. case 'term_node':
  658. case 'term_relation':
  659. case 'term_synonym':
  660. $module = 'taxonomy';
  661. break;
  662. }
  663. return $module;
  664. }
  665. /**
  666. * Lookup stored table information for a domain.
  667. *
  668. * @param $domain_id
  669. * The domain_id taken from {domain}.
  670. */
  671. function domain_prefix_lookup($domain_id, $clear = FALSE) {
  672. static $domain_prefix;
  673. if ($clear || !isset($domain_prefix[$domain_id])) {
  674. $domain_prefix[$domain_id] = array();
  675. $result = db_query("SELECT domain_id, status, tablename, module, source FROM {domain_prefix} WHERE domain_id = %d", $domain_id);
  676. while ($data = db_fetch_array($result)) {
  677. $domain_prefix[$domain_id][$data['tablename']] = $data;
  678. }
  679. }
  680. return $domain_prefix[$domain_id];
  681. }
  682. /**
  683. * Names of tables explicitly not allowed to be copied
  684. */
  685. function domain_prefix_disallow() {
  686. return array(
  687. 'domain',
  688. 'domain_conf',
  689. 'domain_prefix',
  690. 'domain_theme',
  691. 'domain_user'
  692. );
  693. }
  694. /**
  695. * Implement hook_domainupdate().
  696. */
  697. function domain_prefix_domainupdate($op, $domain = array(), $edit = array()) {
  698. switch ($op) {
  699. case 'create':
  700. $rule = variable_get('domain_prefix_options', 1);
  701. if ($rule) {
  702. // Get the current settings.
  703. $settings = unserialize(variable_get('domain_prefix', NULL));
  704. if (!empty($settings)) {
  705. $settings['domain_id'] = $domain['domain_id'];
  706. drupal_execute('domain_prefix_form', $settings, $domain['domain_id'], $edit['domain_arguments']);
  707. }
  708. }
  709. break;
  710. case 'delete':
  711. domain_prefix_drop_records($domain['domain_id']);
  712. break;
  713. }
  714. }
  715. /**
  716. * Drop tables created by this module.
  717. *
  718. * @param $domain_id
  719. * The domain_id taken from {domain}.
  720. */
  721. function domain_prefix_drop_records($domain_id) {
  722. $result = db_query("SELECT tablename FROM {domain_prefix} WHERE domain_id = %d AND status > 1", $domain_id);
  723. $prefix = domain_prefix_string($domain_id);
  724. while ($tables = db_fetch_array($result)) {
  725. $table = db_escape_table($prefix . $tables['tablename']);
  726. $exists = domain_prefix_table_exists($prefix, $tables['tablename']);
  727. if ($exists > 0) {
  728. db_query("DROP TABLE {%s}", $table);
  729. drupal_set_message(t('!string table dropped.', array('!string' => $table)));
  730. }
  731. }
  732. db_query("DELETE FROM {domain_prefix} WHERE domain_id = %d", $domain_id);
  733. }
  734. /**
  735. * Uniform prefix string
  736. *
  737. * @param $domain_id
  738. * The domain_id taken from {domain}.
  739. */
  740. function domain_prefix_string($domain_id) {
  741. return 'domain_'. $domain_id .'_';
  742. }
  743. /**
  744. * Implement hook_domaininstall()
  745. */
  746. function domain_prefix_domaininstall() {
  747. // If Domain Prefix is being used, check to see that it is installed correctly.
  748. if (module_exists('domain_prefix') && !function_exists('_domain_prefix_load')) {
  749. drupal_set_message(t('The Domain Prefix module is not installed correctly. Please edit your settings.php file as described in <a href="!url">INSTALL.txt</a>', array('!url' => base_path() . drupal_get_path('module', 'domain_prefix') .'/INSTALL.txt')));
  750. }
  751. }

Related topics