Added interpreter for pseudocode for displaying exhibitions, events.

Added basic search.
Added object pages.
This commit is contained in:
2018-06-15 11:26:25 +02:00
committed by Stefan Rohde-Enslin
parent a49746ab10
commit 80485a98ab
19 changed files with 1573 additions and 82 deletions

View File

@ -69,13 +69,15 @@ function ensureEnvironment() {
$settings = array_merge(
[
"startPage" => "1",
"pageTitle" => "md:cms",
"logo" => "",
"url" => "",
"mdVersion" => "https://www.museum-digital.de/nat/",
"maxFileSize" => 300000,
"defaultLang" => "en"
"startPage" => "1",
"pageTitle" => "md:cms",
"logo" => "",
"url" => "",
"cacheRefreshInterval" => 0,
"mdVersion" => "https://rlp.museum-digital.de/",
"mdImgFolder" => "https://rlp.museum-digital.de/data/rlp/",
"maxFileSize" => 300000,
"defaultLang" => "en"
],
json_decode(file_get_contents(__DIR__ . "/../data/settings.json"), true)
);
@ -109,6 +111,42 @@ function loadPublicPages() {
}
/**
* Query or cache pages.
*
* @param string $url URL to query.
* @param string $area The type of the queried page. If caching is enabled, renew cache every X seconds.
* @param array $settings Settings variable.
*
* @return array
*/
function queryCachePage(string $url, string $area = "", array $settings = ['cacheRefreshInterval' => 0]) {
// Ignore caching if cacheRefreshInterval equals zero.
if ($settings['cacheRefreshInterval'] == 0) {
return file_get_contents($url);
}
$fileDir = __DIR__ . "/../data/caches/$area";
ensureDir($fileDir);
$fileName = md5($url);
$filePath = "$fileDir/$fileName.json";
// Load from cache.
if (file_exists($filePath) && time() - filemtime($filePath) < $settings['cacheRefreshInterval']) {
return file_get_contents($filePath);
}
// Refresh cache.
$contents = file_get_contents($url);
file_put_contents($filePath, $contents, LOCK_EX);
return $contents;
}
/**
* Function scanDirConts is a wrapper around scandir(), which removes [".", ".."].
*
@ -320,4 +358,78 @@ function checkPreviewAccess($sessionStarted = false) {
}
/**
* Function checking if a string starts with another.
*
* @param string $haystack String to check.
* @param string $needle Potential start of $haystack.
*
* @return boolean
*/
function startswith(string $haystack, string $needle):bool {
if (substr($haystack, 0, strlen($needle)) == $needle) return (true);
else return (false);
}
/**
* Function checking if a string starts with any input from the input array.
*
* @param string $haystack String to check.
* @param string[] $needles Array containing potential start values of $haystack.
*
* @return boolean
*/
function startswithAny(string $haystack, array $needles):bool {
$output = false;
foreach ($needles as $needle) {
$output = startswith($haystack, $needle);
if ($output == true) return $output;
}
return $output;
}
/**
* Curling web pages.
* Function to check errors.
*
* @param string $url URL to query.
* @param string $host Authentication data. Optional.
*
* @return string
*/
function runCurl(string $url, string $host = ""):string {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
// curl_setopt($curl, CURLOPT_RESOLVE, ["www.example.com:443:172.16.1.1"]);
curl_setopt($curl, CURLOPT_ENCODING, '');
if ($host) curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host: $host"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
/**
* This function cuts down a string and adds a period in case it's longer than length to create a snippet.
*
* @param string $string Input text to cut down
* @param int $length Length of the snippet to create
*
* @return string
*/
function createTextSnippet($string, $length) {
if (strlen($string) > $length) {
$string = substr($string, 0, $length);
$string = substr($string, 0, strrpos($string, ' '));
$string .= ' ...';
}
return $string;
}
?>

View File

@ -11,13 +11,18 @@
* Function for checking a text for the existence of pseudocode
* for embedding from museum-digital.
*
* @param string $text Input string.
* @param string $text Input string.
* @param array $settings General settings.
*
* @return string
*/
function checkForEmbeds(string $text):string {
function checkForEmbeds(string $text, array $settings):string {
$embedOptions = [
"singleObjectTile",
"singleObjectDetails",
"singleCollectionTile",
"exhibitionCalendar",
"eventCalendar"
];
@ -26,13 +31,32 @@ function checkForEmbeds(string $text):string {
if (strpos($text, $option) === false) continue;
$position = strpos($text, $option) - 1;
$nextTag = strpos($text, "<", $position);
$nextWhitespace = strpos($text, " ", $position);
$nextTag = $nextWhitespace = strlen($text);
if (strpos($text, "<", $position) !== false) $nextTag = strpos($text, "<", $position);
if (strpos($text, " ", $position) !== false) $nextWhitespace = strpos($text, " ", $position);
$end = min($nextTag, $nextWhitespace, strlen($text));
$end = min($nextTag, $nextWhitespace);
$pseudocode = substr($text, $position, $position + $end);
echo $pseudocode;
$pseudocode = substr($text, $position, $end - $position);
$command = substr($pseudocode, 1, strpos($pseudocode, "]") - 1);
$arguments = [];
if (strpos($pseudocode, "{") !== false) $arguments = explode("&", substr($pseudocode, strpos($pseudocode, "{") + 1, -1));
switch ($command) {
case "singleObjectTile":
$text = str_replace($pseudocode, embedObject($arguments, $settings), $text);
break;
case "singleObjectDetails":
$text = str_replace($pseudocode, embedObject($arguments, $settings, true), $text);
break;
case "exhibitionCalendar":
$text = str_replace($pseudocode, embedExhibitionCalendar($arguments), $text);
break;
case "eventCalendar":
$text = str_replace($pseudocode, embedEventCalendar($arguments), $text);
break;
}
}
@ -40,4 +64,302 @@ function checkForEmbeds(string $text):string {
}
/**
* Function drawObjectTile creates a tile with just the most basic information on an object.
*
* @param string[] $contents Input data fetched from the object API at museum-digital.
* @param array $settings Settings variable.
*
* @return string
*/
function drawObjectTile(array $contents, array $settings):string {
$output = '
<div class="objTile">
';
if (count($contents['object_images']) > 0) {
foreach ($contents['object_images'] as $image) {
if ($image['is_main'] != "j") continue;
$output .= '
<img src="' . $settings['mdImgFolder'] . $image['folder'] . '/' . $image['preview'] . '" />';
}
}
$output .= '
<div>
<h4>' . $contents['object_name'] . '</h4>
<div>
<a href="./object.php?id=' . $contents['object_id'] . '" class="toTranslate" data-content="More"></a>
<a href="' . $settings['mdVersion'] . '?t=objekt&oges=' . $contents['object_id'] . '" class="toTranslate" data-content="MoreAtMuseumDigital">' . $contents['object_name'] . '</a>
</div>
</div>
</div>
';
return $output;
}
/**
* Function drawObjectDetails creates a tile with just the most basic information on an object.
*
* @param string[] $contents Input data fetched from the object API at museum-digital.
* @param array $settings Settings variable.
*
* @return string
*/
function drawObjectDetails(array $contents, array $settings):string {
$output = '
<div class="objDetails">
<h2>' . $contents['object_name'] . '</h2>
';
if (count($contents['object_images']) > 0) {
foreach ($contents['object_images'] as $image) {
if ($image['is_main'] != "j") continue;
$output .= '
<img class="objMainImg" src="' . $settings['mdImgFolder'] . $image['folder'] . '/' . $image['filename_loc'] . '" />';
}
}
$output .= '
<div>
';
$output .= '
<p>
' . $contents['object_description'] . '
</p>';
$simpleDefinedConts = [
"object_material_technique",
"object_dimensions",
];
$output .= "
<dl>";
if (count($contents['object_collection']) > 0) {
$output .= '
<dt class="toTranslate" data-content="Collection"></dt>';
foreach ($contents['object_collection'] as $collection) {
$output .= '
<dd>' . $collection['collection_name'] . '</dd>
';
}
}
foreach ($simpleDefinedConts as $value) {
$output .= "
<dt class='toTranslate' data-content='" . $value . "'></dt>
<dd>" . $contents[$value] . "</dd>
";
}
if (count($contents['object_tags']) > 0) {
$output .= '
<dt class="toTranslate" data-content="Tags"></dt>';
foreach ($contents['object_tags'] as $tag) {
$output .= '
<dd>' . $tag['tag_name'] . '</dd>
';
}
}
if (count($contents['object_relation_places']) > 0) {
$output .= '
<dt class="toTranslate" data-content="Places"></dt>';
foreach ($contents['object_relation_places'] as $place) {
$output .= '
<dd>' . $place['place']['place_name'] . '</dd>
';
}
}
if (count($contents['object_relation_people']) > 0) {
$output .= '
<dt class="toTranslate" data-content="People"></dt>';
foreach ($contents['object_relation_people'] as $people) {
$output .= '
<dd>' . $people['people']['displayname'] . '</dd>
';
}
}
$output .= "
</dl>";
if (count($contents['object_events']) > 0) {
$output .= "
<div class='events'>";
foreach ($contents['object_events'] as $event) {
$output .= '
<div class="objevent">
<h5 class="toTranslate" data-content="eventType' . $event['event_type'] . '"></h5>
<table>
';
if (isset($event['people'])) $output .= '
<tr>
<th class="toTranslate" data-content="... who"></th>
<td class="vcard h-card">
<a rel="tag" class="u-url fn p-name url">' . $event['people']['displayname'] . '</a>
</td>
</tr>';
if (isset($event['time'])) $output .= '
<tr>
<th class="toTranslate" data-content="... when"></th>
<td>
<a>' . $event['time']['time_name'] . '</a>
</td>
</tr>';
if (isset($event['place'])) $output .= '
<tr>
<th class="toTranslate" data-content="... where"></th>
<td class="h-geo geo">
<a rel="tag">' . $event['place']['place_name'] . '</a>
</td>
</tr>';
$output .= '
</table>
</div>
';
}
$output .= '
</div>
';
}
$output .= '
<p class="metadataLine">
<span><span class="toTranslate" data-content="Metadata"></span></span>
<span><span class="toTranslate" data-content="LastUpdated"></span>: ' . $contents['object_last_updated'] . '</span>
<span><span class="toTranslate" data-content="Licence"></span>: ' . $contents['licence']['metadata_rights_status'] . '</span>
<span><a class="toTranslate" data-content="ObjectAtMuseumDigital" href="' . $settings['mdVersion'] . '?t=objekt&oges=' . $contents['object_id'] . '"></a></span>
</p>
</div>
';
$output .= '
</div>
';
return $output;
}
/**
* Function for displaying objects.
*
* @param array $arguments Arguments / GET parameters for urls to query.
* @param array $settings Settings variable.
* @param boolean $showDetails Optional. By default, only a tile with most basic information is displayed.
*
* @return string
*/
function embedObject(array $arguments, array $settings, bool $showDetails = false):string {
$toIgnore = ["t=", "output="];
$srcArgs = "t=objekt";
foreach ($arguments as $arg) {
if (startsWithAny($arg, $toIgnore)) continue;
$srcArgs .= "&" . $arg;
}
$srcArgs .= "&output=json";
$contents = json_decode(queryCachePage($settings['mdVersion'] . "?$srcArgs", "object", $settings), true);
if (!$showDetails) return drawObjectTile($contents, $settings);
else return drawObjectDetails($contents, $settings);
}
/**
* Function for embedding event calendar.
*
* @param array $arguments Arguments / GET parameters for urls to query.
*
* @return string
*/
function embedExhibitionCalendar(array $arguments):string {
$toIgnore = ["t=", "calendar=", "output="];
$srcArgs = "t=exhibitions_overview&calendar=1";
foreach ($arguments as $arg) {
if (startsWithAny($arg, $toIgnore)) continue;
$srcArgs .= "&" . $arg;
}
$srcArgs .= "&output=json";
$srcURL = "apiMirror.php?area=exhibitions&args=" . urlencode($srcArgs);
if (isset($_GET['y'])) $y = $_GET['y'];
else $y = date("Y");
if (isset($_GET['m'])) $m = $_GET['m'];
else $m = date("m");
$prevMonth = strtotime('-1 month', strtotime("$y-$m-01"));
$nextMonth = strtotime('+1 month', strtotime("$y-$m-01"));
$output = '
<section class="mdCalendar"
data-year="'.$y.'" data-month="'.$m.'" data-colored="1"
data-today="./index.php?id=' . $GLOBALS['id'] . '&' . date("Y").'&m='.date("m") . '"
data-prev="./index.php?id=' . $GLOBALS['id'] . '&y=' . date("Y", $prevMonth).'&m='.date("m", $prevMonth) . '"
data-next="./index.php?id=' . $GLOBALS['id'] . '&y=' . date("Y", $nextMonth).'&m='.date("m", $nextMonth) . '"
data-src="' . $srcURL . '&y='.date("Y", $nextMonth).'&m='.date("m", $nextMonth) . '">
</section>
';
return $output;
}
/**
* Function for embedding event calendar.
*
* @param array $arguments Arguments / GET parameters for urls to query.
*
* @return string
*/
function embedEventCalendar(array $arguments):string {
$toIgnore = ["t=", "calendar=", "output="];
$srcArgs = "t=events&calendar=1";
foreach ($arguments as $arg) {
if (startsWithAny($arg, $toIgnore)) continue;
$srcArgs .= "&" . $arg;
}
$srcArgs .= "&output=json";
$srcURL = "apiMirror.php?area=events&args=" . urlencode($srcArgs);
if (isset($_GET['y'])) $y = $_GET['y'];
else $y = date("Y");
if (isset($_GET['m'])) $m = $_GET['m'];
else $m = date("m");
$prevMonth = strtotime('-1 month', strtotime("$y-$m-01"));
$nextMonth = strtotime('+1 month', strtotime("$y-$m-01"));
$output = '
<section class="mdCalendar"
data-year="'.$y.'" data-month="'.$m.'" data-colored="1"
data-today="./index.php?id=' . $GLOBALS['id'] . '&' . date("Y").'&m='.date("m") . '"
data-prev="./index.php?id=' . $GLOBALS['id'] . '&y=' . date("Y", $prevMonth).'&m='.date("m", $prevMonth) . '"
data-next="./index.php?id=' . $GLOBALS['id'] . '&y=' . date("Y", $nextMonth).'&m='.date("m", $nextMonth) . '"
data-src="' . $srcURL . '&y='.date("Y", $nextMonth).'&m='.date("m", $nextMonth) . '">
</section>
';
return $output;
}
?>

52
inc/search.php Normal file
View File

@ -0,0 +1,52 @@
<?PHP
/**
* This file provides search functions.
*
* @file
*
* @author Joshua Ramon Enslin <joshua@jrenslin.de>
*/
/**
* Function for searching in all static pages.
*
* @param string $searchTerm Search term.
*
* @return array
*/
function searchInPages(string $searchTerm) {
$files = scanDirConts(__DIR__ . "/../data/static");
$results = [];
foreach ($files as $file) {
$curResults = [];
$contents = json_decode(file_get_contents(__DIR__ . "/../data/static/$file"), true);
if (!$contents['public']) continue; // Don't display non-public files.
$curResults['inTitle'] = substr_count($contents['title'], $searchTerm);
$curResults['inDescription'] = substr_count($contents['content'], $searchTerm);
$curResults['priority'] = $curResults['inTitle'] * 3 + $curResults['inDescription'];
if ($curResults['priority'] == 0) continue;
$curResults['title'] = $contents['title'];
// Sanitize content for snippets.
$snippet = preg_replace('/[\[{\(].*[\]}\)]/U' , '', strip_tags($contents['content']));
$curResults['snippet'] = createTextSnippet($snippet, 180);
$results[$file] = $curResults;
}
usort($results, function($a, $b) {
if ($a == $b) return 0;
return ($a > $b) ? -1 : 1;
});
return $results;
}
?>

View File

@ -10,25 +10,30 @@
/**
* Prints the head element of an HTML page in the public frontend.
*
* @param string $page ID of the current page.
* @param string $title Title of the page.
* @param string $icon The icon of the website.
* @param array $settings Settings variable.
* @param string $page ID of the current page.
* @param string $title Title of the page.
* @param string $icon The icon of the website.
* @param string $additional Additional HTML to inject.
*
* @return string
*/
function printPublicHead(string $page = "home", string $title = "Home", string $icon = ""):string {
function printPublicHead(array $settings, string $page = "home", string $title = "Home", string $icon = "", $additional = ""):string {
$output = '<!DOCTYPE html>
<html lang="en" id="' . $page . '">
<head>
<!-- Content Security policies -->
<meta http-equiv="Content-Security-Policy" content="default-src \'none\'; script-src \'self\'; connect-src \'self\'; img-src \'self\'; style-src \'self\' \'unsafe-inline\'; font-src \'self\';" />
<meta http-equiv="Content-Security-Policy" content="default-src \'none\'; script-src \'self\'; connect-src \'self\' ' . $settings['mdVersion'] . '; img-src \'self\' ' . $settings['mdVersion'] . '; style-src \'self\' \'unsafe-inline\'; font-src \'self\';" />
<title>' . $title . '</title>
<link rel="stylesheet" type="text/css" href="themes/default/default.css">
<link rel="stylesheet" type="text/css" href="themes/imports.css">
<meta http-equiv="content-type" content="text/html;charset=utf-8">';
$output .= $additional;
if ($icon) {
$output .= '
<link rel="shortcut icon" sizes="16x16 32x32" href="' . $icon . '" />
@ -39,6 +44,7 @@ function printPublicHead(string $page = "home", string $title = "Home", string $
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script type="text/javascript" src="./js/main.js"></script>
<script type="text/javascript" src="./js/mdCalendar.js" defer></script>
</head>
<body>
@ -151,4 +157,37 @@ function printErrorPage(string $content):string {
}
/**
* Function for generating the standard navigation of the public parts of the page.
*
* @param array $pages List of all pages.
*
* @return string
*/
function generatePublicNav($pages):string {
$output = '<nav>';
$output .= buildPageOrder(
$pages,
function() {
return "<ul>";
},
function() {
return "</ul>";
},
function($inputs, string $toAdd) {
$output = "
<li";
if (!$inputs['public']) $output .= " class='notPublic'";
$output .= ">
<a href='./?id=" . $inputs['id'] . "'>" . $inputs['title'] . "</a>
$toAdd
</li>
";
return $output;
}
);
$output .= '</nav>';
return $output;
}
?>