<commit_before><?php
require_once ('config.php');
require_once ('functions.php');

//	Set up the database connection
$mysqli = new mysqli(DB_IP, DB_USER, DB_PASS, DB_TABLE);
if ($mysqli->connect_errno) {
	echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

if (!$mysqli->set_charset("utf8mb4")) {
	printf("Error loading character set utf8mb4: %s\n", $mysqli->error);
	exit();
}

function insert_bench($lat, $long, $inscription, $userID)
{
	$inscription = htmlspecialchars($inscription, ENT_NOQUOTES);

	$address = get_place_name($lat, $long);

	global $mysqli;
	$insert_bench = $mysqli->prepare(
		"INSERT INTO `benches`
		       (`benchID`,`latitude`,`longitude`,`address`, `inscription`,`description`,`present`,`published`, `added`,  `userID`)
		VALUES (NULL,      ?,        ?,          ?,          ?,           '',           '1'  ,    '1', CURRENT_TIMESTAMP, ?      )");
	$insert_bench->bind_param('ddssi', $lat, $long, $address, $inscription, $userID);
	$insert_bench->execute();
	$resultID = $insert_bench->insert_id;
	if ($resultID) {
		return $resultID;
	} else {
		return null;
	}
}

function edit_bench($lat, $long, $inscription, $benchID, $published=true)
{
	$inscription = htmlspecialchars($inscription, ENT_NOQUOTES);
	$address = get_place_name($lat, $long);

	global $mysqli;
	$edit_bench = $mysqli->prepare(
		"UPDATE `benches`
		    SET `latitude` =    ?,
		        `longitude` =   ?,
		        `address` =     ?,
		        `inscription` = ?,
		        `published` =   ?
		  WHERE `benches`.`benchID` = ?");
	$edit_bench->bind_param('ddssii', $lat, $long, $address, $inscription, $published, $benchID);
	$edit_bench->execute();
	$edit_bench->close();

	return true;
}

function update_bench_address($benchID, $benchLat, $benchLong) {
	$address = get_place_name($benchLat, $benchLong);
	global $mysqli;
	$edit_bench = $mysqli->prepare(
		"UPDATE `benches`
		    SET `address` = ?
		  WHERE `benches`.`benchID` = ?");
	$edit_bench->bind_param('si', $address, $benchID);
	$edit_bench->execute();
	$edit_bench->close();

	return $address;
}


function insert_media($benchID, $userID, $sha1, $licence="CC BY-SA 4.0", $import=null, $media_type=null)
{
	global $mysqli;
	$insert_media = $mysqli->prepare(
		'INSERT INTO `media`
		       (`mediaID`,`benchID`,`userID`,`sha1`, `licence`, `importURL`, `media_type`)
		VALUES (NULL,      ?,        ?,       ?,      ?,         ?         ,  ?);'
	);

	$insert_media->bind_param('iissss', $benchID, $userID, $sha1, $licence, $import, $media_type);
	$insert_media->execute();
	$resultID = $insert_media->insert_id;
	if ($resultID) {
		return $resultID;
	} else {
		return null;
	}
}

function edit_media_type($mediaID, $media_type) {
	global $mysqli;
	$edit_media = $mysqli->prepare(
		"UPDATE `media`
		    SET `media_type` =    ?
		  WHERE `media`.`media_type` = ?");
	$edit_media->bind_param('ss', $media_type, $mediaID);
	$edit_media->execute();
	$edit_media->close();

	return true;
}

function insert_user($provider, $providerID, $name)
{
	global $mysqli;

	$insert_user = $mysqli->prepare("INSERT INTO `users`
		       (`userID`, `provider`, `providerID`, `name`)
		VALUES (NULL,     ?,           ?,            ?);"
	);

	$insert_user->bind_param('sss', $provider, $providerID, $name);

	$insert_user->execute();

	$resultID = $insert_user->insert_id;
	$insert_user->close();
	if ($resultID) {
		return $resultID;
	} else {
		return null;
	}
}

function get_nearest_benches($lat, $long, $distance=0.5, $limit=20)
{
	global $mysqli;

	$get_benches = $mysqli->prepare(
		"SELECT
			(
				6371 * ACOS(COS(RADIANS(?)) *
				COS(RADIANS(latitude)) *
				COS(RADIANS(longitude) -
				RADIANS(?)) +
				SIN(RADIANS(?)) *
				SIN(RADIANS(latitude)))
			)
			AS distance, benchID, latitude, longitude, inscription, published
		FROM
			benches
		WHERE published = true
		HAVING distance < ?
		ORDER BY distance
		LIMIT 0 , ?");

	$get_benches->bind_param('ddddd', $lat, $long, $lat, $distance, $limit );
	$get_benches->execute();

	/* bind result variables */
	$get_benches->bind_result($dist, $benchID, $benchLat, $benchLong, $benchInscription, $published);

	# Build GeoJSON feature collection array
	$geojson = array(
		'type'      => 'FeatureCollection',
		'features'  => array()
	);
	# Loop through rows to build feature arrays
	while($get_benches->fetch()) {
		$feature = array(
			'id' => $benchID,
			'type' => 'Feature',
			'geometry' => array(
				'type' => 'Point',
				# Pass Longitude and Latitude Columns here
				'coordinates' => array($benchLong, $benchLat)
			),
			# Pass other attribute columns here
			'properties' => array(
				'popupContent' => $benchInscription,
			),
		);
		# Add feature arrays to feature collection array
		array_push($geojson['features'], $feature);
	}

	return $geojson;
}

function get_bench($benchID){
	return get_all_benches($benchID);
}

function get_all_benches($id = 0)
{
	global $mysqli;

	if(0==$id){
		$benchQuery = ">";
	} else {
		$benchQuery = "=";
	}

	$get_benches = $mysqli->prepare(
		"SELECT benchID, latitude, Longitude, inscription, published FROM benches
		WHERE published = true
		AND benchID {$benchQuery} ?
		LIMIT 0 , 2048");

	$get_benches->bind_param('i', $id);
	$get_benches->execute();

	/* bind result variables */
	$get_benches->bind_result($benchID, $benchLat, $benchLong, $benchInscription, $published);

	# Build GeoJSON feature collection array<commit_msg>Show pin on deleted screen<commit_after><?php
require_once ('config.php');
require_once ('functions.php');

//	Set up the database connection
$mysqli = new mysqli(DB_IP, DB_USER, DB_PASS, DB_TABLE);
if ($mysqli->connect_errno) {
	echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

if (!$mysqli->set_charset("utf8mb4")) {
	printf("Error loading character set utf8mb4: %s\n", $mysqli->error);
	exit();
}

function insert_bench($lat, $long, $inscription, $userID)
{
	$inscription = htmlspecialchars($inscription, ENT_NOQUOTES);

	$address = get_place_name($lat, $long);

	global $mysqli;
	$insert_bench = $mysqli->prepare(
		"INSERT INTO `benches`
		       (`benchID`,`latitude`,`longitude`,`address`, `inscription`,`description`,`present`,`published`, `added`,  `userID`)
		VALUES (NULL,      ?,        ?,          ?,          ?,           '',           '1'  ,    '1', CURRENT_TIMESTAMP, ?      )");
	$insert_bench->bind_param('ddssi', $lat, $long, $address, $inscription, $userID);
	$insert_bench->execute();
	$resultID = $insert_bench->insert_id;
	if ($resultID) {
		return $resultID;
	} else {
		return null;
	}
}

function edit_bench($lat, $long, $inscription, $benchID, $published=true)
{
	$inscription = htmlspecialchars($inscription, ENT_NOQUOTES);
	$address = get_place_name($lat, $long);

	global $mysqli;
	$edit_bench = $mysqli->prepare(
		"UPDATE `benches`
		    SET `latitude` =    ?,
		        `longitude` =   ?,
		        `address` =     ?,
		        `inscription` = ?,
		        `published` =   ?
		  WHERE `benches`.`benchID` = ?");
	$edit_bench->bind_param('ddssii', $lat, $long, $address, $inscription, $published, $benchID);
	$edit_bench->execute();
	$edit_bench->close();

	return true;
}

function update_bench_address($benchID, $benchLat, $benchLong) {
	$address = get_place_name($benchLat, $benchLong);
	global $mysqli;
	$edit_bench = $mysqli->prepare(
		"UPDATE `benches`
		    SET `address` = ?
		  WHERE `benches`.`benchID` = ?");
	$edit_bench->bind_param('si', $address, $benchID);
	$edit_bench->execute();
	$edit_bench->close();

	return $address;
}


function insert_media($benchID, $userID, $sha1, $licence="CC BY-SA 4.0", $import=null, $media_type=null)
{
	global $mysqli;
	$insert_media = $mysqli->prepare(
		'INSERT INTO `media`
		       (`mediaID`,`benchID`,`userID`,`sha1`, `licence`, `importURL`, `media_type`)
		VALUES (NULL,      ?,        ?,       ?,      ?,         ?         ,  ?);'
	);

	$insert_media->bind_param('iissss', $benchID, $userID, $sha1, $licence, $import, $media_type);
	$insert_media->execute();
	$resultID = $insert_media->insert_id;
	if ($resultID) {
		return $resultID;
	} else {
		return null;
	}
}

function edit_media_type($mediaID, $media_type) {
	global $mysqli;
	$edit_media = $mysqli->prepare(
		"UPDATE `media`
		    SET `media_type` =    ?
		  WHERE `media`.`media_type` = ?");
	$edit_media->bind_param('ss', $media_type, $mediaID);
	$edit_media->execute();
	$edit_media->close();

	return true;
}

function insert_user($provider, $providerID, $name)
{
	global $mysqli;

	$insert_user = $mysqli->prepare("INSERT INTO `users`
		       (`userID`, `provider`, `providerID`, `name`)
		VALUES (NULL,     ?,           ?,            ?);"
	);

	$insert_user->bind_param('sss', $provider, $providerID, $name);

	$insert_user->execute();

	$resultID = $insert_user->insert_id;
	$insert_user->close();
	if ($resultID) {
		return $resultID;
	} else {
		return null;
	}
}

function get_nearest_benches($lat, $long, $distance=0.5, $limit=20)
{
	global $mysqli;

	$get_benches = $mysqli->prepare(
		"SELECT
			(
				6371 * ACOS(COS(RADIANS(?)) *
				COS(RADIANS(latitude)) *
				COS(RADIANS(longitude) -
				RADIANS(?)) +
				SIN(RADIANS(?)) *
				SIN(RADIANS(latitude)))
			)
			AS distance, benchID, latitude, longitude, inscription, published
		FROM
			benches
		WHERE published = true
		HAVING distance < ?
		ORDER BY distance
		LIMIT 0 , ?");

	$get_benches->bind_param('ddddd', $lat, $long, $lat, $distance, $limit );
	$get_benches->execute();

	/* bind result variables */
	$get_benches->bind_result($dist, $benchID, $benchLat, $benchLong, $benchInscription, $published);

	# Build GeoJSON feature collection array
	$geojson = array(
		'type'      => 'FeatureCollection',
		'features'  => array()
	);
	# Loop through rows to build feature arrays
	while($get_benches->fetch()) {
		$feature = array(
			'id' => $benchID,
			'type' => 'Feature',
			'geometry' => array(
				'type' => 'Point',
				# Pass Longitude and Latitude Columns here
				'coordinates' => array($benchLong, $benchLat)
			),
			# Pass other attribute columns here
			'properties' => array(
				'popupContent' => $benchInscription,
			),
		);
		# Add feature arrays to feature collection array
		array_push($geojson['features'], $feature);
	}

	return $geojson;
}

function get_bench($benchID){
	return get_all_benches($benchID, false);
}

function get_all_benches($id = 0, $only_published = true)
{
	global $mysqli;

	if(0==$id){
		$benchQuery = ">";
	} else {
		$benchQuery = "=";
	}

	if ($only_published){
		$where = "WHERE published = true AND benchID {$benchQuery} ?";
	} else {
		$where = "WHERE benchID {$benchQuery} ?";
	}

	$get_benches = $mysqli->prepare(
		"SELECT benchID, latitude, Longitude, inscription, published FROM benches
		{$where}
		LIMIT 0 , 2048");

	$get_benches->bind_param('i', $id);
	$get_benches->execute();

	/* bind result variables */
	$get_benches->bind_result($benchID, $benchLat, $benchLong, $benchInscription, $published);

	# Build GeoJSON feature collection array
{'fraction_non_alphanumeric': 0.17560443555717142, 'fraction_numerical': 0.0049081985093619345, 'mean_word_length': 4.0150410209662715, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 101, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3655968', 'n_tokens_mistral': 4350, 'n_tokens_neox': 3954, 'n_words': 791}