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
![]() |
|||||
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 | protected function replaceSiteForLienWeb(AbstractWikiTemplate $template, array $mapData): array |
||||
111 | { |
||||
112 | if (isset($this->config[$this->registrableDomain]['site']) && $template instanceof LienWebTemplate) { |
||||
113 | $mapData['site'] = $this->config[$this->registrableDomain]['site']; |
||||
114 | if (empty($mapData['site'])) { |
||||
115 | unset($mapData['site']); |
||||
116 | } |
||||
117 | } |
||||
118 | return $mapData; |
||||
119 | } |
||||
120 | |||||
121 | protected function replacePeriodique(array $mapData, AbstractWikiTemplate $template): array |
||||
122 | { |
||||
123 | if (isset($this->config[$this->registrableDomain]['périodique']) |
||||
124 | && (!empty($mapData['périodique']) |
||||
125 | || $template instanceof OuvrageTemplate) |
||||
126 | ) { |
||||
127 | $mapData['périodique'] = $this->config[$this->registrableDomain]['périodique']; |
||||
128 | if (empty($mapData['périodique'])) { |
||||
129 | unset($mapData['périodique']); |
||||
130 | } |
||||
131 | } |
||||
132 | return $mapData; |
||||
133 | } |
||||
134 | |||||
135 | /** |
||||
136 | * Replace 'éditeur' on {article} todo move |
||||
137 | */ |
||||
138 | protected function replaceEditor(array $mapData): array |
||||
139 | { |
||||
140 | if (isset($this->config[$this->registrableDomain]['éditeur']) |
||||
141 | && !empty($mapData['éditeur'])) { |
||||
142 | $mapData['éditeur'] = $this->config[$this->registrableDomain]['éditeur']; |
||||
143 | if (empty($mapData['éditeur'])) { |
||||
144 | unset($mapData['éditeur']); |
||||
145 | } |
||||
146 | } |
||||
147 | return $mapData; |
||||
148 | } |
||||
149 | |||||
150 | /** |
||||
151 | * Use config_press stripFromAuthor (string array) and stripFromTitle, to remove text from author and title. |
||||
152 | */ |
||||
153 | protected function stripParamValue(string $configParam, array $mapData, string $templateParam): array |
||||
154 | { |
||||
155 | if (!empty($this->config[$this->registrableDomain][$configParam]) |
||||
156 | && isset($this->config[$this->registrableDomain][$configParam]) |
||||
157 | && !empty($mapData[$templateParam])) { |
||||
158 | $stripText = $this->config[$this->registrableDomain][$configParam]; // string|array |
||||
159 | $mapData[$templateParam] = trim(str_ireplace((string) $stripText, '', (string) $mapData[$templateParam])); |
||||
0 ignored issues
–
show
It seems like
str_ireplace((string)$st...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
![]() |
|||||
160 | if (empty($mapData[$templateParam])) { |
||||
161 | unset($mapData[$templateParam]); |
||||
162 | } |
||||
163 | } |
||||
164 | return $mapData; |
||||
165 | } |
||||
166 | |||||
167 | protected function isScientificDomain(): bool |
||||
168 | { |
||||
169 | if (isset($this->publisherData['scientific domain'][$this->registrableDomain])) { |
||||
170 | return true; |
||||
171 | } |
||||
172 | return strpos('.revues.org', (string) $this->registrableDomain) > 0; |
||||
173 | } |
||||
174 | } |