* @author Uldis Bojars (adaptation to PHP4) * @author Thomas Schandl (addition of SIOCThread) */ define('FORUM_NODE', 'forum'); // TODO: Not used at the moment, remove it? define('AUTHORS_NODE', 'authors'); define('EXPORTER_URL', 'http://wiki.sioc-project.org/index.php/PHPExportAPI'); define('EXPORTER_VERSION', '1.01'); /** * Main exporter class * * Generates RDF/XML content of SIOC export. * - Sets up parameters for generating RDF/XML * - Sets up parameters for SIOC URL generation */ class SIOCExporter { var $_title; var $_blog_url; var $_sioc_url; var $_encoding; var $_generator; var $_urlseparator; var $_urlequal; var $_url4type; // e.g. type or sioc_type var $_url4id; // TS e. g. id or sioc_id var $_url4page; var $_url_usetype; // TS: if true: appends the "type" of a class to the _url4id in order to compose the string for the "id part" of the siocURL. e. g. for a forum that could produce "forum_id=" or "forum_sioc_id=" var $_url_suffix; // TS: custom parameter to be appended at the end of a siocURL var $_type_table; var $_ignore_suffix; // TS: for types in this table the _url_suffix won't be appended to their siocURL var $_export_email; var $object; function SIOCExporter() { $this->_urlseparator = '&'; $this->_urlequal = '='; $this->_url4type = 'type'; $this->_url4id = 'id'; $this->_url4page = 'page'; $this->_url_usetype = true; $this->_url_suffix = ''; $this->_type_table = array(); $this->_ignore_suffix = array(); $this->_export_email = false; $this->_encoding = 'UTF-8'; } function setURLParameters($type='type', $id='id', $page='page', $url_usetype = true, $urlseparator='&', $urlequal='=', $suffix='') { $this->_urlseparator = $urlseparator; $this->_urlequal = $urlequal; $this->_url4type = $type; $this->_url4id = $id; $this->_url4page = $page; $this->_url_usetype = $url_usetype; $this->_url_suffix = $suffix; } function setParameters($title, $url, $sioc_url, $encoding, $generator, $export_email=false) { $this->_title = $title; $this->_blog_url = $url; $this->_sioc_url = $sioc_url; $this->_encoding = $encoding; $this->_generator = $generator; $this->_export_email = $export_email; } // Assigns only a single object for now function addObject( &$obj ) { $this->object = &$obj; } // TS: Used to replace _url4id in the siocURL for a given type (site, forum, etc.) with a parameter ($name) of your choice // E.g. b2evo exporter uses "blog=" instead of "sioc_id=" in the siocURL of a forum function setURLTypeParm($type, $name) { $this->_type_table[$type] = $name; } function setSuffixIgnore($type) { $this->_ignore_suffix[$type] = 1; } function siocURL($type, $id, $page=""){ $type_part = $this->_url4type .$this->_urlequal . $type ; if ($id) { if ( isset($this->_type_table[$type]) ) $myID = $this->_type_table[$type] ; else $myID = (($this->_url_usetype) ? $type . '_' : '') . $this->_url4id ; $id_part = $this->_urlseparator . $myID . $this->_urlequal . $id ; } else { $id_part = ''; } ($page) ? $page_part = $this->_urlseparator . $this->_url4page . $this->_urlequal . $page : $page_part='' ; ($this->_url_suffix && !isset($this->_ignore_suffix[$type])) ? $suffix = $this->_urlseparator . $this->_url_suffix : $suffix = ''; $siocURL = $this->_sioc_url . $type_part . $id_part . $page_part . $suffix ; return clean($siocURL); } function export( $rdf_content='' ) { header('Content-Type: application/rdf+xml; charset='.$this->_encoding); echo $this->makeRDF($rdf_content); } function makeRDF( $rdf_content='' ) { $rdf = '_encoding.'" ?>'."\n"; $rdf .= ' SIOC profile for "'.clean($this->_title).'" A SIOC profile describes the structure and contents of a community site (e.g., weblog) in a machine processable form. For more information refer to the '.clean('SIOC project page').' '."\n"; if ($rdf_content) $rdf .= $rdf_content; if ($this->object) $rdf .= $this->object->getContent( $this ); $rdf .= "\n"; return $rdf; } } /** * Generic SIOC Object * * All SIOC objects are derived from this. */ class SIOCObject { var $_note = ''; function addNote($note) { $this->_note = $note; } function getContent( &$exp ) { $rdf = "\n"; $rdf .= "\tGeneric SIOC Object\n"; $rdf .= "\n"; return $rdf; } } /** * SIOC::Site object * * Contains information about main SIOC page including: * - site description * - list of forums * - list of users */ class SIOCSite extends SIOCObject { var $type = 'site'; var $_url; var $_name; var $_description; var $_forums; var $_users; var $_page; var $_next_users; var $_next_forums; var $_usergroup_uri; function SIOCSite($url, $name, $description, $page='', $usergroup_uri='') { $this->_url = $url; $this->_name = $name; $this->_description = $description; $this->_forums = array(); $this->_users = array(); $this->_page = $page; $this->_next_users = false; $this->_next_forums = false; $this->_usergroup_uri = $usergroup_uri; } function addForum($id, $url) { $this->_forums[$id] = $url; } function addUser($id, $url) { $this->_users[$id] = $url; } function setNextPageUsers($next) { $this->_next_users = $next; } function setNextPageForums($next) { $this->_next_forums = $next; } function getContent( &$exp ) { $rdf = "_url) ."\">\n"; $rdf .="\t" . clean($this->_name) . "\n"; $rdf .= "\t" . clean($this->_description) . "\n"; $rdf .= "\t_url) ."\"/>\n"; if($this->_forums) { foreach ($this->_forums as $id => $url) { $rdf .= "\t\n"; } } if($this->_next_forums) { $rdf .= "\tsiocURL('site', "", $this->_page+1) ."\"/>\n"; } if($this->_usergroup_uri) { $rdf .= "\t_usergroup_uri . "\"/>\n"; } else { $rdf .= "\t\n"; } $rdf .= "\n"; // Forums if($this->_forums) { $rdf .= "\n"; foreach ($this->_forums as $id => $url) { $rdf .= '\n"; $rdf .= "\t\n"; $rdf .= "\tsiocURL('forum', $id) ."\"/>\n"; $rdf .= "\n"; } } // Usergroup if($this->_users) { $rdf .= "\n"; if($this->_usergroup_uri) { $rdf .= '\n"; } else { $rdf .= '\n"; } $rdf .= "\tAuthors for \"" . clean($this->_name) . "\"\n"; foreach ($this->_users as $id => $url) { $rdf .= "\t\n"; $rdf .= "\t\t\n"; $rdf .= "\t\t\tsiocURL('user', $id). "\"/>\n"; $rdf .= "\t\t\n"; $rdf .= "\t\n"; } if($this->_next_users) { $rdf .= "\tsiocURL('site', "", $this->_page+1) ."\"/>\n"; } $rdf .= "\n"; } return $rdf; } } // Export detaille d'un utilisateur /** * SIOC::User object * * Contains user profile information */ class SIOCUser extends SIOCObject { var $type = 'user'; var $_id; var $_nick; var $_uri; var $_name; var $_email; var $_sha1; var $_homepage; var $_foaf_uri; var $_role; var $_sioc_url; var $_foaf_url; function SIOCUser($id, $uri, $name, $email, $homepage='', $foaf_uri='', $role=false, $nick='', $sioc_url='', $foaf_url='') { $this->_id = $id; $this->_uri = $uri; $this->_name = $name; if (preg_match_all('/^.+@.+\..+$/Ui', $email, $check, PREG_SET_ORDER)) { if (preg_match_all('/^mailto:(.+@.+\..+$)/Ui', $email, $matches, PREG_SET_ORDER)) { $this->_email = $email; $this->_sha1 = sha1($email); } else { $this->_email = "mailto:".$email; $this->_sha1 = sha1("mailto:".$email); } } $this->_homepage = $homepage; $this->_foaf_uri = $foaf_uri; $this->_url = $foaf_uri; $this->_role = $role; $this->_nick = $nick; $this->_foaf_url = $foaf_url; $this->_sioc_url = $sioc_url; } function getContent( &$exp ) { $rdf = "_foaf_uri) . "\">\n"; if($this->_name) $rdf .= "\t". $this->_name . "\n"; if($this->_email) { $rdf .= "\t" . $this->_sha1 . "\n"; } if($this->_foaf_url) { $rdf .= "\t_foaf_url ."\"/>\n"; } $rdf .= "\t\n"; $rdf .= "\t\t_uri) ."\">\n"; if($this->_nick) $rdf .= "\t\t\t" . $this->_nick . "\n"; if($this->_email) { if ($exp->_export_email) { $rdf .= "\t\t\t_email ."\"/>\n"; } $rdf .= "\t\t\t" . $this->_sha1 . "\n"; } if($this->_role) { $rdf .= "\t\t\t\n"; $rdf .= "\t\t\t\t\n"; $rdf .= "\t\t\t\t\t" . $this->_role . "\n"; $rdf .= "\t\t\t\t\n"; $rdf .= "\t\t\t\n"; } if($this->_sioc_url) { $rdf .= "\t\t\t_sioc_url ."\"/>\n"; } $rdf .= "\t\t\n"; $rdf .= "\t\n"; $rdf .= "\n"; return $rdf; } } // Export detaille d'un utilisateur /** * SIOC::Thread object * * Contains information about a SIOC Thread in a SIOC Forum - list of posts in that thread */ class SIOCThread extends SIOCObject { var $type = 'thread'; var $_id; var $_url; var $_page; var $_posts; var $_next; var $_views; var $_topics; var $_related; function SIOCThread($id, $url, $page, $views='', $topics='') { $this->_id = $id; $this->_url = $url; $this->_page = $page; $this->_posts = array(); $this->_next = false; $this->_views = $views; $this->_topics = $topics; $this->_related = array(); } function addPost($id, $url, $prev='', $next='') { $this->_posts[$id] = array("url" => $url, "prev" => $prev, "next" => $next); } // add links to things that are similar to this via sioc:related_to function addRelated($id, $url) { $this->_related[$id] = $url; } function setNextPage($next) { $this->_next = $next; } function getContent( &$exp) { $rdf .= '\n"; $rdf .= "\t_url) . "\"/>\n"; if ($this->_views) $rdf .= "\t" . $this->_views . "\n"; if ($this->_note) $rdf .= "\t" . $this->_note . "\n"; if ($this->_topics) { foreach ($this->_topics as $id => $topic) { $rdf .= "\t" . $topic . "\n"; // todo - each topic needs to have a URI } } /*if($this->_topics) { foreach($this->_topics as $url=>$topic) { $rdf .= "\t\n"; } } */ if ($this->_related) { foreach ($this->_related as $id => $url) { $rdf .= "\t\n"; $rdf .= "\t\t\n"; $rdf .= "\t\n"; // todo - each topic needs to have a URI } } if ($this->_posts) { foreach($this->_posts as $id => $data) { $rdf .= "\t\n"; $rdf .= "\t\t\n"; $rdf .= "\t\t\tsiocURL('post', $id) . "\"/>\n"; if ($data[prev]) { $rdf .= "\t\t\t\n"; } if ($data[next]) { $rdf .= "\t\t\t\n"; } $rdf .= "\t\t\n"; $rdf .= "\t\n"; } } if($this->_next) { $rdf .= "\rsiocURL('thread', $this->_id, $this->_page+1) ."\"/>\n"; } $rdf .= "\n"; return $rdf; } } // Export d'un forum avec une liste de posts -variable (next with seeAlso) /** * SIOC::Forum object * * Contains information about SIOC Forum (blog, ...): * - description of a forum * - list of posts within a forum [partial, paged] */ class SIOCForum extends SIOCObject { var $type = 'forum'; var $_id; var $_url; var $_page; var $_posts; var $_next; var $_blog_title; var $_description; var $_threads; var $_parents; var $_type; var $_creator; var $_administrator; function SIOCForum($id, $url, $page, $title='', $descr='', $type='sioc:Forum', $creator='', $admin='') { $this->_id = $id; $this->_url = $url; $this->_page = $page; $this->_posts = array(); $this->_next = false; $this->_blog_title = $title; $this->_description = $descr; $this->_threads = array(); $this->_parents = array(); $this->_type = $type; $this->_creator = $creator; $this->_administrator = $admin; } function addPost($id, $url) { $this->_posts[$id] = $url; } function addThread($id, $url) { $this->_threads[$id] = $url; } function addParentForum($id, $url) { $this->_parents[$id] = $url; } function setNextPage($next) { $this->_next = $next; } function getContent( &$exp) { $rdf .= '<'.$this->_type. ' rdf:about="' . clean($this->_url) . "\">\n"; if ($this->_type != 'sioc:Forum') $rdf .= "\t\n"; $rdf .= "\t_url) . "\"/>\n"; if ($this->_blog_title) $rdf .= "\t" . $this->_blog_title . "\n"; if ($this->_description) $rdf .= "\t" . $this->_description . "\n"; if ($this->_note) $rdf .= "\t" . $this->_note . "\n"; if ($this->_parents) { foreach($this->_parents as $id => $uri) { $rdf .= "\t\n"; $rdf .= "\t\t\n"; $rdf .= "\t\t\tsiocURL('forum', $id) . "\"/>\n"; $rdf .= "\t\t\n"; $rdf .= "\t\n"; } } if ($this->_threads) { foreach($this->_threads as $id => $uri) { $rdf .= "\t\n"; $rdf .= "\t\t\n"; $rdf .= "\t\t\tsiocURL('thread', $id) . "\"/>\n"; $rdf .= "\t\t\n"; $rdf .= "\t\n"; } } if($this->_posts) { foreach($this->_posts as $id => $url) { $rdf .= "\t\n"; $rdf .= "\t\t\n"; $rdf .= "\t\t\tsiocURL('post', $id) . "\"/>\n"; $rdf .= "\t\t\n"; $rdf .= "\t\n"; } } if ($this->_creator) { if ($this->_creator->_id) { $rdf .= "\t\n"; $rdf .= "\t\t_creator->_uri) ."\">\n"; if($this->_creator->_sioc_url) { $rdf .= "\t\t\t_creator->_sioc_url ."\"/>\n"; } else $rdf .= "\t\t\tsiocURL('user', $this->_creator->_id). "\"/>\n"; $rdf .= "\t\t\n"; $rdf .= "\t\n"; $rdf .= "\t\n"; $rdf .= "\t\t_creator->_foaf_uri) ."\">\n"; if($this->_creator->_foaf_url) { $rdf .= "\t\t\t_creator->_foaf_url ."\"/>\n"; } else $rdf .= "\t\t\tsiocURL('user', $this->_creator->_id). "\"/>\n"; $rdf .= "\t\t\n"; $rdf .= "\t\n"; } else { $rdf .= "\t\n"; $rdf .= "\t\t_creator->_name) $rdf .= " foaf:name=\"" . $this->_creator->_name ."\""; if($this->_creator->_sha1) $rdf .= " foaf:mbox_sha1sum=\"" . $this->_creator->_sha1 ."\""; if($this->_creator->_name) $rdf .= ">\n\t\t\t_creator->_homepage ."\"/>\n\t\t\n"; else $rdf .= "/>\n"; $rdf .= "\t\n"; } } if ($this->_administrator) { if ($this->_administrator->_id) { $rdf .= "\t\n"; $rdf .= "\t\t_administrator->_uri) ."\">\n"; if($this->_administrator->_sioc_url) { $rdf .= "\t\t\t_administrator->_sioc_url ."\"/>\n"; } else $rdf .= "\t\t\tsiocURL('user', $this->_administrator->_id). "\"/>\n"; $rdf .= "\t\t\n"; $rdf .= "\t\n"; } } if($this->_next) { $rdf .= "\rsiocURL('forum', $this->_id, $this->_page+1) ."\"/>\n"; } $rdf .= "_type.">"; return $rdf; } } /** * SIOC::Post object * * Contains information about a post */ class SIOCPost extends SIOCObject { var $type = 'post'; var $_url; var $_subject; var $_content; var $_encoded; var $_creator; var $_created; var $_updated; var $_topics; var $_links; var $_comments; var $_reply_of; var $_type; var $_has_part; function SIOCPost($url, $subject, $content, $encoded, $creator, $created, $updated="", $topics=array(), $links=array(), $type='sioc:Post', $has_part=array() ) { $this->_url = $url; $this->_subject = $subject; $this->_content = $content; $this->_encoded = $encoded; $this->_creator = $creator; $this->_created = $created; $this->_updated = $updated; $this->_topics = $topics; $this->_links = $links; $this->_comments = array(); $this->_reply_of = array(); $this->_type = $type; $this->_has_part = $has_part; // echo $this->_content; } function addComment($id, $url) { $this->_comments[$id] = $url; } function addReplyOf($id, $url) { $this->_reply_of[$id] = $url; } function getContent( &$exp ) { $rdf = '<'.$this->_type." rdf:about=\"" . clean($this->_url) . "\">\n"; if ($this->_type != 'sioc:Post') $rdf .= "\t\n"; if ($this->_subject) { $rdf .= "\t" . $this->_subject . "\n"; } if ($this->_creator) { if ($this->_creator->_id) { $rdf .= "\t\n"; $rdf .= "\t\t_creator->_uri) ."\">\n"; if($this->_creator->_sioc_url) { $rdf .= "\t\t\t_creator->_sioc_url ."\"/>\n"; } else $rdf .= "\t\t\tsiocURL('user', $this->_creator->_id). "\"/>\n"; $rdf .= "\t\t\n"; $rdf .= "\t\n"; $rdf .= "\t\n"; $rdf .= "\t\t_creator->_foaf_uri) ."\">\n"; if($this->_creator->_foaf_url) { $rdf .= "\t\t\t_creator->_foaf_url ."\"/>\n"; } else $rdf .= "\t\t\tsiocURL('user', $this->_creator->_id). "\"/>\n"; $rdf .= "\t\t\n"; $rdf .= "\t\n"; } else { $rdf .= "\t\n"; $rdf .= "\t\t_creator->_name) $rdf .= " foaf:name=\"" . $this->_creator->_name ."\""; if($this->_creator->_sha1) $rdf .= " foaf:mbox_sha1sum=\"" . $this->_creator->_sha1 ."\""; if($this->_creator->_name) $rdf .= ">\n\t\t\t_creator->_homepage ."\"/>\n\t\t\n"; else $rdf .= "/>\n"; $rdf .= "\t\n"; } } $rdf .= "\t" . $this->_created . "\n"; if ($this->_updated AND ($this->_created != $this->_updated) ) $rdf .= "\t" . $this->_updated . "\n"; $rdf .= "\t" . pureContent($this->_content) . "\n"; $rdf .= "\t_encoded . "]]>\n"; if($this->_topics) { foreach($this->_topics as $url=>$topic) { $rdf .= "\t\n"; } } if($this->_links) { foreach($this->_links as $url=>$link) { $rdf .= "\t\n"; } } if($this->_has_part) { foreach($this->_has_part as $url=>$part) { $rdf .= "\t\n"; $rdf .= "\t\t\n"; $rdf .= "\t\n"; } } if($this->_reply_of) { foreach($this->_reply_of as $id => $url) { $rdf .= "\t\n"; $rdf .= "\t\t\n"; $rdf .= "\t\t\tsiocURL('post', $id) . "\"/>\n"; $rdf .= "\t\t\n"; $rdf .= "\t\n"; } } if($this->_comments) { foreach($this->_comments as $id => $url) { $rdf .= "\t\n"; $rdf .= "\t\t\n"; // if($comments->f('comment_trackback')) $rdf .= "\t\t\t" . POST_TRACKBACK . "\n"; // else $rdf .= "\t\t\t" . POST_COMMENT . "\n"; $rdf .= "\t\t\tsiocURL('comment', $id) . "\"/>\n"; $rdf .= "\t\t\n"; $rdf .= "\t\n"; } } $rdf .= "_type.">"; return $rdf; } } /** * "Clean" text * * Transforms text so that it can be safely put into XML markup */ if (!function_exists('clean')) { function clean( $text ) { # return htmlentities( $text ); return htmlentities2( $text ); } } /** * HTML Entities 2 * * Same a HTMLEntities, but avoids double-encoding of entities */ if (!function_exists('htmlentities2')) { function htmlentities2($myHTML) { $translation_table=get_html_translation_table (HTML_ENTITIES,ENT_QUOTES); $translation_table[chr(38)] = '&'; return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/","&" , strtr($myHTML, $translation_table)); //return htmlentities(strtr(str_replace(' ', '%20', $myHTML), $translation_table)); } } /** * pureContent * * Prepares text-only representation of HTML content */ if (!function_exists('pureContent')) { function pureContent($content) { // Remove HTML tags // May add more cleanup code later, if validation errors are found return strip_tags($content); } } ?>