summaryrefslogtreecommitdiff
path: root/urlcache/urlcache.class
blob: b058a1c76f3f639339cdb37fa1ecc8dfc40166aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/*
**	$Id$
**
**	Relog Internet Tools 3 Library ("ITOOLS3")     
**
**	it_urlcache.class - Interface to URL content cache in DB
**
**	This class relies on a DB table with the following scheme:
**	url		CHAR(255)	NOT NULL,
**	updateinterval	INT		NOT NULL,
**	lastupdate	INT		NOT NULL,
**	nextupdate	INT		NOT NULL,
**	contentmd5	CHAR(32)	NOT NULL,
**	content		TEXT		NOT NULL,
**	PRIMARY KEY	(url),
**	KEY		(nextupdate)
*/

/* PRIVATE */
define("_IT_URLCACHE_DEFAULT_INTERVAL", 300);
define("_IT_URLCACHE_MAX_CONNECTIONS", 30);

class it_urlcache extends it_db_record
{
	/* PRIVATE */
	var $default_interval = _IT_URLCACHE_DEFAULT_INTERVAL;
	var $max_connections = _IT_URLCACHE_MAX_CONNECTIONS;

function set_default_interval($default_interval)
{
	$this->default_interval = $default_interval;
}


function set_max_connections($max_connections)
{
	$this->max_connections = $max_connections;
}


/*
 * Register an URL to be fetched by background process.
 * Note: get() will return an empty string until the url is fetched for the
 * first time
 */
function register($url)
{
	if (!$this->read($url))
	{
		$tags = array("url" => $url, "updateinterval" => $this->default_interval);
		$this->create($tags);
	}
}


/*
 * Removes the url from the cache. It won't be fetched again until register()
 * is called again.
 */
function unregister($url)
{
	if ($this->read($url))
		$this->delete();
}


/*
 * Get content of url. Returns empty string if it was never successfully
 * fetched.
 */
function get_content($url)
{
	$this->read($url);
	return $this->data['content'];
}


/*
 * Try to refetch all URLs in the database.
 * Note: This is intented for background process use only as it may take
 * a while to finish...
 */
function update_cache()
{
	$now = time();
	$pages = array();

	$result = $this->table->safe_sql_select("WHERE nextupdate < $now", "url");

	while (list($url) = $this->table->db->fetch_array($result))
		$pages[] = $url;

	$this->table->db->free($result);

	$this->_fetch_urls($pages, $now);
}


/* PRIVATE */
function _update_cache($url, $content, $now)
{
	$contentmd5 = md5($content);

	if ($this->read($url))
	{
		$interval = $this->data['updateinterval'];
		$age = $now - $this->data['lastupdate'];

		if ($this->data['contentmd5'] == $contentmd5)
		{
			if ($age > ($interval * 2))
				$interval = $interval * 1.5;

			$tags = array("updateinterval" => $interval, "nextupdate" => $now + $interval);
		}
		else
		{
			if ($age < ($interval / 2))
				$interval = $interval / 1.5;

			if ($interval < $this->default_interval)
				$interval = $this->default_interval;

			$tags = array("updateinterval" => $interval, "lastupdate" => $now, "nextupdate" => $now + $interval, "contentmd5" => $contentmd5, "content" => $content);
		}

		$this->update($tags);
	}
	else
	{
		$tags = array("url" => $url, "updateinterval" => $this->default_interval, "lastupdate" => $now, "nextupdate" => $now + $this->default_interval, "contentmd5" => $contentmd5, "content" => $content);
		$this->create($tags);
	}
}


/*
 * PRIVATE
 * Fetch URL in array $urls and store update cache database accordingly
 */
function _fetch_urls($pages, $now)
{
	$pos = 0;
	$fds = array();
	$urls = array();
	$content = array();
	$count = 0;

	for ($pos = 0; ($pos < count($pages)) && ($pos < $this->max_connections); $pos++)
	{
		debug("Opening $pages[$pos]", 10);

		if ($fd = fopen($pages[$pos], "r"))
		{
			socket_set_blocking($fd, false);
			$fds[] = $fd;
			$urls[] = $pages[$pos];
			$content[] = "";
			$count++;
			debug("Opened $pages[$pos]", 10);
		}
	}

	while ($count > 0)
	{
		$read = 0;

		for ($i = 0; $i < count($fds); $i++)
		{
			if ($fds[$i])
			{
				if (!feof($fds[$i]))
				{
					$data = fread($fds[$i], 4096);
					$content[$i] .= $data;
					$read += strlen($data);
					debug("Read " . strlen($data) . " from $urls[$i]", 10);
				}

				if (feof($fds[$i]))
				{
					fclose($fds[$i]);
					$fds[$i] = 0;
					$count--;
					$this->_update_cache($urls[$i], $content[$i], $now);
					debug("Finished $urls[$i]: " . strlen($content[$i]) . " bytes", 10);

					/* Schedule next url for retrieval */
					if ($pos < count($pages))
					{
						if ($fd = fopen($pages[$pos], "r"))
						{
							socket_set_blocking($fd, false);
							$fds[$i] = $fd;
							$urls[$i] = $pages[$pos];
							$content[$i] = "";
							$count++;
							debug("Opened $pages[$pos]", 10);
						}

						$pos++;
					}
				}
			}
		}

		/* We are in non-blocking mode, be nice */
		if (($count > 0) && ($read == 0))
		{
			sleep(1);
			debug("Sleeping...", 10);
		}
	}
}

} /* End class it_urlcache */