1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Extractor |
5
|
|
|
* @author Iurii Makukh <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2015, Iurii Makukh |
7
|
|
|
* @license https://www.gnu.org/licenses/gpl.html GNU/GPLv3 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace gplcart\modules\extractor\handlers; |
11
|
|
|
|
12
|
|
|
use gplcart\core\models\Translation; |
13
|
|
|
use gplcart\modules\extractor\models\Extractor as ExtractorModel; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* String extractor handler |
17
|
|
|
*/ |
18
|
|
|
class Extractor |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Translation UI model class instance |
23
|
|
|
* @var \gplcart\core\models\Translation $translation |
24
|
|
|
*/ |
25
|
|
|
protected $translation; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Extractor model instance |
29
|
|
|
* @var \gplcart\modules\extractor\models\Extractor $extractor |
30
|
|
|
*/ |
31
|
|
|
protected $extractor; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param ExtractorModel $extractor |
35
|
|
|
* @param Translation $translation |
36
|
|
|
*/ |
37
|
|
|
public function __construct(ExtractorModel $extractor, Translation $translation) |
38
|
|
|
{ |
39
|
|
|
$this->extractor = $extractor; |
40
|
|
|
$this->translation = $translation; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Processes one extraction job iteration |
45
|
|
|
* @param array $job |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
public function process(array &$job) |
49
|
|
|
{ |
50
|
|
|
if (!isset($job['context']['offset'])) { |
51
|
|
|
$job['context']['offset'] = 0; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$scanned = $this->extractor->scan(array('directory' => $job['data']['directory'])); |
55
|
|
|
$files = array_slice($scanned, $job['context']['offset'], $job['data']['limit']); |
56
|
|
|
|
57
|
|
|
if (empty($files)) { |
58
|
|
|
$job['status'] = false; |
59
|
|
|
$job['done'] = $job['total']; |
60
|
|
|
return $job; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
foreach ($files as $file) { |
64
|
|
|
foreach ($this->extractor->extractFromFile($file) as $string) { |
65
|
|
|
if (!$this->exists($string, $job)) { |
66
|
|
|
$job['inserted']++; |
67
|
|
|
gplcart_file_csv($job['data']['file'], array($string, '')); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$job['context']['offset'] += count($files); |
73
|
|
|
$job['done'] = $job['context']['offset']; |
74
|
|
|
return $job; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Check if the string already exists in the file |
79
|
|
|
* @param string $string |
80
|
|
|
* @param array $job |
81
|
|
|
* @return boolean |
82
|
|
|
*/ |
83
|
|
|
protected function exists($string, array $job) |
84
|
|
|
{ |
85
|
|
|
// Check core translation for dublicates |
86
|
|
|
if (!empty($job['data']['check_file'])) { |
87
|
|
|
$translations = $this->translation->loadTranslation($job['data']['check_file']); |
88
|
|
|
if (isset($translations[$string])) { |
89
|
|
|
return true; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// Check the string already written |
94
|
|
|
$handle = fopen($job['data']['file'], 'r'); |
95
|
|
|
|
96
|
|
|
$found = false; |
97
|
|
|
while (($data = fgetcsv($handle, 1000)) !== false) { |
98
|
|
|
if (isset($data[0]) && $data[0] === $string) { |
99
|
|
|
$found = true; |
100
|
|
|
break; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
fclose($handle); |
105
|
|
|
return $found; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
|