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
|
|
|
namespace App\Domain\WikiOptimizer\Handlers; |
11
|
|
|
|
12
|
|
|
use App\Domain\Models\Wiki\WikiTemplateInterface; |
13
|
|
|
use App\Domain\Utils\TextUtil; |
14
|
|
|
use App\Domain\WikiOptimizer\OptiStatus; |
15
|
|
|
|
16
|
|
|
class PredictErrorParameterHandler implements OptimizeHandlerInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var WikiTemplateInterface |
20
|
|
|
*/ |
21
|
|
|
protected $template; |
22
|
|
|
/** |
23
|
|
|
* @var OptiStatus |
24
|
|
|
*/ |
25
|
|
|
protected $optiStatus; |
26
|
|
|
|
27
|
|
|
public function __construct(WikiTemplateInterface $template, OptiStatus $summary) |
28
|
|
|
{ |
29
|
|
|
$this->template = $template; |
30
|
|
|
$this->optiStatus = $summary; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
// Correction des parametres rejetés à l'hydratation données. |
34
|
|
|
public function handle() |
35
|
|
|
{ |
36
|
|
|
if (empty($this->template->parametersErrorFromHydrate)) { |
|
|
|
|
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
$allParamsAndAlias = $this->template->getParamsAndAlias(); |
|
|
|
|
40
|
|
|
|
41
|
|
|
foreach ($this->template->parametersErrorFromHydrate as $name => $value) { |
42
|
|
|
if (!is_string($name)) { |
43
|
|
|
// example : 1 => "ouvrage collectif" from |ouvrage collectif| |
44
|
|
|
continue; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// delete error parameter if no value |
48
|
|
|
if (empty($value)) { |
49
|
|
|
unset($this->template->parametersErrorFromHydrate[$name]); |
50
|
|
|
|
51
|
|
|
continue; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$maxDistance = 1; |
55
|
|
|
if (mb_strlen($name) >= 4) { |
56
|
|
|
$maxDistance = 2; |
57
|
|
|
} |
58
|
|
|
if (mb_strlen($name) >= 8) { |
59
|
|
|
$maxDistance = 3; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$predName = TextUtil::predictCorrectParam($name, $allParamsAndAlias, $maxDistance); |
63
|
|
|
if ($predName && mb_strlen($name) >= 5 && empty($this->template->getParam($predName))) { |
64
|
|
|
$predName = $this->template->getAliasParam($predName); |
|
|
|
|
65
|
|
|
$this->template->setParam($predName, $value); |
66
|
|
|
$this->optiStatus->addSummaryLog(sprintf('%s⇒%s ?', $name, $predName)); |
67
|
|
|
$this->optiStatus->setNotCosmetic(true); |
68
|
|
|
unset($this->template->parametersErrorFromHydrate[$name]); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |