-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsynchronizer.php
330 lines (293 loc) · 11.4 KB
/
synchronizer.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
<?php
/**
* Abstract base class for synchronizers
*
* @author gharlan
*/
abstract class rex_developer_synchronizer
{
const ID_FILE = '.rex_id';
const IGNORE_FILE = '.rex_ignore';
/** Force the current status in db */
const FORCE_DB = 1;
/** Force the current status in file system */
const FORCE_FILES = 2;
protected $dirname;
protected $baseDir;
protected $files;
/**
* Constructor
*
* @param string $dirname Name of directory, which will be used for this synchronizer
* @param string[] $files Array of file names, which will be synchronized within each item directory
*/
public function __construct($dirname, array $files)
{
$this->dirname = $dirname;
$this->baseDir = rex_developer_manager::getBasePath() . '/' . $dirname . '/';
$this->files = $files;
}
/**
* The method should return all items from the base system which should be synchronized to the file system
*
* @return rex_developer_synchronizer_item[]
*/
abstract protected function getItems();
/**
* The method is called, when a new item is created by the file system
*
* Use the method to add the new item to the base system. The method should return the new ID of the item.
*
* @param rex_developer_synchronizer_item $item New item
* @return int ID of new item
*/
abstract protected function addItem(rex_developer_synchronizer_item $item);
/**
* The method is called, when an existing item is edited by the file system
*
* Use the method to edit the item in the base system
*
* @param rex_developer_synchronizer_item $item
*/
abstract protected function editItem(rex_developer_synchronizer_item $item);
/**
* The method is called, when an existing item is deleted by the file system (`FORCE_FILES` is activated)
*
* Use the method to delete the item in the base system
*
* @param rex_developer_synchronizer_item $item
*/
protected function deleteItem(rex_developer_synchronizer_item $item)
{
}
/**
* Runs the synchronizer
*
* @param bool $force Flag, whether the synchronizers should run in force mode (`rex_developer_synchronizer::FORCE_DB/FILES`)
*/
public function run($force = false)
{
$idLists = rex_config::get('developer', 'items', array());
$idList = isset($idLists[$this->dirname]) ? $idLists[$this->dirname] : array();
if (isset($idList[0])) {
$idList = array_flip($idList);
}
$origIdList = $idList;
list($existing, $new) = $this->getNewAndExistingDirs();
$this->synchronizeReceivedItems($idList, $existing, $force);
$this->removeItems($idList, $existing, $force);
$this->addNewItems($idList, $existing, true);
$this->addNewItems($idList, $new, false);
if ($idList !== $origIdList) {
$idLists[$this->dirname] = $idList;
rex_config::set('developer', 'items', $idLists);
}
}
private function getNewAndExistingDirs()
{
$existing = array();
$new = array();
$dirs = self::glob($this->baseDir . '*', GLOB_ONLYDIR | GLOB_NOSORT | GLOB_MARK);
if (is_array($dirs)) {
foreach ($dirs as $dir) {
if (!file_exists($dir . self::IGNORE_FILE)) {
$file = basename(self::getFile($dir, self::ID_FILE));
if (
file_exists($dir . $file) &&
(sscanf($file, '%d' . self::ID_FILE, $id) || ($id = ((int) rex_file::get($dir . $file))))
) {
if (isset($existing[$id])) {
trigger_error(
'There are two item directories with the same ID: "' . $existing[$id] . '" and "' . basename($dir) . '"',
E_USER_ERROR
);
}
$existing[$id] = basename($dir);
} else {
$new[] = basename($dir);
}
}
}
}
return array($existing, $new);
}
private function synchronizeReceivedItems(&$idList, &$existing, $force = false)
{
$force = $force ? (int) $force : false;
foreach ($this->getItems() as $item) {
$id = $item->getId();
$name = $item->getName();
$existingDir = null;
if (isset($existing[$id])) {
$existingDir = $existing[$id];
unset($existing[$id]);
} elseif (self::FORCE_FILES === $force) {
$this->deleteItem($item);
unset($idList[$id]);
continue;
}
if (rex_config::get('developer', 'rename') || !$existingDir) {
$dirBase = self::getFilename($name);
if (rex_config::get('developer', 'dir_suffix')) {
$dirBase .= ' ['.$id.']';
}
$dir = $dirBase;
$i = 1;
while ((!$existingDir || !self::equalFilenames($existingDir, $dir)) && file_exists($this->baseDir . $dir)) {
$dir = $dirBase . ' [' . ++$i . ']';
}
if (!$existingDir) {
if (!rex_file::put($this->baseDir . $dir . '/' . $id . self::ID_FILE, '')) {
continue;
}
} elseif (!self::equalFilenames($existingDir, $dir)) {
rename($this->baseDir . $existingDir, $this->baseDir . $dir);
}
$dir = $this->baseDir . $dir . '/';
} else {
$dir = $this->baseDir . $existingDir . '/';
}
$lastUpdated = self::FORCE_DB !== $force && isset($idList[$id]) ? $idList[$id] : 0;
$updated = self::FORCE_FILES === $force ? 0 : max(1, $item->getUpdated());
$dbUpdated = $updated;
$updateFiles = array();
$files = array();
$prefix = '';
if (rex_config::get('developer', 'prefix')) {
$prefix = $id . '.' . $name . '.';
}
foreach ($this->files as $file) {
$filePath = self::getFile($dir, $file, $prefix, rex_config::get('developer', 'rename'));
$files[] = $filePath;
$fileMtime = @filemtime($filePath);
$fileExists = $fileMtime !== false;
$fileUpdated = self::FORCE_DB !== $force && $fileExists ? $fileMtime : 0;
if ($dbUpdated > $fileUpdated && $dbUpdated > $lastUpdated || !$fileExists) {
rex_file::put($filePath, $item->getFile($file));
touch($filePath, $updated);
} elseif ($fileUpdated > $dbUpdated) {
$updated = max($updated, $fileUpdated);
$updateFiles[$file] = rex_file::get($filePath);
}
}
if ($dbUpdated != $updated) {
$this->editItem(new rex_developer_synchronizer_item($id, $name, $updated, $updateFiles));
}
$idList[$id] = $updated;
}
}
private function removeItems(&$idList, &$existing, $force = false)
{
if (self::FORCE_FILES === $force) {
return;
}
foreach ($existing as $id => $dir) {
$dir = $this->baseDir . $dir . '/';
if (self::FORCE_DB === $force || isset($idList[$id])) {
unset($existing[$id]);
unset($idList[$id]);
if (rex_config::get('developer', 'delete')) {
rex_dir::delete($dir);
} else {
rex_file::put($dir . self::IGNORE_FILE, '');
unlink(self::getFile($dir, self::ID_FILE));
}
}
}
}
private function addNewItems(&$idList, $dirs, $withId)
{
foreach ($dirs as $i => $dir) {
$dir = $this->baseDir . $dir . '/';
$addFiles = array();
$add = false;
$updated = time();
foreach ($this->files as $file) {
$filePath = self::getFile($dir, $file);
if (file_exists($filePath)) {
$add = true;
$addFiles[$file] = rex_file::get($filePath);
touch($filePath, $updated);
} else {
$addFiles[$file] = '';
}
}
$id = $withId ? $i : null;
$name = strtr(basename($dir), '_', ' ');
if ($add && $id = $this->addItem(new rex_developer_synchronizer_item($id, $name, $updated, $addFiles))) {
rex_file::put($dir . $id . self::ID_FILE, '');
$idList[$id] = $updated;
}
}
}
/**
* Gets the real path for an item file
*
* Item files can be prefixed by the user, so e.g. "example.template.php" will match the item file "template.php"
*
* @param string $dir Directory
* @param string $file File name
* @param string $defaultPrefix Default prefix
* @param bool $rename
* @return string Real File path
*/
protected static function getFile($dir, $file, $defaultPrefix = '', $rename = false)
{
$defaultPath = $dir . self::getFilename($defaultPrefix . $file);
if (file_exists($defaultPath)) {
$path = $defaultPath;
} elseif (file_exists($dir . $file)) {
$path = $dir . $file;
} elseif (is_array($glob = self::glob($dir . '*' . $file)) && !empty($glob)) {
$path = $dir . basename($glob[0]);
}
if (isset($path)) {
if ($rename && !self::equalFilenames($path, $defaultPath)) {
rename($path, $defaultPath);
return $defaultPath;
}
return $path;
}
return $defaultPath;
}
/**
* Replaces special chars
*
* @param string $name
* @return string
*/
protected static function getFilename($name)
{
if (!rex_config::get('developer', 'umlauts')) {
$name = str_replace(
array('Ä', 'Ö', 'Ü', 'ä', 'ö', 'ü', 'ß'),
array('Ae', 'Oe', 'Ue', 'ae', 'oe', 'ue', 'ss'),
$name
);
$name = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $name);
}
$name = preg_replace('@[\\\\|:<>?*"\'+]@', '', $name);
$name = strtr($name, '[]/', '()-');
return ltrim(rtrim($name, ' .'));
}
/**
* Checks whether the filenames are equal (independent of UTF8 NFC and NFD)
*
* @param string $filename1
* @param string $filename2
* @return bool
*/
protected static function equalFilenames($filename1, $filename2)
{
$search = array("A\xcc\x88", "O\xcc\x88", "U\xcc\x88", "a\xcc\x88", "o\xcc\x88", "u\xcc\x88");
$replace = array('Ä', 'Ö', 'Ü', 'ä', 'ö', 'ü');
$filename1 = str_replace($search, $replace, $filename1);
$filename2 = str_replace($search, $replace, $filename2);
return $filename1 === $filename2;
}
public static function glob($pattern, $flags = 0)
{
$pattern = str_replace(['[',']',"\f[","\f]"], ["\f[","\f]",'[[]','[]]'], $pattern);
return glob($pattern, $flags);
}
}