PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.8.3
Import WP – CSV & XML Import Export for WordPress v2.8.3
2.15.1 2.15.0 2.14.24 2.14.23 2.7.0 2.7.1 2.7.10 2.7.11 2.7.12 2.7.13 2.7.14 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.9.0 2.9.1 All 144 releases
jc-importer / class / Common / Runner / ImporterRunner.php

ImporterRunner.php in Import WP – CSV & XML Import Export for WordPress 2.8.3, at class/Common/Runner/ImporterRunner.php

238 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImportWP\Common\Runner;
4
5 use ImportWP\Common\Importer\DataParser;
6 use ImportWP\Common\Importer\Exception\FileException;
7 use ImportWP\Common\Importer\Exception\MapperException;
8 use ImportWP\Common\Importer\Exception\ParserException;
9 use ImportWP\Common\Importer\Importer;
10 use ImportWP\Common\Util\Logger;
11 use ImportWP\Common\Util\Util;
12
13 class ImporterRunner extends Runner
14 {
15 /**
16 * @var Importer
17 */
18 protected $importer;
19
20 /**
21 * @var string
22 */
23 protected $object_type = 'importer';
24
25 public function __construct($properties, $importer)
26 {
27 parent::__construct($properties);
28 $this->importer = $importer;
29 }
30
31 public function setup($state, $state_data, $config, $user, $id)
32 {
33 $state->populate($state_data);
34
35 if (!$state->validate($config['id'])) {
36 throw new \Exception("Importer session has changed");
37 }
38
39 if ($state->has_status('running')) {
40
41 $section = $state->get_section();
42 if (isset($state_data['progress'][$section]) && $state_data['progress'][$section]['end'] - $state_data['progress'][$section]['start'] <= $state_data['progress'][$section]['current_row']) {
43
44 // Does this user or any user have any dangling jobs?
45 $dangling = $this->try_process_dangling_state($id, $user, $state);
46 if (!$dangling) {
47 $this->is_timeout = true;
48 $state_data['duration'] = floatval($state_data['duration']) + Logger::timer();
49 return $state_data;
50 }
51
52 switch ($state->get_section()) {
53 case 'import':
54
55 if ($this->importer->getMapper()->permission() && $this->importer->getMapper()->permission()->allowed_method('remove')) {
56
57 // importer delete
58 $state_data['section'] = 'delete';
59
60 // generate list of items to be deleted
61 $object_ids = $this->importer->getMapper()->get_objects_for_removal();
62
63 $config = get_site_option('iwp_importer_config_' . $id);
64 $config['delete_ids'] = $object_ids;
65 update_site_option('iwp_importer_config_' . $id, $config);
66
67 $state_data['progress']['delete']['start'] = 0;
68 $state_data['progress']['delete']['end'] = $object_ids ? count($object_ids) : 0;
69 } else {
70 $state_data['section'] = '';
71 $state_data['status'] = 'complete';
72 }
73
74 break;
75 case 'delete':
76
77 // importer complete
78 $state_data['section'] = '';
79 $state_data['status'] = 'complete';
80
81 break;
82 }
83 }
84
85 // Get increase index, locking record, and saving to user importer state
86 if (!empty($state_data['section'])) {
87 $state_data['progress'][$state_data['section']]['current_row']++;
88 update_site_option('iwp_importer_state_' . $id . '_' . $user, array_merge($state_data, ['last_modified' => current_time('timestamp')]));
89 }
90 }
91
92 $state_data['duration'] = floatval($state_data['duration'] ?? 0) + Logger::timer();
93
94 return $state_data;
95 }
96
97 public function process_row($id, $user, $importer_state, $session, $section, $progress)
98 {
99 $stats = [
100 'inserts' => 0,
101 'updates' => 0,
102 'deletes' => 0,
103 'skips' => 0,
104 'errors' => 0,
105 ];
106
107 if ($section === 'import') {
108
109
110 // TODO: Run through field map from config (xml or csv)
111 $data_parser = new DataParser($this->importer->getParser(), $this->importer->getMapper(), $this->importer->config->getData());
112
113 $i = $progress['start'] + $progress['current_row'] - 1;
114
115 /**
116 * @var ParsedData $data
117 */
118 $data = null;
119
120 try {
121
122 $data = $data_parser->get($i);
123
124 $skip_record = $this->importer->filterRecords();
125 $skip_record = apply_filters('iwp/importer/skip_record', $skip_record, $data, $this->importer);
126
127 if ($skip_record) {
128
129 Logger::write('import -skip-record=' . $i);
130
131 $stats['skips']++;
132
133 // set data to null, to flag chunk as skipped
134 Util::write_status_log_file_message($id, $session, "Skipped Record", 'S', $progress['current_row']);
135
136 $data = null;
137 } else {
138
139 // import
140 $data = apply_filters('iwp/importer/before_mapper', $data, $this->importer);
141 $data->map();
142
143 if ($data->isInsert()) {
144
145 Logger::write('import:' . $i . ' -success -insert');
146
147 $stats['inserts']++;
148
149 $message = apply_filters('iwp/status/record_inserted', 'Record Inserted: #' . $data->getId(), $data->getId(), $data);
150 Util::write_status_log_file_message($id, $session, $message, 'S', $progress['current_row']);
151 }
152
153 if ($data->isUpdate()) {
154
155 Logger::write('import:' . $i . ' -success -update');
156
157 $stats['updates']++;
158
159 $message = apply_filters('iwp/status/record_updated', 'Record Updated: #' . $data->getId(), $data->getId(), $data);
160 Util::write_status_log_file_message($id, $session, $message, 'S', $progress['current_row']);
161 }
162 }
163 } catch (ParserException $e) {
164
165 $stats['errors']++;
166 Logger::error('import:' . $i . ' -parser-error=' . $e->getMessage());
167 Util::write_status_log_file_message($id, $session, $e->getMessage(), 'E', $progress['current_row']);
168 } catch (MapperException $e) {
169
170 $stats['errors']++;
171 Logger::error('import:' . $i . ' -mapper-error=' . $e->getMessage());
172 Util::write_status_log_file_message($id, $session, $e->getMessage(), 'E', $progress['current_row']);
173 } catch (FileException $e) {
174
175 $stats['errors']++;
176 Logger::error('import:' . $i . ' -file-error=' . $e->getMessage());
177 Util::write_status_log_file_message($id, $session, $e->getMessage(), 'E', $progress['current_row']);
178 }
179
180 $this->update_importer_stats($importer_state, $stats);
181 Util::write_status_session_to_file($id, $importer_state);
182
183 delete_site_option('iwp_importer_state_' . $id . '_' . $user);
184 return;
185 }
186
187 if ($section === 'delete') {
188 if ($this->importer->getMapper()->permission() && $this->importer->getMapper()->permission()->allowed_method('remove')) {
189
190 $GLOBALS['wp_object_cache']->delete('iwp_importer_config_' . $id, 'options');
191 $config = get_site_option('iwp_importer_config_' . $id);
192 $i = $progress['current_row'] - 1;
193
194 $object_ids = $config['delete_ids'];
195 if ($object_ids && count($object_ids) > $i) {
196 $object_id = $object_ids[$i];
197 $this->importer->getMapper()->delete($object_id);
198 $stats['deletes']++;
199
200 Logger::write('delete:' . $i . ' -object=' . $object_id);
201
202 $message = apply_filters('iwp/status/record_deleted', 'Record Deleted: #' . $object_id, $object_id);
203 Util::write_status_log_file_message($id, $session, $message, 'D', $progress['current_row']);
204 }
205 }
206
207 $this->update_importer_stats($importer_state, $stats);
208 Util::write_status_session_to_file($id, $importer_state);
209
210 delete_site_option('iwp_importer_state_' . $id . '_' . $user);
211 return;
212 }
213 }
214
215 function update_importer_stats($importer_state, $stats)
216 {
217 $importer_state->update(function ($state) use ($stats) {
218 if (!isset($state['stats'])) {
219 $state['stats'] = [
220 'inserts' => 0,
221 'updates' => 0,
222 'deletes' => 0,
223 'skips' => 0,
224 'errors' => 0,
225 ];
226 }
227
228 $state['stats']['inserts'] += $stats['inserts'];
229 $state['stats']['updates'] += $stats['updates'];
230 $state['stats']['deletes'] += $stats['deletes'];
231 $state['stats']['skips'] += $stats['skips'];
232 $state['stats']['errors'] += $stats['errors'];
233
234 return $state;
235 });
236 }
237 }
238