|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author Donatas Navidonskis <[email protected]> |
|
5
|
|
|
* @since 2017 |
|
6
|
|
|
* @class LangCollectorTask |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
class LangCollectorTask extends BuildTask { |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
protected $title = "Lang Collector Task"; |
|
12
|
|
|
|
|
13
|
|
|
protected $description = "Parameters: |
|
14
|
|
|
- module: One or more modules to limit collection (comma-separated) |
|
15
|
|
|
- merge: Merge new strings with existing ones already defined (default: FALSE) |
|
16
|
|
|
- example: /dev/tasks/LangCollectorTask \"module=mysite,themes/default&merge=true\" |
|
17
|
|
|
"; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Merge the same created entities if task is running again |
|
21
|
|
|
* |
|
22
|
|
|
* @var bool |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $merge = false; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Given modules from the user |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $module = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Text Collector instance to parse an entities |
|
35
|
|
|
* |
|
36
|
|
|
* @var i18nTextCollector |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $textCollector; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Initialize within permissions |
|
42
|
|
|
* |
|
43
|
|
|
* @return SS_HTTPResponse |
|
44
|
|
|
*/ |
|
45
|
|
|
public function init() { |
|
46
|
|
|
parent::init(); |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
$canAccess = (Director::isDev() || Director::is_cli() || Permission::check("ADMIN")); |
|
49
|
|
|
if (! $canAccess) { |
|
50
|
|
|
return Security::permissionFailure($this); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function run($request) { |
|
55
|
|
|
increase_time_limit_to(); |
|
56
|
|
|
|
|
57
|
|
|
$this->writeMessage($this->description); |
|
58
|
|
|
|
|
59
|
|
|
try { |
|
60
|
|
|
$this->mergeWith($request->getVars()); |
|
61
|
|
|
} catch (LangCollectorException $ex) { |
|
62
|
|
|
$this->writeMessage($ex->getMessage()); |
|
63
|
|
|
exit; |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$this->runCollector(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Run text collector |
|
71
|
|
|
* |
|
72
|
|
|
* @return void |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function runCollector() { |
|
75
|
|
|
$this->textCollector = new i18nTextCollector(); |
|
76
|
|
|
|
|
77
|
|
|
$modules = @$this->textCollector->collect($this->module, $this->merge); |
|
78
|
|
|
|
|
79
|
|
|
foreach ($modules as $moduleName => $entities) { |
|
80
|
|
|
if (count($entities) <= 0) { |
|
81
|
|
|
continue; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// find or create a new module |
|
85
|
|
|
$module = LangModule::findOrCreate($moduleName); |
|
86
|
|
|
$this->writeMessage("Collecting module {$module->Name}"); |
|
87
|
|
|
|
|
88
|
|
|
foreach ($entities as $namespace => $options) { |
|
89
|
|
|
$value = $options[0]; |
|
90
|
|
|
$title = isset($options[1]) ? $options[1] : ''; |
|
91
|
|
|
|
|
92
|
|
|
$entity = $module->mergeOrAddEntity($namespace, $value, $title, $this->merge); |
|
93
|
|
|
|
|
94
|
|
|
if ($entity instanceof LangEntity) { |
|
95
|
|
|
$this->writeMessage("{$namespace} - $value"); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Write output message |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $message |
|
105
|
|
|
* |
|
106
|
|
|
* @return void |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function writeMessage($message) { |
|
109
|
|
|
Debug::message($message, false); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Collect parameters from given options array and merge |
|
114
|
|
|
* it with class properties |
|
115
|
|
|
* |
|
116
|
|
|
* @param array $options |
|
117
|
|
|
* |
|
118
|
|
|
* @return void |
|
119
|
|
|
* @throws EmptyModuleException |
|
120
|
|
|
* @throws InvalidLocaleException |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function mergeWith($options = []) { |
|
123
|
|
|
if (! array_key_exists('module', $options) || empty($options['module'])) { |
|
124
|
|
|
throw new EmptyModuleException("Please set one or more (comma-separated) module names"); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$this->module = explode(',', $options['module']); |
|
128
|
|
|
|
|
129
|
|
|
if (array_key_exists('merge', $options)) { |
|
130
|
|
|
$this->merge = (bool) $options['merge']; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.