PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/simple-history/templates/template-settings-tab-debug.php

https://bitbucket.org/GraphicFusion/crafti-landing
PHP | 258 lines | 191 code | 51 blank | 16 comment | 5 complexity | f956b3abf2d4240fe066d8e66802e65b MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, Apache-2.0, GPL-3.0
  1. <?php
  2. defined( 'ABSPATH' ) or die();
  3. global $wpdb;
  4. $table_name = $wpdb->prefix . SimpleHistory::DBTABLE;
  5. $table_name_contexts = $wpdb->prefix . SimpleHistory::DBTABLE_CONTEXTS;
  6. $period_days = (int) 14;
  7. $period_start_date = DateTime::createFromFormat( 'U', strtotime( "-$period_days days" ) );
  8. $period_end_date = DateTime::createFromFormat( 'U', time() );
  9. /**
  10. * Size of database in both number or rows and table size
  11. */
  12. echo "<h3>Database size</h3>";
  13. // Get table sizes in mb
  14. $sql_table_size = sprintf( '
  15. SELECT table_name AS "table_name",
  16. round(((data_length + index_length) / 1024 / 1024), 2) "size_in_mb"
  17. FROM information_schema.TABLES
  18. WHERE table_schema = "%1$s"
  19. AND table_name IN ("%2$s", "%3$s");
  20. ',
  21. DB_NAME, // 1
  22. $table_name, // 2
  23. $table_name_contexts
  24. );
  25. $table_size_result = $wpdb->get_results( $sql_table_size );
  26. // Get num of rows for each table
  27. $total_num_rows_table = (int) $wpdb->get_var( "select count(*) FROM {$table_name}" );
  28. $total_num_rows_table_contexts = (int) $wpdb->get_var( "select count(*) FROM {$table_name_contexts}" );
  29. $table_size_result[0]->num_rows = $total_num_rows_table;
  30. $table_size_result[1]->num_rows = $total_num_rows_table_contexts;
  31. echo "<table class='widefat'>";
  32. printf(
  33. '<thead>
  34. <tr>
  35. <th>%1$s</th>
  36. <th>%2$s</th>
  37. <th>%3$s</th>
  38. </tr>
  39. </thead>
  40. ',
  41. _x("Table name", "debug dropin", "simple-history"),
  42. _x("Size", "debug dropin", "simple-history"),
  43. _x("Rows", "debug dropin", "simple-history")
  44. );
  45. $loopnum = 0;
  46. foreach ( $table_size_result as $one_table ) {
  47. $size = sprintf(
  48. _x('%s MB', "debug dropin", "simple-history"),
  49. $one_table->size_in_mb
  50. );
  51. $rows = sprintf(
  52. _x('%s rows', "debug dropin", "simple-history"),
  53. number_format_i18n( $one_table->num_rows, 0 )
  54. );
  55. printf( '<tr class="%4$s">
  56. <td>%1$s</td>
  57. <td>%2$s</td>
  58. <td>%3$s</td>
  59. </tr>',
  60. $one_table->table_name,
  61. $size,
  62. $rows,
  63. $loopnum % 2 ? " alt " : ""
  64. );
  65. $loopnum++;
  66. }
  67. echo "</table>";
  68. $logQuery = new SimpleHistoryLogQuery();
  69. $rows = $logQuery->query( array(
  70. "posts_per_page" => 1,
  71. ) );
  72. // This is the number of rows with occasions taken into consideration
  73. $total_accassions_rows_count = $rows["total_row_count"];
  74. echo "<p>";
  75. printf(
  76. _x('Total %s rows, when grouped by occasion id.', "debug dropin", "simple-history" ),
  77. $total_accassions_rows_count
  78. );
  79. echo "</p>";
  80. // echo "<h4>Clear history interval</h4>";
  81. // echo "<p>" . $this->sh->get_clear_history_interval() . "</p>";
  82. /**
  83. * Output a list of all active loggers, including name, slug, comment, message, capability and number of rows
  84. * Retrieve them in order by the number of rows they have in the db
  85. * Loggers with 0 rows in the db will not be included in the array, so we need to find those
  86. * and add them manually last
  87. */
  88. $arr_logger_slugs = array();
  89. foreach ( $this->sh->getInstantiatedLoggers() as $oneLogger ) {
  90. $arr_logger_slugs[] = $oneLogger["instance"]->slug;
  91. }
  92. $sql_logger_counts = sprintf( '
  93. SELECT logger, count(id) as count
  94. FROM %1$s
  95. WHERE logger IN ("%2$s")
  96. GROUP BY logger
  97. ORDER BY count DESC
  98. ', $table_name, join( $arr_logger_slugs, '","' ) );
  99. $logger_rows_count = $wpdb->get_results( $sql_logger_counts, OBJECT_K );
  100. // Find loggers with no rows in db and append to array
  101. $missing_logger_slugs = array_diff( $arr_logger_slugs, array_keys( $logger_rows_count ) );
  102. foreach ( $missing_logger_slugs as $one_missing_logger_slug ) {
  103. $logger_rows_count[$one_missing_logger_slug] = (object) array(
  104. "logger" => $one_missing_logger_slug,
  105. "count" => 0
  106. );
  107. }
  108. echo "<h3>";
  109. _ex("Loggers", "debug dropin", "simple-history");
  110. echo "</h3>";
  111. echo "<p>";
  112. printf(
  113. _x('Listing %1$d loggers, ordered by rows count in database.', "debug dropin", "simple-history"),
  114. sizeof( $arr_logger_slugs ) // 1
  115. );
  116. echo "</p>";
  117. echo "<table class='widefat fixed' cellpadding=2>";
  118. printf(
  119. '
  120. <thead>
  121. <tr>
  122. <th>%1$s</th>
  123. <th>%2$s</th>
  124. <th>%3$s</th>
  125. <th>%4$s</th>
  126. <th>%5$s</th>
  127. <th>%6$s</th>
  128. </tr>
  129. </thead>
  130. ',
  131. _x("Logger name", "debug dropin", "simple-history"),
  132. _x("Slug", "debug dropin", "simple-history"),
  133. _x("Description", "debug dropin", "simple-history"),
  134. _x("Messages", "debug dropin", "simple-history"),
  135. _x("Capability", "debug dropin", "simple-history"),
  136. _x("Rows count", "debug dropin", "simple-history")
  137. );
  138. $loopnum = 0;
  139. foreach ( $logger_rows_count as $one_logger_slug => $one_logger_val ) {
  140. $logger = $this->sh->getInstantiatedLoggerBySlug( $one_logger_slug );
  141. if ( ! $logger ) {
  142. continue;
  143. }
  144. if ( isset( $logger_rows_count[ $one_logger_slug ] ) ) {
  145. $one_logger_count = $logger_rows_count[ $one_logger_slug ];
  146. } else {
  147. // logger was not is sql result, so fake result
  148. $one_logger_count = new stdclass;
  149. $one_logger_count->count = 0;
  150. }
  151. $logger_info = $logger->getInfo();
  152. $logger_messages = isset( $logger_info["messages"] ) ? (array) $logger_info["messages"] : array();
  153. $html_logger_messages = "";
  154. foreach ( $logger_messages as $message_key => $message ) {
  155. $html_logger_messages .= sprintf( '<li>%1$s</li>', esc_html( $message ) );
  156. }
  157. if ( $html_logger_messages ) {
  158. $str_num_message_strings = sprintf(
  159. _x('%1$s message strings', "debug dropin", "simple-history"),
  160. sizeof( $logger_messages )
  161. );
  162. $html_logger_messages = sprintf( '
  163. <p>%1$s</p>
  164. <ul class="hide-if-js">
  165. %2$s
  166. </ul>
  167. ',
  168. $str_num_message_strings, // 1
  169. $html_logger_messages // 2
  170. );
  171. } else {
  172. $html_logger_messages = "<p>No message strings</p>";
  173. }
  174. printf(
  175. '
  176. <tr class="%6$s">
  177. <td>
  178. <p><strong>%3$s</strong>
  179. </td>
  180. <td>
  181. <p><code>%2$s</code></p>
  182. </td>
  183. <td>
  184. <p>%4$s</p>
  185. </td>
  186. <td>
  187. %7$s
  188. </td>
  189. <td>
  190. <p>%5$s</p>
  191. </td>
  192. <td>
  193. <p>%1$s</p>
  194. </td>
  195. </tr>
  196. ',
  197. number_format_i18n( $one_logger_count->count ),
  198. esc_html( $one_logger_slug ), // 2
  199. esc_html( $logger_info["name"] ),
  200. esc_html( $logger_info["description"] ), // 4
  201. esc_html( $logger->getCapability() ), // 5
  202. $loopnum % 2 ? " alt " : "", // 6
  203. $html_logger_messages // 7
  204. );
  205. $loopnum++;
  206. }
  207. echo "</table>";