Passed
Branch dev (6bb8f6)
by Dispositif
03:01
created

ExternRefWorker::processRefContent()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 17
nc 6
nop 1
dl 0
loc 32
rs 9.0777
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of dispositif/wikibot application
5
 * 2019 © 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\Examples;
11
12
use App\Application\Color;
13
use App\Application\ExternRefTransformer;
14
use App\Application\RefBotWorker;
15
use App\Application\WikiBotConfig;
16
use App\Infrastructure\CirrusSearch;
17
use App\Infrastructure\ServiceFactory;
18
use Throwable;
19
20
include __DIR__.'/../ZiziBot_Bootstrap.php';
21
// VOIR EN BAS
22
23
/**
24
 * TODO move PROCESS FROM BOTTOM
25
 * Class Ref2ArticleWorker
26
 *
27
 * @package App\Application\Examples
28
 */
29
class ExternRefWorker extends RefBotWorker
30
{
31
    const TASK_NAME           = "Amélioration références : URL ⇒ {modèle}"; // 😎
32
    const TASK_BOT_FLAG       = false;
33
    const SLEEP_AFTER_EDITION = 30;
34
35
    protected $botFlag = false;
36
    protected $modeAuto = false;
37
    /**
38
     * @var ExternRefTransformer
39
     */
40
    protected $transformer;
41
42
    /**
43
     * ExternalRefWorker constructor.
44
     */
45
    protected function setUpInConstructor(): void
46
    {
47
        $transformer = new ExternRefTransformer($this->log);
48
        $transformer->skipUnauthorised = false;
49
50
        $this->transformer = $transformer;
51
        //todo? move in __constructor + parent::__constructor()
52
    }
53
54
    // todo private (refac constructor->run())
55
    public function processRefContent($refContent): string
56
    {
57
        // todo Temporary Skip URL
58
        if (preg_match('#books\.google#', $refContent)) {
59
            return $refContent;
60
        }
61
62
        try {
63
            $result = $this->transformer->process($refContent);
64
        } catch (Throwable $e) {
65
            echo "** Problème détecté 234242\n";
66
            $this->log->critical($e->getMessage()." ".$e->getFile().":".$e->getLine());
67
68
            // TODO : parse $e->message pour traitement, taskName, botflag...
69
            return $refContent;
70
        }
71
72
        if ($result === $refContent) {
73
            return $refContent;
74
        }
75
76
        // Gestion semi-auto
77
        if (!$this->transformer->skipUnauthorised) {
78
            echo Color::BG_LIGHT_RED."--".Color::NORMAL." ".$refContent."\n";
79
            echo Color::BG_LIGHT_GREEN."++".Color::NORMAL." $result \n\n";
80
            $ask = readline(Color::LIGHT_MAGENTA."*** Conserver cette modif ? [y/n]".Color::NORMAL);
81
            if ($ask !== 'y') {
82
                return $refContent;
83
            }
84
        }
85
86
        return $result;
87
    }
88
89
90
91
}
92
93
$wiki = ServiceFactory::wikiApi();
94
$botConfig = new WikiBotConfig();
95
96
// &srqiprofile=popular_inclinks_pv&srsort=last_edit_desc
97
$list = new CirrusSearch(
98
    'https://fr.wikipedia.org/w/api.php?action=query&list=search'
99
    //    .'&srsearch=%22https%3A%2F%2Fjournals.openedition.org%22+insource%3A%2F%5C%3Cref%5C%3Ehttps%5C%3A%5C%2F%5C%2Fjournals%5C.openedition%5C.org%5B%5E%5C%3E%5D%2B%5C%3C%5C%2Fref%3E%2F'
100
    .'&srsearch=%22https%3A%2F%2F%22+insource%3A%2F%5C%3Cref%5C%3Ehttps%5C%3A%5C%2F%5C%2F%5B%5E%5C%3E%5D%2B%5C%3C%5C%2Fref%3E%2F'
101
    .'&formatversion=2&format=json&srnamespace=0&srlimit=100&srqiprofile=popular_inclinks_pv&srsort=random'
102
);
103
104
105
//$list = new PageList(['Pierre Zind', 'Économie du Soudan du Sud']);
106
107
new ExternRefWorker($botConfig, $wiki, $list);
108