start.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Elgg garbage collector.
  4. *
  5. * @package ElggGarbageCollector
  6. */
  7. elgg_register_event_handler('init', 'system', 'garbagecollector_init');
  8. function garbagecollector_init() {
  9. $period = elgg_get_plugin_setting('period', 'garbagecollector');
  10. switch ($period) {
  11. case 'weekly':
  12. case 'monthly':
  13. case 'yearly':
  14. break;
  15. default:
  16. $period = 'monthly';
  17. }
  18. // Register cron hook
  19. elgg_register_plugin_hook_handler('cron', $period, 'garbagecollector_cron');
  20. elgg_register_plugin_hook_handler('gc', 'system', 'garbagecollector_orphaned_metastrings');
  21. elgg_register_plugin_hook_handler('gc', 'system', 'garbagecollector_entities');
  22. }
  23. /**
  24. * Cron job
  25. */
  26. function garbagecollector_cron($hook, $entity_type, $returnvalue, $params) {
  27. echo elgg_echo('garbagecollector') . "\n";
  28. // Now, because we are nice, trigger a plugin hook to let other plugins do some GC
  29. $rv = true;
  30. $period = elgg_get_plugin_setting('period','garbagecollector');
  31. elgg_trigger_plugin_hook('gc', 'system', array('period' => $period));
  32. // Now we optimize all tables
  33. $tables = garbagecollector_get_tables();
  34. foreach ($tables as $table) {
  35. echo elgg_echo('garbagecollector:optimize', array($table));
  36. if (garbagecollector_optimize_table($table) !== false) {
  37. echo elgg_echo('garbagecollector:ok');
  38. } else {
  39. echo elgg_echo('garbagecollector:error');
  40. }
  41. echo "\n";
  42. }
  43. echo elgg_echo('garbagecollector:done');
  44. }
  45. /**
  46. * Get array of table names
  47. *
  48. * @return array
  49. */
  50. function garbagecollector_get_tables() {
  51. static $tables;
  52. if (isset($tables)) {
  53. return $tables;
  54. }
  55. $table_prefix = elgg_get_config('dbprefix');
  56. $result = get_data("SHOW TABLES LIKE '$table_prefix%'");
  57. $tables = array();
  58. if (is_array($result) && !empty($result)) {
  59. foreach ($result as $row) {
  60. $row = (array) $row;
  61. if (is_array($row) && !empty($row)) {
  62. foreach ($row as $element) {
  63. $tables[] = $element;
  64. }
  65. }
  66. }
  67. }
  68. return $tables;
  69. }
  70. /**
  71. * Optimize a table
  72. *
  73. * @param string $table Database table name
  74. * @return bool
  75. */
  76. function garbagecollector_optimize_table($table) {
  77. $table = sanitise_string($table);
  78. return update_data("OPTIMIZE TABLE $table");
  79. }
  80. /**
  81. * Garbage collect stub and fragments from any broken delete/create calls
  82. *
  83. * @return void
  84. */
  85. function garbagecollector_entities() {
  86. $dbprefix = elgg_get_config('dbprefix');
  87. $tables = array(
  88. 'site' => 'sites_entity',
  89. 'object' => 'objects_entity',
  90. 'group' => 'groups_entity',
  91. 'user' => 'users_entity',
  92. );
  93. foreach ($tables as $type => $table) {
  94. delete_data("DELETE FROM {$dbprefix}{$table}
  95. WHERE guid NOT IN (SELECT guid FROM {$dbprefix}entities)");
  96. delete_data("DELETE FROM {$dbprefix}entities
  97. WHERE type = '$type' AND guid NOT IN (SELECT guid FROM {$dbprefix}{$table})");
  98. }
  99. }
  100. /**
  101. * Delete any orphaned entries in metastrings.
  102. *
  103. * @return void
  104. */
  105. function garbagecollector_orphaned_metastrings() {
  106. $dbprefix = elgg_get_config('dbprefix');
  107. // Garbage collect metastrings
  108. echo elgg_echo('garbagecollector:gc:metastrings');
  109. // If memcache is enabled then we need to flush it of deleted values
  110. if (is_memcache_available()) {
  111. $select_query = "
  112. SELECT * FROM {$dbprefix}metastrings WHERE
  113. (
  114. (id NOT IN (SELECT name_id FROM {$dbprefix}metadata)) AND
  115. (id NOT IN (SELECT value_id FROM {$dbprefix}metadata)) AND
  116. (id NOT IN (SELECT name_id FROM {$dbprefix}annotations)) AND
  117. (id NOT IN (SELECT value_id FROM {$dbprefix}annotations))
  118. )";
  119. $dead = get_data($select_query);
  120. if ($dead) {
  121. static $metastrings_memcache;
  122. if (!$metastrings_memcache) {
  123. $metastrings_memcache = new \ElggMemcache('metastrings_memcache');
  124. }
  125. foreach ($dead as $d) {
  126. $metastrings_memcache->delete($d->string);
  127. }
  128. }
  129. }
  130. $query = "
  131. DELETE FROM {$dbprefix}metastrings WHERE
  132. (
  133. (id NOT IN (SELECT name_id FROM {$dbprefix}metadata)) AND
  134. (id NOT IN (SELECT value_id FROM {$dbprefix}metadata)) AND
  135. (id NOT IN (SELECT name_id FROM {$dbprefix}annotations)) AND
  136. (id NOT IN (SELECT value_id FROM {$dbprefix}annotations))
  137. )";
  138. $result = delete_data($query);
  139. if ($result !== false) {
  140. echo elgg_echo('garbagecollector:ok');
  141. } else {
  142. echo elgg_echo('garbagecollector:error');
  143. }
  144. }