Merge pull request #34 from MrPetovan/bug/fix-submit-inserts

Fix submit inserts
This commit is contained in:
Tobias Diekershoff 2017-11-01 08:11:03 +01:00 committed by GitHub
commit 21ca069227
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 432 additions and 450 deletions

View File

@ -140,10 +140,12 @@ function q($sql)
$ret = null;
if ($db) {
$ret = $db->q(vsprintf($sql, $args));
$final_sql = vsprintf($sql, $args);
$ret = $db->q($final_sql);
if ($db->db->errno) {
logger('dba: ' . $db->db->error);
logger('dba: ' . $db->db->error . ' sql: ' . $final_sql);
}
} else {
error_log(__FILE__ . ':' . __LINE__ . ' $db has gone');

View File

@ -1,5 +1,4 @@
<?php
/*
Based on a submitted URL, take note of the site it mentions.
Ensures that the site health will be tracked if it wasn't already.
@ -11,7 +10,6 @@
if (!function_exists('notice_site')) {
function notice_site($url, $check_health = false)
{
global $a;
//Parse the domain from the URL.
@ -25,12 +23,10 @@ function notice_site($url, $check_health=false)
//If it exists, see if we need to update any flags / statuses.
if (!empty($result) && isset($result[0])) {
$entry = $result[0];
//If we are allowed to do health checks...
if ($check_health) {
//And the site is in bad health currently, do a check now.
//This is because you have a high certainty the site may perform better now.
if ($entry['health_score'] < -40) {
@ -42,9 +38,7 @@ function notice_site($url, $check_health=false)
elseif (strtotime($entry['dt_last_probed']) < time() - $a->config['site-health']['min_probe_delay']) {
run_site_probe($entry['id'], $entry);
}
}
}
//If it does not exist.
@ -67,27 +61,24 @@ function notice_site($url, $check_health=false)
$entry = $result[0];
run_site_probe($result[0]['id'], $entry);
}
}
}
//Give other scripts the site health.
return isset($entry) ? $entry : false;
}}
}
}
//Extracts the site from a given URL.
if (!function_exists('parse_site_from_url')) {
function parse_site_from_url($url)
{
//Currently a simple implementation, but may improve over time.
#TODO: support subdirectories?
$urlMeta = parse_url($url);
return $urlMeta['scheme'] . '://' . $urlMeta['host'];
}}
}
}
//Performs a ping to the given site ID
//You may need to notice the site first before you know it's ID.
@ -100,7 +91,6 @@ function parse_site_from_url($url)
if (!function_exists('run_site_probe')) {
function run_site_probe($id, &$entry_out)
{
global $a;
//Get the site information from the DB, based on the ID.
@ -123,27 +113,22 @@ function run_site_probe($id, &$entry_out)
//Prepare the CURL call.
$handle = curl_init();
$options = array(
//Timeouts
CURLOPT_TIMEOUT => max($a->config['site-health']['probe_timeout'], 1), //Minimum of 1 second timeout.
CURLOPT_CONNECTTIMEOUT => 1,
//Redirecting
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 8,
//SSL
CURLOPT_SSL_VERIFYPEER => true,
// CURLOPT_VERBOSE => true,
// CURLOPT_CERTINFO => true,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS,
//Basic request
CURLOPT_USERAGENT => 'friendica-directory-probe-1.0',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $probe_location
);
curl_setopt_array($handle, $options);
@ -161,7 +146,6 @@ function run_site_probe($id, &$entry_out)
//When it's the certificate that doesn't work.
if ($sslcert_issues) {
//Probe again, without strict SSL.
$options[CURLOPT_SSL_VERIFYPEER] = false;
@ -177,7 +161,6 @@ function run_site_probe($id, &$entry_out)
//Store new status.
$curl_statuscode = curl_errno($handle);
}
//Gather more meta.
@ -215,7 +198,6 @@ function run_site_probe($id, &$entry_out)
}
if (!$parse_failed) {
$given_base_url_match = $data->url == $base_url;
//Record the probe speed in a probes table.
@ -258,7 +240,6 @@ function run_site_probe($id, &$entry_out)
if (isset($data->no_scrape_url)) {
$parsedDataQuery .= sprintf("`no_scrape_url` = '%s',", dbesc($data->no_scrape_url));
}
}
//Get the new health.
@ -285,39 +266,46 @@ function run_site_probe($id, &$entry_out)
if ($result && isset($result[0])) {
$entry_out = $result[0];
}
}}
}
}
//Determines the new health score after a probe has been executed.
if (!function_exists('health_score_after_probe')) {
function health_score_after_probe($current, $probe_success, $time = null, $version = null, $ssl_issues = null)
{
//Probe failed, costs you 30 points.
if(!$probe_success) return max($current-30, -100);
if (!$probe_success) {
return max($current - 30, -100);
}
//A good probe gives you 20 points.
$current += 20;
//Speed scoring.
if (intval($time) > 0) {
//Pentaly / bonus points.
if ($time > 800) $current -= 10; //Bad speed.
elseif ($time > 400) $current -= 5; //Still not good.
elseif ($time > 250) $current += 0; //This is normal.
elseif ($time > 120) $current += 5; //Good speed.
else $current += 10; //Excellent speed.
if ($time > 800) {
$current -= 10; //Bad speed.
} elseif ($time > 400) {
$current -= 5; //Still not good.
} elseif ($time > 250) {
$current += 0; //This is normal.
} elseif ($time > 120) {
$current += 5; //Good speed.
} else {
$current += 10; //Excellent speed.
}
//Cap for bad speeds.
if ($time > 800) $current = min(40, $current);
elseif ($time > 400) $current = min(60, $current);
if ($time > 800) {
$current = min(40, $current);
} elseif ($time > 400) {
$current = min(60, $current);
}
}
//Version check.
if (!empty($version)) {
$versionParts = explode('.', $version);
//Older than 3.x.x?
@ -333,7 +321,6 @@ function health_score_after_probe($current, $probe_success, $time=null, $version
#TODO: See if this needs to be more dynamic.
#TODO: See if this is a proper indicator of health.
}
//SSL problems? That's a big deal.
@ -343,19 +330,25 @@ function health_score_after_probe($current, $probe_success, $time=null, $version
//Don't go beyond +100 or -100.
return max(min(100, $current), -100);
}}
}
}
//Changes a score into a name. Used for classes and such.
if (!function_exists('health_score_to_name')) {
function health_score_to_name($score)
{
if ($score < -50) return 'very-bad';
elseif ($score < 0) return 'bad';
elseif ($score < 30) return 'neutral';
elseif ($score < 50) return 'ok';
elseif ($score < 80) return 'good';
else return 'perfect';
}}
if ($score < -50) {
return 'very-bad';
} elseif ($score < 0) {
return 'bad';
} elseif ($score < 30) {
return 'neutral';
} elseif ($score < 50) {
return 'ok';
} elseif ($score < 80) {
return 'good';
} else {
return 'perfect';
}
}
}

View File

@ -1,14 +1,16 @@
<?php
require_once 'datetime.php';
require_once 'site-health.php';
require_once 'Scrape.php';
require_once 'Photo.php';
require_once('datetime.php');
require_once('site-health.php');
function run_submit($url) {
function run_submit($url)
{
global $a;
if(! strlen($url))
if (!strlen($url)) {
return false;
}
logger('Updating: ' . $url);
@ -26,6 +28,8 @@ function run_submit($url) {
dbesc($nurl)
);
$profile_id = null;
if (count($r)) {
$profile_exists = true;
$profile_id = $r[0]['id'];
@ -33,10 +37,7 @@ function run_submit($url) {
$r = q("UPDATE `profile` SET
`available` = 0,
`updated` = '%s'
WHERE `id` = %d LIMIT 1",
dbesc(datetime_convert()),
intval($profile_id)
WHERE `id` = %d LIMIT 1", dbesc(datetime_convert()), intval($profile_id)
);
}
@ -53,32 +54,27 @@ function run_submit($url) {
}
}
require_once('Scrape.php');
//Skip the scrape? :D
$noscrape = $site_health && $site_health['no_scrape_url'];
if ($noscrape) {
//Find out who to look up.
$which = str_replace($site_health['base_url'], '', $url);
$noscrape = preg_match('~/profile/([^/]+)~', $which, $matches) === 1;
//If that did not fail...
if ($noscrape) {
$parms = noscrape_dfrn($site_health['no_scrape_url'].'/'.$matches[1]);
$noscrape = !!$parms; //If the result was false, do a scrape after all.
$params = noscrape_dfrn($site_health['no_scrape_url'] . '/' . $matches[1]);
$noscrape = !!$params; //If the result was false, do a scrape after all.
}
}
if (!$noscrape) {
$parms = scrape_dfrn($url);
$params = scrape_dfrn($url);
}
// Empty result is due to an offline site.
if(!count($parms) > 1){
if (!count($params) > 1) {
//For large sites this could lower the health too quickly, so don't track health.
//But for sites that are already in bad status. Do a cleanup now.
if ($profile_exists && $site_health['health_score'] < $a->config['maintenance']['remove_profile_health_threshold']) {
@ -87,17 +83,14 @@ function run_submit($url) {
}
return false;
}
} elseif (x($params, 'explicit-hide') && $profile_exists) {
// We don't care about valid dfrn if the user indicates to be hidden.
elseif($parms['explicit-hide'] && $profile_exists) {
logger('User opted out of the directory.');
nuke_record($url);
return true; //This is a good update.
}
if((x($parms,'hide')) || (! (x($parms,'fn')) && (x($parms,'photo')))) {
if ((x($params, 'hide')) || (!(x($params, 'fn')) && (x($params, 'photo')))) {
if ($profile_exists) {
logger('Profile inferred to be opted out of the directory.');
nuke_record($url);
@ -106,17 +99,18 @@ function run_submit($url) {
}
// This is most likely a problem with the site configuration. Ignore.
if(validate_dfrn($parms)) {
if (validate_dfrn($params)) {
logger('Site is unavailable');
return false;
}
$photo = $parms['photo'];
$photo = $params['photo'];
dbesc_array($parms);
dbesc_array($params);
if(x($parms,'comm'))
$parms['comm'] = intval($parms['comm']);
if (x($params, 'comm')) {
$params['comm'] = intval($params['comm']);
}
if ($profile_exists) {
$r = q("UPDATE `profile` SET
@ -133,48 +127,46 @@ function run_submit($url) {
`available` = 1,
`updated` = '%s'
WHERE `id` = %d LIMIT 1",
$parms['fn'],
$parms['pdesc'],
$parms['locality'],
$parms['region'],
$parms['postal-code'],
$parms['country-name'],
$params['fn'],
$params['pdesc'],
$params['locality'],
$params['region'],
$params['postal-code'],
$params['country-name'],
dbesc($url),
dbesc($nurl),
intval($parms['comm']),
$parms['tags'],
intval($params['comm']),
$params['tags'],
dbesc(datetime_convert()),
intval($profile_id)
);
logger('Update returns: ' . $r);
}
else {
} else {
$r = q("INSERT INTO `profile` ( `name`, `pdesc`, `locality`, `region`, `postal-code`, `country-name`, `homepage`, `nurl`, `comm`, `tags`, `created`, `updated` )
VALUES ( '%s', '%s', '%s', '%s' , '%s', '%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', '%s' )",
$parms['fn'],
$parms['pdesc'],
$parms['locality'],
$parms['region'],
$parms['postal-code'],
$parms['country-name'],
VALUES ( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', '%s' )",
$params['fn'],
x($params, 'pdesc') ? $params['pdesc'] : '',
x($params, 'locality') ? $params['locality'] : '',
x($params, 'region') ? $params['region'] : '',
x($params, 'postal-code') ? $params['postal-code'] : '',
x($params, 'country-name') ? $params['country-name'] : '',
dbesc($url),
dbesc($nurl),
intval($parms['comm']),
$parms['tags'],
intval($params['comm']),
x($params, 'tags') ? $params['tags'] : '',
dbesc(datetime_convert()),
dbesc(datetime_convert())
);
logger('Insert returns: ' . $r);
$r = q("SELECT `id` FROM `profile` WHERE ( `homepage` = '%s' or `nurl` = '%s' ) order by id asc",
$r = q("SELECT `id` FROM `profile` WHERE ( `homepage` = '%s' or `nurl` = '%s' ) ORDER BY `id` ASC",
dbesc($url),
dbesc($nurl)
);
if(count($r))
if (count($r)) {
$profile_id = $r[count($r) - 1]['id'];
}
if (count($r) > 1) {
q("DELETE FROM `photo` WHERE `profile-id` = %d LIMIT 1",
@ -184,12 +176,10 @@ function run_submit($url) {
intval($r[0]['id'])
);
}
}
if($parms['tags']) {
$arr = explode(' ', $parms['tags']);
if(count($arr)) {
if ($params['tags']) {
$arr = explode(' ', $params['tags']);
foreach ($arr as $t) {
$t = strip_tags(trim($t));
$t = substr($t, 0, 254);
@ -208,12 +198,9 @@ function run_submit($url) {
}
}
}
}
$submit_photo_start = microtime(true);
require_once("Photo.php");
$photo_failure = false;
$status = false;
@ -225,13 +212,11 @@ function run_submit($url) {
$img->scaleImageSquare(80);
$r = $img->store($profile_id);
}
$r = q("UPDATE `profile` SET `photo` = '%s' WHERE `id` = %d LIMIT 1",
dbesc($a->get_baseurl() . '/photo/' . $profile_id . '.jpg'),
$r = q("UPDATE `profile` SET `photo` = '%s' WHERE `id` = %d LIMIT 1", dbesc($a->get_baseurl() . '/photo/' . $profile_id . '.jpg'),
intval($profile_id)
);
$status = true;
}
else{
} else {
nuke_record($url);
return false;
}
@ -241,23 +226,23 @@ function run_submit($url) {
$time = round(($submit_end - $submit_start) * 1000);
//Record the scrape speed in a scrapes table.
if($site_health && $status) q(
if ($site_health && $status) {
q(
"INSERT INTO `site-scrape` (`site_health_id`, `dt_performed`, `request_time`, `scrape_time`, `photo_time`, `total_time`)" .
"VALUES (%u, NOW(), %u, %u, %u, %u)",
$site_health['id'],
$parms['_timings']['fetch'],
$parms['_timings']['scrape'],
$params['_timings']['fetch'],
$params['_timings']['scrape'],
$photo_time,
$time
);
return $status;
}
return $status;
}
function nuke_record($url) {
function nuke_record($url)
{
$nurl = str_replace(array('https:', '//www.'), array('http:', '//'), $url);
$r = q("SELECT `id` FROM `profile` WHERE ( `homepage` = '%s' OR `nurl` = '%s' ) ",

View File

@ -1,10 +1,12 @@
<?php
require_once('include/submit.php');
require_once('include/sync.php');
require_once 'include/submit.php';
require_once 'include/sync.php';
function submit_content(&$a) {
use Friendica\Directory\App;
function submit_content(App &$a)
{
//Decode the URL.
$url = hex2bin(notags(trim($_GET['url'])));
@ -13,6 +15,6 @@ function submit_content(&$a) {
//Run the submit sequence.
run_submit($url);
exit;
exit;
}