<?php
#
# abstracts_rdf.php - Helictite Abstracts RDF 1.1, presented in Turtle format
#
# Revision history
# Version  Date        Author          Changes
# 1.0      2023-05-29  Glenn Baddeley  Initial version, referred to by helictite_rdf.php
# 1.1      2023-06-17  GB              Update property names
# 1.2      2023-06-29  GB              Add :hasAbstractors, Add GenAbstractor, Add isInContent
# 1.3      2023-07-23  GB              Add generated date time, Update Abstract property order
# 1.4      2026-03-04  GB              Get data from PEDATA files rather than include files 'contentsN_vars.php',
#
# Update sVersionDate below

# Change the default encoding of data sent from web server to browser from gzip to no encoding
header_remove( "Content-Encoding" );
header( "Content-Encoding: none");

# Change the default content type from text/html to text/turtle UTF-8
header_remove( "Content-Type" );
header( "Content-Type: text/turtle;charset=UTF-8");

# Include configuration constants and PEDATA functions
require 'helweb_config.php';
if( $cbDebugErrs ) { ini_set('display_errors', 1); error_reporting(E_ALL); }

# Current version of this PHP file
$sVersionDate = "1.4 of 2026-03-04";

# Generate the namespace triples 
echo "# Abstracts RDF data for Helictite, Journal of Australasian Spelelogical Research
# Version $sVersionDate
# Generated " . date('Y-m-d H:i') . "
# RDF 1.1 Turtle format - https://www.w3.org/TR/turtle
# Helictite namespaces
@prefix : <https://helictite.caves.org.au/abstracts_rdf.php#> .
@prefix hs: <https://helictite.caves.org.au/schema_rdf.php#> .
@prefix h: <https://helictite.caves.org.au/helictite_rdf.php#> .
# Dublin Core DCMI Metadata properties, defined at
# https://www.dublincore.org/specifications/dublin-core/dcmi-terms/
@prefix dc: <http://purl.org/dc/terms/> .   # Superset of http://purl.org/dc/elements/1.1/
# schema.org properties
@prefix so: <http://www.schema.org/> .

";

# Generate all other triples
GenAbstractors();
GenAbstracts();

# End of generated data

######## Functions ########

function GenAbstractors() {
  global $sPedataErrMsg;

  echo "# Abstractor objects

";

  # Get data for abstractors
  $aAbstractors = []; $aNoCache = $aNoMetaData = $aNoSectData = null;
  if( !GetPedata( 'abstractor', $aNoCache, $aAbstractors, $aNoMetaData, $aNoSectData ) ) {
    echo GetPedataError() . "\n";
  }

  # Generate triples for all abstractors
  foreach($aAbstractors as $sInitials=>$r) {
    # Generate triple for an abstractor
    echo ":ar_$sInitials a hs:Abstractor;
 hs:abstractorKey \"$sInitials\";
 hs:abstractorName \"" . ($r['fn'] ?? '?') . "\" .

";
  }
}

###########################

function GenAbstracts() {
  global $sPedataErrMsg;

  echo "# Abstract objects

";

  # Get volume groups data
  $aVolGroup = []; $aNoCache = $aNoMetaData = $aNoSectData = null;
  if( !GetPedata( 'volgroup', $aNoCache, $aVolGroup, $aNoMetaData, $aNoSectData ) ) {
    echo GetPedataError() . "\n";
    return;
  }
  $iVolGroupLast = count($aVolGroup) - 1;
  if( $iVolGroupLast < 0 ) {
    echo "Error: Invalid VolGroupLast=$iVolGroupLast\n";
    return;
  }

  # Process all volume groups
  for( $iVolGrp = $iVolGroupLast ; $iVolGrp >= 0 ; $iVolGrp-- ) {

    # Get data for contents of the volume group
    $aContentsData = [];
    if( !GetPedata( 'contents*', $aNoCache, $aContentsData, $aNoMetaData, $aNoSectData, $iVolGrp ) ) {
      echo GetPedataError() . "\n";
      return;
    }

    # Generate triples for all contents that have abstracts
    foreach($aContentsData as $sContentId=>$r) {
      if( isset($r['abstract']) ) {

        # List of Abstractors
        $sAbstRep = '';
        if( isset($r['abstractors']) ) {
          $aAbstors = str_getcsv( $r['abstractors'] );
          foreach( $aAbstors as $v)
            $sAbstRep .= " :ar_" . trim($v);
        }

        # Generate triple for an abstract
        echo ":a_$sContentId a hs:Abstract;
 dc:title \"" . ($r['title'] ?? '?') . "\";
 so:abstract \"\"\"" . $r['abstract'] . "\"\"\";
 hs:isInContent :c_$sContentId;
 hs:hasAbstractors ($sAbstRep ) .

";
      }
    }
  }

}