Passed
Push — master ( 9ad278...1bcf8b )
by Dispositif
08:55
created

ScanWiki2DB::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 5
dl 0
loc 15
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application
4
 * 2019 : Philippe M. <[email protected]>
5
 * For the full copyright and MIT license information, please view the LICENSE file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Application;
11
12
use App\Domain\Utils\TemplateParser;
13
use App\Infrastructure\PageListInterface;
14
use Exception;
15
use Mediawiki\Api\MediawikiFactory;
16
17
18
/**
19
 * From a titles list, scan the wiki and add the {ouvrage} citations into the database.
20
 */
21
class ScanWiki2DB
22
{
23
    private $wiki;
24
    private $db;
25
    private $bot;
26
    /**
27
     * @var PageListInterface
28
     */
29
    private $pageList;
30
    private $priority;
31
32
    public function __construct(
33
        MediawikiFactory $wiki,
34
        QueueInterface $dbAdapter,
35
        WikiBotConfig $bot,
36
        PageListInterface $list,
37
        ?int $priority
38
        = 0
39
    ) {
40
        $this->wiki = $wiki; // ServiceFactory::wikiApi();
41
        $this->db = $dbAdapter;
42
        $this->bot = $bot;
43
        $this->pageList = $list;
44
        $this->priority = $priority;
45
46
        $this->process();
47
    }
48
49
    /**
50
     * @throws Exception
51
     */
52
    public function process():void
53
    {
54
        $titles = $this->pageList->getPageTitles();
55
        if (empty($titles)) {
56
            echo "pageList vide.\n";
57
58
            return;
59
        }
60
        foreach ($titles as $title) {
61
            $this->pageScan($title);
62
            sleep(4);
63
        }
64
    }
65
66
    /**
67
     * @param string $title
68
     *
69
     * @return bool
70
     * @throws Exception
71
     */
72
    public function pageScan(string $title): bool
73
    {
74
        sleep(2);
75
        echo "\n-------------------------------------\n\n";
76
        echo date("Y-m-d H:i:s")."\n";
77
        echo $title."\n";
78
79
        $page = new WikiPageAction($this->wiki, $title); // todo injection
80
        $ns = $page->getNs();
81
        if ($ns !== 0) {
82
            echo "SKIP : namespace $ns";
83
84
            return false;
85
        }
86
        $text = $page->getText();
87
        if (empty($text)) {
88
            echo "SKIP : texte vide\n";
89
90
            return false;
91
        }
92
93
        try {
94
            $parsedTemplates = TemplateParser::parseAllTemplateByName('ouvrage', $text);
95
        } catch (Exception $e) {
96
            dump($e);
97
98
            return false;
99
        }
100
101
        if (empty($parsedTemplates)) {
102
            return false;
103
        }
104
105
        $result = $this->insertDB($parsedTemplates['ouvrage'], $title);
106
107
        return !empty($result);
108
    }
109
110
    /**
111
     * @param        $ouvrages
112
     * @param string $title
113
     *
114
     * @return mixed
115
     */
116
    protected function insertDB($ouvrages, string $title)
117
    {
118
        $data = [];
119
        foreach ($ouvrages as $res) {
120
            $oneData = [
121
                'page' => $title,
122
                'raw' => $res['raw'],
123
                'priority' => $this->priority,
124
            ];
125
            // filtre doublon
126
            if (!in_array($oneData, $data)) {
127
                $data[] = $oneData;
128
            }
129
        }
130
131
        $result = $this->db->insertPageOuvrages($data);
1 ignored issue
show
Bug introduced by
The method insertPageOuvrages() does not exist on App\Application\QueueInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to App\Application\QueueInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

131
        /** @scrutinizer ignore-call */ 
132
        $result = $this->db->insertPageOuvrages($data);
Loading history...
132
        dump($result);
133
134
        return $result;
135
    }
136
137
}
138