[MediaWiki-commits] [Gerrit] purtle[master]: Add JSON-LD support.

2017-09-25 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/371446 )

Change subject: Add JSON-LD support.
..


Add JSON-LD support.

Bug: T44063
Change-Id: I77d6a771abfef5b00b1b151c48aa950be30896cf
---
M README.md
M composer.json
A src/JsonLdRdfWriter.php
M src/RdfWriterBase.php
M src/RdfWriterFactory.php
A tests/data/DumpHeader.jsonld
A tests/data/DumpHeader.nt
A tests/data/DumpHeader.rdf
A tests/data/DumpHeader.ttl
A tests/data/EricMiller.jsonld
A tests/data/LabeledBlankNode.jsonld
A tests/data/NumberedBlankNode.jsonld
A tests/data/Numbers.jsonld
A tests/data/Predicates.jsonld
A tests/data/Resources.jsonld
A tests/data/TextWithSpecialChars.jsonld
A tests/data/Texts.jsonld
A tests/data/Triples.jsonld
A tests/data/Values.jsonld
A tests/phpunit/JsonLdRdfWriterTest.php
M tests/phpunit/NTriplesRdfWriterTest.php
M tests/phpunit/RdfWriterFactoryTest.php
M tests/phpunit/RdfWriterTestBase.php
M tests/phpunit/TurtleRdfWriterTest.php
24 files changed, 784 insertions(+), 16 deletions(-)

Approvals:
  Smalyshev: Looks good to me, approved
  jenkins-bot: Verified
  Thiemo Mättig (WMDE): Looks good to me, but someone else must approve



diff --git a/README.md b/README.md
index 95321b5..edd0618 100644
--- a/README.md
+++ b/README.md
@@ -1,14 +1,15 @@
 # Purtle
 
 **Purtle** is a fast, lightweight RDF generator. It provides a "fluent" 
interface for
-generating RDF output in Turtle, XML/RDF or N-Triples. The fluent interface 
allows the
+generating RDF output in Turtle, JSON-LD, XML/RDF or N-Triples. The fluent 
interface allows the
 resulting PHP code to be structured just like Turtle notation for RDF, hence 
the name: "Purtle"
 is a contraction of "PHP Turtle".
 
-The three concrete classes implementing the common `RdfWriter` interface are:
+The concrete classes implementing the common `RdfWriter` interface are:
 * `TurtleRdfWriter` outputs Turtle
 * `XmlRdfWriter` outputs XML/RDF
 * `NTriplesRdfWriter` outputs N-Triples
+* `JsonLdRdfWriter` outputs JSON-LD
 
 The PHP code would look something like this:
 
diff --git a/composer.json b/composer.json
index 5bf8b1b..d714be3 100644
--- a/composer.json
+++ b/composer.json
@@ -5,7 +5,8 @@
"keywords": [
"RDF",
"Serializer",
-   "Turtle"
+   "Turtle",
+   "JSON-LD"
],
"homepage": "https://mediawiki.org/wiki/Purtle";,
"license": "GPL-2.0+",
@@ -18,6 +19,9 @@
},
{
"name": "Thiemo Mättig"
+   },
+   {
+   "name": "C. Scott Ananian"
}
],
"support": {
diff --git a/src/JsonLdRdfWriter.php b/src/JsonLdRdfWriter.php
new file mode 100644
index 000..2366a98
--- /dev/null
+++ b/src/JsonLdRdfWriter.php
@@ -0,0 +1,368 @@
+https://www.w3.org/TR/json-ld/#the-context
+* @var string[]
+*/
+   protected $context = [];
+
+   /**
+* The JSON-LD "@graph" array, which lists all the nodes
+* described by this JSON-LD object.
+* @see https://www.w3.org/TR/json-ld/#named-graphs
+* @var array[]|null
+*/
+   private $graph = [];
+
+   /**
+* A collection of predicates about a specific subject.  The
+* subject is identified by the "@id" key in this array; the other
+* keys identify JSON-LD properties.
+* @see https://www.w3.org/TR/json-ld/#dfn-edge
+* @var array
+*/
+   private $predicates = [];
+
+   /**
+* A sequence of zero or more IRIs, nodes, or values, which are the
+* destination targets of the current predicates.
+* @see https://www.w3.org/TR/json-ld/#dfn-list
+* @var array
+*/
+   private $values = [];
+
+   /**
+* True iff we have written the opening of the "@graph" field.
+* @var bool
+*/
+   private $wroteGraph = false;
+
+   /**
+* JSON-LD objects describing a single node can omit the "@graph" field;
+* this variable remains false only so long as we can guarantee that
+* only a single node has been described.
+* @var bool
+*/
+   private $disableGraphOpt = false;
+
+   /**
+* The IRI for the RDF `type` property.
+*/
+   const RDF_TYPE_IRI = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type';
+
+   /**
+* @param string $role
+* @param BNodeLabeler|null $labeler
+*/
+   public function __construct( $role = parent::DOCUMENT_ROLE, 
BNodeLabeler $labeler = null ) {
+   parent::__construct( $role, $labeler );
+
+   // The following named methods are protected, not private, so we
+   // can invoke them directly w/o function wrappers.
+   $this->transitionTable[self::STATE_START][self::STATE_DOCUMENT] 
=
+   

[MediaWiki-commits] [Gerrit] purtle[master]: Add JSON-LD support.

2017-08-11 Thread C. Scott Ananian (Code Review)
C. Scott Ananian has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/371446 )

Change subject: Add JSON-LD support.
..

Add JSON-LD support.

Change-Id: I77d6a771abfef5b00b1b151c48aa950be30896cf
---
M README.md
M composer.json
A src/JsonLdRdfWriter.php
M src/RdfWriterFactory.php
A tests/data/EricMiller.json
A tests/data/LabeledBlankNode.json
A tests/data/NumberedBlankNode.json
A tests/data/Numbers.json
A tests/data/Predicates.json
A tests/data/Resources.json
A tests/data/TextWithSpecialChars.json
A tests/data/Texts.json
A tests/data/Triples.json
A tests/data/Values.json
A tests/phpunit/JsonLdRdfWriterTest.php
M tests/phpunit/NTriplesRdfWriterTest.php
M tests/phpunit/RdfWriterFactoryTest.php
M tests/phpunit/RdfWriterTestBase.php
M tests/phpunit/TurtleRdfWriterTest.php
M tests/phpunit/XmlRdfWriterTest.php
20 files changed, 583 insertions(+), 15 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/purtle refs/changes/46/371446/1

diff --git a/README.md b/README.md
index 95321b5..1d4e39c 100644
--- a/README.md
+++ b/README.md
@@ -1,14 +1,15 @@
 # Purtle
 
 **Purtle** is a fast, lightweight RDF generator. It provides a "fluent" 
interface for
-generating RDF output in Turtle, XML/RDF or N-Triples. The fluent interface 
allows the
+generating RDF output in Turtle, JSON-LD, XML/RDF or N-Triples. The fluent 
interface allows the
 resulting PHP code to be structured just like Turtle notation for RDF, hence 
the name: "Purtle"
 is a contraction of "PHP Turtle".
 
-The three concrete classes implementing the common `RdfWriter` interface are:
+The four concrete classes implementing the common `RdfWriter` interface are:
 * `TurtleRdfWriter` outputs Turtle
 * `XmlRdfWriter` outputs XML/RDF
 * `NTriplesRdfWriter` outputs N-Triples
+* `JsonLdRdfWriter` outputs JSON-LD
 
 The PHP code would look something like this:
 
diff --git a/composer.json b/composer.json
index 1243c49..1c8f823 100644
--- a/composer.json
+++ b/composer.json
@@ -5,7 +5,8 @@
"keywords": [
"RDF",
"Serializer",
-   "Turtle"
+   "Turtle",
+   "JSON-LD"
],
"homepage": "https://mediawiki.org/wiki/Purtle";,
"license": "GPL-2.0+",
@@ -18,6 +19,9 @@
},
{
"name": "Thiemo Mättig"
+   },
+   {
+   "name": "C. Scott Ananian"
}
],
"support": {
diff --git a/src/JsonLdRdfWriter.php b/src/JsonLdRdfWriter.php
new file mode 100644
index 000..184b826
--- /dev/null
+++ b/src/JsonLdRdfWriter.php
@@ -0,0 +1,262 @@
+transitionTable[self::STATE_START][self::STATE_DOCUMENT] 
= function () {
+   $this->beginJson();
+   };
+   
$this->transitionTable[self::STATE_DOCUMENT][self::STATE_FINISH] = function () {
+   $this->finishJson();
+   };
+   
$this->transitionTable[self::STATE_OBJECT][self::STATE_PREDICATE] = function () 
{
+   $this->finishPredicate();
+   };
+   $this->transitionTable[self::STATE_OBJECT][self::STATE_SUBJECT] 
= function () {
+   $this->finishPredicate();
+   $this->finishSubject();
+   };
+   
$this->transitionTable[self::STATE_OBJECT][self::STATE_DOCUMENT] = function () {
+   $this->finishPredicate();
+   $this->finishSubject();
+   $this->finishDocument();
+   };
+   }
+
+   private function isTopLevel() {
+   return $this->role === self::DOCUMENT_ROLE;
+   }
+
+   private function contextify( $base, $local = null ) {
+   $this->expandShorthand( $base, $local );
+
+   if ( $base === 'rdf' && $local === 'type' ) {
+   return '@type';
+   } elseif ( $local === null ) {
+   return $base;
+   } else {
+   if ( $base !== '_' && $this->isPrefix( $base ) ) {
+   $prefixes = $this->getPrefixes();
+   if ( $base === '' ) {
+   // Empty prefix not supported; use full 
IRI
+   return $prefixes[ $base ] . $local;
+   }
+   $this->context[ $base ] = $prefixes[ $base ];
+   }
+   return $base . ':' . $local;
+   }
+   }
+
+   /**
+* Write document header
+*/
+   private function beginJson() {
+   if ( $this->isTopLevel() ) {
+   $this->write( "{\n" );
+   $this->write( function () {
+   // If this buffer is drained