offload.module 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * The offload module allows site admins to prune older data from sites with
  4. * heavy workloads in order to maintain performance without having to lose the
  5. * historical data. It achieves this result by allowing a non-web back-office
  6. * to automatically replicate historical data to a back office database, then
  7. * remove the replicated data from the historical table within Drupal.
  8. *
  9. * By default, the module is configured to work with
  10. * - accesslog (from core)
  11. * It also works with contrib modules:
  12. * - zeitgeist
  13. * And potentially any other module using logging tables with a monotone key sequence
  14. *
  15. * A sample replication client in PHP-GTK is provided in the "client" directory
  16. *
  17. * (c) 2007 Ouest Systèmes Informatiques.
  18. * Licensed under the CeCILL 2.0
  19. * http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
  20. *
  21. */
  22. define(OFFLOADVARTABLES, 'offload_tables');
  23. define(OFFLOADVARIP, 'offload_ip');
  24. define(OFFLOADPERM, 'offload history');
  25. define(OFFLOADVERSION, '$Id$');
  26. function offload_help($section)
  27. {
  28. $ret = null;
  29. switch ($section)
  30. {
  31. case 'admin/help#offload':
  32. $ret = t('<p>The offload module allows site admins to prune older data from sites with heavy workloads in order to maintain performance without having to lose the historical data. It achieves this result by allowing a non-web back-office to automatically replicate historical data to a back office database, then remove the replicated data from the historical table within Drupal.</p>');
  33. break;
  34. }
  35. return $ret;
  36. }
  37. /*
  38. * Implementation of hook_menu().
  39. */
  40. function offload_menu($may_cache)
  41. {
  42. $items = array();
  43. if ($may_cache)
  44. {
  45. $items[] = array(
  46. 'path' => 'admin/settings/offload',
  47. 'title' => t('History off-load '),
  48. 'callback' => 'drupal_get_form',
  49. 'callback arguments' => array('offload_settings'),
  50. 'access' => user_access(OFFLOADPERM),
  51. );
  52. }
  53. return $items;
  54. }
  55. function offload_perm()
  56. {
  57. return array
  58. (
  59. OFFLOADPERM,
  60. );
  61. }
  62. function offload_settings()
  63. {
  64. $ar1 = variable_get(OFFLOADVARTABLES, array(
  65. array('accesslog', 'aid', NULL)) /* table name, id, highest key offloaded */
  66. );
  67. $ar2 = array();
  68. foreach ($ar1 as $table)
  69. {
  70. $ar2[] = "{$table[0]} ({$table[1]} = {$table[2]})";
  71. }
  72. $form[OFFLOADVARIP] = array(
  73. '#type' => 'textfield',
  74. '#title' => t('Client IP address'),
  75. '#default_value' => variable_get(OFFLOADVARIP, '127.0.0.1'),
  76. '#description' => t('The single IP address allowed to use the offload module as a client. Be warned that, in shared hosting situations, the default 127.0.0.1 may <em>not</em> be safe'),
  77. );
  78. $form['info']= array(
  79. '#type' => 'fieldset',
  80. '#title' => t('Tables configured for offload'),
  81. '#description' => t('In future versions, this will be settable. For now the list is hardcoded'),
  82. );
  83. $form['info']['tables'] = array(
  84. '#type' => 'markup',
  85. '#value' => theme('item_list', $ar2),
  86. );
  87. $form['advanced'] = array(
  88. '#type' => 'fieldset',
  89. '#title' => t('Advanced information'),
  90. '#collapsible' => TRUE,
  91. '#collapsed' => TRUE,
  92. );
  93. $form['advanced']['version'] = array(
  94. '#type' => 'markup',
  95. '#description' => 'CVS Version',
  96. '#value' => OFFLOADVERSION,
  97. '#prefix' => '<p>',
  98. '#suffix' => '</p><div class="description">CVS Version</div>',
  99. );
  100. return system_settings_form($form);
  101. }
  102. function offload_xmlrpc()
  103. {
  104. return array(
  105. 'offload.info' => 'offload_info',
  106. 'offload.login' => 'offload_authenticate',
  107. 'offload.table_status' => 'offload_table_status',
  108. );
  109. }
  110. function offload_info()
  111. {
  112. return t('Offload module is alive');
  113. }
  114. function offload_authenticate($name, $pass)
  115. {
  116. $ret = user_authenticate($name, $pass);
  117. return $ret;
  118. }
  119. function offload_table_status($ar_tables)
  120. {
  121. global $db_prefix;
  122. $sq = 'SHOW TABLE STATUS';
  123. $q = db_query($sq);
  124. $len = strlen($db_prefix);
  125. while ($row = db_fetch_array($q))
  126. {
  127. if (strncmp($row['Name'], $db_prefix, $len) !== 0)
  128. continue;
  129. $name = substr($row['Name'], $len);
  130. if (array_key_exists($name, $ar_tables))
  131. {
  132. $ar_tables[$name]['remote'] = $row['Rows'];
  133. }
  134. }
  135. return $ar_tables;
  136. }