PageRenderTime 101ms CodeModel.GetById 40ms app.highlight 3ms RepoModel.GetById 37ms app.codeStats 0ms

/import.php

http://showslow.googlecode.com/
PHP | 61 lines | 47 code | 12 blank | 2 comment | 11 complexity | 0c82a6bc1eacb093c429cb0f2d9306b9 MD5 | raw file
 1<?php
 2require_once(dirname(__FILE__).'/global.php');
 3
 4$sites = file('php://stdin');
 5
 6$user_id = 1;
 7
 8$pairs = '';
 9$first = true;
10
11foreach ($sites as $site) {
12	if ($first) {
13		$first = false;
14	} else {
15		$pairs .= ', ';
16	}
17
18	$site = trim($site);
19
20	$url = filter_var($site, FILTER_VALIDATE_URL);
21
22	# let's try to beautify the URL by appending http://www.
23	if ($url === false) {
24		if (substr($site, 0, 3) == 'www') {
25			$url = filter_var('http://'.$site.'/', FILTER_VALIDATE_URL);
26		}
27		else
28		{
29			$url = filter_var('http://www.'.$site.'/', FILTER_VALIDATE_URL);
30		}
31	}
32	else
33	{
34		# skipping non-http URLs
35		if (substr($url, 0, 7) != 'http://' && substr($url, 0, 8) != 'https://') {
36			echo "Skipping non-http URL: $url\n";
37			continue;
38		}
39	}
40
41	echo "Importing URL: $url ...";
42	if ($url === false) {
43		echo "Bad data ($site)\n";
44		continue;
45	}
46	else
47	{
48		echo " OK\n";
49	}
50
51	$url_id = getUrlId($url);
52	$pairs .= '('.$user_id.','.$url_id.')';
53}
54
55$query = "INSERT IGNORE INTO user_urls (user_id, url_id) VALUES $pairs";
56
57$result = mysql_query($query);
58
59if (!$result) {
60	error_log(mysql_error());
61}