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

TalkPageEditTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B sendOuvrageErrorsOnTalkPage() 0 52 8
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\OuvrageEdit;
12
13
use App\Infrastructure\ServiceFactory;
14
use Psr\Log\LoggerInterface;
15
use Psr\Log\NullLogger;
16
use Throwable;
17
18
trait TalkPageEditTrait
19
{
20
    /**
21
     * todo extract to class ?
22
     *
23
     * @param array                $rows Collection of citations
24
     * @param LoggerInterface|null $log
25
     *
26
     * @return bool
27
     */
28
    private function sendOuvrageErrorsOnTalkPage(array $rows, LoggerInterface $log = null): bool
29
    {
30
        if ($log === null) {
31
            $log = new NullLogger();
32
        }
33
        if (empty($rows[0]) || empty($rows[0]['page'])) {
34
            return false;
35
        }
36
        $mainTitle = $rows[0]['page'];
37
        $log->notice("** Send Error Message on talk page. Wait 3...");
38
        sleep(3);
39
40
        // format wiki message
41
        $errorList = '';
42
        foreach ($this->pageWorkStatus->errorWarning[$mainTitle] as $error) {
43
            $errorList .= sprintf("* <span style=\"background:#FCDFE8\"><nowiki>%s</nowiki></span> \n", $error);
44
        }
45
46
        $diffStr = '';
47
        try {
48
            // get last bot revision ID
49
            $main = ServiceFactory::wikiPageAction($mainTitle, true);
50
            if (getenv('BOT_NAME') === $main->getLastRevision()->getUser()) {
51
                $id = $main->getLastRevision()->getId();
52
                $diffStr = sprintf(
53
                    ' ([https://fr.wikipedia.org/w/index.php?title=%s&diff=%s diff])',
54
                    str_replace(' ', '_', $mainTitle),
55
                    $id
56
                );
57
            }
58
        } catch (Throwable $e) {
59
            unset($e);
60
        }
61
62
        $errorCategoryName = sprintf('Signalement %s', getenv('BOT_NAME'));
63
64
        $errorMessage = file_get_contents(self::ERROR_MSG_TEMPLATE);
1 ignored issue
show
Bug introduced by
The constant App\Application\OuvrageE...ait::ERROR_MSG_TEMPLATE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
65
        $errorMessage = str_replace('##CATEGORY##', $errorCategoryName, $errorMessage);
66
        $errorMessage = str_replace('##ERROR LIST##', trim($errorList), $errorMessage);
67
        $errorMessage = str_replace('##ARTICLE##', $mainTitle, $errorMessage);
68
        $errorMessage = str_replace('##DIFF##', $diffStr, $errorMessage);
69
70
        // Edit wiki talk page
71
        try {
72
            $talkPage = ServiceFactory::wikiPageAction('Discussion:'.$mainTitle);
73
            $editInfo = ServiceFactory::editInfo('🍄 Signalement erreur {ouvrage}', false, false, 5); // 💩
74
75
            return $talkPage->addToBottomOrCreatePage("\n".$errorMessage, $editInfo);
76
        } catch (Throwable $e) {
77
            $log->warning('Exception after addToBottomOrCreatePage() '.$e->getMessage());
78
79
            return false;
80
        }
81
    }
82
}
83