|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of dispositif/wikibot application (@github) |
|
4
|
|
|
* 2019/2020 © 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\Domain; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
use App\Domain\Models\Wiki\AbstractWikiTemplate; |
|
15
|
|
|
use App\Domain\Utils\TextUtil; |
|
16
|
|
|
use Exception; |
|
17
|
|
|
use Psr\Log\LoggerInterface; |
|
18
|
|
|
use Psr\Log\NullLogger; |
|
19
|
|
|
|
|
20
|
|
|
abstract class AbstractTemplateOptimizer implements TemplateOptimizerInterface |
|
21
|
|
|
{ |
|
22
|
|
|
use SummaryLogTrait; |
|
23
|
|
|
|
|
24
|
|
|
protected $originTemplate; |
|
25
|
|
|
|
|
26
|
|
|
protected $wikiPageTitle; |
|
27
|
|
|
|
|
28
|
|
|
protected $optiTemplate; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var LoggerInterface|NullLogger |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $log; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* todo Refac add doTasks() in constructor if no optional method needed between doTasks() and getOptiTemplate() |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct( |
|
39
|
|
|
AbstractWikiTemplate $template, |
|
40
|
|
|
?string $wikiPageTitle = null, |
|
41
|
|
|
?LoggerInterface $log = null |
|
42
|
|
|
) { |
|
43
|
|
|
$this->originTemplate = $template; |
|
44
|
|
|
$this->optiTemplate = clone $template; |
|
45
|
|
|
$this->wikiPageTitle = ($wikiPageTitle) ?? null; |
|
46
|
|
|
$this->log = $log ?? new NullLogger(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function getOptiTemplate(): AbstractWikiTemplate |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->optiTemplate; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* TODO : return "" instead of null ? |
|
56
|
|
|
* |
|
57
|
|
|
* @param $name |
|
58
|
|
|
* |
|
59
|
|
|
* @return string|null |
|
60
|
|
|
* @throws Exception |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function getParam(string $name): ?string |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->optiTemplate->getParam($name); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
protected function hasParamValue(string $name): bool |
|
68
|
|
|
{ |
|
69
|
|
|
return !empty($this->optiTemplate->getParam($name)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function unsetParam($name) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->optiTemplate->unsetParam($name); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param $name |
|
79
|
|
|
* @param $value |
|
80
|
|
|
* |
|
81
|
|
|
* @throws Exception |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function setParam($name, $value) |
|
84
|
|
|
{ |
|
85
|
|
|
// todo : overwrite setParam() ? |
|
86
|
|
|
if (!empty($value) || $this->optiTemplate->getParam($name)) { |
|
87
|
|
|
$this->optiTemplate->setParam($name, $value); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Correction des parametres rejetés à l'hydratation données. |
|
93
|
|
|
* |
|
94
|
|
|
* @throws Exception |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function cleanAndPredictErrorParameters() |
|
97
|
|
|
{ |
|
98
|
|
|
if (empty($this->optiTemplate->parametersErrorFromHydrate)) { |
|
99
|
|
|
return; |
|
100
|
|
|
} |
|
101
|
|
|
$allParamsAndAlias = $this->optiTemplate->getParamsAndAlias(); |
|
102
|
|
|
|
|
103
|
|
|
foreach ($this->optiTemplate->parametersErrorFromHydrate as $name => $value) { |
|
104
|
|
|
if (!is_string($name)) { |
|
105
|
|
|
// example : 1 => "ouvrage collectif" from |ouvrage collectif| |
|
106
|
|
|
continue; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// delete error parameter if no value |
|
110
|
|
|
if (empty($value)) { |
|
111
|
|
|
unset($this->optiTemplate->parametersErrorFromHydrate[$name]); |
|
112
|
|
|
|
|
113
|
|
|
continue; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$maxDistance = 1; |
|
117
|
|
|
if (mb_strlen($name) >= 4) { |
|
118
|
|
|
$maxDistance = 2; |
|
119
|
|
|
} |
|
120
|
|
|
if (mb_strlen($name) >= 8) { |
|
121
|
|
|
$maxDistance = 3; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$predName = TextUtil::predictCorrectParam($name, $allParamsAndAlias, $maxDistance); |
|
125
|
|
|
if ($predName && mb_strlen($name) >= 5) { |
|
126
|
|
|
if (empty($this->getParam($predName))) { |
|
127
|
|
|
$predName = $this->optiTemplate->getAliasParam($predName); |
|
128
|
|
|
$this->setParam($predName, $value); |
|
129
|
|
|
$this->addSummaryLog(sprintf('%s⇒%s ?', $name, $predName)); |
|
130
|
|
|
$this->notCosmetic = true; |
|
|
|
|
|
|
131
|
|
|
unset($this->optiTemplate->parametersErrorFromHydrate[$name]); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|