1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of dispositif/wikibot application (@github) |
5
|
|
|
* 2019/2020 © Philippe M. <[email protected]> |
6
|
|
|
* For the full copyright and MIT license information, please view the license file. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace App\Application; |
11
|
|
|
|
12
|
|
|
use Codedungeon\PHPCliColors\Color; |
13
|
|
|
use Throwable; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Ref2ArticleWorker |
17
|
|
|
* |
18
|
|
|
* @package App\Application\Examples |
19
|
|
|
*/ |
20
|
|
|
class ExternRefWorker extends RefBotWorker |
21
|
|
|
{ |
22
|
|
|
const TASK_BOT_FLAG = false; |
23
|
|
|
const SLEEP_AFTER_EDITION = 10; // sec |
24
|
|
|
const DELAY_AFTER_LAST_HUMAN_EDIT = 15; // minutes |
25
|
|
|
const CHECK_EDIT_CONFLICT = true; |
26
|
|
|
const ARTICLE_ANALYZED_FILENAME = __DIR__.'/resources/article_externRef_edited.txt'; |
27
|
|
|
|
28
|
|
|
protected $titleBotFlag = false; |
29
|
|
|
protected $modeAuto = true; |
30
|
|
|
/** |
31
|
|
|
* @var ExternRefTransformer |
32
|
|
|
*/ |
33
|
|
|
protected $transformer; |
34
|
|
|
|
35
|
|
|
protected function setUpInConstructor(): void |
36
|
|
|
{ |
37
|
|
|
$transformer = new ExternRefTransformer($this->log); |
38
|
|
|
$transformer->skipUnauthorised = false; |
39
|
|
|
|
40
|
|
|
$this->transformer = $transformer; |
41
|
|
|
//todo? move in __constructor + parent::__constructor() |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// todo private (refac constructor->run()) |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Traite contenu d'une <ref> ou bien lien externe (précédé d'une puce). |
48
|
|
|
* |
49
|
|
|
* @param $refContent |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function processRefContent($refContent): string |
54
|
|
|
{ |
55
|
|
|
// todo // hack Temporary Skip URL |
56
|
|
|
if (preg_match('#books\.google#', $refContent)) { |
57
|
|
|
return $refContent; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
try { |
61
|
|
|
$result = $this->transformer->process($refContent); |
62
|
|
|
} catch (Throwable $e) { |
63
|
|
|
echo "** Problème détecté 234242\n"; |
64
|
|
|
$this->log->critical($e->getMessage()." ".$e->getFile().":".$e->getLine()); |
65
|
|
|
|
66
|
|
|
// TODO : parse $e->message pour traitement, taskName, botflag... |
67
|
|
|
return $refContent; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($result === $refContent) { |
71
|
|
|
return $refContent; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Gestion semi-auto |
75
|
|
|
if (!$this->transformer->skipUnauthorised) { |
76
|
|
|
echo Color::BG_LIGHT_RED."--".Color::NORMAL." ".$refContent."\n"; |
77
|
|
|
echo Color::BG_LIGHT_GREEN."++".Color::NORMAL." $result \n\n"; |
78
|
|
|
|
79
|
|
|
if (!$this->modeAuto) { |
80
|
|
|
$ask = readline(Color::LIGHT_MAGENTA."*** Conserver cette modif ? [y/n/auto]".Color::NORMAL); |
81
|
|
|
if ($ask === 'auto') { |
82
|
|
|
$this->modeAuto = true; |
83
|
|
|
} |
84
|
|
|
if ($ask !== 'y' && $ask !== 'auto') { |
85
|
|
|
return $refContent; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
if (preg_match('#{{lien brisé#i', $result)) { |
90
|
|
|
$this->titleTaskname .= ', ⚠️️lien brisé'; |
91
|
|
|
$this->titleBotFlag = false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $result; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|