wpml-st-translations-file-queue.php
5.17 KB
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
<?php
use WPML\ST\TranslationFile\EntryQueries;
use WPML\ST\TranslationFile\QueueFilter;
class WPML_ST_Translations_File_Queue {
const DEFAULT_LIMIT = 20000;
const TIME_LIMIT = 10; // seconds
const LOCK_FIELD = '_wpml_st_file_scan_in_progress';
/** @var WPML_ST_Translations_File_Dictionary */
private $file_dictionary;
/** @var WPML_ST_Translations_File_Scan */
private $file_scan;
/** @var WPML_ST_Translations_File_Scan_Storage */
private $file_scan_storage;
/** @var WPML_Language_Records */
private $language_records;
/** @var int */
private $limit;
/** @var WPML_Transient */
private $transient;
/**
* @param WPML_ST_Translations_File_Dictionary $file_dictionary
* @param WPML_ST_Translations_File_Scan $file_scan
* @param WPML_ST_Translations_File_Scan_Storage $file_scan_storage
* @param WPML_Language_Records $language_records
* @param int $limit
* @param WPML_Transient $transient
*/
public function __construct(
WPML_ST_Translations_File_Dictionary $file_dictionary,
WPML_ST_Translations_File_Scan $file_scan,
WPML_ST_Translations_File_Scan_Storage $file_scan_storage,
WPML_Language_Records $language_records,
$limit,
WPML_Transient $transient
) {
$this->file_dictionary = $file_dictionary;
$this->file_scan = $file_scan;
$this->file_scan_storage = $file_scan_storage;
$this->language_records = $language_records;
$this->limit = $limit;
$this->transient = $transient;
}
/**
* @param QueueFilter|null $queueFilter
*/
public function import( QueueFilter $queueFilter = null ) {
$this->file_dictionary->clear_skipped();
$files = $this->file_dictionary->get_not_imported_files();
if ( count( $files ) ) {
$this->lock();
$start_time = time();
$imported = 0;
foreach ( $files as $file ) {
if ( $imported >= $this->limit || time() - $start_time > self::TIME_LIMIT ) {
break;
}
if ( ! $queueFilter || $queueFilter->isSelected( $file ) ) {
$translations = $this->file_scan->load_translations( $file->get_full_path() );
try {
$number_of_translations = count( $translations );
if ( ! $number_of_translations ) {
throw new RuntimeException( 'File is empty' );
}
$translations = $this->constrain_translations_number(
$translations,
$file->get_imported_strings_count(),
$this->limit - $imported
);
$imported += $imported_in_file = count( $translations );
$this->file_scan_storage->save(
$translations,
$file->get_domain(),
$this->map_language_code( $file->get_file_locale() )
);
$file->set_imported_strings_count( $file->get_imported_strings_count() + $imported_in_file );
if ( $file->get_imported_strings_count() >= $number_of_translations ) {
$file->set_status( WPML_ST_Translations_File_Entry::IMPORTED );
} else {
$file->set_status( WPML_ST_Translations_File_Entry::PARTLY_IMPORTED );
}
} catch ( WPML_ST_Bulk_Strings_Insert_Exception $e ) {
$file->set_status( WPML_ST_Translations_File_Entry::PARTLY_IMPORTED );
break;
} catch ( Exception $e ) {
$file->set_status( WPML_ST_Translations_File_Entry::IMPORTED );
}
} else {
$file->set_status( WPML_ST_Translations_File_Entry::SKIPPED );
}
$this->file_dictionary->save( $file );
do_action( 'wpml_st_translations_file_post_import', $file );
}
$this->unlock();
}
}
/**
* @param string $locale
*
* @return string
*/
private function map_language_code( $locale ) {
$language_code = $this->language_records->get_language_code( $locale );
if ( $language_code ) {
return $language_code;
}
return $locale;
}
/**
* @return bool
*/
public function is_completed() {
return 0 === count( $this->file_dictionary->get_not_imported_files() ) &&
0 < count( $this->file_dictionary->get_imported_files() );
}
/**
* @return string[]
*/
public function get_processed() {
return wp_list_pluck( $this->file_dictionary->get_imported_files(), 'path' );
}
/**
* @return bool
*/
public function is_processing() {
return 0 !== count( $this->file_dictionary->get_not_imported_files() );
}
/**
* @return int
*/
public function get_pending() {
return count( $this->file_dictionary->get_not_imported_files() );
}
public function mark_as_finished() {
foreach ( $this->file_dictionary->get_imported_files() as $file ) {
$file->set_status( WPML_ST_Translations_File_Entry::FINISHED );
$this->file_dictionary->save( $file );
}
}
/**
* @param array $translations
* @param int $offset
* @param int $limit
*
* @return array
*/
private function constrain_translations_number( array $translations, $offset, $limit ) {
if ( $limit > count( $translations ) ) {
return $translations;
}
return array_slice( $translations, $offset, $limit );
}
public function is_locked() {
return (bool) $this->transient->get( self::LOCK_FIELD );
}
private function lock() {
$this->transient->set( self::LOCK_FIELD, 1, MINUTE_IN_SECONDS * 5 );
}
private function unlock() {
$this->transient->delete( self::LOCK_FIELD );
}
}