feat: ✨ make the website load for different subdomains
This commit is contained in:
parent
f00e97d294
commit
d39595d577
4 changed files with 80 additions and 54 deletions
|
@ -3,8 +3,10 @@
|
|||
class MySQLAdapter {
|
||||
public $raw;
|
||||
|
||||
function __construct() {
|
||||
$db_params = json_decode(file_get_contents('secrets/db.json'), true);
|
||||
function __construct($SITE_CONFIG) {
|
||||
$this->SITE_CONFIG = $SITE_CONFIG;
|
||||
|
||||
$db_params = $SITE_CONFIG['db'];
|
||||
|
||||
try {
|
||||
if(false !== getenv('MYSQL_HOST')) {
|
||||
|
@ -14,18 +16,16 @@ class MySQLAdapter {
|
|||
getenv('MYSQL_PORT'));
|
||||
}
|
||||
else {
|
||||
$this->raw = mysqli_connect($db_params['MYSQL_HOST'],
|
||||
$db_params['MYSQL_USER'], $db_params['MYSQL_PASSWORD'],
|
||||
$db_params['MYSQL_DATABASE'],
|
||||
$db_params['MYSQL_PORT']);
|
||||
$this->raw = mysqli_connect($db_params['host'],
|
||||
$db_params['user'], $db_params['password'],
|
||||
$db_params['database'],
|
||||
$db_params['port']);
|
||||
}
|
||||
} catch (\Throwable $th) {
|
||||
echo 'Connection failed<br>';
|
||||
echo 'Error number: ' . mysqli_connect_errno() . '<br>';
|
||||
echo 'Error message: ' . mysqli_connect_error() . '<br>';
|
||||
die();
|
||||
|
||||
//throw $th;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -79,12 +79,13 @@ class MySQLAdapter {
|
|||
|
||||
$qry = "
|
||||
INSERT INTO posts
|
||||
(post_path, post_path_depth, post_metadata, post_content)
|
||||
(host, post_path, post_path_depth, post_metadata, post_content)
|
||||
VALUES
|
||||
( ?, ?, ?, ?) AS new
|
||||
( ?, ?, ?, ?, ?) AS new
|
||||
ON DUPLICATE KEY UPDATE post_path=new.post_path;";
|
||||
|
||||
$this->_exec($qry, "siss",
|
||||
$this->_exec($qry, "ssiss",
|
||||
$this->SITE_CONFIG['HTTP_HOST'],
|
||||
$post_path,
|
||||
$path_depth,
|
||||
json_encode($post_metadata),
|
||||
|
@ -110,27 +111,28 @@ class MySQLAdapter {
|
|||
|
||||
$qry = "INSERT INTO path_access_counts
|
||||
(access_time,
|
||||
post_path, agent, referrer,
|
||||
host, post_path, agent, referrer,
|
||||
path_access_count,
|
||||
path_processing_time)
|
||||
VALUES ( from_unixtime(floor(unix_timestamp(CURRENT_TIMESTAMP) / 300)*300),
|
||||
?, ?, ?, 1, ?
|
||||
?, ?, ?, ?, 1, ?
|
||||
) AS new
|
||||
ON DUPLICATE KEY
|
||||
UPDATE path_access_count=path_access_counts.path_access_count+1,
|
||||
path_processing_time=path_access_counts.path_processing_time+new.path_processing_time;
|
||||
";
|
||||
|
||||
$this->_exec($qry, "sssd", $post_path, $agent, $referrer, $time);
|
||||
$this->_exec($qry, "ssssd", $this->SITE_CONFIG['HTTP_HOST'], $post_path, $agent, $referrer, $time);
|
||||
|
||||
if(preg_match('/^user/', $agent)) {
|
||||
$this->_exec("UPDATE posts SET post_access_count=post_access_count+1 WHERE post_path=?", "s", $post_path);
|
||||
$this->_exec("UPDATE posts SET post_access_count=post_access_count+1 WHERE post_path=? AND host=?", "ss",
|
||||
$post_path, $this->SITE_CONFIG['HTTP_HOST']);
|
||||
}
|
||||
}
|
||||
|
||||
function get_post_access_counters() {
|
||||
$qry = "
|
||||
SELECT post_path, agent, path_access_count, path_processing_time
|
||||
SELECT host, post_path, agent, path_access_count, path_processing_time
|
||||
FROM path_access_counts
|
||||
WHERE path_last_access_time > ( CURRENT_TIMESTAMP - INTERVAL 10 MINUTE );
|
||||
";
|
||||
|
@ -157,7 +159,7 @@ class MySQLAdapter {
|
|||
|
||||
function get_post_access_counters_line() {
|
||||
$qry = "
|
||||
SELECT access_time, post_path, agent, referrer, path_access_count, path_processing_time
|
||||
SELECT host, access_time, post_path, agent, referrer, path_access_count, path_processing_time
|
||||
FROM path_access_counts
|
||||
WHERE access_time < ( CURRENT_TIMESTAMP - INTERVAL 6 MINUTE )
|
||||
ORDER BY access_time DESC;
|
||||
|
@ -169,7 +171,7 @@ class MySQLAdapter {
|
|||
try {
|
||||
$data = $this->_exec($qry, "")->fetch_all(MYSQLI_ASSOC);
|
||||
|
||||
$data_prefix="access_metrics,host=" . $_SERVER['SERVER_NAME'];
|
||||
$data_prefix="access_metrics";
|
||||
|
||||
$out_data = "";
|
||||
|
||||
|
@ -180,7 +182,8 @@ class MySQLAdapter {
|
|||
if($path == '') {
|
||||
$path = '/';
|
||||
}
|
||||
$out_data .= $data_prefix . ",agent=".$post_data['agent'].",path=".$path.",referrer=".$post_data['referrer'];
|
||||
$out_data .= $data_prefix . ",host=" . $post_data['host'] . ",agent=".$post_data['agent'];
|
||||
$out_data .= ",path=".$path.",referrer=".$post_data['referrer'];
|
||||
|
||||
$out_data .= " access_sum=" . $post_data['path_access_count'] . ",time_sum=" . $post_data['path_processing_time'];
|
||||
$out_data .= " " . strtotime($post_data['access_time']) . "000000000\n";
|
||||
|
@ -205,8 +208,8 @@ class MySQLAdapter {
|
|||
$this->_exec("
|
||||
UPDATE posts
|
||||
SET post_settings_cache=NULL
|
||||
WHERE post_path LIKE ?;
|
||||
", "s", $post_path . "%");
|
||||
WHERE host = ? AND post_path LIKE ?;
|
||||
", "ss", $this->SITE_CONFIG['HTTP_HOST'], $post_path . "%");
|
||||
}
|
||||
|
||||
function update_or_create_post($post_path, $post_metadata, $post_content) {
|
||||
|
@ -219,15 +222,16 @@ class MySQLAdapter {
|
|||
|
||||
$qry = "
|
||||
INSERT INTO posts
|
||||
(post_path, post_path_depth, post_metadata, post_content)
|
||||
(host, post_path, post_path_depth, post_metadata, post_content)
|
||||
VALUES
|
||||
( ?, ?, ?, ?) AS new
|
||||
( ?, ?, ?, ?, ?) AS new
|
||||
ON DUPLICATE KEY
|
||||
UPDATE post_metadata=new.post_metadata,
|
||||
post_content=new.post_content,
|
||||
post_update_time=CURRENT_TIMESTAMP;";
|
||||
|
||||
$this->_exec($qry, "siss",
|
||||
$this->_exec($qry, "ssiss",
|
||||
$this->SITE_CONFIG['HTTP_HOST'],
|
||||
$post_path,
|
||||
$path_depth,
|
||||
json_encode($post_metadata),
|
||||
|
@ -240,8 +244,8 @@ class MySQLAdapter {
|
|||
$post_settings = $this->_exec("
|
||||
SELECT post_path, post_settings_cache
|
||||
FROM posts
|
||||
WHERE post_path = ?
|
||||
", "s", $post_path)->fetch_assoc();
|
||||
WHERE post_path = ? AND host = ?
|
||||
", "ss", $post_path, $this->SITE_CONFIG['HTTP_HOST'])->fetch_assoc();
|
||||
|
||||
if(!isset($post_settings)) {
|
||||
return [];
|
||||
|
@ -259,8 +263,8 @@ class MySQLAdapter {
|
|||
$post_metadata = $this->_exec("
|
||||
SELECT post_path, post_metadata
|
||||
FROM posts
|
||||
WHERE post_path = ?
|
||||
", "s", $post_path)->fetch_assoc();
|
||||
WHERE post_path = ? AND host = ?
|
||||
", "ss", $post_path, $this->SITE_CONFIG['HTTP_HOST'])->fetch_assoc();
|
||||
|
||||
if(isset($post_metadata['post_metadata'])) {
|
||||
$post_metadata = json_decode($post_metadata['post_metadata'], true);
|
||||
|
@ -282,12 +286,13 @@ class MySQLAdapter {
|
|||
$with_subposts = false, $with_settings = true) {
|
||||
|
||||
$qry = "SELECT *
|
||||
FROM posts WHERE post_path = ?
|
||||
FROM posts
|
||||
WHERE post_path = ? AND host = ?
|
||||
";
|
||||
|
||||
$post_path = $this->_sanitize_path($post_path);
|
||||
|
||||
$post_data = $this->_exec($qry, "s", $post_path)->fetch_assoc();
|
||||
$post_data = $this->_exec($qry, "ss", $post_path, $this->SITE_CONFIG['HTTP_HOST'])->fetch_assoc();
|
||||
|
||||
if(!isset($post_data)) {
|
||||
$post_data = ['found' => false];
|
||||
|
@ -319,12 +324,15 @@ class MySQLAdapter {
|
|||
|
||||
$qry = "SELECT post_path, post_metadata, post_update_time
|
||||
FROM posts
|
||||
WHERE (post_path LIKE CONCAT(?,'/%'))
|
||||
WHERE
|
||||
host = ?
|
||||
AND (post_path LIKE CONCAT(?,'/%'))
|
||||
AND post_path_depth = ?
|
||||
ORDER BY post_create_time DESC
|
||||
LIMIT 10";
|
||||
ORDER BY post_path ASC
|
||||
LIMIT 50";
|
||||
|
||||
$post_data = $this->_exec($qry, "si", $path, $path_depth+1)->fetch_all(MYSQLI_ASSOC);
|
||||
$post_data = $this->_exec($qry, "ssi", $this->SITE_CONFIG['HTTP_HOST'],
|
||||
$path, $path_depth+1)->fetch_all(MYSQLI_ASSOC);
|
||||
|
||||
$fn = function($data) {
|
||||
return $this->_normalize_post_data($data);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue