Commit 418bb4e1 authored by Vadim's avatar Vadim

Fixed errors in abstract class TyposClientInterface

parent ea9e8580
......@@ -138,14 +138,16 @@ abstract class TyposClientInterface
private function replaceTypoInArticle(string $typo, string $corrected, string $context, TyposArticle $article) {
$lastException = null;
$isAlreadyFixed = false;
$isContextNotFound = false;
/*
// Replace -- to - in context string
// BUG# 12799
$context = str_replace("\xe2\x80\x94", "-", $context);
// BUG# 12799 Need to change quotes and preg quote
$context = preg_quote(preg_replace('#«([^«]*)»#', '"$1"', $context));
*/
// Trying to replace typo in text
try {
......@@ -154,11 +156,13 @@ abstract class TyposClientInterface
return;
} catch (\Exception $e) {
error_log($e->getMessage());
if ($e->getCode() != 404 && $e->getCode() != 405) {
throw $e;
// If a corrected of the typo is found in the text, remember this and,
// if in other parts (title and subtitle) the typo or context is not found,
// then we believe that the typo has been already corrected
if ($e->getCode() == 208) {
$isAlreadyFixed = true;
}
// If context was not found in text then remember this
// and after all search if we not found a typo, then throw
// 405 exception and not 404
......@@ -174,11 +178,9 @@ abstract class TyposClientInterface
return;
} catch (\Exception $e) {
error_log($e->getMessage());
if ($e->getCode() != 404 && $e->getCode() != 405) {
throw $e;
if ($e->getCode() == 208) {
$isAlreadyFixed = true;
}
if ($e->getCode() == 405) {
$isContextNotFound = true;
}
......@@ -189,6 +191,9 @@ abstract class TyposClientInterface
error_log("Trying to find a typo in article subtitle...");
$article->subtitle = $this->replaceTypoInText($typo, $corrected, $context, $article->subtitle);
} catch (\Exception $e) {
if (($e->getCode() == 404 || $e->getCode() == 405) && $isAlreadyFixed) {
throw new \Exception("Already fixed", 208);
}
if ($e->getCode() == 404 && $isContextNotFound) {
throw new \Exception("Context not found", 405);
}
......@@ -209,27 +214,26 @@ abstract class TyposClientInterface
*
* @return string Text with typo replaced by corrected
* @throws \Exception If something goes wrong
"Already fixed", 208
"Typo not found", 404
"Context not found", 405
*/
private function replaceTypoInText(string $typo, string $corrected, string $context, $text) {
private function replaceTypoInText(string $typo, string $corrected, string $context, string $text) {
// Strip all tags from text
// Also copy input string
$originalText = $text;
$text = strip_tags($text);
/*
// BUG# 13121
$typo = str_replace("\xc2\xa0", " ", $typo);
$corrected = str_replace("\xc2\xa0", " ", $corrected);
$context = preg_replace("\xc2\xa0", "", $context);
$context = str_replace("\xc2\xa0", " ", $context);
$text = str_replace("\xc2\xa0", " ", $text);
*/
// Find all typos in text, capture an offset of each typo
$typos = [];
preg_match("#{$typo}#", $text, $typos, PREG_OFFSET_CAPTURE);
if (count($typos) == 0) {
if (!preg_match_all("#{$typo}#", $text, $typos, PREG_OFFSET_CAPTURE)) {
// Check for already fixed typo
preg_match("#{$corrected}#", $text, $typos, PREG_OFFSET_CAPTURE);
if (count($typos) != 0) {
if (preg_match("#{$corrected}#", $text, $typos, PREG_OFFSET_CAPTURE)) {
throw new \Exception("Already fixed", 208);
}
......@@ -237,22 +241,20 @@ abstract class TyposClientInterface
}
// Find a context in text, capture it offset
$contextMatch = [];
preg_match("#{$context}#", $text, $contextMatch, PREG_OFFSET_CAPTURE);
// If a context was changed then report an error,
// cannot locate typo in a new context, must be
// fixed manually
if (count($contextMatch) == 0) {
throw new \Exception("Context not found", 405);
if (!preg_match("#{$context}#", $text, $contextMatch, PREG_OFFSET_CAPTURE)) {
// If a context was changed then report an error,
// cannot locate typo in a new context, must be
// fixed manually
throw new \Exception("Context not found", 405);
}
$contextMatch = $contextMatch[0];
$contextOffset = $contextMatch[1];
$contextOffset = $contextMatch[0][1];
// Find a concrete typo that we want to fix
$indexOfTypo = null;
foreach ($typos as $index => $match) {
foreach ($typos[0] as $index => $match) {
$typoOffset = $match[1];
if ($typoOffset >= $contextOffset) {
$indexOfTypo = $index;
......@@ -274,4 +276,4 @@ abstract class TyposClientInterface
$originalText);
}
}
\ No newline at end of file
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment