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

CodexNotificationWorker::processSpecialActions()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
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
11
namespace App\Application\Notification;
12
13
14
use App\Application\GoogleBooksWorker;
15
use App\Application\InfrastructurePorts\PageListForAppInterface;
16
use App\Application\OuvrageScan\ScanWiki2DB;
17
use App\Application\WikiBotConfig;
18
use App\Infrastructure\DbAdapter;
19
use App\Infrastructure\PageList;
20
use App\Infrastructure\ServiceFactory;
21
use Throwable;
22
23
class CodexNotificationWorker extends NotificationWorker
24
{
25
    public const ARTICLE_ANALYZED_FILENAME = __DIR__.'/resources/article_externRef_edited.txt';
26
    public const PROCESS_TASKNAME          = '🔔 Amélioration de références : URL ⇒ ';
27
28
    /**
29
     * todo Refac that stupid idea :)
30
     * Delete article title in the log text file.
31
     */
32
    private function deleteEditedArticleFile(string $title): void
33
    {
34
        $text = file_get_contents(self::ARTICLE_ANALYZED_FILENAME);
35
        $newText = str_replace($title."\n", '', $text);
36
        if (!empty($text) && $text !== $newText) {
37
            @file_put_contents(self::ARTICLE_ANALYZED_FILENAME, $newText);
38
        }
39
    }
40
41
    /**
42
     * Process external URL completion to wiki-template.
43
     */
44
    private function processExternLinks(PageListForAppInterface $pageList)
45
    {
46
        try {
47
            $wiki = ServiceFactory::getMediawikiFactory(); // todo inject+interface
48
            $botConfig = new WikiBotConfig($this->logger);
49
            $botConfig->taskName = self::PROCESS_TASKNAME;
50
            //new ExternRefWorker($botConfig, $wiki, new PageList([$article], null, new InternetDomainParser()));
51
52
            new GoogleBooksWorker($botConfig, $wiki, $pageList);
53
            sleep(10);
54
        } catch (Throwable $e) {
55
            unset($e);
56
        }
57
    }
58
59
    /**
60
     * Process wikiSan for future {ouvrage} completion
61
     *
62
     * @param string $article
63
     */
64
    private function processWikiscanForOuvrage(string $article): void
65
    {
66
        try {
67
            $wiki = ServiceFactory::getMediawikiFactory(); // todo inject+interface
68
            $list = new PageList([$article]);
69
            // todo inject+interface DbAdapterInterface
70
            new ScanWiki2DB($wiki, new DbAdapter(), new WikiBotConfig($this->logger), $list, 15);
71
        } catch (Throwable $e) {
72
            echo $e->getMessage();
73
        }
74
    }
75
76
    /**
77
     * @param $notif
78
     */
79
    protected function processSpecialActions($notif)
80
    {
81
        if (isset($notif['title']) && in_array($notif['title']['namespace'], ['', 'Discussion'])) {
82
            // PROCESS ARTICLES
83
            $article = $notif['title']['text'];
84
85
            // wikiScan for {ouvrage} completion
86
            $this->processWikiscanForOuvrage($article);
87
88
89
            // URL => wiki-template completion
90
            $this->deleteEditedArticleFile($article);
91
            $this->processExternLinks(new PageList([$article])); // todo pagelist factory
92
        }
93
    }
94
}
95