Passed
Push — dev ( b8ced8...f7ea32 )
by Dispositif
07:14
created

PageScan   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 38
c 0
b 0
f 0
dl 0
loc 62
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B pageScan() 0 49 7
A __construct() 0 5 1
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\Examples;
11
12
use App\Application\WikiBotConfig;
13
use App\Application\WikiPageAction;
14
use App\Domain\Utils\TemplateParser;
15
use App\Infrastructure\DbAdapter;
16
use App\Infrastructure\ServiceFactory;
17
use Exception;
18
19
//use App\Application\CLI;
20
21
include __DIR__.'/../myBootstrap.php';
22
23
/**
24
 * From json list of articles => add to SQL page_ouvrages
25
 */
26
$process = new PageScan();
27
28
$articles = file(__DIR__.'/../resources/importISBN_nov.txt');
29
30
foreach ($articles as $article) {
31
    $article = trim($article);
32
    if (empty($article)) {
33
        continue;
34
    }
35
    $process->pageScan($article, 0);
36
    sleep(4);
37
}
38
39
class PageScan
40
{
41
    private $wiki;
42
    private $db;
43
    private $bot;
44
45
    public function __construct()
46
    {
47
        $this->wiki = ServiceFactory::wikiApi();
48
        $this->db = new DbAdapter();
49
        $this->bot = new WikiBotConfig();
50
    }
51
52
    public function pageScan(string $title, ?int $priority = 0): bool
53
    {
54
        sleep(2);
55
        echo "\n-------------------------------------\n\n";
56
        echo date("Y-m-d H:i:s")."\n";
57
        echo $title."\n";
58
59
        $page = new WikiPageAction($this->wiki, $title);
60
        $ns = $page->getNs();
61
        if ($ns !== 0) {
62
            echo "SKIP : namespace $ns";
63
64
            return false;
65
        }
66
        $text = $page->getText();
67
        if (empty($text)) {
68
            echo "SKIP : texte vide\n";
69
70
            return false;
71
        }
72
73
        try {
74
            $parse = TemplateParser::parseAllTemplateByName('ouvrage', $text);
75
        } catch (Exception $e) {
76
            dump($e);
77
78
            return false;
79
        }
80
81
        if (empty($parse)) {
82
            return false;
83
        }
84
        $data = [];
85
        foreach ($parse['ouvrage'] as $res) {
86
            $thisdata = [
87
                'page' => $title,
88
                'raw' => $res['raw'],
89
                'priority' => $priority,
90
            ];
91
            // filtre doublon
92
            if (!in_array($thisdata, $data)) {
93
                $data[] = $thisdata;
94
            }
95
        }
96
97
        $result = $this->db->insertPageOuvrages($data);
98
        dump($result);
99
100
        return !empty($result);
101
    }
102
103
}
104