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\Application\OuvrageEdit\Validators; |
11
|
|
|
|
12
|
|
|
use App\Application\InfrastructurePorts\DbAdapterInterface; |
13
|
|
|
use App\Application\WikiPageAction; |
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
|
16
|
|
|
class ArticleValidForEditionValidator implements ValidatorInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $title; |
22
|
|
|
/** |
23
|
|
|
* @var LoggerInterface |
24
|
|
|
*/ |
25
|
|
|
protected $log; |
26
|
|
|
/** |
27
|
|
|
* @var DbAdapterInterface |
28
|
|
|
*/ |
29
|
|
|
protected $db; |
30
|
|
|
/** |
31
|
|
|
* @var WikiPageAction |
32
|
|
|
*/ |
33
|
|
|
protected $wikiPageAction; |
34
|
|
|
|
35
|
|
|
public function __construct(string $title, LoggerInterface $logger, DbAdapterInterface $db, WikiPageAction $wikiPageAction) |
36
|
|
|
{ |
37
|
|
|
$this->title = $title; |
38
|
|
|
$this->log = $logger; |
39
|
|
|
$this->db = $db; |
40
|
|
|
$this->wikiPageAction = $wikiPageAction; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function validate(): bool |
44
|
|
|
{ |
45
|
|
|
if ( |
46
|
|
|
!$this->hasLastRevision() |
47
|
|
|
|| $this->botIsLastEditor() |
48
|
|
|
|| !$this->isInMainNamespace() |
49
|
|
|
|| !$this->hasWikitext() |
50
|
|
|
) { |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return true; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function hasLastRevision(): bool |
58
|
|
|
{ |
59
|
|
|
// Page supprimée ? |
60
|
|
|
if ($this->wikiPageAction->getLastRevision() === null) { |
61
|
|
|
$this->log->warning("SKIP : page supprimée !\n"); |
62
|
|
|
$this->db->deleteArticle($this->title); |
63
|
|
|
|
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function botIsLastEditor(): bool |
71
|
|
|
{ |
72
|
|
|
// HACK |
73
|
|
|
if ($this->wikiPageAction->getLastEditor() === getenv('BOT_NAME')) { |
74
|
|
|
$this->log->notice("SKIP : édité recemment par le bot.\n"); |
75
|
|
|
$this->db->skipArticle($this->title); |
76
|
|
|
|
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function isInMainNamespace(): bool |
84
|
|
|
{ |
85
|
|
|
// todo include a sandbox page ? |
86
|
|
|
if ($this->wikiPageAction->getNs() !== 0) { |
87
|
|
|
$this->log->notice("SKIP : page n'est pas dans Main (ns 0)\n"); |
88
|
|
|
$this->db->skipArticle($this->title); |
89
|
|
|
|
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
return true; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function hasWikitext(): bool |
96
|
|
|
{ |
97
|
|
|
$wikiText = $this->wikiPageAction->getText(); |
98
|
|
|
|
99
|
|
|
if (empty($wikiText)) { |
100
|
|
|
$this->log->warning("SKIP : this->wikitext vide\n"); |
101
|
|
|
$this->db->skipArticle($this->title); |
102
|
|
|
|
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return true; |
107
|
|
|
} |
108
|
|
|
} |