PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/AdminTools/models/cron/trim-logs.query.php

https://github.com/TitanKing/todoplugins
PHP | 148 lines | 85 code | 15 blank | 48 comment | 4 complexity | 9fb2ccceae35d4e49ec71bc99341e589 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * Trim Logs - Count general logs.
  4. * @author Jason Schoeman [titan@phpdevshell.org], Ross Kuyper, Contact: rosskuyper@gmail.com.
  5. *
  6. */
  7. class PHPDS_countLogsQuery extends PHPDS_query
  8. {
  9. protected $sql = "
  10. SELECT
  11. COUNT(*)
  12. FROM
  13. _db_core_logs
  14. ";
  15. protected $singleValue = true;
  16. }
  17. /**
  18. * Trim Logs - Delete general logs.
  19. * @author Jason Schoeman [titan@phpdevshell.org], Ross Kuyper, Contact: rosskuyper@gmail.com.
  20. *
  21. */
  22. class PHPDS_deleteLogsQuery extends PHPDS_query
  23. {
  24. protected $sql = "
  25. DELETE FROM
  26. _db_core_logs
  27. ORDER BY
  28. log_time
  29. ASC
  30. LIMIT
  31. %s
  32. ";
  33. }
  34. /**
  35. * Trim Logs - Count node access logs.
  36. * @author Jason Schoeman [titan@phpdevshell.org], Ross Kuyper, Contact: rosskuyper@gmail.com.
  37. *
  38. */
  39. class PHPDS_countAccessLogsQuery extends PHPDS_query
  40. {
  41. protected $sql = "
  42. SELECT
  43. COUNT(*)
  44. FROM
  45. _db_core_node_access_logs
  46. ";
  47. protected $singleValue = true;
  48. }
  49. /**
  50. * Trim Logs - Delete node access logs.
  51. * @author Jason Schoeman [titan@phpdevshell.org], Ross Kuyper, Contact: rosskuyper@gmail.com.
  52. *
  53. */
  54. class PHPDS_deleteAccessLogsQuery extends PHPDS_query
  55. {
  56. protected $sql = "
  57. DELETE FROM
  58. _db_core_node_access_logs
  59. ORDER BY
  60. timestamp
  61. ASC
  62. LIMIT
  63. %s
  64. ";
  65. }
  66. /**
  67. * Trim Logs - Trim Logs
  68. * @author Jason Schoeman [titan@phpdevshell.org], Ross Kuyper, Contact: rosskuyper@gmail.com.
  69. *
  70. */
  71. class PHPDS_trimLogsQuery extends PHPDS_query
  72. {
  73. /**
  74. * Initiate query invoke command.
  75. * @return array
  76. */
  77. public function invoke($parameters = null)
  78. {
  79. // Get trimming setting.
  80. $settings = $this->db->getSettings(array('trim_logs'), 'AdminTools');
  81. /////////////////////////////////////////////////////
  82. /////////////// GENERAL LOGS ////////////////////////
  83. /////////////////////////////////////////////////////
  84. // Check how many general logs we have...
  85. $count_general_logs = $this->db->invokeQuery('PHPDS_countLogsQuery');
  86. // Check if anything needs to be trimmed.
  87. if ($count_general_logs > $settings['trim_logs']) {
  88. // Number that needs trimming from general logs.
  89. $trim_count_general = $count_general_logs - $settings['trim_logs'];
  90. // Trim general records!
  91. $this->db->invokeQuery('PHPDS_deleteLogsQuery', $trim_count_general);
  92. // Show ok message!
  93. $job_status_general = __('I have trimmed required <b>general</b> logs.');
  94. } else {
  95. $trim_count_general = 0;
  96. $job_status_general = __('Nothing to trim in <b>general</b> logs');
  97. }
  98. // How many general records.
  99. $general_records = sprintf(__('We have <b>%s</b> <b>general</b> logs.'), $count_general_logs);
  100. $trim_records_general = sprintf(__('I need to trim <b>%s</b> <b>general</b> logs.'), $trim_count_general);
  101. /////////////////////////////////////////////////////
  102. /////////////// ACCESS LOGS /////////////////////////
  103. /////////////////////////////////////////////////////
  104. // Check how many access logs we have...
  105. $count_access_logs = $this->db->invokeQuery('PHPDS_countAccessLogsQuery');
  106. // Check if anything needs to be trimmed.
  107. if ($count_access_logs > $settings['trim_logs']) {
  108. // Number that needs trimming from access logs.
  109. $trim_count_access = $count_access_logs - $settings['trim_logs'];
  110. // Trim access records!
  111. $this->db->invokeQuery('PHPDS_deleteAccessLogsQuery', $trim_count_access);
  112. // Show ok message!
  113. $job_status_access = __('I have trimmed required <b>access</b> logs.');
  114. } else {
  115. $trim_count_access = 0;
  116. $job_status_access = __('Nothing to trim in <b>access</b> logs');
  117. }
  118. // How many access records.
  119. $access_records = sprintf(__('We have <b>%s</b> <b>access</b> logs.'), $count_access_logs);
  120. $trim_records_access = sprintf(__('I need to trim <b>%s</b> <b>access</b> logs.'), $trim_count_access);
  121. return array(
  122. 'general_records' => $general_records,
  123. 'trim_records_general' => $trim_records_general,
  124. 'job_status_general' => $job_status_general,
  125. 'access_records' => $access_records,
  126. 'trim_records_access' => $trim_records_access,
  127. 'job_status_general' => $job_status_general,
  128. 'access_records' => $access_records,
  129. 'trim_records_access' => $trim_records_access,
  130. 'job_status_access' => $job_status_access
  131. );
  132. }
  133. }