1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of dispositif/wikibot application |
4
|
|
|
* 2019 © Philippe M. <[email protected]> |
5
|
|
|
* For the full copyright and MIT license information, please 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 Exception; |
16
|
|
|
|
17
|
|
|
abstract class RefBotWorker extends AbstractBotTaskWorker |
18
|
|
|
{ |
19
|
|
|
const TASK_BOT_FLAG = false; |
20
|
|
|
|
21
|
|
|
protected $warning = false; |
22
|
|
|
protected $botFlagOnPage; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @inheritDoc |
26
|
|
|
* @throws Exception |
27
|
|
|
*/ |
28
|
|
|
protected function processDomain(string $title, ?string $text): ?string |
29
|
|
|
{ |
30
|
|
|
$this->taskName = static::TASK_NAME; |
31
|
|
|
$this->botFlagOnPage = static::TASK_BOT_FLAG; |
32
|
|
|
|
33
|
|
|
return $this->processText($text); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param $text |
38
|
|
|
* |
39
|
|
|
* @return string|string[] |
40
|
|
|
* @throws Exception |
41
|
|
|
*/ |
42
|
|
|
public function processText($text) |
43
|
|
|
{ |
44
|
|
|
$refs = WikiTextUtil::extractAllRefs($text); |
45
|
|
|
if (empty($refs)) { |
46
|
|
|
return $text; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
foreach ($refs as $ref) { |
50
|
|
|
$refContent = WikiTextUtil::stripFinalPoint(trim($ref[1])); |
51
|
|
|
|
52
|
|
|
$newRefContent = $this->processRefContent($refContent); |
|
|
|
|
53
|
|
|
|
54
|
|
|
$text = $this->replaceRefInText($ref, $newRefContent, $text); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $text; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function replaceRefInText(array $ref, string $replace, string $text) |
61
|
|
|
{ |
62
|
|
|
if (WikiTextUtil::stripFinalPoint(trim($replace)) === WikiTextUtil::stripFinalPoint(trim($ref[1]))) { |
63
|
|
|
// echo Color::BG_LIGHT_GRAY."xx".Color::NORMAL." ".$ref[1]."\n"; |
64
|
|
|
return $text; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$replace .= '.'; // ending point |
68
|
|
|
$result = str_replace($ref[1], $replace, $ref[0]); |
69
|
|
|
$this->log->debug(Color::BG_LIGHT_RED."--".Color::NORMAL." ".$ref[0]."\n"); |
70
|
|
|
$this->log->debug(Color::BG_LIGHT_GREEN."++".Color::NORMAL." $result \n\n"); |
71
|
|
|
|
72
|
|
|
return str_replace($ref[0], $result, $text); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function hasWarning(): bool |
76
|
|
|
{ |
77
|
|
|
return (bool)$this->warning; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|