-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcontroller.php
589 lines (562 loc) · 20 KB
/
controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
<?php
declare(strict_types=1);
namespace Concrete\Package\CommunityTranslation;
use CommunityTranslation\Api\EntryPoint as ApiEntryPoint;
use CommunityTranslation\Console\Command;
use CommunityTranslation\Entity\Locale as LocaleEntity;
use CommunityTranslation\Notification\CategoryInterface;
use CommunityTranslation\Parser\ConcreteCMSParser;
use CommunityTranslation\Parser\Provider as ParserProvider;
use CommunityTranslation\Repository\Locale as LocaleRepository;
use CommunityTranslation\Repository\Package as PackageRepository;
use CommunityTranslation\Service\EntitiesEventSubscriber;
use CommunityTranslation\Service\EventSubscriber;
use CommunityTranslation\ServiceProvider;
use Concrete\Core\Asset\AssetList;
use Concrete\Core\Config\Repository\Repository;
use Concrete\Core\Database\EntityManager\Provider\ProviderAggregateInterface;
use Concrete\Core\Database\EntityManager\Provider\StandardPackageProvider;
use Concrete\Core\Package\Package;
use Concrete\Core\Page\Page;
use Concrete\Core\Routing\Router;
use Doctrine\ORM\EntityManager;
use Gettext\Translations;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use ReflectionClass;
defined('C5_EXECUTE') or die('Access Denied.');
/**
* The package controller.
*
* Handle installation/upgrading and startup of Community Translation.
*/
class Controller extends Package implements ProviderAggregateInterface
{
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Package\Package::$appVersionRequired
*/
protected $appVersionRequired = '9.0.3a1';
/**
* The package unique handle.
*
* @var string
*/
protected $pkgHandle = 'community_translation';
/**
* The package version.
*
* @var string
*/
protected $pkgVersion = '1.7.1';
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Package\Package::$pkgAutoloaderRegistries
*/
protected $pkgAutoloaderRegistries = [
'src' => 'CommunityTranslation',
];
/**
* This needs to be static because the core may use different instances of this controller class while upgrading.
*/
protected static string $upgradingFromVersion = '';
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Package\Package::getPackageName()
*/
public function getPackageName()
{
return t('Community Translation');
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Package\Package::getPackageDescription()
*/
public function getPackageDescription()
{
return t('Community-driven collaborative translation');
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Database\EntityManager\Provider\ProviderAggregateInterface::getEntityManagerProvider()
*/
public function getEntityManagerProvider()
{
return new StandardPackageProvider($this->app, $this, [
'src/Entity' => 'CommunityTranslation\Entity',
]);
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Package\Package::install()
*/
public function install()
{
parent::install();
$this->installXml();
$this->registerServiceProvider();
$this->configureSourceLocale();
$this->refreshLatestPackageVersions();
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Package\Package::upgradeCoreData()
*/
public function upgradeCoreData()
{
$e = $this->getPackageEntity();
if ($e !== null) {
self::$upgradingFromVersion = (string) $e->getPackageVersion();
}
parent::upgradeCoreData();
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Package\Package::upgrade()
*/
public function upgrade()
{
parent::upgrade();
$this->installXml();
if (self::$upgradingFromVersion !== '') {
if (version_compare(self::$upgradingFromVersion, '0.4.0') < 0) {
$this->refreshLatestPackageVersions();
}
if (version_compare(self::$upgradingFromVersion, '1.6.0') < 0) {
$this->updatePackageNamesAndSources();
$this->deletePage('/dashboard/community_translation/options/cli');
}
}
}
/**
* Initialize the package.
*/
public function on_start()
{
$this->registerServiceProvider();
$this->registerParsers();
$this->app->make('director')->addSubscriber($this->app->make(EventSubscriber::class));
if ($this->app->isRunThroughCommandLineInterface()) {
$this->registerCLICommands();
} else {
$this->registerAssets();
$this->registerRoutes();
}
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Package\Package::getTranslatableStrings()
*/
public function getTranslatableStrings(Translations $translations)
{
$classPrefix = 'CommunityTranslation\\Notification\\Category';
$filePrefix = $this->getPackagePath() . '/src/Notification/Category';
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($filePrefix));
foreach ($iterator as $file) {
/** @var \SplFileInfo $file */
if (!$file->isFile() || strcasecmp($file->getExtension(), 'php') !== 0) {
continue;
}
$filePath = str_replace(DIRECTORY_SEPARATOR, '/', $file->getPathname());
$relativePath = basename(substr($filePath, strlen($filePrefix) + 1), '.php');
$className = $classPrefix . '\\' . str_replace('/', '\\', $relativePath);
if (!is_a($className, CategoryInterface::class, true)) {
continue;
}
$class = new ReflectionClass($className);
if ($class->isAbstract()) {
continue;
}
$translations->insert('NotificationCategory', $className::getDescription());
}
}
/**
* Install/refresh stuff from the XML installation file.
*/
private function installXml(): void
{
$this->installContentFile('install.xml');
}
/**
* Configure the source locale.
*/
private function configureSourceLocale(): void
{
$em = $this->app->make(EntityManager::class);
$repo = $this->app->make(LocaleRepository::class);
if ($repo->findOneBy(['isSource' => true]) === null) {
$locale = new LocaleEntity('en_US');
$locale
->setIsApproved(true)
->setIsSource(true)
;
$em->persist($locale);
$em->flush($locale);
}
}
private function refreshLatestPackageVersions(): void
{
$ees = $this->app->make(EntitiesEventSubscriber::class);
$packageRepo = $this->app->make(PackageRepository::class);
$em = $this->app->make(EntityManager::class);
foreach ($packageRepo->findAll() as $package) {
$ees->refreshPackageLatestVersion($em, $package);
}
}
private function updatePackageNamesAndSources(): void
{
$cn = $this->app->make(EntityManager::class)->getConnection();
$cn->executeStatement(
<<<'EOT'
UPDATE CommunityTranslationPackages
INNER JOIN CommunityTranslationRemotePackages ON CommunityTranslationPackages.handle = CommunityTranslationRemotePackages.handle
SET CommunityTranslationPackages.fromRemotePackage = 1
EOT
);
$cn->executeStatement(
<<<'EOT'
UPDATE CommunityTranslationPackages
INNER JOIN CommunityTranslationGitRepositories ON CommunityTranslationPackages.handle = CommunityTranslationGitRepositories.packageHandle
SET CommunityTranslationPackages.fromGitRepository = 1
EOT
);
$rs = $cn->executeQuery(
<<<'EOT'
SELECT
id,
packageHandle
FROM
CommunityTranslationGitRepositories
WHERE
packageName = ''
EOT
);
while (($row = $rs->fetchAssociative()) !== false) {
$segments = preg_split('/[_-]+/', $row['packageHandle'], -1, PREG_SPLIT_NO_EMPTY);
if ($segments === []) {
$packageName = $row['packageHandle'];
} else {
$parts = [];
foreach ($segments as $segment) {
$len = mb_strlen($segment);
if ($len === 1) {
$parts[] = mb_strtoupper($segment);
} else {
$parts[] = mb_strtoupper(mb_substr($segment, 0, 1)) . mb_substr($segment, 1);
}
}
$packageName = implode(' ', $parts);
}
$cn->update('CommunityTranslationGitRepositories', ['packageName' => $packageName], ['id' => $row['id']]);
}
}
private function deletePage(string $path): void
{
$page = Page::getByPath($path);
if ($page && !$page->isError()) {
$page->delete();
}
}
/**
* Register some commonly used service classes.
*/
private function registerServiceProvider(): void
{
$provider = $this->app->make(ServiceProvider::class);
$provider->register();
}
private function registerParsers(): void
{
$this->app->make(ParserProvider::class)->registerParserClass(ConcreteCMSParser::class);
}
/**
* Register the CLI commands.
*/
private function registerCLICommands(): void
{
$console = $this->app->make('console');
$console->addCommands([
new Command\AcceptPendingJoinRequestsCommand(),
new Command\NotifyPackageVersionsCommand(),
new Command\ProcessGitRepositoriesCommand(),
new Command\ProcessRemotePackagesCommand(),
new Command\SendNotificationsCommand(),
new Command\TransifexGlossaryCommand(),
new Command\TransifexTranslationsCommand(),
]);
}
/**
* Register the assets.
*/
private function registerAssets(): void
{
$al = AssetList::getInstance();
if (!empty($_ENV['COMTRA_VUE_DEBUG'])) {
$al->register('javascript', 'vue', 'js/vue.unminified.js', ['minify' => false, 'combine' => false, 'version' => '2.999.999'], $this);
}
$al->registerMultiple([
'community_translation/table-sortable' => [
['css', 'css/table-sortable.css', ['minify' => false, 'combine' => true], $this],
['javascript', 'js/table-sortable.js', ['minify' => false, 'combine' => true], $this],
],
'community_translation/bootstrap' => [
['javascript', 'js/bootstrap.js', ['minify' => false, 'combine' => false], $this],
],
'community_translation/markdown-it' => [
['javascript', 'js/markdown-it.js', ['minify' => false, 'combine' => false], $this],
],
'community_translation/progress-bar' => [
['css', 'css/progress-bar.css', ['minify' => false, 'combine' => true], $this],
],
'community_translation/online-translation' => [
['css', 'css/online-translation.css', ['minify' => false, 'combine' => false], $this],
],
]);
$al->registerGroupMultiple([
'community_translation/table-sortable' => [
[
['css', 'font-awesome'],
['css', 'community_translation/table-sortable'],
['javascript', 'jquery'],
['javascript', 'community_translation/table-sortable'],
],
],
'community_translation/progress-bar' => [
[
['css', 'community_translation/progress-bar'],
],
],
'community_translation/online-translation' => [
[
['css', 'font-awesome'],
['css', 'community_translation/online-translation'],
['javascript', 'jquery'],
['javascript', 'vue'],
['javascript', 'community_translation/bootstrap'],
['javascript', 'community_translation/markdown-it'],
],
],
]);
}
/**
* Register the routes.
*/
private function registerRoutes(): void
{
$config = $this->app->make(Repository::class);
$onlineTranslationPath = '/' . trim((string) $config->get('community_translation::paths.onlineTranslation'), '/');
$apiBasePath = '/' . trim((string) $config->get('community_translation::paths.api'), '/');
$handleRegex = '[A-Za-z0-9]([A-Za-z0-9\_]*[A-Za-z0-9])?';
$localeRegex = '[a-zA-Z]{2,3}([_\-][a-zA-Z0-9]{2,3})?';
$this->app->make(Router::class)->registerMultiple([
// Online Translation
"{$onlineTranslationPath}/{packageVersionID}/{localeID}" => [
'Concrete\Package\CommunityTranslation\Controller\Frontend\OnlineTranslation::view',
null,
['packageVersionID' => 'unreviewed|[1-9][0-9]*', 'localeID' => $localeRegex],
[],
'',
[],
['GET'],
],
"{$onlineTranslationPath}/action/save_comment/{localeID}" => [
'Concrete\Package\CommunityTranslation\Controller\Frontend\OnlineTranslation::save_comment',
null,
['localeID' => $localeRegex],
[],
'',
[],
['POST'],
],
"{$onlineTranslationPath}/action/delete_comment/{localeID}" => [
'Concrete\Package\CommunityTranslation\Controller\Frontend\OnlineTranslation::delete_comment',
null,
['localeID' => $localeRegex],
[],
'',
[],
['POST'],
],
"{$onlineTranslationPath}/action/load_all_places/{localeID}" => [
'Concrete\Package\CommunityTranslation\Controller\Frontend\OnlineTranslation::load_all_places',
null,
['localeID' => $localeRegex],
[],
'',
[],
['POST'],
],
"{$onlineTranslationPath}/action/process_translation/{localeID}" => [
'Concrete\Package\CommunityTranslation\Controller\Frontend\OnlineTranslation::process_translation',
null,
['localeID' => $localeRegex],
[],
'',
[],
['POST'],
],
"{$onlineTranslationPath}/action/load_translation/{localeID}" => [
'Concrete\Package\CommunityTranslation\Controller\Frontend\OnlineTranslation::load_translation',
null,
['localeID' => $localeRegex],
[],
'',
[],
['POST'],
],
"{$onlineTranslationPath}/action/save_glossary_entry/{localeID}" => [
'Concrete\Package\CommunityTranslation\Controller\Frontend\OnlineTranslation::save_glossary_entry',
null,
['localeID' => $localeRegex],
[],
'',
[],
['POST'],
],
"{$onlineTranslationPath}/action/delete_glossary_entry/{localeID}" => [
'Concrete\Package\CommunityTranslation\Controller\Frontend\OnlineTranslation::delete_glossary_entry',
null,
['localeID' => $localeRegex],
[],
'',
[],
['POST'],
],
"{$onlineTranslationPath}/action/download/{localeID}" => [
'Concrete\Package\CommunityTranslation\Controller\Frontend\OnlineTranslation::download',
null,
['localeID' => $localeRegex],
[],
'',
[],
['POST'],
],
"{$onlineTranslationPath}/action/upload/{localeID}" => [
'Concrete\Package\CommunityTranslation\Controller\Frontend\OnlineTranslation::upload',
null,
['localeID' => $localeRegex],
[],
'',
[],
['POST'],
],
"{$onlineTranslationPath}/action/save_notifications/{packageID}" => [
'Concrete\Package\CommunityTranslation\Controller\Frontend\OnlineTranslation::save_notifications',
null,
['packageID' => '\d+'],
[],
'',
[],
['POST'],
],
// API Entry Points
"{$apiBasePath}/rate-limit" => [
ApiEntryPoint\GetRateLimit::class . '::__invoke',
null,
[],
[],
'',
[],
['GET'],
],
"{$apiBasePath}/locales" => [
ApiEntryPoint\GetLocales::class . '::__invoke',
null,
[],
[],
'',
[],
['GET'],
],
"{$apiBasePath}/packages" => [
ApiEntryPoint\GetPackages::class . '::__invoke',
null,
[],
[],
'',
[],
['GET'],
],
"{$apiBasePath}/package/{packageHandle}/versions" => [
ApiEntryPoint\GetPackageVersions::class . '::__invoke',
null,
['packageHandle' => $handleRegex],
[],
'',
[],
['GET'],
],
"{$apiBasePath}/package/{packageHandle}/{packageVersion}/locales/{minimumLevel}" => [
ApiEntryPoint\GetPackageVersionLocales::class . '::__invoke',
null,
['packageHandle' => $handleRegex, 'minimumLevel' => '[0-9]{1,3}'],
[],
'',
[],
['GET'],
],
"{$apiBasePath}/package/{packageHandle}/{packageVersion}/translations/{localeID}/{formatHandle}" => [
ApiEntryPoint\GetPackageVersionTranslations::class . '::__invoke',
null,
['packageHandle' => $handleRegex, 'localeID' => $localeRegex, 'formatHandle' => $handleRegex],
[],
'',
[],
['GET'],
],
"{$apiBasePath}/fill-translations/{formatHandle}" => [
ApiEntryPoint\FillTranslations::class . '::__invoke',
null,
['formatHandle' => $handleRegex],
[],
'',
[],
['POST'],
],
"{$apiBasePath}/package/{packageHandle}/{packageVersion}/translatables/{formatHandle}" => [
ApiEntryPoint\ImportPackageVersionTranslatables::class . '::__invoke',
null,
['packageHandle' => $handleRegex, 'formatHandle' => $handleRegex],
[],
'',
[],
['POST'],
],
"{$apiBasePath}/translations/{localeID}/{formatHandle}/{approve}" => [
ApiEntryPoint\ImportTranslations::class . '::__invoke',
null,
['localeID' => $localeRegex, 'formatHandle' => $handleRegex, 'approve' => '[01]'],
[],
'',
[],
['POST'],
],
"{$apiBasePath}/import/package" => [
ApiEntryPoint\ImportPackage::class . '::__invoke',
null,
[],
[],
'',
[],
['PUT'],
],
"{$apiBasePath}/{unrecognizedPath}" => [
ApiEntryPoint\Unrecognized::class . '::__invoke',
null,
['unrecognizedPath' => '.*'],
],
"{$apiBasePath}" => [
ApiEntryPoint\Unrecognized::class . '::__invoke',
],
]);
}
}