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 RefBotWorker extends AbstractBotTaskWorker |
19
|
|
|
{ |
20
|
|
|
public const TASK_BOT_FLAG = false; |
21
|
|
|
|
22
|
|
|
protected $warning = false; |
23
|
|
|
|
24
|
|
|
public function hasWarning(): bool |
25
|
|
|
{ |
26
|
|
|
return (bool)$this->warning; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritDoc |
31
|
|
|
* @throws Exception |
32
|
|
|
*/ |
33
|
|
|
protected function processWithDomainWorker(string $title, string $text): ?string |
34
|
|
|
{ |
35
|
|
|
return $this->processText($text); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param $text |
40
|
|
|
* |
41
|
|
|
* @return string|string[] |
42
|
|
|
* @throws Exception |
43
|
|
|
*/ |
44
|
|
|
public function processText($text) |
45
|
|
|
{ |
46
|
|
|
$refs = WikiTextUtil::extractRefsAndListOfLinks($text); |
47
|
|
|
if ($refs === []) { |
48
|
|
|
$this->log->debug('empty extractRefsAndListOfLinks'); |
49
|
|
|
|
50
|
|
|
return $text; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
foreach ($refs as $ref) { |
54
|
|
|
$refContent = WikiTextUtil::stripFinalPoint(trim($ref[1])); |
55
|
|
|
|
56
|
|
|
$newRefContent = $this->processRefContent($refContent); |
57
|
|
|
|
58
|
|
|
$text = $this->replaceRefInText($ref, $newRefContent, $text); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $text; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public abstract function processRefContent($refContent): string; |
65
|
|
|
|
66
|
|
|
protected function replaceRefInText(array $ref, string $replace, string $text) |
67
|
|
|
{ |
68
|
|
|
// Pas de changement |
69
|
|
|
if (WikiTextUtil::stripFinalPoint(trim($replace)) === WikiTextUtil::stripFinalPoint(trim($ref[1]))) { |
70
|
|
|
return $text; |
71
|
|
|
} |
72
|
|
|
// Ajout point final si </ref> détecté |
73
|
|
|
if (preg_match('#</ref>#', $ref[0])) { |
74
|
|
|
$replace .= '.'; // ending point |
75
|
|
|
} |
76
|
|
|
$result = str_replace($ref[1], $replace, $ref[0]); |
77
|
|
|
$this->log->debug(Color::BG_LIGHT_RED."--".Color::NORMAL." ".$ref[0]."\n"); |
78
|
|
|
$this->log->debug(Color::BG_LIGHT_GREEN."++".Color::NORMAL." $result \n\n"); |
79
|
|
|
|
80
|
|
|
return str_replace($ref[0], $result, $text); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|