Skip to content

Commit

Permalink
Merge pull request #26 from aaronpk/master
Browse files Browse the repository at this point in the history
use Mf2 library to parse rels
  • Loading branch information
aaronpk committed Apr 15, 2016
2 parents b9fbe8f + 54aa383 commit eed3468
Show file tree
Hide file tree
Showing 8 changed files with 576 additions and 443 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"minimum-stability": "dev",
"require": {
"php": ">=5.4",
"mf2/mf2": "~0.2"
"mf2/mf2": ">=0.2"
},
"require-dev": {
"phpunit/phpunit": ">=4.8.13"
Expand Down
904 changes: 482 additions & 422 deletions coverage/MentionClient.php.html

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions coverage/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ <h3>Project Risks</h3>
<footer>
<hr/>
<p>
<small>Generated by <a href="http://github.com/sebastianbergmann/php-code-coverage" target="_top">PHP_CodeCoverage 3.3.0-3-g9b2a050</a> using <a href="http://php.net/" target="_top">PHP 5.6.19</a> and <a href="https://phpunit.de/">PHPUnit 5.4-g1206dd3</a> at Fri Apr 15 6:43:46 UTC 2016.</small>
<small>Generated by <a href="http://github.com/sebastianbergmann/php-code-coverage" target="_top">php-code-coverage 4.0-dev</a> using <a href="http://php.net/" target="_top">PHP 5.6.19</a> and <a href="https://phpunit.de/">PHPUnit 5.4-ged59ed9</a> at Fri Apr 15 8:06:34 UTC 2016.</small>
</p>
</footer>
</div>
Expand Down Expand Up @@ -175,7 +175,7 @@ <h3>Project Risks</h3>
.yAxis.tickFormat(d3.format('d'));

d3.select('#methodCoverageDistribution svg')
.datum(getCoverageDistributionData([0,0,0,0,0,0,0,0,0,0,0,23], "Method Coverage"))
.datum(getCoverageDistributionData([0,0,0,0,0,0,0,0,0,0,0,24], "Method Coverage"))
.transition().duration(500).call(chart);

nv.utils.windowResize(chart.update);
Expand Down Expand Up @@ -225,7 +225,7 @@ <h3>Project Risks</h3>
chart.yAxis.axisLabel('Cyclomatic Complexity');

d3.select('#classComplexity svg')
.datum(getComplexityData([[100,83,"<a href=\"MentionClient.php.html#4\">MentionClient<\/a>"]], 'Class Complexity'))
.datum(getComplexityData([[100,95,"<a href=\"MentionClient.php.html#4\">MentionClient<\/a>"]], 'Class Complexity'))
.transition()
.duration(500)
.call(chart);
Expand All @@ -249,7 +249,7 @@ <h3>Project Risks</h3>
chart.yAxis.axisLabel('Method Complexity');

d3.select('#methodComplexity svg')
.datum(getComplexityData([[100,1,"<a href=\"MentionClient.php.html#24\">MentionClient::setProxy<\/a>"],[100,6,"<a href=\"MentionClient.php.html#28\">MentionClient::discoverPingbackEndpoint<\/a>"],[100,5,"<a href=\"MentionClient.php.html#63\">MentionClient::sendPingbackToEndpoint<\/a>"],[100,3,"<a href=\"MentionClient.php.html#82\">MentionClient::sendPingback<\/a>"],[100,6,"<a href=\"MentionClient.php.html#98\">MentionClient::_findWebmentionEndpointInHTML<\/a>"],[100,5,"<a href=\"MentionClient.php.html#112\">MentionClient::_findWebmentionEndpointInHeader<\/a>"],[100,9,"<a href=\"MentionClient.php.html#124\">MentionClient::discoverWebmentionEndpoint<\/a>"],[100,1,"<a href=\"MentionClient.php.html#169\">MentionClient::sendWebmentionToEndpoint<\/a>"],[100,3,"<a href=\"MentionClient.php.html#184\">MentionClient::sendWebmention<\/a>"],[100,7,"<a href=\"MentionClient.php.html#200\">MentionClient::findOutgoingLinks<\/a>"],[100,1,"<a href=\"MentionClient.php.html#230\">MentionClient::findLinksInText<\/a>"],[100,4,"<a href=\"MentionClient.php.html#235\">MentionClient::findLinksInJSON<\/a>"],[100,4,"<a href=\"MentionClient.php.html#246\">MentionClient::sendMentions<\/a>"],[100,8,"<a href=\"MentionClient.php.html#270\">MentionClient::sendFirstSupportedMention<\/a>"],[100,1,"<a href=\"MentionClient.php.html#297\">MentionClient::enableDebug<\/a>"],[100,2,"<a href=\"MentionClient.php.html#303\">MentionClient::_debug<\/a>"],[100,2,"<a href=\"MentionClient.php.html#311\">MentionClient::_head<\/a>"],[100,2,"<a href=\"MentionClient.php.html#328\">MentionClient::_get<\/a>"],[100,2,"<a href=\"MentionClient.php.html#346\">MentionClient::_post<\/a>"],[100,5,"<a href=\"MentionClient.php.html#364\">MentionClient::_parse_headers<\/a>"],[100,1,"<a href=\"MentionClient.php.html#373\">MentionClient::anonymous function<\/a>"],[100,2,"<a href=\"MentionClient.php.html#388\">MentionClient::xmlrpc_encode_request<\/a>"],[100,3,"<a href=\"MentionClient.php.html#401\">MentionClient::c<\/a>"]], 'Method Complexity'))
.datum(getComplexityData([[100,1,"<a href=\"MentionClient.php.html#27\">MentionClient::setProxy<\/a>"],[100,9,"<a href=\"MentionClient.php.html#31\">MentionClient::discoverPingbackEndpoint<\/a>"],[100,5,"<a href=\"MentionClient.php.html#77\">MentionClient::sendPingbackToEndpoint<\/a>"],[100,3,"<a href=\"MentionClient.php.html#96\">MentionClient::sendPingback<\/a>"],[100,3,"<a href=\"MentionClient.php.html#112\">MentionClient::_parseBody<\/a>"],[100,6,"<a href=\"MentionClient.php.html#120\">MentionClient::_findWebmentionEndpointInHTML<\/a>"],[100,5,"<a href=\"MentionClient.php.html#135\">MentionClient::_findWebmentionEndpointInHeader<\/a>"],[100,15,"<a href=\"MentionClient.php.html#147\">MentionClient::discoverWebmentionEndpoint<\/a>"],[100,1,"<a href=\"MentionClient.php.html#208\">MentionClient::sendWebmentionToEndpoint<\/a>"],[100,3,"<a href=\"MentionClient.php.html#223\">MentionClient::sendWebmention<\/a>"],[100,7,"<a href=\"MentionClient.php.html#239\">MentionClient::findOutgoingLinks<\/a>"],[100,1,"<a href=\"MentionClient.php.html#269\">MentionClient::findLinksInText<\/a>"],[100,4,"<a href=\"MentionClient.php.html#274\">MentionClient::findLinksInJSON<\/a>"],[100,4,"<a href=\"MentionClient.php.html#285\">MentionClient::sendMentions<\/a>"],[100,8,"<a href=\"MentionClient.php.html#309\">MentionClient::sendFirstSupportedMention<\/a>"],[100,1,"<a href=\"MentionClient.php.html#336\">MentionClient::enableDebug<\/a>"],[100,2,"<a href=\"MentionClient.php.html#342\">MentionClient::_debug<\/a>"],[100,2,"<a href=\"MentionClient.php.html#350\">MentionClient::_head<\/a>"],[100,2,"<a href=\"MentionClient.php.html#367\">MentionClient::_get<\/a>"],[100,2,"<a href=\"MentionClient.php.html#385\">MentionClient::_post<\/a>"],[100,5,"<a href=\"MentionClient.php.html#403\">MentionClient::_parse_headers<\/a>"],[100,1,"<a href=\"MentionClient.php.html#412\">MentionClient::anonymous function<\/a>"],[100,2,"<a href=\"MentionClient.php.html#427\">MentionClient::xmlrpc_encode_request<\/a>"],[100,3,"<a href=\"MentionClient.php.html#440\">MentionClient::c<\/a>"]], 'Method Complexity'))
.transition()
.duration(500)
.call(chart);
Expand Down
10 changes: 5 additions & 5 deletions coverage/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@
</div>
</td>
<td class="success small"><div align="right">100.00%</div></td>
<td class="success small"><div align="right">196&nbsp;/&nbsp;196</div></td>
<td class="success small"><div align="right">225&nbsp;/&nbsp;225</div></td>
<td class="success big"> <div class="progress">
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="100.00" aria-valuemin="0" aria-valuemax="100" style="width: 100.00%">
<span class="sr-only">100.00% covered (success)</span>
</div>
</div>
</td>
<td class="success small"><div align="right">100.00%</div></td>
<td class="success small"><div align="right">17&nbsp;/&nbsp;17</div></td>
<td class="success small"><div align="right">18&nbsp;/&nbsp;18</div></td>
<td class="success big"> <div class="progress">
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="100.00" aria-valuemin="0" aria-valuemax="100" style="width: 100.00%">
<span class="sr-only">100.00% covered (success)</span>
Expand All @@ -77,15 +77,15 @@
</div>
</td>
<td class="success small"><div align="right">100.00%</div></td>
<td class="success small"><div align="right">196&nbsp;/&nbsp;196</div></td>
<td class="success small"><div align="right">225&nbsp;/&nbsp;225</div></td>
<td class="success big"> <div class="progress">
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="100.00" aria-valuemin="0" aria-valuemax="100" style="width: 100.00%">
<span class="sr-only">100.00% covered (success)</span>
</div>
</div>
</td>
<td class="success small"><div align="right">100.00%</div></td>
<td class="success small"><div align="right">17&nbsp;/&nbsp;17</div></td>
<td class="success small"><div align="right">18&nbsp;/&nbsp;18</div></td>
<td class="success big"> <div class="progress">
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="100.00" aria-valuemin="0" aria-valuemax="100" style="width: 100.00%">
<span class="sr-only">100.00% covered (success)</span>
Expand All @@ -108,7 +108,7 @@ <h4>Legend</h4>
<span class="success"><strong>High</strong>: 90% to 100%</span>
</p>
<p>
<small>Generated by <a href="http://github.com/sebastianbergmann/php-code-coverage" target="_top">PHP_CodeCoverage 3.3.0-3-g9b2a050</a> using <a href="http://php.net/" target="_top">PHP 5.6.19</a> and <a href="https://phpunit.de/">PHPUnit 5.4-g1206dd3</a> at Fri Apr 15 6:43:46 UTC 2016.</small>
<small>Generated by <a href="http://github.com/sebastianbergmann/php-code-coverage" target="_top">php-code-coverage 4.0-dev</a> using <a href="http://php.net/" target="_top">PHP 5.6.19</a> and <a href="https://phpunit.de/">PHPUnit 5.4-ged59ed9</a> at Fri Apr 15 8:06:34 UTC 2016.</small>
</p>
</footer>
</div>
Expand Down
59 changes: 49 additions & 10 deletions src/IndieWeb/MentionClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ class MentionClient {

private $_headers = array();
private $_body = array();
private $_rels = array();
private $_supportsPingback = array();
private $_supportsWebmention = array();
private $_pingbackServer = array();
private $_webmentionServer = array();

private static $_proxy = false;

public $usemf2 = true; // for testing, can set this to false to avoid using the Mf2 parser

/**
* @codeCoverageIgnore
*/
Expand Down Expand Up @@ -46,11 +49,22 @@ public function discoverPingbackEndpoint($target) {
if(!$this->c('body', $target)) {
$body = static::_get($target);
$this->c('body', $target, $body['body']);
$this->_parseBody($target, $body['body']);
}
$body = $this->c('body', $target);
if(preg_match("/<link rel=\"pingback\" href=\"([^\"]+)\" ?\/?>/i", $body, $match)) {
$this->c('pingbackServer', $target, $match[1]);
$this->c('supportsPingback', $target, true);
if($rels=$this->c('rels', $target)) {
// If the mf2 parser is present, then rels will have been set, and use that instead
if(count($rels)) {
if(array_key_exists('pingback', $rels)) {
$this->c('pingbackServer', $target, $rels['pingback'][0]);
$this->c('supportsPingback', $target, true);
}
}
} else {
$body = $this->c('body', $target);
if(preg_match("/<link rel=\"pingback\" href=\"([^\"]+)\" ?\/?>/i", $body, $match)) {
$this->c('pingbackServer', $target, $match[1]);
$this->c('supportsPingback', $target, true);
}
}
}

Expand Down Expand Up @@ -95,14 +109,23 @@ public function sendPingback($sourceURL, $targetURL) {
}
}

protected function _parseBody($target, $html) {
if(class_exists('\Mf2\Parser') && $this->usemf2) {
$parser = new \Mf2\Parser($html, $target);
list($rels, $alternates) = $parser->parseRelsAndAlternates();
$this->c('rels', $target, $rels);
}
}

protected function _findWebmentionEndpointInHTML($body, $targetURL=false) {
$endpoint = false;

$body = preg_replace('/<!--(.*)-->/Us', '', $body);
if(preg_match('/<(?:link|a)[ ]+href="([^"]+)"[ ]+rel="[^" ]* ?webmention ?[^" ]*"[ ]*\/?>/i', $body, $match)
|| preg_match('/<(?:link|a)[ ]+rel="[^" ]* ?webmention ?[^" ]*"[ ]+href="([^"]+)"[ ]*\/?>/i', $body, $match)) {
if(preg_match('/<(?:link|a)[ ]+href="([^"]*)"[ ]+rel="[^" ]* ?webmention ?[^" ]*"[ ]*\/?>/i', $body, $match)
|| preg_match('/<(?:link|a)[ ]+rel="[^" ]* ?webmention ?[^" ]*"[ ]+href="([^"]*)"[ ]*\/?>/i', $body, $match)) {
$endpoint = $match[1];
}
if($endpoint && $targetURL && function_exists('\Mf2\resolveUrl')) {
if($endpoint !== false && $targetURL && function_exists('\Mf2\resolveUrl')) {
// Resolve the URL if it's relative
$endpoint = \Mf2\resolveUrl($targetURL, $endpoint);
}
Expand Down Expand Up @@ -153,10 +176,26 @@ public function discoverWebmentionEndpoint($target) {
if(!$this->c('body', $target)) {
$body = static::_get($target);
$this->c('body', $target, $body['body']);
$this->_parseBody($target, $body['body']);
}
if($endpoint=$this->_findWebmentionEndpointInHTML($this->c('body', $target), $target)) {
$this->c('webmentionServer', $target, $endpoint);
$this->c('supportsWebmention', $target, true);
if($rels=$this->c('rels', $target)) {
// If the mf2 parser is present, then rels will have been set, so use that instead
if(count($rels)) {
if(array_key_exists('webmention', $rels)) {
$endpoint = $rels['webmention'][0];
$this->c('webmentionServer', $target, $endpoint);
$this->c('supportsWebmention', $target, true);
} elseif(array_key_exists('http://webmention.org/', $rels) || array_key_exists('http://webmention.org', $rels)) {
$endpoint = $rels[array_key_exists('http://webmention.org/', $rels) ? 'http://webmention.org/' : 'http://webmention.org'][0];
$this->c('webmentionServer', $target, $endpoint);
$this->c('supportsWebmention', $target, true);
}
}
} else {
if($endpoint=$this->_findWebmentionEndpointInHTML($this->c('body', $target), $target)) {
$this->c('webmentionServer', $target, $endpoint);
$this->c('supportsWebmention', $target, true);
}
}
}

Expand Down
20 changes: 20 additions & 0 deletions tests/DiscoverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,26 @@ public function testDiscoverPingbackEndpointInBodyLink() {
$this->assertEquals('http://webmention.example/pingback', $endpoint);
}

public function testDiscoverPingbackEndpointInBodyWithoutMf2() {
$target = 'http://target.example.com/body-link.html';
$this->client->usemf2 = false;
$endpoint = $this->client->discoverPingbackEndpoint($target);
$this->assertEquals('http://webmention.example/pingback', $endpoint);
}

public function testDiscoverWebmentionEndpointInBodyA() {
$target = 'http://target.example.com/body-a.html';
$endpoint = $this->client->discoverWebmentionEndpoint($target);
$this->assertEquals('http://webmention.example/webmention', $endpoint);
}

public function testDiscoverWebmentionEndpointInBodyAWithoutMf2() {
$target = 'http://target.example.com/body-a.html';
$this->client->usemf2 = false;
$endpoint = $this->client->discoverWebmentionEndpoint($target);
$this->assertEquals('http://webmention.example/webmention', $endpoint);
}

public function testShouldNotDiscoverWebmentionEndpointInBodyComment() {
$target = 'http://target.example.com/false-endpoint-in-comment.html';
$endpoint = $this->client->discoverWebmentionEndpoint($target);
Expand All @@ -87,6 +101,12 @@ public function testDiscoverWebmentionEndpointInDocumentOrder2() {
$this->assertEquals('http://webmention.example/a', $endpoint);
}

public function testDiscoverWebmentionEndpointIsEmptyString() {
$target = 'http://target.example.com/empty-string.html';
$endpoint = $this->client->discoverWebmentionEndpoint($target);
$this->assertEquals('http://target.example.com/empty-string.html', $endpoint);
}

public function testDiscoverWebmentionEndpointInWebmentionRocksTest1() {
$target = "http://target.example.com/webmention-rocks-test-1.html";
$endpoint = $this->client->discoverWebmentionEndpoint($target);
Expand Down
2 changes: 1 addition & 1 deletion tests/data/target.example.com/document-order-2.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<html>
<body>
<p>You can send me a <a rel="webmention" href="http://webmention.example/a">webmention</a> or pingback</p>
<link rel="webmention" href="http://webmention.example/link">
<link href="http://webmention.example/link" rel="webmention">
</body>
</html>
14 changes: 14 additions & 0 deletions tests/data/target.example.com/empty-string.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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>
<link rel="webmention" href="">
</head>
<body>
<p>You can send me a webmention</p>
</body>
</html>

0 comments on commit eed3468

Please sign in to comment.