PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/deleteignored.php

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