Location: PHPKode > scripts > Lightning Notes > ln_distro/ln_distro/admin/rdf/classes/note_rdf_generator.php
<?php

# RDF Generator
# Class to generate RDF Channels (RDF Version 1.0)

class RDFGenerator
{
# XML encoding
# @var string
var $encoding = 'ISO-8859-1';

# XML version
# @var string
var $version = '1.0';

# XML namespaces
# @var array
var $namespaces = array('RDF'    => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
                        'XML'    => 'http://purl.org/rss/1.0/',
                        'DC'    => 'http://purl.org/dc/elements/1.1/'
                        );

# RDF channel
# @var array
var $channel = array();

# RDF items
# @var array
var $items = array();

# RDF image
# @var array
var $image = array();

# RDF textinput
# @var array
var $input = array();

# check if DC Dublin Core metadata are used
# @var boolean
var $checkdc = false;

# prints out generated channel in RDF-Format
# @param  void

function printRDF() {
    #header('Content-Type: text/xml');
    print htmlentities($this->_buildRDF());
}

# writes channel to file
# @param  string  $file   path to file

function writeRDF($file) {
    $data = $this->_buildRDF();

    if (!$fh = fopen($file, 'w')) {
        $this->error('Could not open file <b>'.$file.'</b>');
    } else {
        flock($fh,2);
        fwrite($fh, $data);
        flock($fh,3);
        fclose($fh);
    }
}


# creates new channel and adds basic channel data
# @param  array   $ch  channel data

function createChannel($ch) {
    $must = array('title', 'link');
    $temp = $this->checkData($ch, $must, 'new Channel');
    $this->channel = $temp;
}


# adds image to channel
# @param  string   $img  image data

function addImage($img) {
    $must = array('title', 'link', 'url');
    $temp = $this->checkData($img, $must, 'Image');
    $this->image = $temp;
}


# adds textinput form to channel
# @param  string   $in  form data

function addTextinput($in) {
    $must = array('title', 'link', 'name');
    $temp = $this->checkData($in, $must, 'Textinput form');
    $this->input = $temp;
}


# adds item to channel
# @param  string   $item  item data

function addItem($item) {
    $nr = count($this->items)+1;
    $must = array('title', 'url');
    $temp = $this->checkData($item, $must, 'Item '.$nr.'');

    if ( isset($temp['dc']) && !empty($temp['dc'])) {
        $this->checkdc = true;
    }
    array_push($this->items, $temp);
}


# sets XML version
# @param  string   $str  XML version

function setVersion($str) {
    $this->version = $str;
}


# sets XML encoding
# @param  string   $str  XML encoding

function setEncoding($str) {
    $this->encoding = $str;
}


# set namespaces
# @param   array   $ns  namespaces

function setNamespaces($ns) {
    for (reset($ns); list($k, $v) = each($ns);) {
        if (isset($this->namespaces[$k])) {
            $this->namespaces[$k] = $v;
        }
        else {
            $this->error('Unknown Namespace <b>'.$k.'</b>');
        }
    }
}


# generates channel
# @param  void

function _buildRDF() {

    $rdf  = '<?xml version="'.$this->version.'" encoding="'.$this->encoding.'" ?>';
    $rdf .= '<?xml-stylesheet href="c/style.css" type="text/css"?>';
    $rdf .= "\n";
    $rdf .= '<rdf:RDF ';
    $rdf .= 'xmlns:rdf="'.$this->namespaces['RDF'].'" ';
    $rdf .= 'xmlns="'.$this->namespaces['XML'].'" ';
    $rdf .= 'xmlns:dc="'.$this->namespaces['DC'].'"';
    $rdf .= '>';
    $rdf .= "\n";

    $rdf .= $this->_buildChannel();
    $rdf .= $this->_buildImage();
    $rdf .= $this->_buildItems();
    $rdf .= $this->_buildInput();

    $rdf .= '</rdf:RDF>';

    return $rdf;
}

# generates basic channel data
# @param  void

function _buildChannel() {

$isodate = date("Y-m-d");

    $channel = '<channel rdf:about="'.$this->channel['link'].'">';
    $channel .= "\n";
    for (reset($this->channel); list($k, $v) = each($this->channel);) {
        if ( strtoupper($k) != 'DC' ) {
            $channel .= '<'.$k.'>'.htmlentities($v).'</'.$k.'>';
            $channel .= "\n";
        }
    }

    if (isset($this->image) && !empty($this->image)) {
        $channel .= '<image rdf:resource="'.$this->image['url'].'"/>';
        $channel .= "\n";
    }

    if (isset($this->input) && !empty($this->input)) {
        $channel .= '<textinput rdf:resource="'.$this->input['link'].'"/>';
        $channel .= "\n";
    }

    if (isset($this->items) && !empty($this->items)) {
        $channel .= '<items><rdf:Seq>';
        $channel .= "\n";
            for ($i = 0; $i < count($this->items); $i++) {
                $channel .= '<rdf:li rdf:resource="http://alexanderbecker.net'.$this->items[$i]['url'].'"/>';
                $channel .= "\n";
            }
        $channel .= '</rdf:Seq></items>';
        $channel .= "\n";
    }

    if (isset($this->channel['dc']) && !empty($this->channel['dc'])) {
        for (reset($this->channel['dc']); list($k, $v) = each($this->channel['dc']);) {
            $channel .= '<dc:'.$k.'>'.htmlentities($v).'</dc:'.$k.'>';
            $channel .= "\n";
        }
        $channel .= '<dc:date>'.$isodate.'</dc:date>';
        $channel .= "\n";
    }
    $channel .= '</channel>';
    $channel .= "\n";
    return $channel;
}


# generates image data
# @param  void

function _buildImage() {
    if (isset($this->image) && !empty($this->image)) {
        $image  = '<image rdf:about="'.$this->image['url'].'">';
        $image .= "\n";
        $image .= '<title>'.htmlentities($this->image['title']).'</title>';
        $image .= "\n";
        $image .= '<link>'.$this->image['link'].'</link>';
        $image .= "\n";
        $image .= '<url>'.$this->image['url'].'</url>';
        $image .= "\n";
        $image .= '</image>';
        $image .= "\n";

        return $image;
    }
    else {
        return '';
    }
}

# generates items data
# @param  void

function _buildItems() {
    if (isset($this->items) && !empty($this->items)) {
        $item = '';
        for ($i = 0; $i < count($this->items); $i++) {
            $item .= '<item rdf:about="http://alexanderbecker.net'.$this->items[$i]['url'].'">';
            $item .= "\n";
            $item .= '<title>'.htmlentities($this->items[$i]['title']).'</title>';
            $item .= "\n";
            $item .= '<link>http://alexanderbecker.net'.$this->items[$i]['url'].'</link>';
            $item .= "\n";
            $item .= '<description>'.htmlentities($this->items[$i]['excerpt']).'</description>';
            $item .= "\n";
            $item .= '<dc:creator>'.htmlentities($this->items[$i]['author']).'</dc:creator>';
            $item .= "\n";
            $item .= '<dc:date>'.date("Y-m-d",(strtotime($this->items[$i]['date']))).'</dc:date>';
            $item .= "\n";
            $item .= '</item>';
            $item .= "\n";
        }
        return $item;
    }
    else {
        return '';
    }
}


# generates textinput data
# @param  void

function _buildInput() {
    if (isset($this->input) && !empty($this->input)) {
        $input  = '<textinput rdf:about="'.$this->input['link'].'">';
        $input .= "\n";
        $input .= '<title>'.htmlentities($this->input['title']).'</title>';
        $input .= '<description>'.htmlentities($this->input['description']).'</description>';
        $input .= '<name>'.htmlentities($this->input['name']).'</name>';
        $input .= '<link>'.$this->input['link'].'</link>';
        $input .= '</textinput>';
        $input .= "\n";

        return $input;
    }
    else {
        return '';
    }
}


# checks if all needed data is added to channel
# @param  array    $data    data of channel section
# @param  array    $needed  needed data for this section
# @param  string   $submsg  Errormessage
# @return array    $temp    checked channel data

function checkData($data, $needed = array(), $submsg = '') {
    $temp = array();
    for (reset($data); list($k, $v) = each($data);) {
        $temp[strtolower($k)] = $v;
    }
    for ($i = 0; $i < count($needed); $i++) {
        if ( !isset($temp[strtolower($needed[$i])]) ) {
            $this->error('<b>'.strtoupper($needed[$i]).'</b> for '.$submsg.' is missing');
        }
    }
    return $temp;
}


# Errormessage
# @param  string   $msg  Errormessage

function error($msg) {
//    die(printf("ERROR: %s", $msg));
}

} // end of class

?>
Return current item: Lightning Notes