Skip to content

Commit 12b3b2d

Browse files
committed
fixed url parsing. related: #112
1 parent f682a34 commit 12b3b2d

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

src/Providers/Api/Wikipedia.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function getDescription()
6868
if ($pages) {
6969
$page = current($pages);
7070

71-
return strip_tags($page['extract']);
71+
return isset($page['extract']) ? strip_tags($page['extract']) : null;
7272
}
7373
}
7474

@@ -86,14 +86,16 @@ public function getImagesUrls()
8686

8787
$imgs = [];
8888

89-
foreach ($page['images'] as $image) {
90-
switch (strrchr($image['title'], '.')) {
91-
case '.png':
92-
case '.jpg':
93-
case '.gif':
94-
case '.jpeg':
95-
$imgs[] = $image['title'];
96-
break;
89+
if (isset($page['images'])) {
90+
foreach ($page['images'] as $image) {
91+
switch (strrchr($image['title'], '.')) {
92+
case '.png':
93+
case '.jpg':
94+
case '.gif':
95+
case '.jpeg':
96+
$imgs[] = $image['title'];
97+
break;
98+
}
9799
}
98100
}
99101

src/Url.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,5 +536,14 @@ private function setPath($path)
536536
$this->info['file'] = isset($parts['filename']) ? $parts['filename'] : null;
537537
$this->info['extension'] = isset($parts['extension']) ? $parts['extension'] : null;
538538
$this->info['content'] = null;
539+
540+
// bugfix /wiki/Supernatural_(U.S._TV_series) is parsed as:
541+
// path: /wiki/
542+
// file: Supernatural_(U.S
543+
// extension: _TV_series)
544+
if (!empty($this->info['extension']) && !preg_match('/^\w+$/', $this->info['extension'])) {
545+
$this->info['file'] .= '.'.$this->info['extension'];
546+
$this->info['extension'] = null;
547+
}
539548
}
540549
}

tests/UrlTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,12 @@ public function testDomain()
5353
$this->assertSame('domain', $url->getDomain());
5454
$this->assertSame('domain.com.au', $url->getDomain(1));
5555
}
56+
57+
public function testPathsWithDots()
58+
{
59+
$url = new Embed\Url('https://en.wikipedia.org/wiki/Supernatural_(U.S._TV_series)');
60+
$this->assertNull($url->getExtension());
61+
$this->assertSame('/wiki/Supernatural_(U.S._TV_series)', $url->getPath());
62+
$this->assertSame('Supernatural_(U.S._TV_series)', $url->getDirectoryPosition(1));
63+
}
5664
}

0 commit comments

Comments
 (0)