Passed
Branch master (309757)
by Dispositif
03:20 queued 54s
created

AbstractRefBotWorker::processText()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 21
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
/*
3
 * This file is part of dispositif/wikibot application (@github)
4
 * 2019-2023 © Philippe M./Irønie  <[email protected]>
5
 * For the full copyright and MIT license information, view the license file.
6
 */
7
8
declare(strict_types=1);
9
10
11
namespace App\Application;
12
13
14
use App\Domain\Utils\WikiTextUtil;
15
use Codedungeon\PHPCliColors\Color;
16
use Exception;
17
18
abstract class AbstractRefBotWorker extends AbstractBotTaskWorker
19
{
20
    public const TASK_BOT_FLAG = false;
21
    public const MAX_REFS_PROCESSED_IN_ARTICLE = 30;
22
23
    protected $warning = false;
24
25
    public function hasWarning(): bool
26
    {
27
        return (bool)$this->warning;
28
    }
29
30
    /**
31
     * @inheritDoc
32
     * @throws Exception
33
     */
34
    protected function processWithDomainWorker(string $title, string $text): ?string
35
    {
36
        return $this->processText($text);
37
    }
38
39
    /**
40
     * @param $text
41
     *
42
     * @return string|string[]
43
     * @throws Exception
44
     */
45
    public function processText($text)
46
    {
47
        $refs = WikiTextUtil::extractRefsAndListOfLinks($text);
48
        if ($refs === []) {
49
            $this->log->debug('empty extractRefsAndListOfLinks');
50
51
            return $text;
52
        }
53
54
        // Avoid memory leak problem : bot limited to N refs in an article
55
        $refs = array_slice($refs, 0, self::MAX_REFS_PROCESSED_IN_ARTICLE, true);
56
57
        foreach ($refs as $ref) {
58
            $refContent = WikiTextUtil::stripFinalPoint(trim($ref[1]));
59
60
            $newRefContent = $this->processRefContent($refContent);
61
62
            $text = $this->replaceRefInText($ref, $newRefContent, $text);
63
        }
64
65
        return $text;
66
    }
67
68
    public abstract function processRefContent($refContent): string;
69
70
    protected function replaceRefInText(array $ref, string $replace, string $text)
71
    {
72
        // Pas de changement
73
        if (WikiTextUtil::stripFinalPoint(trim($replace)) === WikiTextUtil::stripFinalPoint(trim($ref[1]))) {
74
            return $text;
75
        }
76
        $replace = $this->addFinalPeriod($ref[0], $replace);
77
        $result = str_replace($ref[1], $replace, $ref[0]);
78
        $this->printDiff($ref[0], $result);
79
80
        return str_replace($ref[0], $result, $text);
81
    }
82
83
    /**
84
     * Add a final period '.' before an eventual '</ref>'
85
     */
86
    protected function addFinalPeriod($ref, string $replace): string
87
    {
88
        if (preg_match('#</ref>#', $ref)) {
89
            $replace .= '.';
90
        }
91
        return $replace;
92
    }
93
94
    protected function printDiff(string $before, string $after, string $level = 'debug'): void
95
    {
96
        $this->log->log($level, sprintf("%s--%s %s\n", Color::BG_LIGHT_RED, Color::NORMAL, $before));
97
        $this->log->log($level, sprintf("%s--%s %s\n", Color::BG_LIGHT_GREEN, Color::NORMAL, $after));
98
    }
99
}
100