fallback nick and url if not set in txt

This commit is contained in:
sørenpeter 2024-04-20 10:28:50 +02:00
parent ccf39b7c91
commit 2b553bceb9
5 changed files with 81 additions and 54 deletions

View file

@ -144,7 +144,7 @@ a.author {
.profile {
padding: 0 1rem;
margin-bottom: 2rem;
margin-bottom: 1rem;
}
.profile-grid {

View file

@ -334,6 +334,7 @@ function getTwtsFromTwtxtString($url) {
}
if (str_starts_with($currentLine, '#')) {
// Check if comments (starting with #) have some metadata
if (!is_null(getSingleParameter('url', $currentLine))) {
$currentURL = getSingleParameter('url', $currentLine);
@ -364,6 +365,18 @@ function getTwtsFromTwtxtString($url) {
}
}
// Fallback for nick and url if not set in twtxt.txt
if ($twtxtData->nick === "") {
$host_to_nick = parse_url($url, PHP_URL_HOST);
$host_to_nick = str_replace("www.", "", $host_to_nick);
$host_to_nick = explode(".", $host_to_nick)[0];
$twtxtData->nick = $host_to_nick;
}
if ($twtxtData->mainURL === "") {
$twtxtData->mainURL = $url;
}
if (!str_starts_with($currentLine, '#')) {
$explodedLine = explode("\t", $currentLine);
if (count($explodedLine) >= 2) {

View file

@ -23,11 +23,19 @@ $twt_posts = array_filter($twts, function ($twt) use ($pattern) {
return !preg_match($pattern, $twt->originalTwtStr);
});
$twt_gallery = array_filter($twts, function ($twt) {
return preg_match('/!\[(.*?)\]\((.*?)\)/', $twt->originalTwtStr);
//return preg_match('/(<img[^>]+>)/i', $twt->content);
});
// Get active view/filter
$is_gallery = str_contains(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), "gallery");
$is_replies = str_contains(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), "replies");
if ($is_gallery) {
//$twts = $twt_gallery;
$posts_active = "";
$replies_active = "";
$gallery_artive = "class='active'";

View file

@ -9,6 +9,7 @@ if (!empty($_GET['search'])) {
$pattern = preg_quote($search, '/');
$pattern = "/^.*$pattern.*\$/mi";
// TODO: Make into a genral function in twtxt.php since i use it so much in profile.php and potentialle other places
$twts = array_filter($twts, function ($twt) use ($pattern) {
return preg_match($pattern, $twt->content);
});

View file

@ -4,6 +4,8 @@
// Base on code from: https://social.dfaria.eu/search
// Add all tags to one array
$tags = [];
foreach ($twts as $twt) {
$tag_array = getTagsFromTwt($twt->content);
@ -12,34 +14,36 @@ foreach ($twts as $twt) {
}
}
natcasesort($tags);
$tag_count = array_count_values($tags);
//arsort($tag_count, SORT_STRING);
//ksort($tag_count, SORT_STRING);
//strnatcasecmp($tag_count);
if(!empty($tags)) {
$max_count = max($tag_count);
natcasesort($tags);
$tag_count = array_count_values($tags);
//arsort($tag_count, SORT_STRING);
//ksort($tag_count, SORT_STRING);
//strnatcasecmp($tag_count);
$min_font_size = 10;
$max_font_size = 30;
$num_intermediate_levels = 1;
$font_size_interval = ($max_font_size - $min_font_size) / ($num_intermediate_levels + 1);
$max_count = max($tag_count);
// Get current URI to keep the `?profile=URL` as the base for links to tags
$uri = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$min_font_size = 10;
$max_font_size = 30;
$num_intermediate_levels = 1;
$font_size_interval = ($max_font_size - $min_font_size) / ($num_intermediate_levels + 1);
// Remove existing `(?|&)search=word` params from base URI
// https://stackoverflow.com/a/4937540
$uri = preg_replace('/.search=\w+&?/', '$1', $uri);
// Get current URI to keep the `?profile=URL` as the base for links to tags
$uri = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
// Remove existing `(?|&)search=word` params from base URI
// https://stackoverflow.com/a/4937540
$uri = preg_replace('/.search=\w+&?/', '$1', $uri);
// Get active tag, if any
$active_tag = "";
if (!empty($_GET['search'])) {
// Get active tag, if any
$active_tag = "";
if (!empty($_GET['search'])) {
$active_tag = $_GET['search'];
}
}
foreach ($tag_count as $tag => $count) {
foreach ($tag_count as $tag => $count) {
$font_size = $min_font_size + ($count / $max_count) * $font_size_interval;
$tag = str_replace('#', '', $tag);
@ -49,29 +53,30 @@ foreach ($tag_count as $tag => $count) {
echo '<a href="'.$uri.'&search='.$tag.'" style="font-size: '.$font_size.'px;">#'. $tag .'</a> ';
}
//echo '<a href="?search='.$tag.'">#'. $tag .' ('.$count.')</a> ';
}
}
// Detail/summary with top tags and solo tags
// Detail/summary with top tags and solo tags
/*
$top_tags = array_filter($tag_count, function($val){return ($val>1);});
$solo_tags = array_diff($tag_count, $top_tags);
krsort($solo_tags, SORT_STRING);
/*
$top_tags = array_filter($tag_count, function($val){return ($val>1);});
$solo_tags = array_diff($tag_count, $top_tags);
krsort($solo_tags, SORT_STRING);
echo "<details><summary> Tags: ";
echo "<details><summary> Tags: ";
foreach ($top_tags as $tag => $count) {
foreach ($top_tags as $tag => $count) {
$tag = str_replace('#', '', $tag);
echo '<a href="?search='.$tag.'">#'.$tag.'</a> ';
}
}
echo "</summary>";
echo "</summary>";
foreach ($solo_tags as $tag => $count) {
foreach ($solo_tags as $tag => $count) {
$tag = str_replace('#', '', $tag);
echo '<a href="?search='.$tag.'">#'.$tag.'</a> ';
}
echo "</details>";
*/
}
echo "</details>";
*/