From ea280fc1449f487a8361cf1d13f43994742725b0 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Mon, 29 Nov 2021 22:31:17 +0100 Subject: [PATCH] Add non-empty-string phpdoc types to NodaWikidataFetcher --- src/NodaWikidataFetcher.php | 42 ++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/src/NodaWikidataFetcher.php b/src/NodaWikidataFetcher.php index 6500fbf..63a84d8 100644 --- a/src/NodaWikidataFetcher.php +++ b/src/NodaWikidataFetcher.php @@ -90,7 +90,7 @@ final class NodaWikidataFetcher { /** * Attempts to fetch a Wikidata ID from a provided URL. * - * @param string $linkUrl Link to a page. + * @param non-empty-string $linkUrl Link to a page. * * @return string */ @@ -138,7 +138,7 @@ final class NodaWikidataFetcher { /** * Attempts to fetch a Wikidata ID from a provided URL. * - * @param string $linkUrl Link to a Wikipedia page. + * @param non-empty-string $linkUrl Link to a Wikipedia page. * * @return string */ @@ -169,15 +169,15 @@ final class NodaWikidataFetcher { } $t_wikibase = (string)$t_wikibase_href; - if (!empty($t_wikibase)) { - if (($wikidata_id_end = strrpos($t_wikibase, '/')) !== false) { - $wikidata_id = trim(substr($t_wikibase, $wikidata_id_end + 1), '/'); + // if (!empty($t_wikibase)) { + if (($wikidata_id_end = strrpos($t_wikibase, '/')) !== false) { + $wikidata_id = trim(substr($t_wikibase, $wikidata_id_end + 1), '/'); - if (substr($wikidata_id, 0, 1) === 'Q') { - return $wikidata_id; - } + if (substr($wikidata_id, 0, 1) === 'Q') { + return $wikidata_id; } } + // } return ''; @@ -279,7 +279,7 @@ final class NodaWikidataFetcher { * @param array $checkagainstLanguage The language to check against. * @param array $data Data fetched from Wikidata. * - * @return array> + * @return array{0: array, 1: array} */ public static function getWikidataWikipediaTranslationSources(array $checkagainstLanguage, array $data) { @@ -477,12 +477,11 @@ final class NodaWikidataFetcher { // Cut off overly long articles if (mb_strlen($input) > 600) { if (strpos($input, PHP_EOL . PHP_EOL, 600) !== false) { - $input = substr($input, 0, strpos($input, PHP_EOL . PHP_EOL, 600)); + $input = trim(substr($input, 0, strpos($input, PHP_EOL . PHP_EOL, 600))); } } - // Trim again to make really, really no superfluous whitespaces remain - $input = trim($input); + if (empty($input)) return ''; $input = str_replace("'", "ยด", MD_STD::preg_replace_str("/\&\#91\;[0-9]\&\#93\;/", '', $input)); @@ -814,13 +813,14 @@ final class NodaWikidataFetcher { * * @return void */ - public function getWikidataTranslationsForPersinst(array $data, int $persinst_id) { + public function getWikidataTranslationsForPersinst(array $data, int $persinst_id):void { $checkagainstLanguage = self::LANGUAGES_TO_CHECK; - $insertStmt = $this->_mysqli_noda->do_prepare("CALL nodaInsertPersinstTranslation(?, ?, ?, ?, ?)"); - list($languagesToFetch, $wikilinks) = self::getWikidataWikipediaTranslationSources($checkagainstLanguage, $data); + if (empty($languagesToFetch)) { + return; + } try { $contents = MD_STD::runCurlMulti($languagesToFetch, 10000); @@ -829,6 +829,8 @@ final class NodaWikidataFetcher { throw new MDExpectedException("Failed to initialize a request. Try pressing F5 to run the requests again."); } + $insertStmt = $this->_mysqli_noda->do_prepare("CALL nodaInsertPersinstTranslation(?, ?, ?, ?, ?)"); + $this->_mysqli_noda->autocommit(false); foreach ($checkagainstLanguage as $lang) { @@ -1220,6 +1222,9 @@ final class NodaWikidataFetcher { $checkagainstLanguage = self::LANGUAGES_TO_CHECK; list($languagesToFetch, $wikilinks) = self::getWikidataWikipediaTranslationSources($checkagainstLanguage, $data); + if (empty($languagesToFetch)) { + return; + } try { $contents = MD_STD::runCurlMulti($languagesToFetch, 10000); @@ -1542,9 +1547,10 @@ final class NodaWikidataFetcher { $checkagainstLanguage = self::LANGUAGES_TO_CHECK; - $insertStmt = $this->_mysqli_noda->do_prepare("CALL nodaInsertTagTranslation(?, ?, ?, ?, ?)"); - list($languagesToFetch, $wikilinks) = self::getWikidataWikipediaTranslationSources($checkagainstLanguage, $data); + if (empty($languagesToFetch)) { + return; + } try { $contents = MD_STD::runCurlMulti($languagesToFetch, 10000); @@ -1553,6 +1559,8 @@ final class NodaWikidataFetcher { throw new MDExpectedException("Failed to initialize a request. Try pressing F5 to run the requests again."); } + $insertStmt = $this->_mysqli_noda->do_prepare("CALL nodaInsertTagTranslation(?, ?, ?, ?, ?)"); + $this->_mysqli_noda->autocommit(false); foreach ($checkagainstLanguage as $lang) {