Allow using Wikidata links for fetching information for actors

This commit is contained in:
Joshua Ramon Enslin 2021-05-05 01:26:32 +02:00
parent 6dcdb3aff6
commit 041d3598eb
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE
2 changed files with 109 additions and 2 deletions

View File

@ -82,6 +82,54 @@ final class NodaWikidataFetcher {
/** @var MDMysqli */ /** @var MDMysqli */
private MDMysqli $_mysqli_noda; 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. * Attempts to fetch a Wikidata ID from a provided URL.
* *
@ -91,8 +139,8 @@ final class NodaWikidataFetcher {
*/ */
public static function getWikidataIdFromWikipedia(string $linkUrl):string { public static function getWikidataIdFromWikipedia(string $linkUrl):string {
if (!filter_var($linkUrl, FILTER_VALIDATE_URL)) { if (strpos($linkUrl, ".wikipedia.org/") === false) {
throw new MDExpectedException("Invalid URL"); return '';
} }
$wikipedia_cont = MD_STD::runCurl($linkUrl); $wikipedia_cont = MD_STD::runCurl($linkUrl);

View File

@ -0,0 +1,59 @@
<?PHP
/**
* This script contains tests for the home page.
*
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
*/
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 <joshua@museum-digital.de>
* @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 <joshua@museum-digital.de>
* @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 <joshua@museum-digital.de>
* @group ValidOutput
*
* @return void
*/
public function testCanGetWikidataIdByWikidataId():void {
self::assertEquals(NodaWikidataFetcher::getWikidataIdFromWikidataLink("https://www.wikidata.org/wiki/Q106697"), "Q106697");
}
}