diff --git a/public_html/index.php b/public_html/index.php index 519f63d..e4cede2 100644 --- a/public_html/index.php +++ b/public_html/index.php @@ -1,353 +1,360 @@ offer search * 2) show result list and offer changing search terms * * TODO not yet implemented: * - search for countries * - search for subjects * - functional restrictions (e.g., 'wa' cannot be combined with other collections) */ require __DIR__ . '/vendor/autoload.php'; $debug = 0; $GLOBALS['collections'] = array( 'pe', 'co', 'sh', 'wa' ); $GLOBALS['collection'] = array( 'pe' => array( 'de' => 'Personen', 'en' => 'Persons', ), 'co' => array( 'de' => 'Firmen/Organisationen', 'en' => 'Companies/Organizations', ), 'sh' => array( 'de' => 'Länder-Sach', 'en' => 'Countries-Subjects', ), 'wa' => array( 'de' => 'Waren', 'en' => 'Commodities/Wares', ) ); # allow switch to German by param if (isset($_REQUEST['lang']) and $_REQUEST['lang'] == "de") { $lang = "de"; } else { $lang = 'en'; } # ONE collection may be preselected by GET param # multiple in POST, otherwise use all collections as default if (isset($_GET['coll']) and $_GET['coll'] == 'sh_wa') { $selected_collections = array('sh', 'wa'); } elseif (isset($_GET['coll']) and in_array($_GET['coll'], $GLOBALS['collections'])) { $selected_collections[] = $_GET['coll']; } elseif (isset($_POST['coll'])) { $selected_collections = $_POST['coll']; } else { $selected_collections = $GLOBALS['collections']; } $bl_title = ($lang == 'en') ? 'PM20 fulltext search' : 'PM20 Volltext-Suche'; output_header($lang, $bl_title, $selected_collections); // global logic // for GET requests, a page with only header and footer is // created, no debug info if ($_SERVER["REQUEST_METHOD"] == "POST") { do_search($lang, $selected_collections); } output_footer($lang, $debug); ////////////////// function do_search($lang, $selected_collections) { $collections = '"' . join('", "', $selected_collections) . '"'; // Retrieve form data $searchfor = htmlspecialchars($_POST["q"]); # require at least 3 characters if (mb_strlen($searchfor) < 3) { return; } elseif (mb_strlen($searchfor) == 3 and mb_substr($searchfor, 2, 1) == '*') { return; } $filter_online = (array_key_exists('exclude_meta', $_POST)) ? 'filter(bound(?online) && ?online > 0)' : ''; $query_folder = <<', $query, ''; $api = new RestClient([ "headers" => [ "Content-type" => "application/sparql-query; charset=UTF-8", "Accept" => "application/sparql-results+json", ], ]); $service_url = 'https://query.wikidata.org/sparql'; $result = $api->post($service_url, $query); $matches = json_decode($result->response)->{'results'}->{'bindings'}; //print "
";
   //print_r($matches);
 
   output_resultlist($lang, $matches);
 }
 
 function output_header($lang, $bl_title, $selected_collections) {
 
   // Retrieve form data
   $searchfor = htmlspecialchars($_POST["q"]);
   $exclude_metadata_only = (array_key_exists('exclude_meta', $_POST)
       or ($_SERVER['REQUEST_METHOD'] == 'GET'))
     ? 'checked' : '';
 
   # override $selected_collections for POST requests
   if (isset($_POST['coll'])) {
     $selected_collections = $_POST['coll'];
   }
 
   $bl_description = ($lang == 'en')
     ? 'Search folders via linked Wikidata items.'
     : 'Suche Mappen über die verknüpften Wikidata-Items';
   $bl_hint = ($lang == 'en')
     ? '(At least 3 characters; use * for right-truncation)'
     : '(Mindestens drei Zeichen; benutze * für Rechtstrunkierung)';
 
 echo <<
 
 
   
   $bl_title
   
   
   
   
 
 
 
 

$bl_title

$bl_description

$bl_hint

EOF; foreach ($GLOBALS['collections'] as $coll) { echo "   '; } $bl_action = ($lang == 'en') ? 'Search' : 'Suche'; $bl_exclude_meta = ($lang == 'en') ? 'Exclude metadata-only records' : 'Reine Metadaten-Sätze ausschließen'; echo <<

EOF; } function output_resultlist($lang, $matches) { $bl_results = ($lang == 'en') ? 'Resultlist:' : 'Ergebnisse:'; echo "

$bl_results

"; echo '
    '; $cnt = count($matches); foreach ($matches as $entry) { $url = $entry->pm20->value; $wd = $entry->item->value; $item_label = $entry->itemLabel->value; $collection = $entry->collection->value; $label = $item_label; if (isset($entry->pmLabel) and ($collection == 'pe' or $collection == 'co')) { $label = $entry->pmLabel->value; } $label = htmlentities($label, ENT_QUOTES, 'UTF-8', false); ## online document counts in WD are outdated! ##$online_count = (isset($entry->online)) ## ? '(' . $entry->online->value . ') ' ## : ''; echo "
  • $label" , "  Wikidata Icon
  • " ; } echo '
'; echo "$cnt ", ($lang == 'en') ? 'matches' : 'Treffer'; } function output_footer($lang, $debug) { $bl_footer = ($lang == 'en') ? 'All folders of the 20th Century Press Archives of ZBW (PM20) are linked to Wikidata items. The items include synonyms and other text in multiple languages, which is used for searching. More on PM20 in Wikidata here.' : 'Alle Mappen des Pressearchiv 20. Jahrhundert der ZBW (PM20) sind in Wikidata-Datenobjekten verlinkt. Diese Datenobjekte enthalten Synonyme und sonstigen Text in unterschiedlichen Sprachen, der zum Suchen benutzt wird. Mehr über Pressemappen in Wikidata hier.'; echo "
 
"; echo "
$bl_footer
"; if ($debug) { echo <<Debug
     EOF;
     print_r( $_POST);
     echo '
'; } echo << EOF; }