Skip to content

Commit

Permalink
Refactor ALL the things
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenDeDauw committed Nov 8, 2015
1 parent 87041b6 commit f7f55e1
Show file tree
Hide file tree
Showing 18 changed files with 482 additions and 110 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
@@ -1,8 +1,6 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7
- hhvm
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -11,7 +11,7 @@
**JsonDumpReader** provides ways to read from and iterate through the [Wikibase](http://wikiba.se/)
entities in a Wikibase Repository JSON dump.

Works with PHP 5.4+, PHP7 and HHVM.
Works with PHP 5.6+, PHP7 and HHVM.

## Installation

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -17,7 +17,8 @@
}
],
"require": {
"php": ">=5.4.0",
"php": ">=5.6.0",
"jeroen/rewindable-generator": "~1.1",
"wikibase/data-model-serialization": "~1.1|~2.0"
},
"require-dev": {
Expand Down
10 changes: 5 additions & 5 deletions phpcs.xml
Expand Up @@ -3,7 +3,7 @@
- https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml
- https://github.com/squizlabs/PHP_CodeSniffer/tree/master/CodeSniffer/Standards
-->
<ruleset name="Replicator">
<ruleset name="JsonDumpReader">
<rule ref="Generic.Classes" />
<rule ref="Generic.CodeAnalysis" />
<rule ref="Generic.CodeAnalysis.UnusedFunctionParameter.Found">
Expand All @@ -19,8 +19,8 @@
<rule ref="Generic.Files.LineEndings" />
<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="125" />
<property name="absoluteLineLimit" value="125" />
<property name="lineLimit" value="130" />
<property name="absoluteLineLimit" value="130" />
</properties>
</rule>
<rule ref="Generic.Files.OneClassPerFile" />
Expand All @@ -33,8 +33,8 @@

<rule ref="Generic.Metrics.NestingLevel">
<properties>
<property name="nestingLevel" value="3" />
<property name="absoluteNestingLevel" value="3" />
<property name="nestingLevel" value="4" />
<property name="absoluteNestingLevel" value="4" />
</properties>
</rule>

Expand Down
1 change: 0 additions & 1 deletion phpunit.xml.dist
Expand Up @@ -10,7 +10,6 @@
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
strict="false"
verbose="true">
<testsuites>
<testsuite name="JsonDumpReaderTests">
Expand Down
2 changes: 2 additions & 0 deletions src/DumpReader.php
Expand Up @@ -3,6 +3,7 @@
namespace Wikibase\JsonDumpReader;

/**
* Package public
* @since 1.0.0
*
* @licence GNU GPL v2+
Expand All @@ -14,6 +15,7 @@ interface DumpReader {
* Returns the next JSON object (as string) or null if the end of the dump has been reached.
*
* @return string|null
* @throws DumpReadingException
*/
public function nextJsonLine();

Expand Down
16 changes: 16 additions & 0 deletions src/DumpReadingException.php
@@ -0,0 +1,16 @@
<?php

namespace Wikibase\JsonDumpReader;

use RuntimeException;

/**
* Package public
* @since 1.0.0
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class DumpReadingException extends RuntimeException {

}
63 changes: 21 additions & 42 deletions src/JsonDumpIterator.php → src/Iterator/EntityDumpIterator.php
@@ -1,26 +1,29 @@
<?php

namespace Wikibase\JsonDumpReader;
namespace Wikibase\JsonDumpReader\Iterator;

use Deserializers\Deserializer;
use Deserializers\Exceptions\DeserializationException;
use Iterator;
use Wikibase\DataModel\Entity\EntityDocument;

/**
* Package private
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class JsonDumpIterator implements \Iterator {
class EntityDumpIterator implements Iterator {

/**
* @var Deserializer
*/
private $deserializer;

/**
* @var ExtractedDumpReader
* @var Iterator
*/
private $dumpReader;
private $dumpIterator;

/**
* @var callable|null
Expand All @@ -32,25 +35,15 @@ class JsonDumpIterator implements \Iterator {
*/
private $current = null;

/**
* @var string|null
*/
private $currentJson = null;

public function __construct( DumpReader $dumpReader, Deserializer $entityDeserializer ) {
$this->dumpReader = $dumpReader;
public function __construct( Iterator $objectIterator, Deserializer $entityDeserializer ) {
$this->dumpIterator = $objectIterator;
$this->deserializer = $entityDeserializer;
}

/**
* Sets a callback that will be called with a string message when an error occurs.
* Overrides previously set callbacks.
*
* @since 1.0.0
*
* @param callable $errorReporter
* @param callable|null $errorReporter
*/
public function onError( callable $errorReporter ) {
public function onError( callable $errorReporter = null ) {
$this->errorReporter = $errorReporter;
}

Expand All @@ -61,46 +54,33 @@ public function current() {
return $this->current;
}

/**
* @return EntityDocument|null
*/
public function getCurrentJson() {
return $this->currentJson;
}

/**
* @return EntityDocument|null
*/
public function next() {
$data = $this->getNextFromJson();
$data = $this->getNextFromObject();

$this->currentJson = $data[0];
$this->current = $data[1];
$this->current = $data;

return $this->current;
}

private function getNextFromJson() {
do {
$json = $this->dumpReader->nextJsonLine();
if ( $json === null ) {
return null;
}
private function getNextFromObject() {
while ( true ) {
$jsonData = $this->dumpIterator->current();
$this->dumpIterator->next();

$data = json_decode( $json, true );
if ( $data === null ) {
$this->reportError( json_last_error_msg() );
return [ null, null ];
if ( $jsonData === null ) {
return null;
}

try {
return [ $json, $this->deserializer->deserialize( $data ) ];
return $this->deserializer->deserialize( $jsonData );
}
catch ( DeserializationException $ex ) {
$this->reportError( $ex->getMessage() );
}
}
while ( true );

return null;
}
Expand All @@ -121,8 +101,7 @@ public function valid() {

public function rewind() {
$this->current = null;
$this->currentJson = null;
$this->dumpReader->rewind();
$this->dumpIterator->rewind();
$this->next();
}

Expand Down
107 changes: 107 additions & 0 deletions src/Iterator/ObjectDumpIterator.php
@@ -0,0 +1,107 @@
<?php

namespace Wikibase\JsonDumpReader\Iterator;

use Iterator;

/**
* Package private
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class ObjectDumpIterator implements Iterator {

/**
* @var callable|null
*/
private $errorReporter = null;

/**
* @var string|null
*/
private $current = null;

/**
* @var int
*/
private $key = 0;

/**
* @var Iterator
*/
private $stringIterator;

public function __construct( Iterator $stringIterator ) {
$this->stringIterator = $stringIterator;
}

/**
* @param callable|null $errorReporter
*/
public function onError( callable $errorReporter = null ) {
$this->errorReporter = $errorReporter;
}

/**
* @return string|null
*/
public function current() {
return $this->current;
}

/**
* @return string|null
*/
public function next() {
$data = $this->getNextFromString();

$this->current = $data;
$this->key++;

return $this->current;
}

private function getNextFromString() {
while ( true ) {
$jsonString = $this->stringIterator->current();
$this->stringIterator->next();

if ( $jsonString === null ) {
return null;
}

$data = json_decode( $jsonString, true );
if ( $data === null ) {
$this->reportError( json_last_error_msg() );
}
else {
return $data;
}
}

return null;
}

private function reportError( $errorMessage ) {
if ( $this->errorReporter !== null ) {
call_user_func( $this->errorReporter, $errorMessage );
}
}

public function key() {
return $this->key;
}

public function valid() {
return $this->current !== null;
}

public function rewind() {
$this->current = null;
$this->key = -1;
$this->stringIterator->rewind();
$this->next();
}

}

0 comments on commit f7f55e1

Please sign in to comment.