Passed
Push — master ( 8c018f...e6de3a )
by Dispositif
02:50
created

PublisherLogicTrait::replaceSitenameByConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 3
c 2
b 1
f 0
nc 1
nop 2
dl 0
loc 8
rs 10
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
namespace App\Domain\ExternLink;
11
12
use App\Domain\Models\Wiki\AbstractWikiTemplate;
13
use App\Domain\Models\Wiki\ArticleTemplate;
14
use App\Domain\Models\Wiki\LienWebTemplate;
15
use App\Domain\Models\Wiki\OuvrageTemplate;
16
use App\Domain\Utils\WikiTextUtil;
17
use Symfony\Component\Yaml\Yaml;
18
19
trait PublisherLogicTrait
20
{
21
    /**
22
     * todo extract Infra getcont form file + inject
23
     */
24
    protected function importConfigAndData(): void
25
    {
26
        // todo REFAC DataObject[] (WTF?)
27
        $this->config = Yaml::parseFile(self::CONFIG_PRESSE);
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
28
        $skipFromFile = file(
29
            self::SKIP_DOMAIN_FILENAME,
30
            FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
31
        );
32
        $this->skip_domain = $skipFromFile ?: [];
33
34
        $this->publisherData['newspaper'] = json_decode(file_get_contents(self::CONFIG_NEWSPAPER_JSON), true, 512, JSON_THROW_ON_ERROR);
35
        $this->publisherData['scientific domain'] = json_decode(
36
            file_get_contents(self::CONFIG_SCIENTIFIC_JSON),
37
            true,
38
            512,
39
            JSON_THROW_ON_ERROR
40
        );
41
        $this->publisherData['scientific wiki'] = json_decode(
42
            file_get_contents(self::CONFIG_SCIENTIFIC_WIKI_JSON),
43
            true,
44
            512,
45
            JSON_THROW_ON_ERROR
46
        );
47
    }
48
49
    /**
50
     * TODO rename/move Postprocess data site/périodique
51
     * Replace 'site' and 'périodique' with wikidata URL of newspapers and scientific journals or config_press (YAML).
52
     */
53
    protected function replaceSitenameByConfig(array $mapData, AbstractWikiTemplate $template): array
54
    {
55
        // Wikidata replacements
56
        $mapData = $this->replaceWithWDataNewspaper($mapData, $template);
57
        $mapData = $this->replaceWithWDataSciences($mapData);
58
59
        // config_press YAML replacements
60
        return $this->replaceWithHumanConfig($template, $mapData);
61
    }
62
63
    /**
64
     * Replace 'site' with wikidata URL of newspapers
65
     */
66
    protected function replaceWithWDataNewspaper(array $mapData, AbstractWikiTemplate $template): array
67
    {
68
        if (!empty($this->publisherData['newspaper'][$this->registrableDomain])) {
69
            $frwiki = $this->publisherData['newspaper'][$this->registrableDomain]['frwiki'];
70
            $label = $this->publisherData['newspaper'][$this->registrableDomain]['fr'];
71
            if (isset($mapData['site']) || $template instanceof LienWebTemplate) {
72
                $mapData['site'] = WikiTextUtil::wikilink($label, $frwiki);
73
            }
74
            if (isset($mapData['périodique']) || $template instanceof ArticleTemplate) {
75
                $mapData['périodique'] = WikiTextUtil::wikilink($label, $frwiki);
76
            }
77
        }
78
        return $mapData;
79
    }
80
81
    /**
82
     * Replace 'périodique' with wikidata URL of scientific journals
83
     */
84
    protected function replaceWithWDataSciences(array $mapData): array
85
    {
86
        if (isset($mapData['périodique']) && isset($this->publisherData['scientific wiki'][$mapData['périodique']])) {
87
            $mapData['périodique'] = WikiTextUtil::wikilink(
88
                $mapData['périodique'],
89
                $this->publisherData['scientific wiki'][$mapData['périodique']]
90
            );
91
        }
92
        return $mapData;
93
    }
94
95
    /**
96
     * Replace 'site' and 'périodique' with config_press (YAML)
97
     */
98
    protected function replaceWithHumanConfig(AbstractWikiTemplate $template, array $mapData): array
99
    {
100
        $mapData = $this->replaceSiteForLienWeb($template, $mapData);
101
        $mapData = $this->replacePeriodique($mapData, $template);
102
103
        $mapData = $this->replaceEditor($mapData);
104
105
        $mapData = $this->stripParamValue('stripfromauthor', $mapData, 'auteur1');
106
107
        return $this->stripParamValue('stripfromtitle', $mapData, 'titre');
108
    }
109
110
    /**
111
     * @param AbstractWikiTemplate $template
112
     * @param array $mapData
113
     *
114
     * @return array
115
     */
116
    protected function replaceSiteForLienWeb(AbstractWikiTemplate $template, array $mapData): array
117
    {
118
        if (isset($this->config[$this->registrableDomain]['site']) && $template instanceof LienWebTemplate) {
119
            $mapData['site'] = $this->config[$this->registrableDomain]['site'];
120
            if (empty($mapData['site'])) {
121
                unset($mapData['site']);
122
            }
123
        }
124
        return $mapData;
125
    }
126
127
    /**
128
     * @param array $mapData
129
     * @param AbstractWikiTemplate $template
130
     *
131
     * @return array
132
     */
133
    protected function replacePeriodique(array $mapData, AbstractWikiTemplate $template): array
134
    {
135
        if (isset($this->config[$this->registrableDomain]['périodique'])
136
            && (!empty($mapData['périodique'])
137
                || $template instanceof OuvrageTemplate)
138
        ) {
139
            $mapData['périodique'] = $this->config[$this->registrableDomain]['périodique'];
140
            if (empty($mapData['périodique'])) {
141
                unset($mapData['périodique']);
142
            }
143
        }
144
        return $mapData;
145
    }
146
147
    /**
148
     * Replace 'éditeur' on {article} todo move
149
     */
150
    protected function replaceEditor(array $mapData): array
151
    {
152
        if (isset($this->config[$this->registrableDomain]['éditeur'])
153
            && !empty($mapData['éditeur'])) {
154
            $mapData['éditeur'] = $this->config[$this->registrableDomain]['éditeur'];
155
            if (empty($mapData['éditeur'])) {
156
                unset($mapData['éditeur']);
157
            }
158
        }
159
        return $mapData;
160
    }
161
162
    /**
163
     * Use config_press stripFromAuthor (string array) and stripFromTitle, to remove text from author and title.
164
     */
165
    protected function stripParamValue(string $configParam, array $mapData, string $templateParam): array
166
    {
167
        if (!empty($this->config[$this->registrableDomain][$configParam])
168
            && is_array($this->config[$this->registrableDomain][$configParam])
169
            && !empty($mapData[$templateParam])) {
170
            $stripTextArray = $this->config[$this->registrableDomain][$configParam];
171
            foreach ($stripTextArray as $stripText) {
172
                $mapData[$templateParam] = trim(str_ireplace($stripText, '', $mapData[$templateParam]));
0 ignored issues
show
Bug introduced by
It seems like str_ireplace($stripText,...apData[$templateParam]) can also be of type array; however, parameter $string of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

172
                $mapData[$templateParam] = trim(/** @scrutinizer ignore-type */ str_ireplace($stripText, '', $mapData[$templateParam]));
Loading history...
173
                if (empty($mapData[$templateParam])) {
174
                    unset($mapData[$templateParam]);
175
                }
176
            }
177
        }
178
        return $mapData;
179
    }
180
181
    protected function isScientificDomain(): bool
182
    {
183
        if (isset($this->publisherData['scientific domain'][$this->registrableDomain])) {
184
            return true;
185
        }
186
        return strpos('.revues.org', $this->registrableDomain) > 0;
187
    }
188
}