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\Infrastructure; |
11
|
|
|
|
12
|
|
|
use App\Application\InfrastructurePorts\DbAdapterInterface; |
13
|
|
|
use DateInterval; |
14
|
|
|
use DateTime; |
15
|
|
|
use Exception; |
16
|
|
|
use Simplon\Mysql\Mysql; |
17
|
|
|
use Simplon\Mysql\MysqlException; |
18
|
|
|
use Simplon\Mysql\PDOConnector; |
19
|
|
|
use Throwable; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* TODO return DTO !!! |
23
|
|
|
* Temporary SQL play. https://github.com/fightbulc/simplon_mysql . |
24
|
|
|
* Class DbAdapter. |
25
|
|
|
*/ |
26
|
|
|
class DbAdapter implements DbAdapterInterface |
27
|
|
|
{ |
28
|
|
|
public const OPTI_VALID_DATE = '2020-04-05 00:00:00'; // v0.77 |
29
|
|
|
protected $db; |
30
|
|
|
protected $pdoConn; // v.34 sous-titre sans maj |
31
|
|
|
|
32
|
|
|
public function __construct() |
33
|
|
|
{ |
34
|
|
|
$pdo = new PDOConnector( |
35
|
|
|
getenv('MYSQL_HOST'), getenv('MYSQL_USER'), getenv('MYSQL_PASSWORD'), getenv('MYSQL_DATABASE') |
36
|
|
|
); |
37
|
|
|
$this->pdoConn = $pdo->connect('utf8', ['port' => getenv('MYSQL_PORT')]); |
38
|
|
|
$this->db = new Mysql($this->pdoConn); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getOptiValidDate(): string |
42
|
|
|
{ |
43
|
|
|
return self::OPTI_VALID_DATE; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param $datas |
48
|
|
|
* |
49
|
|
|
* @return array|bool |
50
|
|
|
* @throws Exception |
51
|
|
|
*/ |
52
|
|
|
public function insertPageOuvrages(array $datas) |
53
|
|
|
{ |
54
|
|
|
// check if article already in db |
55
|
|
|
$page = $datas[0]['page']; |
56
|
|
|
$count = $this->db->fetchRowMany( |
57
|
|
|
'SELECT id from page_ouvrages WHERE page=:page', |
58
|
|
|
['page' => $page] |
59
|
|
|
); |
60
|
|
|
if (null !== $count) { |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// add the citations |
65
|
|
|
return $this->db->insertMany('page_ouvrages', $datas); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get one new row (page, raw) to complete. |
70
|
|
|
* Order by isbn (NULL first) |
71
|
|
|
* |
72
|
|
|
* @return array|null |
73
|
|
|
*/ |
74
|
|
|
public function getNewRaw(): ?array |
75
|
|
|
{ |
76
|
|
|
try { |
77
|
|
|
$row = $this->db->fetchRow( |
78
|
|
|
'SELECT id,page,raw FROM page_ouvrages |
79
|
|
|
WHERE raw <> "" AND (opti IS NULL OR opti = "" OR optidate IS NULL OR optidate < :validDate ) AND (edited IS NULL) AND skip=0 |
80
|
|
|
ORDER BY priority DESC,id |
81
|
|
|
LIMIT 1', |
82
|
|
|
[ |
83
|
|
|
'validDate' => self::OPTI_VALID_DATE, |
84
|
|
|
] |
85
|
|
|
); |
86
|
|
|
} catch (Throwable $e) { |
87
|
|
|
echo "SQL : No more queue to process \n"; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $row ?? null; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Update DB with completed data from CompleteProcess. |
95
|
|
|
* |
96
|
|
|
* @param array $finalData |
97
|
|
|
* |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
public function sendCompletedData(array $finalData): bool |
101
|
|
|
{ |
102
|
|
|
try { |
103
|
|
|
$result = $this->db->update( |
104
|
|
|
'page_ouvrages', |
105
|
|
|
['raw' => $finalData['raw']], // condition |
106
|
|
|
$finalData |
107
|
|
|
); |
108
|
|
|
} catch (MysqlException $e) { |
109
|
|
|
dump($e); |
110
|
|
|
|
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return !empty($result); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
//------------------------------------------------------ |
118
|
|
|
// EDIT QUEUE |
119
|
|
|
//------------------------------------------------------ |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* TODO DTO !! |
123
|
|
|
* Get batch of citations(template) for edit process. |
124
|
|
|
* |
125
|
|
|
* @param int|null $limit |
126
|
|
|
* |
127
|
|
|
* @return string|null |
128
|
|
|
* @throws Exception |
129
|
|
|
*/ |
130
|
|
|
public function getAllRowsOfOneTitleToEdit(?int $limit = 100): ?string |
131
|
|
|
{ |
132
|
|
|
// // ----------- TEST ---- |
133
|
|
|
// // it works |
134
|
|
|
// $store = new PageOuvrageStore($this->db); |
135
|
|
|
// $pageOuvrageModel = $store->read( |
136
|
|
|
// (new ReadQueryBuilder())->addCondition(PageOuvrageDTO::COLUMN_PAGE, 'Autorail Pauline') |
137
|
|
|
// ); |
138
|
|
|
// dump($pageOuvrageModel); |
139
|
|
|
// die('stooooop'); |
140
|
|
|
// |
141
|
|
|
// // ---------- end TEST ---- |
142
|
|
|
|
143
|
|
|
$e = null; |
|
|
|
|
144
|
|
|
try { |
145
|
|
|
$pageInfo = $this->pdoConn->query( |
146
|
|
|
' |
147
|
|
|
SELECT A.page FROM page_ouvrages A |
148
|
|
|
WHERE A.notcosmetic=1 AND A.opti IS NOT NULL |
149
|
|
|
AND NOT EXISTS |
150
|
|
|
(SELECT B.* FROM page_ouvrages B |
151
|
|
|
WHERE ( |
152
|
|
|
B.edited IS NOT NULL |
153
|
|
|
OR B.optidate < "'.self::OPTI_VALID_DATE.'" |
154
|
|
|
OR B.optidate IS NULL |
155
|
|
|
OR B.opti IS NULL |
156
|
|
|
OR B.opti="" |
157
|
|
|
OR B.skip=1 |
158
|
|
|
OR B.raw="" |
159
|
|
|
) |
160
|
|
|
AND A.page = B.page |
161
|
|
|
) |
162
|
|
|
ORDER BY A.priority DESC,A.optidate,RAND() |
163
|
|
|
LIMIT '.$limit.' |
164
|
|
|
' |
165
|
|
|
); |
166
|
|
|
|
167
|
|
|
// No page to edit |
168
|
|
|
$rows = $pageInfo->fetchAll(); |
169
|
|
|
if (empty($rows)) { |
170
|
|
|
return '[]'; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$page = $rows[0]['page']; // get first page to edit ? |
174
|
|
|
|
175
|
|
|
// todo Replace bellow with PageOuvrageDTO |
176
|
|
|
// Order by optidate for first version in edit commentary ? |
177
|
|
|
$data = $this->db->fetchRowMany( |
178
|
|
|
'SELECT * FROM page_ouvrages WHERE page=:page ORDER BY optidate DESC', |
179
|
|
|
['page' => $page] |
180
|
|
|
); |
181
|
|
|
$json = json_encode($data, JSON_THROW_ON_ERROR); |
182
|
|
|
} catch (Throwable $e) { |
183
|
|
|
throw new Exception('SQL : No more queue to process', $e->getCode(), $e); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $json; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function deleteArticle(string $title): bool |
190
|
|
|
{ |
191
|
|
|
try { |
192
|
|
|
$result = $this->db->delete( |
193
|
|
|
'page_ouvrages', |
194
|
|
|
['page' => $title] // condition |
195
|
|
|
); |
196
|
|
|
} catch (MysqlException $e) { |
197
|
|
|
dump($e); |
198
|
|
|
|
199
|
|
|
return false; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return !empty($result); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function skipArticle(string $title): bool |
206
|
|
|
{ |
207
|
|
|
try { |
208
|
|
|
$result = $this->db->update( |
209
|
|
|
'page_ouvrages', |
210
|
|
|
['page' => $title], // condition |
211
|
|
|
['skip' => 1] |
212
|
|
|
); |
213
|
|
|
} catch (MysqlException $e) { |
214
|
|
|
dump($e); |
215
|
|
|
|
216
|
|
|
return false; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return !empty($result); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function setLabel(string $title, ?int $val = 0): bool |
223
|
|
|
{ |
224
|
|
|
try { |
225
|
|
|
$result = $this->db->update( |
226
|
|
|
'page_ouvrages', |
227
|
|
|
['page' => $title], // condition |
228
|
|
|
['label' => $val] |
229
|
|
|
); |
230
|
|
|
} catch (MysqlException $e) { |
231
|
|
|
dump($e); |
232
|
|
|
|
233
|
|
|
return false; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return !empty($result); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function skipRow(int $id): bool |
240
|
|
|
{ |
241
|
|
|
try { |
242
|
|
|
$result = $this->db->update( |
243
|
|
|
'page_ouvrages', |
244
|
|
|
['id' => $id], // condition |
245
|
|
|
['skip' => 1] |
246
|
|
|
); |
247
|
|
|
} catch (MysqlException $e) { |
248
|
|
|
dump($e); |
249
|
|
|
|
250
|
|
|
return false; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return !empty($result); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Update DB after wiki edition. |
258
|
|
|
* |
259
|
|
|
* @param array $data |
260
|
|
|
* |
261
|
|
|
* @return bool |
262
|
|
|
*/ |
263
|
|
|
public function sendEditedData(array $data): bool |
264
|
|
|
{ |
265
|
|
|
try { |
266
|
|
|
$result = $this->db->update( |
267
|
|
|
'page_ouvrages', |
268
|
|
|
['id' => $data['id']], // condition |
269
|
|
|
['edited' => date('Y-m-d H:i:s')] |
270
|
|
|
); |
271
|
|
|
} catch (MysqlException $e) { |
272
|
|
|
dump($e); |
273
|
|
|
|
274
|
|
|
return false; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
return !empty($result); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Get a row to monitor edits. |
282
|
|
|
*/ |
283
|
|
|
public function getMonitor(): ?array |
284
|
|
|
{ |
285
|
|
|
$data = null; |
286
|
|
|
// 2 hours ago |
287
|
|
|
$beforeTime = (new DateTime())->sub(new DateInterval('PT3H')); |
288
|
|
|
|
289
|
|
|
try { |
290
|
|
|
$data = $this->db->fetchRowMany( |
291
|
|
|
'SELECT id,page,raw,opti,optidate,edited,verify,skip FROM page_ouvrages WHERE page = ( |
292
|
|
|
SELECT page FROM page_ouvrages |
293
|
|
|
WHERE edited IS NOT NULL |
294
|
|
|
and edited > :afterDate and edited < :beforeDate |
295
|
|
|
and (verify is null or verify < :nextVerifyDate ) |
296
|
|
|
ORDER BY verify,edited |
297
|
|
|
LIMIT 1)', |
298
|
|
|
[ |
299
|
|
|
'afterDate' => '2019-11-26 06:00:00', |
300
|
|
|
'beforeDate' => $beforeTime->format('Y-m-d H:i:s'), |
301
|
|
|
'nextVerifyDate' => (new DateTime())->sub(new DateInterval('P2D'))->format('Y-m-d H:i:s'), |
302
|
|
|
] |
303
|
|
|
); |
304
|
|
|
} catch (Throwable $e) { |
305
|
|
|
echo "SQL : No more queue to process \n"; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
return $data; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
public function updateMonitor(array $data): bool |
312
|
|
|
{ |
313
|
|
|
if (empty($data['page'])) { |
314
|
|
|
throw new Exception('pas de page'); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
try { |
318
|
|
|
$result = $this->db->update( |
319
|
|
|
'page_ouvrages', |
320
|
|
|
['page' => $data['page']], // condition |
321
|
|
|
$data |
322
|
|
|
); |
323
|
|
|
} catch (MysqlException $e) { |
324
|
|
|
dump($e); |
325
|
|
|
|
326
|
|
|
return false; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
return !empty($result); |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|