Browse Source

includes nested h-cite and other objects

if a property such as `in-reply-to` is an h-cite, the URL is still returned as the `in-reply-to` value, and the h-cite object is available in a different part of the response.

closes #6
pull/39/head
Aaron Parecki 8 years ago
parent
commit
ac6d86c0db
7 changed files with 159 additions and 8 deletions
  1. +1
    -3
      controllers/Parse.php
  2. +41
    -5
      lib/Formats/Mf2.php
  3. +51
    -0
      tests/ParseTest.php
  4. +17
    -0
      tests/data/source.example.com/person-tag-is-h-card
  5. +15
    -0
      tests/data/source.example.com/person-tag-is-url
  6. +19
    -0
      tests/data/source.example.com/reply-is-h-cite
  7. +15
    -0
      tests/data/source.example.com/reply-is-url

+ 1
- 3
controllers/Parse.php View File

@ -116,9 +116,7 @@ class Parse {
if($mf2 && count($mf2['items']) > 0) {
$data = Formats\Mf2::parse($mf2, $url, $this->http);
if($data) {
return $this->respond($response, 200, [
'data' => $data,
]);
return $this->respond($response, 200, $data);
}
}

+ 41
- 5
lib/Formats/Mf2.php View File

@ -27,7 +27,7 @@ class Mf2 {
}
// Otherwise check for an h-entry
if(in_array('h-entry', $item['type'])) {
if(in_array('h-entry', $item['type']) || in_array('h-cite', $item['type'])) {
return self::parseHEntry($mf2, $http);
}
}
@ -45,6 +45,7 @@ class Mf2 {
'photo' => null
]
];
$refs = [];
$item = $mf2['items'][0];
@ -56,7 +57,7 @@ class Mf2 {
}
// Always arrays
$properties = ['photo','video','syndication','in-reply-to','like-of','repost-of','category'];
$properties = ['photo','video','syndication'];
foreach($properties as $p) {
if(array_key_exists($p, $item['properties'])) {
$data[$p] = [];
@ -69,6 +70,27 @@ class Mf2 {
}
}
// Always returned as arrays, and may also create external references
$properties = ['in-reply-to','like-of','repost-of','category'];
foreach($properties as $p) {
if(array_key_exists($p, $item['properties'])) {
$data[$p] = [];
foreach($item['properties'][$p] as $v) {
if(is_string($v))
$data[$p][] = $v;
elseif(self::isMicroformat($v) && ($u=self::getPlaintext($v, 'url'))) {
$data[$p][] = $u;
// parse the object and put the result in the "refs" object
$ref = self::parse(['items'=>[$v]], $u, $http);
if($ref) {
$refs[$u] = $ref['data'];
}
}
}
}
}
// Determine if the name is distinct from the content
$name = self::getPlaintext($item, 'name');
$content = null;
@ -114,7 +136,15 @@ class Mf2 {
$data['author'] = self::findAuthor($mf2, $item, $http);
return $data;
$response = [
'data' => $data
];
if(count($refs)) {
$response['refs'] = $refs;
}
return $response;
}
private static function parseHFeed($mf2, $http) {
@ -130,7 +160,9 @@ class Mf2 {
'todo' => 'Not yet implemented. Please see https://github.com/aaronpk/XRay/issues/1'
];
return $data;
return [
'data' => $data
];
}
private static function parseHCard($item, $http, $authorURL=false) {
@ -159,7 +191,11 @@ class Mf2 {
}
}
return $data;
$response = [
'data' => $data
];
return $response;
}
private static function findAuthor($mf2, $item, $http) {

+ 51
- 0
tests/ParseTest.php View File

@ -128,4 +128,55 @@ class ParseTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('unknown', $data->data->type);
}
public function testReplyIsURL() {
$url = 'http://source.example.com/reply-is-url';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('entry', $data['data']['type']);
$this->assertEquals('http://example.com/100', $data['data']['in-reply-to'][0]);
}
public function testReplyIsHCite() {
$url = 'http://source.example.com/reply-is-h-cite';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('entry', $data['data']['type']);
$this->assertEquals('http://example.com/100', $data['data']['in-reply-to'][0]);
$this->assertArrayHasKey('http://example.com/100', $data['refs']);
$this->assertEquals('Example Post', $data['refs']['http://example.com/100']['name']);
$this->assertEquals('http://example.com/100', $data['refs']['http://example.com/100']['url']);
}
public function testPersonTagIsURL() {
$url = 'http://source.example.com/person-tag-is-url';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('entry', $data['data']['type']);
$this->assertEquals('http://alice.example.com/', $data['data']['category'][0]);
}
public function testPersonTagIsHCard() {
$url = 'http://source.example.com/person-tag-is-h-card';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('entry', $data['data']['type']);
$this->assertEquals('http://alice.example.com/', $data['data']['category'][0]);
$this->assertArrayHasKey('http://alice.example.com/', $data['refs']);
$this->assertEquals('card', $data['refs']['http://alice.example.com/']['type']);
$this->assertEquals('http://alice.example.com/', $data['refs']['http://alice.example.com/']['url']);
$this->assertEquals('Alice', $data['refs']['http://alice.example.com/']['name']);
}
}

+ 17
- 0
tests/data/source.example.com/person-tag-is-h-card View File

@ -0,0 +1,17 @@
HTTP/1.1 200 OK
Server: Apache
Date: Wed, 09 Dec 2015 03:29:14 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
<html>
<head>
<title>Test</title>
</head>
<body class="h-entry">
<h2 class="p-name">Hello World</h2>
<div class="p-category h-card">
<a href="http://alice.example.com/" class="u-url p-name">Alice</a>
</div>
</body>
</html>

+ 15
- 0
tests/data/source.example.com/person-tag-is-url View File

@ -0,0 +1,15 @@
HTTP/1.1 200 OK
Server: Apache
Date: Wed, 09 Dec 2015 03:29:14 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
<html>
<head>
<title>Test</title>
</head>
<body class="h-entry">
<h2 class="p-name">Hello World</h2>
<a href="http://alice.example.com/" class="u-category">Alice</a>
</body>
</html>

+ 19
- 0
tests/data/source.example.com/reply-is-h-cite View File

@ -0,0 +1,19 @@
HTTP/1.1 200 OK
Server: Apache
Date: Wed, 09 Dec 2015 03:29:14 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
<html>
<head>
<title>Test</title>
</head>
<body class="h-entry">
<div class="p-in-reply-to h-cite">
<span class="p-name">Example Post</span>
<a href="http://author.example.com/about" class="p-author h-card">Author</a>
<a href="http://example.com/100" class="u-url">permalink</a>
</div>
<p class="e-content p-name">This page has a link to <a href="http://target.example.com">target.example.com</a> and some <b>formatted text</b>.</p>
</body>
</html>

+ 15
- 0
tests/data/source.example.com/reply-is-url View File

@ -0,0 +1,15 @@
HTTP/1.1 200 OK
Server: Apache
Date: Wed, 09 Dec 2015 03:29:14 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
<html>
<head>
<title>Test</title>
</head>
<body class="h-entry">
<a href="http://example.com/100" class="u-in-reply-to">in reply to</a>
<p class="e-content">This page has a link to <a href="http://target.example.com">target.example.com</a> and some <b>formatted text</b>.</p>
</body>
</html>

Loading…
Cancel
Save