Browse Source

fix parsing for hReview

thanks to the new backcompat in php-mf2 0.3.2
pull/39/head
Aaron Parecki 7 years ago
parent
commit
330bc9024d
No known key found for this signature in database GPG Key ID: 276C2817346D6056
3 changed files with 38 additions and 10 deletions
  1. +31
    -0
      lib/XRay/Formats/Mf2.php
  2. +6
    -9
      tests/ParseTest.php
  3. +1
    -1
      tests/data/source.example.com/hReview

+ 31
- 0
lib/XRay/Formats/Mf2.php View File

@ -32,6 +32,10 @@ class Mf2 {
#Parse::debug("mf2:0: Recognized $url as an h-product it is the only item on the page"); #Parse::debug("mf2:0: Recognized $url as an h-product it is the only item on the page");
return self::parseAsHProduct($mf2, $item, $http); return self::parseAsHProduct($mf2, $item, $http);
} }
if(in_array('h-item', $item['type'])) {
#Parse::debug("mf2:0: Recognized $url as an h-product it is the only item on the page");
return self::parseAsHItem($mf2, $item, $http);
}
if(in_array('h-feed', $item['type'])) { if(in_array('h-feed', $item['type'])) {
#Parse::debug("mf2:0: Recognized $url as an h-feed because it is the only item on the page"); #Parse::debug("mf2:0: Recognized $url as an h-feed because it is the only item on the page");
return self::parseAsHFeed($mf2, $http); return self::parseAsHFeed($mf2, $http);
@ -62,6 +66,8 @@ class Mf2 {
return self::parseAsHRecipe($mf2, $item, $http); return self::parseAsHRecipe($mf2, $item, $http);
} elseif(in_array('h-product', $item['type'])) { } elseif(in_array('h-product', $item['type'])) {
return self::parseAsHProduct($mf2, $item, $http); return self::parseAsHProduct($mf2, $item, $http);
} elseif(in_array('h-item', $item['type'])) {
return self::parseAsHItem($mf2, $item, $http);
} else { } else {
#Parse::debug('This object was not a recognized type.'); #Parse::debug('This object was not a recognized type.');
return false; return false;
@ -92,6 +98,8 @@ class Mf2 {
return self::parseAsHRecipe($mf2, $item, $http); return self::parseAsHRecipe($mf2, $item, $http);
} elseif(in_array('h-product', $item['type'])) { } elseif(in_array('h-product', $item['type'])) {
return self::parseAsHProduct($mf2, $item, $http); return self::parseAsHProduct($mf2, $item, $http);
} elseif(in_array('h-item', $item['type'])) {
return self::parseAsHItem($mf2, $item, $http);
} }
} }
} }
@ -135,6 +143,9 @@ class Mf2 {
} elseif(in_array('h-product', $item['type'])) { } elseif(in_array('h-product', $item['type'])) {
#Parse::debug("mf2:6: $url is falling back to the first h-product on the page"); #Parse::debug("mf2:6: $url is falling back to the first h-product on the page");
return self::parseAsHProduct($mf2, $item, $http); return self::parseAsHProduct($mf2, $item, $http);
} elseif(in_array('h-item', $item['type'])) {
#Parse::debug("mf2:6: $url is falling back to the first h-item on the page");
return self::parseAsHItem($mf2, $item, $http);
} }
} }
@ -408,6 +419,26 @@ class Mf2 {
return $response; return $response;
} }
private static function parseAsHItem($mf2, $item, $http) {
$data = [
'type' => 'item'
];
self::collectSingleValues(['name'], ['url'], $item, $data);
self::collectArrayURLValues(['photo','video','audio'], $item, $data, $refs, $http);
$response = [
'data' => $data
];
if(count($refs)) {
$response['data']['refs'] = $refs;
}
return $response;
}
private static function parseAsHEvent($mf2, $item, $http) { private static function parseAsHEvent($mf2, $item, $http) {
$data = [ $data = [
'type' => 'event' 'type' => 'event'

+ 6
- 9
tests/ParseTest.php View File

@ -431,18 +431,15 @@ class ParseTest extends PHPUnit_Framework_TestCase {
$data = json_decode($body, true); $data = json_decode($body, true);
$this->assertEquals('review', $data['data']['type']); $this->assertEquals('review', $data['data']['type']);
$this->assertEquals('Review', $data['data']['name']);
# TODO: backcompat of mf1 parser is kind of messed up right now
#$this->assertEquals('Not great', $data['data']['summary']);
$this->assertEquals('Not great', $data['data']['summary']);
$this->assertEquals('3', $data['data']['rating']); $this->assertEquals('3', $data['data']['rating']);
$this->assertEquals('5', $data['data']['best']); $this->assertEquals('5', $data['data']['best']);
$this->assertEquals('This is the full text of the review', $data['data']['content']['text']); $this->assertEquals('This is the full text of the review', $data['data']['content']['text']);
// $this->assertContains('http://product.example.com/', $data['data']['item']);
// $this->assertArrayHasKey('http://product.example.com/', $data['data']['refs']);
// $this->assertEquals('product', $data['data']['refs']['http://product.example.com/']['type']);
// $this->assertEquals('The Reviewed Product', $data['data']['refs']['http://product.example.com/']['name']);
// $this->assertEquals('http://product.example.com/', $data['data']['refs']['http://product.example.com/']['url']);
$this->assertContains('http://product.example.com/', $data['data']['item']);
$this->assertArrayHasKey('http://product.example.com/', $data['data']['refs']);
$this->assertEquals('item', $data['data']['refs']['http://product.example.com/']['type']);
$this->assertEquals('The Reviewed Product', $data['data']['refs']['http://product.example.com/']['name']);
$this->assertEquals('http://product.example.com/', $data['data']['refs']['http://product.example.com/']['url']);
} }
public function testMf2Recipe() { public function testMf2Recipe() {

+ 1
- 1
tests/data/source.example.com/hReview View File

@ -9,7 +9,7 @@ Connection: keep-alive
<title>Review</title> <title>Review</title>
</head> </head>
<body class="hreview"> <body class="hreview">
<h2 class="fn">Review</h2>
<h2>Review</h2>
<a href="/hReview" class="permalink">permalink</a> <a href="/hReview" class="permalink">permalink</a>

Loading…
Cancel
Save