Skip to content

Commit eed3468

Browse files
committed
Merge pull request #26 from aaronpk/master
use Mf2 library to parse rels
2 parents b9fbe8f + 54aa383 commit eed3468

File tree

8 files changed

+576
-443
lines changed

8 files changed

+576
-443
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"minimum-stability": "dev",
2020
"require": {
2121
"php": ">=5.4",
22-
"mf2/mf2": "~0.2"
22+
"mf2/mf2": ">=0.2"
2323
},
2424
"require-dev": {
2525
"phpunit/phpunit": ">=4.8.13"

coverage/MentionClient.php.html

Lines changed: 482 additions & 422 deletions
Large diffs are not rendered by default.

coverage/dashboard.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ <h3>Project Risks</h3>
136136
<footer>
137137
<hr/>
138138
<p>
139-
<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>
139+
<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>
140140
</p>
141141
</footer>
142142
</div>
@@ -175,7 +175,7 @@ <h3>Project Risks</h3>
175175
.yAxis.tickFormat(d3.format('d'));
176176

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

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

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

251251
d3.select('#methodComplexity svg')
252-
.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'))
252+
.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'))
253253
.transition()
254254
.duration(500)
255255
.call(chart);

coverage/index.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@
4949
</div>
5050
</td>
5151
<td class="success small"><div align="right">100.00%</div></td>
52-
<td class="success small"><div align="right">196&nbsp;/&nbsp;196</div></td>
52+
<td class="success small"><div align="right">225&nbsp;/&nbsp;225</div></td>
5353
<td class="success big"> <div class="progress">
5454
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="100.00" aria-valuemin="0" aria-valuemax="100" style="width: 100.00%">
5555
<span class="sr-only">100.00% covered (success)</span>
5656
</div>
5757
</div>
5858
</td>
5959
<td class="success small"><div align="right">100.00%</div></td>
60-
<td class="success small"><div align="right">17&nbsp;/&nbsp;17</div></td>
60+
<td class="success small"><div align="right">18&nbsp;/&nbsp;18</div></td>
6161
<td class="success big"> <div class="progress">
6262
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="100.00" aria-valuemin="0" aria-valuemax="100" style="width: 100.00%">
6363
<span class="sr-only">100.00% covered (success)</span>
@@ -77,15 +77,15 @@
7777
</div>
7878
</td>
7979
<td class="success small"><div align="right">100.00%</div></td>
80-
<td class="success small"><div align="right">196&nbsp;/&nbsp;196</div></td>
80+
<td class="success small"><div align="right">225&nbsp;/&nbsp;225</div></td>
8181
<td class="success big"> <div class="progress">
8282
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="100.00" aria-valuemin="0" aria-valuemax="100" style="width: 100.00%">
8383
<span class="sr-only">100.00% covered (success)</span>
8484
</div>
8585
</div>
8686
</td>
8787
<td class="success small"><div align="right">100.00%</div></td>
88-
<td class="success small"><div align="right">17&nbsp;/&nbsp;17</div></td>
88+
<td class="success small"><div align="right">18&nbsp;/&nbsp;18</div></td>
8989
<td class="success big"> <div class="progress">
9090
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="100.00" aria-valuemin="0" aria-valuemax="100" style="width: 100.00%">
9191
<span class="sr-only">100.00% covered (success)</span>
@@ -108,7 +108,7 @@ <h4>Legend</h4>
108108
<span class="success"><strong>High</strong>: 90% to 100%</span>
109109
</p>
110110
<p>
111-
<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>
111+
<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>
112112
</p>
113113
</footer>
114114
</div>

src/IndieWeb/MentionClient.php

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ class MentionClient {
1111

1212
private $_headers = array();
1313
private $_body = array();
14+
private $_rels = array();
1415
private $_supportsPingback = array();
1516
private $_supportsWebmention = array();
1617
private $_pingbackServer = array();
1718
private $_webmentionServer = array();
1819

1920
private static $_proxy = false;
2021

22+
public $usemf2 = true; // for testing, can set this to false to avoid using the Mf2 parser
23+
2124
/**
2225
* @codeCoverageIgnore
2326
*/
@@ -46,11 +49,22 @@ public function discoverPingbackEndpoint($target) {
4649
if(!$this->c('body', $target)) {
4750
$body = static::_get($target);
4851
$this->c('body', $target, $body['body']);
52+
$this->_parseBody($target, $body['body']);
4953
}
50-
$body = $this->c('body', $target);
51-
if(preg_match("/<link rel=\"pingback\" href=\"([^\"]+)\" ?\/?>/i", $body, $match)) {
52-
$this->c('pingbackServer', $target, $match[1]);
53-
$this->c('supportsPingback', $target, true);
54+
if($rels=$this->c('rels', $target)) {
55+
// If the mf2 parser is present, then rels will have been set, and use that instead
56+
if(count($rels)) {
57+
if(array_key_exists('pingback', $rels)) {
58+
$this->c('pingbackServer', $target, $rels['pingback'][0]);
59+
$this->c('supportsPingback', $target, true);
60+
}
61+
}
62+
} else {
63+
$body = $this->c('body', $target);
64+
if(preg_match("/<link rel=\"pingback\" href=\"([^\"]+)\" ?\/?>/i", $body, $match)) {
65+
$this->c('pingbackServer', $target, $match[1]);
66+
$this->c('supportsPingback', $target, true);
67+
}
5468
}
5569
}
5670

@@ -95,14 +109,23 @@ public function sendPingback($sourceURL, $targetURL) {
95109
}
96110
}
97111

112+
protected function _parseBody($target, $html) {
113+
if(class_exists('\Mf2\Parser') && $this->usemf2) {
114+
$parser = new \Mf2\Parser($html, $target);
115+
list($rels, $alternates) = $parser->parseRelsAndAlternates();
116+
$this->c('rels', $target, $rels);
117+
}
118+
}
119+
98120
protected function _findWebmentionEndpointInHTML($body, $targetURL=false) {
99121
$endpoint = false;
122+
100123
$body = preg_replace('/<!--(.*)-->/Us', '', $body);
101-
if(preg_match('/<(?:link|a)[ ]+href="([^"]+)"[ ]+rel="[^" ]* ?webmention ?[^" ]*"[ ]*\/?>/i', $body, $match)
102-
|| preg_match('/<(?:link|a)[ ]+rel="[^" ]* ?webmention ?[^" ]*"[ ]+href="([^"]+)"[ ]*\/?>/i', $body, $match)) {
124+
if(preg_match('/<(?:link|a)[ ]+href="([^"]*)"[ ]+rel="[^" ]* ?webmention ?[^" ]*"[ ]*\/?>/i', $body, $match)
125+
|| preg_match('/<(?:link|a)[ ]+rel="[^" ]* ?webmention ?[^" ]*"[ ]+href="([^"]*)"[ ]*\/?>/i', $body, $match)) {
103126
$endpoint = $match[1];
104127
}
105-
if($endpoint && $targetURL && function_exists('\Mf2\resolveUrl')) {
128+
if($endpoint !== false && $targetURL && function_exists('\Mf2\resolveUrl')) {
106129
// Resolve the URL if it's relative
107130
$endpoint = \Mf2\resolveUrl($targetURL, $endpoint);
108131
}
@@ -153,10 +176,26 @@ public function discoverWebmentionEndpoint($target) {
153176
if(!$this->c('body', $target)) {
154177
$body = static::_get($target);
155178
$this->c('body', $target, $body['body']);
179+
$this->_parseBody($target, $body['body']);
156180
}
157-
if($endpoint=$this->_findWebmentionEndpointInHTML($this->c('body', $target), $target)) {
158-
$this->c('webmentionServer', $target, $endpoint);
159-
$this->c('supportsWebmention', $target, true);
181+
if($rels=$this->c('rels', $target)) {
182+
// If the mf2 parser is present, then rels will have been set, so use that instead
183+
if(count($rels)) {
184+
if(array_key_exists('webmention', $rels)) {
185+
$endpoint = $rels['webmention'][0];
186+
$this->c('webmentionServer', $target, $endpoint);
187+
$this->c('supportsWebmention', $target, true);
188+
} elseif(array_key_exists('http://webmention.org/', $rels) || array_key_exists('http://webmention.org', $rels)) {
189+
$endpoint = $rels[array_key_exists('http://webmention.org/', $rels) ? 'http://webmention.org/' : 'http://webmention.org'][0];
190+
$this->c('webmentionServer', $target, $endpoint);
191+
$this->c('supportsWebmention', $target, true);
192+
}
193+
}
194+
} else {
195+
if($endpoint=$this->_findWebmentionEndpointInHTML($this->c('body', $target), $target)) {
196+
$this->c('webmentionServer', $target, $endpoint);
197+
$this->c('supportsWebmention', $target, true);
198+
}
160199
}
161200
}
162201

tests/DiscoverTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,26 @@ public function testDiscoverPingbackEndpointInBodyLink() {
6363
$this->assertEquals('http://webmention.example/pingback', $endpoint);
6464
}
6565

66+
public function testDiscoverPingbackEndpointInBodyWithoutMf2() {
67+
$target = 'http://target.example.com/body-link.html';
68+
$this->client->usemf2 = false;
69+
$endpoint = $this->client->discoverPingbackEndpoint($target);
70+
$this->assertEquals('http://webmention.example/pingback', $endpoint);
71+
}
72+
6673
public function testDiscoverWebmentionEndpointInBodyA() {
6774
$target = 'http://target.example.com/body-a.html';
6875
$endpoint = $this->client->discoverWebmentionEndpoint($target);
6976
$this->assertEquals('http://webmention.example/webmention', $endpoint);
7077
}
7178

79+
public function testDiscoverWebmentionEndpointInBodyAWithoutMf2() {
80+
$target = 'http://target.example.com/body-a.html';
81+
$this->client->usemf2 = false;
82+
$endpoint = $this->client->discoverWebmentionEndpoint($target);
83+
$this->assertEquals('http://webmention.example/webmention', $endpoint);
84+
}
85+
7286
public function testShouldNotDiscoverWebmentionEndpointInBodyComment() {
7387
$target = 'http://target.example.com/false-endpoint-in-comment.html';
7488
$endpoint = $this->client->discoverWebmentionEndpoint($target);
@@ -87,6 +101,12 @@ public function testDiscoverWebmentionEndpointInDocumentOrder2() {
87101
$this->assertEquals('http://webmention.example/a', $endpoint);
88102
}
89103

104+
public function testDiscoverWebmentionEndpointIsEmptyString() {
105+
$target = 'http://target.example.com/empty-string.html';
106+
$endpoint = $this->client->discoverWebmentionEndpoint($target);
107+
$this->assertEquals('http://target.example.com/empty-string.html', $endpoint);
108+
}
109+
90110
public function testDiscoverWebmentionEndpointInWebmentionRocksTest1() {
91111
$target = "http://target.example.com/webmention-rocks-test-1.html";
92112
$endpoint = $this->client->discoverWebmentionEndpoint($target);

tests/data/target.example.com/document-order-2.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
<html>
88
<body>
99
<p>You can send me a <a rel="webmention" href="http://webmention.example/a">webmention</a> or pingback</p>
10-
<link rel="webmention" href="http://webmention.example/link">
10+
<link href="http://webmention.example/link" rel="webmention">
1111
</body>
1212
</html>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
HTTP/1.1 200 OK
2+
Server: Apache
3+
Date: Wed, 09 Dec 2015 03:29:14 GMT
4+
Content-Type: text/html; charset=utf-8
5+
Connection: keep-alive
6+
7+
<html>
8+
<head>
9+
<link rel="webmention" href="">
10+
</head>
11+
<body>
12+
<p>You can send me a webmention</p>
13+
</body>
14+
</html>

0 commit comments

Comments
 (0)