Passed
Branch master (309757)
by Dispositif
03:20 queued 54s
created

ScanWiki2DB::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 0
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\OuvrageScan;
11
12
use App\Application\InfrastructurePorts\DbAdapterInterface;
13
use App\Application\InfrastructurePorts\PageListForAppInterface as PageListInterface;
14
use App\Application\WikiBotConfig;
15
use App\Application\WikiPageAction;
16
use App\Domain\Utils\TemplateParser;
17
use Exception;
18
use Mediawiki\Api\MediawikiFactory;
19
20
21
/**
22
 * From a titles list, scan the wiki and add the {ouvrage} citations into the database.
23
 */
24
class ScanWiki2DB
25
{
26
    private $wiki;
27
    private $db;
28
    private $bot;
29
    /**
30
     * @var PageListInterface
31
     */
32
    private $pageList;
33
    private $priority;
34
35
    public function __construct(
36
        MediawikiFactory   $wiki,
37
        DbAdapterInterface $dbAdapter,
38
        WikiBotConfig      $bot,
39
        PageListInterface  $list,
40
        ?int               $priority
41
        = 0
42
    ) {
43
        $this->wiki = $wiki; // ServiceFactory::wikiApi();
44
        $this->db = $dbAdapter;
45
        $this->bot = $bot;
46
        $this->pageList = $list;
47
        $this->priority = $priority;
48
49
        $this->process();
50
    }
51
52
    /**
53
     * @throws Exception
54
     */
55
    public function process():void
56
    {
57
        $titles = $this->pageList->getPageTitles();
58
        if ($titles === []) {
59
            echo "pageList vide.\n";
60
61
            return;
62
        }
63
        foreach ($titles as $title) {
64
            $this->pageScan($title);
65
            sleep(4);
66
        }
67
    }
68
69
    /**
70
     * @param string $title
71
     *
72
     * @return bool
73
     * @throws Exception
74
     */
75
    public function pageScan(string $title): bool
76
    {
77
        sleep(2);
78
        echo "\n-------------------------------------\n\n";
79
        echo date("Y-m-d H:i:s")."\n";
80
        echo $title."\n";
81
82
        $page = new WikiPageAction($this->wiki, $title); // todo injection
83
        $ns = $page->getNs();
84
        if ($ns !== 0) {
85
            echo "SKIP : namespace $ns";
86
87
            return false;
88
        }
89
        $text = $page->getText();
90
        if (empty($text)) {
91
            echo "SKIP : texte vide\n";
92
93
            return false;
94
        }
95
96
        try {
97
            $parsedTemplates = TemplateParser::parseAllTemplateByName('ouvrage', $text);
98
        } catch (Exception $e) {
99
            dump($e);
100
101
            return false;
102
        }
103
104
        if ($parsedTemplates === []) {
105
            return false;
106
        }
107
108
        $result = $this->insertDB($parsedTemplates['ouvrage'], $title);
109
110
        return !empty($result);
111
    }
112
113
    /**
114
     * @param        $ouvrages
115
     * @param string $title
116
     *
117
     * @return mixed
118
     */
119
    protected function insertDB($ouvrages, string $title)
120
    {
121
        $data = [];
122
        foreach ($ouvrages as $res) {
123
            $oneData = [
124
                'page' => $title,
125
                'raw' => $res['raw'],
126
                'priority' => $this->priority,
127
            ];
128
            // filtre doublon
129
            if (!in_array($oneData, $data)) {
130
                $data[] = $oneData;
131
            }
132
        }
133
134
        $result = $this->db->insertPageOuvrages($data);
135
        dump($result);
136
137
        return $result;
138
    }
139
140
}
141