Skip to content

Commit 7c206ea

Browse files
authored
Fix: Release/5.3.x Support relative links (#422)
* Update Explorer.php (Fix relative URLs in links) * Update Feed.php (Fix relative URLs in links) * Update Node.php (Fix relative URLs in links) * Update XmlParser.php (Fix relative URLs in links) * Update Reader.php (Fix relative URLs in links) * Update Link.php (Fix relative URLs in links)
1 parent d8dcd77 commit 7c206ea

File tree

6 files changed

+42
-4
lines changed

6 files changed

+42
-4
lines changed

src/FeedIo/Explorer.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ public function discover(string $url): array
2929
$stream = $this->client->getResponse($url, new DateTime('@0'));
3030

3131
$internalErrors = libxml_use_internal_errors(true);
32-
$feeds = $this->extractFeeds($stream->getBody());
32+
$feeds = $this->extractFeeds($stream->getBody(), $url);
3333

3434
libxml_use_internal_errors($internalErrors);
3535

3636
return $feeds;
3737
}
3838

39-
protected function extractFeeds(string $html): array
39+
protected function extractFeeds(string $html, string $url = null): array
4040
{
4141
$dom = new DOMDocument();
4242
$dom->loadHTML($html);
@@ -53,6 +53,9 @@ protected function extractFeeds(string $html): array
5353
// returning
5454
$href = 'https:' . $href;
5555
}
56+
if (!parse_url($href, PHP_URL_HOST) && $url){
57+
$href = parse_url($url, PHP_URL_SCHEME) . '://' . parse_url($url, PHP_URL_HOST) . '/' . ltrim($href,'/');
58+
}
5659
$feeds[] = $href;
5760
}
5861
}

src/FeedIo/Feed.php

+2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ public function rewind(): void
134134

135135
public function add(ItemInterface $item): FeedInterface
136136
{
137+
$item->setHostInContent($this->getHostFromLink());
138+
137139
if ($item->getLastModified() > $this->getLastModified()) {
138140
$this->setLastModified($item->getLastModified());
139141
}

src/FeedIo/Feed/Node.php

+27
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,33 @@ protected function setHost(string $link = null): void
150150
}
151151
}
152152

153+
protected function setHostInContent(string $host = null): void
154+
{
155+
if (property_exists($this, 'content')){
156+
if (!is_null($host) && !is_null($this->content)) {
157+
$this->content = preg_replace('!(<*\s*[^>]*)(href=)(.?)(\/[^\/])!','\1 href=\3'.$host.'\4', $this->content );
158+
$this->content = preg_replace('!(<*\s*[^>]*)(src=)(.?)(\/[^\/])!','\1 src=\3'.$host.'\4', $this->content );
159+
}
160+
}
161+
if (property_exists($this, 'description')){
162+
if (!is_null($host) && !is_null($this->description)) {
163+
$this->description = preg_replace('!(<*\s*[^>]*)(href=)(.?)(\/[^\/])!','\1 href=\3'.$host.'\4', $this->description );
164+
$this->description = preg_replace('!(<*\s*[^>]*)(src=)(.?)(\/[^\/])!','\1 src=\3'.$host.'\4', $this->description );
165+
}
166+
}
167+
}
168+
169+
public function getHostFromLink(): ?string
170+
{
171+
if (!is_null($this->getLink())) {
172+
$partsUrl = parse_url($this->getLink());
173+
$result = $partsUrl['scheme']."://".$partsUrl['host'];
174+
} else
175+
$result = null;
176+
177+
return $result;
178+
}
179+
153180
public function getValue(string $name): ?string
154181
{
155182
foreach ($this->getElementIterator($name) as $element) {

src/FeedIo/Parser/XmlParser.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ public function parseNode(NodeInterface $item, DOMElement $element, RuleSet $rul
7676
protected function handleNode(NodeInterface $item, DOMElement $node, RuleSet $ruleSet): void
7777
{
7878
if ($this->isItem($node->tagName) && $item instanceof FeedInterface) {
79-
$newItem = $this->parseNode($item->newItem(), $node, $this->getItemRuleSet());
79+
$linkItem = $item->getLink();
80+
$newItem = $this->parseNode($item->newItem()->setLink($linkItem), $node, $this->getItemRuleSet());
8081
$this->addValidItem($item, $newItem);
8182
} else {
8283
$rule = $ruleSet->get($node->tagName);

src/FeedIo/Reader.php

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public function read(string $url, FeedInterface $feed, DateTime $modifiedSince =
7373
try {
7474
$this->logger->info("hitting {$url}");
7575
$response = $this->client->getResponse($url, $modifiedSince);
76+
$feed->setLink($url);
7677
$document = $this->handleResponse($response, $feed);
7778

7879
return new Result($document, $feed, $modifiedSince, $response, $url);

src/FeedIo/Rule/Link.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ class Link extends RuleAbstract
1717
*/
1818
public function setProperty(NodeInterface $node, \DOMElement $element): void
1919
{
20-
$node->setLink($element->nodeValue);
20+
$nodeValue = $element->nodeValue;
21+
if (parse_url($nodeValue, PHP_URL_HOST) == null) {
22+
$nodeValue = $node->getHostFromLink(). $nodeValue;
23+
}
24+
$node->setLink($nodeValue);
2125
}
2226

2327
/**

0 commit comments

Comments
 (0)