Commit 9ad50642 authored by webchick's avatar webchick

Issue #1866858 by linclark, scor: Test RDFa by parsing RDFa (add the easyrdf library).

parent 626a067d
......@@ -15,7 +15,8 @@
"doctrine/common": "2.3.*@stable",
"guzzle/http": "*",
"kriswallsmith/assetic": "1.1.*@alpha",
"symfony-cmf/routing": "1.0.*@dev"
"symfony-cmf/routing": "1.0.*@dev",
"njh/easyrdf": "0.8.*"
},
"minimum-stability": "dev"
}
......@@ -359,6 +359,39 @@
"routing"
]
},
{
"name": "njh/easyrdf",
"description": "EasyRdf is a PHP library designed to make it easy to consume and produce RDF.",
"version": "0.8.0",
"type": "library",
"keywords": ["RDF", "Semantic Web", "Turtle", "RDFa"],
"homepage": "http://www.aelius.com/njh/easyrdf/",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Nicholas Humfrey",
"email": "njh@aelius.com",
"homepage": "http://www.aelius.com/njh/",
"role": "Developer"
}
],
"support": {
"forum": "http://groups.google.com/group/easyrdf/",
"issues": "http://github.com/njh/easyrdf/issues"
},
"require": {
"php": ">=5.2.8"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/njh/easyrdf"
}
],
"autoload": {
"psr-0": { "EasyRdf": "lib/" }
}
},
{
"name": "symfony/class-loader",
"version": "dev-master",
......
......@@ -23,4 +23,5 @@
'Guzzle\\Common' => $vendorDir . '/guzzle/common/',
'Doctrine\\Common' => $vendorDir . '/doctrine/common/lib/',
'Assetic' => $vendorDir . '/kriswallsmith/assetic/src/',
'EasyRdf_' => $vendorDir . '/njh/easyrdf/lib/',
);
<?php
/**
* EasyRdf
*
* Use this file to load the core of EasyRdf, if you don't have an autoloader.
*
*
* LICENSE
*
* Copyright (c) 2009-2011 Nicholas J Humfrey. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author 'Nicholas J Humfrey" may be used to endorse or
* promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package EasyRdf
* @copyright Copyright (c) 2011 Nicholas J Humfrey
* @license http://www.opensource.org/licenses/bsd-license.php
* @version $Id$
*/
/**
* @see EasyRdf_Exception
*/
require_once "EasyRdf/Exception.php";
/**
* @see EasyRdf_Format
*/
require_once "EasyRdf/Format.php";
/**
* @see EasyRdf_Graph
*/
require_once "EasyRdf/Graph.php";
/**
* @see EasyRdf_GraphStore
*/
require_once "EasyRdf/GraphStore.php";
/**
* @see EasyRdf_Http
*/
require_once "EasyRdf/Http.php";
/**
* @see EasyRdf_Http_Client
*/
require_once "EasyRdf/Http/Client.php";
/**
* @see EasyRdf_Http_Response
*/
require_once "EasyRdf/Http/Response.php";
/**
* @see EasyRdf_Namespace
*/
require_once "EasyRdf/Namespace.php";
/**
* @see EasyRdf_Literal
*/
require_once "EasyRdf/Literal.php";
/**
* @see EasyRdf_Literal_Boolean
*/
require_once "EasyRdf/Literal/Boolean.php";
/**
* @see EasyRdf_Literal_Date
*/
require_once "EasyRdf/Literal/Date.php";
/**
* @see EasyRdf_Literal_DateTime
*/
require_once "EasyRdf/Literal/DateTime.php";
/**
* @see EasyRdf_Literal_Decimal
*/
require_once "EasyRdf/Literal/Decimal.php";
/**
* @see EasyRdf_Literal_HexBinary
*/
require_once "EasyRdf/Literal/HexBinary.php";
/**
* @see EasyRdf_Literal_HTML
*/
require_once "EasyRdf/Literal/HTML.php";
/**
* @see EasyRdf_Literal_Integer
*/
require_once "EasyRdf/Literal/Integer.php";
/**
* @see EasyRdf_Literal_XML
*/
require_once "EasyRdf/Literal/XML.php";
/**
* @see EasyRdf_ParsedUri
*/
require_once "EasyRdf/ParsedUri.php";
/**
* @see EasyRdf_Parser
*/
require_once "EasyRdf/Parser.php";
/**
* @see EasyRdf_Parser_RdfPhp
*/
require_once "EasyRdf/Parser/RdfPhp.php";
/**
* @see EasyRdf_Parser_Ntriples
*/
require_once "EasyRdf/Parser/Ntriples.php";
/**
* @see EasyRdf_Parser_Json
*/
require_once "EasyRdf/Parser/Json.php";
/**
* @see EasyRdf_Parser_Rdfa
*/
require_once "EasyRdf/Parser/Rdfa.php";
/**
* @see EasyRdf_Parser_RdfXml
*/
require_once "EasyRdf/Parser/RdfXml.php";
/**
* @see EasyRdf_Parser_Turtle
*/
require_once "EasyRdf/Parser/Turtle.php";
/**
* @see EasyRdf_Resource
*/
require_once "EasyRdf/Resource.php";
/**
* @see EasyRdf_Serialiser
*/
require_once "EasyRdf/Serialiser.php";
/**
* @see EasyRdf_Serialiser_GraphViz
*/
require_once "EasyRdf/Serialiser/GraphViz.php";
/**
* @see EasyRdf_Serialiser_RdfPhp
*/
require_once "EasyRdf/Serialiser/RdfPhp.php";
/**
* @see EasyRdf_Serialiser_Ntriples
*/
require_once "EasyRdf/Serialiser/Ntriples.php";
/**
* @see EasyRdf_Serialiser_Json
*/
require_once "EasyRdf/Serialiser/Json.php";
/**
* @see EasyRdf_Serialiser_RdfXml
*/
require_once "EasyRdf/Serialiser/RdfXml.php";
/**
* @see EasyRdf_Serialiser_Turtle
*/
require_once "EasyRdf/Serialiser/Turtle.php";
/**
* @see EasyRdf_Sparql_Client
*/
require_once "EasyRdf/Sparql/Client.php";
/**
* @see EasyRdf_Sparql_Result
*/
require_once "EasyRdf/Sparql/Result.php";
/**
* @see EasyRdf_TypeMapper
*/
require_once "EasyRdf/TypeMapper.php";
/**
* @see EasyRdf_Utils
*/
require_once "EasyRdf/Utils.php";
<?php
/**
* EasyRdf
*
* LICENSE
*
* Copyright (c) 2009-2010 Nicholas J Humfrey. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author 'Nicholas J Humfrey" may be used to endorse or
* promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package EasyRdf
* @copyright Copyright (c) 2009-2010 Nicholas J Humfrey
* @license http://www.opensource.org/licenses/bsd-license.php
* @version $Id$
*/
/**
* EasyRdf Exception class
*
* All exceptions thrown by EasyRdf are an instance of this class.
*
* @package EasyRdf
* @copyright Copyright (c) 2009-2010 Nicholas J Humfrey
* @license http://www.opensource.org/licenses/bsd-license.php
*/
class EasyRdf_Exception extends Exception
{
// Comment to make PHP CodeSniffer happy
}
<?php
/**
* EasyRdf
*
* LICENSE
*
* Copyright (c) 2009-2012 Nicholas J Humfrey. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author 'Nicholas J Humfrey" may be used to endorse or
* promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package EasyRdf
* @copyright Copyright (c) 2009-2012 Nicholas J Humfrey
* @license http://www.opensource.org/licenses/bsd-license.php
* @version $Id$
*/
/**
* Class the represents an RDF file format.
*
* For each format, the name, label, URIs and associated MIME Types are
* stored. A single parser and serialiser can also be registered to each
* format.
*
* @package EasyRdf
* @copyright Copyright (c) 2009-2012 Nicholas J Humfrey
* @license http://www.opensource.org/licenses/bsd-license.php
*/
class EasyRdf_Format
{
private static $formats = array();
private $name = array();
private $label = null;
private $uri = null;
private $mimeTypes = array();
private $extensions = array();
private $parserClass = null;
private $serialiserClass = null;
/** Get a list of format names
*
* @return array An array of formats name
*/
public static function getNames()
{
return array_keys(self::$formats);
}
/** Get a list of all the registered formats
*
* @return array An array of format objects
*/
public static function getFormats()
{
return self::$formats;
}
/** Generates an HTTP Accept header string
*
* The string will contain all of the MIME Types that we
* are able to parse.
*
* It is also possible to specify additional MIME types
* in the form array('text/plain' => 0.5) where 0.5 is the
* q value for that type. The types are sorted by q value
* before constructing the string.
*
* @param array $extraTypes extra MIME types to add
* @return string list of supported MIME types
*/
public static function getHttpAcceptHeader($extraTypes = array())
{
$accept = $extraTypes;
foreach (self::$formats as $format) {
if ($format->parserClass and count($format->mimeTypes) > 0) {
$accept = array_merge($accept, $format->mimeTypes);
}
}
arsort($accept, SORT_NUMERIC);
$acceptStr='';
foreach ($accept as $type => $q) {
if ($acceptStr) {
$acceptStr .= ',';
}
if ($q == 1.0) {
$acceptStr .= $type;
} else {
$acceptStr .= sprintf("%s;q=%1.1f", $type, $q);
}
}
return $acceptStr;
}
/** Check if a named graph exists
*
* @param string $name the name of the format
* @return boolean true if the format exists
*/
public static function formatExists($name)
{
return array_key_exists($name, self::$formats);
}
/** Get a EasyRdf_Format from a name, uri or mime type
*
* @param string $query a query string to search for
* @return object the first EasyRdf_Format that matches the query
* @throws EasyRdf_Exception if no format is found
*/
public static function getFormat($query)
{
if (!is_string($query) or $query == null or $query == '') {
throw new InvalidArgumentException(
"\$query should be a string and cannot be null or empty"
);
}
foreach (self::$formats as $format) {
if ($query == $format->name or
$query == $format->uri or
array_key_exists($query, $format->mimeTypes) or
in_array($query, $format->extensions)) {
return $format;
}
}
# No match
throw new EasyRdf_Exception(
"Format is not recognised: $query"
);
}
/** Register a new format
*
* @param string $name The name of the format (e.g. ntriples)
* @param string $label The label for the format (e.g. N-Triples)
* @param string $uri The URI for the format
* @param string $mimeTypes One or more mime types for the format
* @param string $extensions One or more extensions (file suffix)
* @return object The new EasyRdf_Format object
*/
public static function register(
$name,
$label = null,
$uri = null,
$mimeTypes = array(),
$extensions = array()
) {
if (!is_string($name) or $name == null or $name == '') {
throw new InvalidArgumentException(
"\$name should be a string and cannot be null or empty"
);
}
if (!array_key_exists($name, self::$formats)) {
self::$formats[$name] = new EasyRdf_Format($name);
}
self::$formats[$name]->setLabel($label);
self::$formats[$name]->setUri($uri);
self::$formats[$name]->setMimeTypes($mimeTypes);
self::$formats[$name]->setExtensions($extensions);
return self::$formats[$name];
}
/** Remove a format from the registry
*
* @param string $name The name of the format (e.g. ntriples)
*/
public static function unregister($name)
{
unset(self::$formats[$name]);
}
/** Class method to register a parser class to a format name
*
* @param string $name The name of the format (e.g. ntriples)
* @param string $class The name of the class (e.g. EasyRdf_Parser_Ntriples)
*/
public static function registerParser($name, $class)
{
if (!self::formatExists($name)) {
self::register($name);
}
self::getFormat($name)->setParserClass($class);
}
/** Class method to register a serialiser class to a format name
*
* @param string $name The name of the format (e.g. ntriples)
* @param string $class The name of the class (e.g. EasyRdf_Serialiser_Ntriples)
*/
public static function registerSerialiser($name, $class)
{
if (!self::formatExists($name)) {
self::register($name);
}
self::getFormat($name)->setSerialiserClass($class);
}
/** Attempt to guess the document format from some content.
*
* If $filename is given, then the suffix is first used to guess the format.
*
* If the document format is not recognised, null is returned.
*
* @param string $data The document data
* @param string $filename Optional filename
* @return object EasyRdf_Format The format object
*/
public static function guessFormat($data, $filename = null)
{
if (is_array($data)) {
# Data has already been parsed into RDF/PHP
return self::getFormat('php');
}
// First try and identify by the filename
if ($filename and preg_match("/\.(\w+)$/", $filename, $matches)) {
foreach (self::$formats as $format) {
if (in_array($matches[1], $format->extensions)) {
return $format;
}
}
}
// Then try and guess by the first 255 bytes of content
$short = substr($data, 0, 255);
if (preg_match("/^\s*\{/", $short)) {
return self::getFormat('json');
} elseif (preg_match("/<rdf:/i", $short)) {
return self::getFormat('rdfxml');
} elseif (preg_match("/@prefix\s|@base\s/", $short)) {
return self::getFormat('turtle');
} elseif (preg_match("/^\s*<.+> <.+>/m", $short)) {
return self::getFormat('ntriples');
} elseif (preg_match("|http://www.w3.org/2005/sparql-results|", $short)) {
return self::getFormat('sparql-xml');
} elseif (preg_match("/\WRDFa\W/i", $short)) {
return self::getFormat('rdfa');
} elseif (preg_match("/<!DOCTYPE html|<html/i", $short)) {
# We don't support any other microformats embedded in HTML
return self::getFormat('rdfa');
} else {
return null;
}
}