Add non-empty-string phpdoc types to NodaWikidataFetcher

This commit is contained in:
Joshua Ramon Enslin 2021-11-29 22:31:17 +01:00
parent 0ab6f5e608
commit ea280fc144
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE

View File

@ -90,7 +90,7 @@ final class NodaWikidataFetcher {
/** /**
* Attempts to fetch a Wikidata ID from a provided URL. * 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 * @return string
*/ */
@ -138,7 +138,7 @@ final class NodaWikidataFetcher {
/** /**
* Attempts to fetch a Wikidata ID from a provided URL. * 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 * @return string
*/ */
@ -169,7 +169,7 @@ final class NodaWikidataFetcher {
} }
$t_wikibase = (string)$t_wikibase_href; $t_wikibase = (string)$t_wikibase_href;
if (!empty($t_wikibase)) { // if (!empty($t_wikibase)) {
if (($wikidata_id_end = strrpos($t_wikibase, '/')) !== false) { if (($wikidata_id_end = strrpos($t_wikibase, '/')) !== false) {
$wikidata_id = trim(substr($t_wikibase, $wikidata_id_end + 1), '/'); $wikidata_id = trim(substr($t_wikibase, $wikidata_id_end + 1), '/');
@ -177,7 +177,7 @@ final class NodaWikidataFetcher {
return $wikidata_id; return $wikidata_id;
} }
} }
} // }
return ''; return '';
@ -279,7 +279,7 @@ final class NodaWikidataFetcher {
* @param array<string> $checkagainstLanguage The language to check against. * @param array<string> $checkagainstLanguage The language to check against.
* @param array<mixed> $data Data fetched from Wikidata. * @param array<mixed> $data Data fetched from Wikidata.
* *
* @return array<array<string>> * @return array{0: array<string, non-empty-string>, 1: array<string, string>}
*/ */
public static function getWikidataWikipediaTranslationSources(array $checkagainstLanguage, array $data) { public static function getWikidataWikipediaTranslationSources(array $checkagainstLanguage, array $data) {
@ -477,12 +477,11 @@ final class NodaWikidataFetcher {
// Cut off overly long articles // Cut off overly long articles
if (mb_strlen($input) > 600) { if (mb_strlen($input) > 600) {
if (strpos($input, PHP_EOL . PHP_EOL, 600) !== false) { 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 if (empty($input)) return '';
$input = trim($input);
$input = str_replace("'", "´", MD_STD::preg_replace_str("/\&\#91\;[0-9]\&\#93\;/", '', $input)); $input = str_replace("'", "´", MD_STD::preg_replace_str("/\&\#91\;[0-9]\&\#93\;/", '', $input));
@ -814,13 +813,14 @@ final class NodaWikidataFetcher {
* *
* @return void * @return void
*/ */
public function getWikidataTranslationsForPersinst(array $data, int $persinst_id) { public function getWikidataTranslationsForPersinst(array $data, int $persinst_id):void {
$checkagainstLanguage = self::LANGUAGES_TO_CHECK; $checkagainstLanguage = self::LANGUAGES_TO_CHECK;
$insertStmt = $this->_mysqli_noda->do_prepare("CALL nodaInsertPersinstTranslation(?, ?, ?, ?, ?)");
list($languagesToFetch, $wikilinks) = self::getWikidataWikipediaTranslationSources($checkagainstLanguage, $data); list($languagesToFetch, $wikilinks) = self::getWikidataWikipediaTranslationSources($checkagainstLanguage, $data);
if (empty($languagesToFetch)) {
return;
}
try { try {
$contents = MD_STD::runCurlMulti($languagesToFetch, 10000); $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."); 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); $this->_mysqli_noda->autocommit(false);
foreach ($checkagainstLanguage as $lang) { foreach ($checkagainstLanguage as $lang) {
@ -1220,6 +1222,9 @@ final class NodaWikidataFetcher {
$checkagainstLanguage = self::LANGUAGES_TO_CHECK; $checkagainstLanguage = self::LANGUAGES_TO_CHECK;
list($languagesToFetch, $wikilinks) = self::getWikidataWikipediaTranslationSources($checkagainstLanguage, $data); list($languagesToFetch, $wikilinks) = self::getWikidataWikipediaTranslationSources($checkagainstLanguage, $data);
if (empty($languagesToFetch)) {
return;
}
try { try {
$contents = MD_STD::runCurlMulti($languagesToFetch, 10000); $contents = MD_STD::runCurlMulti($languagesToFetch, 10000);
@ -1542,9 +1547,10 @@ final class NodaWikidataFetcher {
$checkagainstLanguage = self::LANGUAGES_TO_CHECK; $checkagainstLanguage = self::LANGUAGES_TO_CHECK;
$insertStmt = $this->_mysqli_noda->do_prepare("CALL nodaInsertTagTranslation(?, ?, ?, ?, ?)");
list($languagesToFetch, $wikilinks) = self::getWikidataWikipediaTranslationSources($checkagainstLanguage, $data); list($languagesToFetch, $wikilinks) = self::getWikidataWikipediaTranslationSources($checkagainstLanguage, $data);
if (empty($languagesToFetch)) {
return;
}
try { try {
$contents = MD_STD::runCurlMulti($languagesToFetch, 10000); $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."); 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); $this->_mysqli_noda->autocommit(false);
foreach ($checkagainstLanguage as $lang) { foreach ($checkagainstLanguage as $lang) {