diff --git a/src/NodaWikidataFetcher.php b/src/NodaWikidataFetcher.php index 89f9258..e5f2803 100644 --- a/src/NodaWikidataFetcher.php +++ b/src/NodaWikidataFetcher.php @@ -82,6 +82,54 @@ final class NodaWikidataFetcher { /** @var MDMysqli */ private MDMysqli $_mysqli_noda; + /** + * Attempts to fetch a Wikidata ID from a provided URL. + * + * @param string $linkUrl Link to a page. + * + * @return string + */ + public static function getWikidataIdFromLink(string $linkUrl):string { + + if (!filter_var($linkUrl, FILTER_VALIDATE_URL)) { + throw new MDExpectedException("Invalid URL"); + } + + if (strpos($linkUrl, "https://www.wikidata.org/wiki/") !== false) { + if ($output = self::getWikidataIdFromWikidataLink($linkUrl)) { + return $output; + } + } + + if (strpos($linkUrl, ".wikipedia.org/") !== false) { + if ($output = self::getWikidataIdFromWikipedia($linkUrl)) { + return $output; + } + } + + return ''; + + } + + /** + * Attempts to fetch a Wikidata ID from a provided URL. + * + * @param string $linkUrl Link to a Wikidata page. + * + * @return string + */ + public static function getWikidataIdFromWikidataLink(string $linkUrl):string { + + if (strpos($linkUrl, "https://www.wikidata.org/wiki/") === false) { + return ''; + } + + $linkUrl = trim($linkUrl, '/ '); + $parts = explode('/', $linkUrl); + return end($parts); + + } + /** * Attempts to fetch a Wikidata ID from a provided URL. * @@ -91,8 +139,8 @@ final class NodaWikidataFetcher { */ public static function getWikidataIdFromWikipedia(string $linkUrl):string { - if (!filter_var($linkUrl, FILTER_VALIDATE_URL)) { - throw new MDExpectedException("Invalid URL"); + if (strpos($linkUrl, ".wikipedia.org/") === false) { + return ''; } $wikipedia_cont = MD_STD::runCurl($linkUrl); diff --git a/tests/NodaWikidataFetcherTest.php b/tests/NodaWikidataFetcherTest.php new file mode 100644 index 0000000..0694f99 --- /dev/null +++ b/tests/NodaWikidataFetcherTest.php @@ -0,0 +1,59 @@ + + */ +declare(strict_types = 1); +use PHPUnit\Framework\TestCase; +require_once __DIR__ . "/../src/NodaWikidataFetcher.php"; +require_once __DIR__ . "/../../MDErrorReporter/exceptions/generic/MDExpectedException.php"; +require_once __DIR__ . "/../../MD_STD/src/MD_STD.php"; + +/** + * Tests for home page. + */ +final class NodaWikidataFetcherTest extends TestCase { + /** + * Test to check whether the HTML page is correctly generated. + * + * @author Joshua Ramon Enslin + * @group ValidOutput + * + * @return void + */ + public function testWikidataIdFromLink():void { + + self::assertEquals(NodaWikidataFetcher::getWikidataIdFromLink("https://www.wikidata.org/wiki/Q106697"), 'Q106697'); + self::assertEquals(NodaWikidataFetcher::getWikidataIdFromLink("https://de.wikipedia.org/wiki/Fedor_Jagor"), 'Q106697'); + + } + + /** + * Test to check whether the HTML page is correctly generated. + * + * @author Joshua Ramon Enslin + * @group ValidOutput + * + * @return void + */ + public function testWikidataIdFromWikipediaRequiresWikipediaLink():void { + + self::assertEquals(NodaWikidataFetcher::getWikidataIdFromWikipedia("https://en.about.museum-digital.org/"), ''); + + } + + /** + * Test to check whether the HTML page is correctly generated. + * + * @author Joshua Ramon Enslin + * @group ValidOutput + * + * @return void + */ + public function testCanGetWikidataIdByWikidataId():void { + + self::assertEquals(NodaWikidataFetcher::getWikidataIdFromWikidataLink("https://www.wikidata.org/wiki/Q106697"), "Q106697"); + + } +}