/deleteignored.php

http://showslow.googlecode.com/ · PHP · 134 lines · 96 code · 29 blank · 9 comment · 21 complexity · 7299182055c35fcd77896cebdff8d050 MD5 · raw file

  1. <?php
  2. require_once('global.php');
  3. # cleaning up URLs that are supposed to be ignored
  4. if ($ignoreURLs !== false && is_array($ignoreURLs)) {
  5. $prefixes = array();
  6. $regexes = array();
  7. foreach ($ignoreURLs as $ignoreString) {
  8. if (preg_match('/^[^a-zA-Z\\\s]/', $ignoreString))
  9. {
  10. $regexes[] = $ignoreString;
  11. }
  12. else
  13. {
  14. $prefixes[] = $ignoreString;
  15. }
  16. }
  17. $ids = array();
  18. if (count($prefixes) > 0 || !$enableNonHTTPURLs) {
  19. # prefix-matching URLs
  20. $query = 'SELECT id FROM urls WHERE ';
  21. $first = true;
  22. foreach ($prefixes as $prefix) {
  23. # safeguard in case prefix is empty
  24. if ($prefix == '') {
  25. continue;
  26. }
  27. if ($first) {
  28. $first = false;
  29. } else {
  30. $query .= ' OR ';
  31. }
  32. $query .= sprintf("LOCATE('%s', LOWER(url)) = 1", mysql_real_escape_string($prefix));
  33. }
  34. if (!$enableNonHTTPURLs) {
  35. if (!$first) {
  36. $query .= ' OR ';
  37. }
  38. $query .= "(LOCATE('http://', LOWER(url)) <> 1 AND LOCATE('https://', LOWER(url)) <> 1)";
  39. }
  40. $result = mysql_query($query);
  41. if (!$result) {
  42. error_log(mysql_error());
  43. exit;
  44. }
  45. while($row = mysql_fetch_assoc($result)) {
  46. $ids[] = $row['id'];
  47. }
  48. }
  49. # checking regexes - this takes time as we need to match every URL to every regex
  50. if (count($regexes) > 0) {
  51. $query = 'SELECT id, url FROM urls';
  52. $result = mysql_query($query);
  53. if (!$result) {
  54. error_log(mysql_error());
  55. exit;
  56. }
  57. while($row = mysql_fetch_assoc($result)) {
  58. $matched = false;
  59. foreach ($regexes as $regex) {
  60. if (preg_match($regex, $row['url'])) {
  61. $matched = true;
  62. break;
  63. }
  64. }
  65. if ($matched) {
  66. $ids[] = $row['id'];
  67. }
  68. }
  69. }
  70. # deleting data for custom metrics
  71. $query = sprintf("DELETE FROM metric WHERE url_id IN (%s)", implode(', ', $ids));
  72. $result = mysql_query($query);
  73. if (!$result) {
  74. error_log(mysql_error());
  75. exit;
  76. }
  77. # deleting data for yslow v2
  78. $query = sprintf("DELETE FROM yslow2 WHERE url_id IN (%s)", implode(', ', $ids));
  79. $result = mysql_query($query);
  80. if (!$result) {
  81. error_log(mysql_error());
  82. exit;
  83. }
  84. # deleting data for pagespeed
  85. $query = sprintf("DELETE FROM pagespeed WHERE url_id IN (%s)", implode(', ', $ids));
  86. $result = mysql_query($query);
  87. if (!$result) {
  88. error_log(mysql_error());
  89. exit;
  90. }
  91. # deleting data for dynatrace
  92. $query = sprintf("DELETE FROM dynatrace WHERE url_id IN (%s)", implode(', ', $ids));
  93. $result = mysql_query($query);
  94. if (!$result) {
  95. error_log(mysql_error());
  96. exit;
  97. }
  98. # resetting urls aggregates
  99. $query = sprintf("UPDATE urls SET last_update = NULL, yslow2_last_id = NULL, pagespeed_last_id = NULL, dynatrace_last_id = NULL WHERE id IN (%s)", implode(', ', $ids));
  100. $result = mysql_query($query);
  101. if (!$result) {
  102. error_log(mysql_error());
  103. exit;
  104. }
  105. }