Passed
Push — master ( 9538c8...6c70b1 )
by Dispositif
07:44
created

RecentChangeWorker::trackUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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