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

RecentChangeWorker   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 35
dl 0
loc 78
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A trackUser() 0 13 1
A requestLastEditsbyUser() 0 23 2
A __construct() 0 4 1
A getLastEditsbyUser() 0 9 2
A consumeList() 0 8 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\ExternLink;
12
13
use App\Application\WikiBotConfig;
14
use App\Infrastructure\InternetDomainParser;
15
use App\Infrastructure\PageList;
16
use App\Infrastructure\ServiceFactory;
17
use Mediawiki\Api\MediawikiApi;
18
use Mediawiki\Api\SimpleRequest;
19
use Psr\Log\LoggerInterface;
20
use Psr\Log\NullLogger;
21
22
/**
23
 * https://www.mediawiki.org/wiki/API:RecentChanges
24
 */
25
class RecentChangeWorker
26
{
27
    const USER_RC_LIMIT = 100;
28
    /**
29
     * @var MediawikiApi
30
     */
31
    private $api;
32
    /**
33
     * @var LoggerInterface
34
     */
35
    private $logger;
36
37
    public function __construct(MediawikiApi $api, ?LoggerInterface $logger = null)
38
    {
39
        $this->api = $api;
40
        $this->logger = $logger ?? new NullLogger();
41
    }
42
43
    public function trackUser(string $user): void
44
    {
45
        echo "**** TRACK " . $user . "*****\n";
46
47
        $titles = $this->getLastEditsbyUser($user);
48
49
        // filter titles already in edited.txt
50
        $edited = file(__DIR__.'/resources/article_externRef_edited.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
51
        $filtered = array_diff($titles, $edited);
52
        $list = new PageList( $filtered ); // TODO PageList factory in App ?
53
        echo ">" . $list->count() . " dans liste\n";
54
55
        $this->consumeList($list);
56
    }
57
58
    // https://www.mediawiki.org/wiki/API:RecentChanges
59
    private function requestLastEditsbyUser(string $user): array
60
    {
61
        $result = $this->api->getRequest(
62
            new SimpleRequest(
63
                'query', [
64
                    'list' => 'recentchanges',
65
                    'rcnamespace' => 0,
66
                    'rcprop' => 'title|timestamp|user|redirect',
67
                    'rcuser' => $user,
68
                    'rclimit' => self::USER_RC_LIMIT,
69
//                    'rcdir' => 'newer', // = older to newer
70
                    'rctype' => 'edit|new',
71
//                    'rcshow' => '!bot',
72
                    'format' => 'php',
73
                ]
74
            )
75
        );
76
77
        if (empty($result)) {
78
            return [];
79
        }
80
81
        return $result['query']['recentchanges'] ?? [];
82
    }
83
84
    private function consumeList(PageList $list): void
85
    {
86
        $wiki = ServiceFactory::getMediawikiFactory();
87
        $botConfig = new WikiBotConfig($this->logger);
88
        $botConfig->taskName = "🦊 Amélioration de références : URL ⇒ ";
89
90
91
        new ExternRefWorker($botConfig, $wiki, $list, null, new InternetDomainParser());
92
    }
93
94
    private function getLastEditsbyUser(string $user): array
95
    {
96
        $recentchanges = $this->requestLastEditsbyUser($user);
97
        $titles = [];
98
        foreach ($recentchanges as $rc) {
99
            $titles[] = $rc['title'];
100
        }
101
102
        return array_unique($titles);
103
    }
104
}