From a68a03e628864ec42e503b9a859a5f7b32c43342 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Thu, 31 Aug 2023 16:09:21 +0200 Subject: [PATCH] Improve wikidata fetcher --- src/NodaWikidataFetcher.php | 48 +++++++++++++++++++------------ tests/NodaWikidataFetcherTest.php | 3 -- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/NodaWikidataFetcher.php b/src/NodaWikidataFetcher.php index d90179e..f647107 100644 --- a/src/NodaWikidataFetcher.php +++ b/src/NodaWikidataFetcher.php @@ -134,7 +134,7 @@ final class NodaWikidataFetcher { $doc->loadXML('
' . trim($input) . '
'); } catch (Exception $e) { - throw new Exception("Failed to load DOMDocument." . PHP_EOL . $e->getMessage() . PHP_EOL . PHP_EOL . $input); + throw new Exception("Failed to load DOMDocument." . PHP_EOL . $e->getMessage() . PHP_EOL . PHP_EOL . '---' . $input . '---'); } $list = $doc->getElementsByTagName("style"); @@ -672,7 +672,6 @@ final class NodaWikidataFetcher { ]; } - # print_r($descs); } @@ -1007,7 +1006,7 @@ final class NodaWikidataFetcher { } /** - * Gets the current description of a place. + * Returns the current description of a place. * * @param integer $onum Place ID. * @@ -1029,6 +1028,29 @@ final class NodaWikidataFetcher { } + /** + * Returns the current description of a tag. + * + * @param integer $tag_id Tag ID. + * + * @return string + */ + private function getTagDescription(int $tag_id):string { + + $result = $this->_mysqli_noda->query_by_stmt("SELECT `tag_anmerkung` + FROM `tag` + WHERE `tag_id` = ?", "i", $tag_id); + + if (!($cur = $result->fetch_row())) { + $result->close(); + return ''; + } + $result->close(); + + return $cur[0]; + + } + /** * Function for entering base information about a place from wikidata. * @@ -1239,14 +1261,14 @@ final class NodaWikidataFetcher { } if (isset($data['claims']['P625'])) { - $latitude_wd = $data['claims']['P625'][0]['mainsnak']['datavalue']['value']['latitude']; - $longitude_wd = $data['claims']['P625'][0]['mainsnak']['datavalue']['value']['longitude']; + $latitude_wd = \filter_var($data['claims']['P625'][0]['mainsnak']['datavalue']['value']['latitude'], FILTER_VALIDATE_FLOAT); + $longitude_wd = \filter_var($data['claims']['P625'][0]['mainsnak']['datavalue']['value']['longitude'], FILTER_VALIDATE_FLOAT); } NodaBatchInserter::linkNodaForPlace($this->_mysqli_noda, $onum, $nodaLinks, $erfasst_von); $this->_mysqli_noda->autocommit(false); - if (!empty($tgn_id)) { + if (!empty($tgn_id) and is_numeric($tgn_id)) { $updateStmt = $this->_mysqli_noda->do_prepare("UPDATE `orte` SET `ort_land` = ? @@ -1257,7 +1279,7 @@ final class NodaWikidataFetcher { unset($updateStmt); } - if (!empty($geonames_id)) { + if (!empty($geonames_id) and is_numeric($geonames_id)) { $updateStmt = $this->_mysqli_noda->do_prepare("UPDATE `orte` SET `ort_geonames` = ? @@ -1342,17 +1364,7 @@ final class NodaWikidataFetcher { $datafromwiki = '"' . $datafromwiki . '" - (Wikipedia (' . $lang . ') ' . date("d.m.Y") . ')'; $datafromwiki = str_replace("'", "ยด", MD_STD::preg_replace_str("/\&\#91\;[0-9]\&\#93\;/", '', $datafromwiki)); - $cergebnis = $this->_mysqli_noda->query_by_stmt("SELECT `tag_anmerkung` - FROM `tag` - WHERE `tag_id` = ?", "i", $tag_id); - - if (!($cinfo = $cergebnis->fetch_row())) { - $cergebnis->close(); - return $output; - } - $cergebnis->close(); - - $tag_anmerkung = $cinfo[0]; + $tag_anmerkung = $this->getTagDescription($tag_id); $this->_mysqli_noda->autocommit(false); diff --git a/tests/NodaWikidataFetcherTest.php b/tests/NodaWikidataFetcherTest.php index 20c75b8..df6184f 100644 --- a/tests/NodaWikidataFetcherTest.php +++ b/tests/NodaWikidataFetcherTest.php @@ -255,9 +255,6 @@ Transclusion expansion time report (%,ms,calls,template) "Start of parsed Wikipedia text should be:" . PHP_EOL . PHP_EOL . $expected . PHP_EOL . PHP_EOL . 'Real start text is: ' . PHP_EOL . PHP_EOL . substr($output, 0, 250) ); - - - } /**