PageRenderTime 23ms CodeModel.GetById 25ms RepoModel.GetById 0ms 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
  2. require_once(dirname(__FILE__).'/global.php');
  3. $sites = file('php://stdin');
  4. $user_id = 1;
  5. $pairs = '';
  6. $first = true;
  7. foreach ($sites as $site) {
  8. if ($first) {
  9. $first = false;
  10. } else {
  11. $pairs .= ', ';
  12. }
  13. $site = trim($site);
  14. $url = filter_var($site, FILTER_VALIDATE_URL);
  15. # let's try to beautify the URL by appending http://www.
  16. if ($url === false) {
  17. if (substr($site, 0, 3) == 'www') {
  18. $url = filter_var('http://'.$site.'/', FILTER_VALIDATE_URL);
  19. }
  20. else
  21. {
  22. $url = filter_var('http://www.'.$site.'/', FILTER_VALIDATE_URL);
  23. }
  24. }
  25. else
  26. {
  27. # skipping non-http URLs
  28. if (substr($url, 0, 7) != 'http://' && substr($url, 0, 8) != 'https://') {
  29. echo "Skipping non-http URL: $url\n";
  30. continue;
  31. }
  32. }
  33. echo "Importing URL: $url ...";
  34. if ($url === false) {
  35. echo "Bad data ($site)\n";
  36. continue;
  37. }
  38. else
  39. {
  40. echo " OK\n";
  41. }
  42. $url_id = getUrlId($url);
  43. $pairs .= '('.$user_id.','.$url_id.')';
  44. }
  45. $query = "INSERT IGNORE INTO user_urls (user_id, url_id) VALUES $pairs";
  46. $result = mysql_query($query);
  47. if (!$result) {
  48. error_log(mysql_error());
  49. }