Commit 418bb4e1 authored by Vadim's avatar Vadim

Fixed errors in abstract class TyposClientInterface

parent ea9e8580
...@@ -138,14 +138,16 @@ abstract class TyposClientInterface ...@@ -138,14 +138,16 @@ abstract class TyposClientInterface
private function replaceTypoInArticle(string $typo, string $corrected, string $context, TyposArticle $article) { private function replaceTypoInArticle(string $typo, string $corrected, string $context, TyposArticle $article) {
$lastException = null; $lastException = null;
$isAlreadyFixed = false;
$isContextNotFound = false; $isContextNotFound = false;
/*
// Replace -- to - in context string // Replace -- to - in context string
// BUG# 12799 // BUG# 12799
$context = str_replace("\xe2\x80\x94", "-", $context); $context = str_replace("\xe2\x80\x94", "-", $context);
// BUG# 12799 Need to change quotes and preg quote // BUG# 12799 Need to change quotes and preg quote
$context = preg_quote(preg_replace('#«([^«]*)»#', '"$1"', $context)); $context = preg_quote(preg_replace('#«([^«]*)»#', '"$1"', $context));
*/
// Trying to replace typo in text // Trying to replace typo in text
try { try {
...@@ -155,10 +157,12 @@ abstract class TyposClientInterface ...@@ -155,10 +157,12 @@ abstract class TyposClientInterface
} catch (\Exception $e) { } catch (\Exception $e) {
error_log($e->getMessage()); error_log($e->getMessage());
if ($e->getCode() != 404 && $e->getCode() != 405) { // If a corrected of the typo is found in the text, remember this and,
throw $e; // 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 // If context was not found in text then remember this
// and after all search if we not found a typo, then throw // and after all search if we not found a typo, then throw
// 405 exception and not 404 // 405 exception and not 404
...@@ -174,11 +178,9 @@ abstract class TyposClientInterface ...@@ -174,11 +178,9 @@ abstract class TyposClientInterface
return; return;
} catch (\Exception $e) { } catch (\Exception $e) {
error_log($e->getMessage()); error_log($e->getMessage());
if ($e->getCode() == 208) {
if ($e->getCode() != 404 && $e->getCode() != 405) { $isAlreadyFixed = true;
throw $e;
} }
if ($e->getCode() == 405) { if ($e->getCode() == 405) {
$isContextNotFound = true; $isContextNotFound = true;
} }
...@@ -189,6 +191,9 @@ abstract class TyposClientInterface ...@@ -189,6 +191,9 @@ abstract class TyposClientInterface
error_log("Trying to find a typo in article subtitle..."); error_log("Trying to find a typo in article subtitle...");
$article->subtitle = $this->replaceTypoInText($typo, $corrected, $context, $article->subtitle); $article->subtitle = $this->replaceTypoInText($typo, $corrected, $context, $article->subtitle);
} catch (\Exception $e) { } catch (\Exception $e) {
if (($e->getCode() == 404 || $e->getCode() == 405) && $isAlreadyFixed) {
throw new \Exception("Already fixed", 208);
}
if ($e->getCode() == 404 && $isContextNotFound) { if ($e->getCode() == 404 && $isContextNotFound) {
throw new \Exception("Context not found", 405); throw new \Exception("Context not found", 405);
} }
...@@ -209,27 +214,26 @@ abstract class TyposClientInterface ...@@ -209,27 +214,26 @@ abstract class TyposClientInterface
* *
* @return string Text with typo replaced by corrected * @return string Text with typo replaced by corrected
* @throws \Exception If something goes wrong * @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 // Strip all tags from text
// Also copy input string // Also copy input string
$originalText = $text; $originalText = $text;
$text = strip_tags($text); $text = strip_tags($text);
/*
// BUG# 13121 // BUG# 13121
$typo = str_replace("\xc2\xa0", " ", $typo); $typo = str_replace("\xc2\xa0", " ", $typo);
$corrected = str_replace("\xc2\xa0", " ", $corrected); $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 // Find all typos in text, capture an offset of each typo
$typos = []; if (!preg_match_all("#{$typo}#", $text, $typos, PREG_OFFSET_CAPTURE)) {
preg_match("#{$typo}#", $text, $typos, PREG_OFFSET_CAPTURE);
if (count($typos) == 0) {
// Check for already fixed typo // Check for already fixed typo
preg_match("#{$corrected}#", $text, $typos, PREG_OFFSET_CAPTURE); if (preg_match("#{$corrected}#", $text, $typos, PREG_OFFSET_CAPTURE)) {
if (count($typos) != 0) {
throw new \Exception("Already fixed", 208); throw new \Exception("Already fixed", 208);
} }
...@@ -237,22 +241,20 @@ abstract class TyposClientInterface ...@@ -237,22 +241,20 @@ abstract class TyposClientInterface
} }
// Find a context in text, capture it offset // Find a context in text, capture it offset
$contextMatch = [];
preg_match("#{$context}#", $text, $contextMatch, PREG_OFFSET_CAPTURE);
if (!preg_match("#{$context}#", $text, $contextMatch, PREG_OFFSET_CAPTURE)) {
// If a context was changed then report an error, // If a context was changed then report an error,
// cannot locate typo in a new context, must be // cannot locate typo in a new context, must be
// fixed manually // fixed manually
if (count($contextMatch) == 0) {
throw new \Exception("Context not found", 405); throw new \Exception("Context not found", 405);
} }
$contextMatch = $contextMatch[0]; $contextOffset = $contextMatch[0][1];
$contextOffset = $contextMatch[1];
// Find a concrete typo that we want to fix // Find a concrete typo that we want to fix
$indexOfTypo = null; $indexOfTypo = null;
foreach ($typos as $index => $match) { foreach ($typos[0] as $index => $match) {
$typoOffset = $match[1]; $typoOffset = $match[1];
if ($typoOffset >= $contextOffset) { if ($typoOffset >= $contextOffset) {
$indexOfTypo = $index; $indexOfTypo = $index;
......
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