PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.7.0
Import WP – CSV & XML Import Export for WordPress v2.7.0
2.15.2 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 All 145 releases
jc-importer / class / Common / Importer / ImporterManager.php

ImporterManager.php in Import WP – CSV & XML Import Export for WordPress 2.7.0, at class/Common/Importer/ImporterManager.php

851 lines 26.5 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\Importer;
4
5 use ImportWP\Common\Filesystem\Filesystem;
6 use ImportWP\Common\Importer\Config\Config;
7 use ImportWP\Common\Importer\File\CSVFile;
8 use ImportWP\Common\Importer\File\XMLFile;
9 use ImportWP\Common\Importer\Mapper\PostMapper;
10 use ImportWP\Common\Importer\Mapper\TermMapper;
11 use ImportWP\Common\Importer\Mapper\UserMapper;
12 use ImportWP\Common\Importer\Parser\CSVParser;
13 use ImportWP\Common\Importer\Parser\XMLParser;
14 use ImportWP\Common\Importer\Permission\Permission;
15 use ImportWP\Common\Importer\State\ImporterState;
16 use ImportWP\Common\Importer\Template\CustomPostTypeTemplate;
17 use ImportWP\Common\Importer\Template\PageTemplate;
18 use ImportWP\Common\Importer\Template\PostTemplate;
19 use ImportWP\Common\Importer\Template\Template;
20 use ImportWP\Common\Importer\Template\TemplateManager;
21 use ImportWP\Common\Importer\Template\TermTemplate;
22 use ImportWP\Common\Importer\Template\UserTemplate;
23 use ImportWP\Common\Model\ImporterModel;
24 use ImportWP\Common\Properties\Properties;
25 use ImportWP\Common\Util\Logger;
26 use ImportWP\Common\Util\Util;
27 use ImportWP\Container;
28 use ImportWP\EventHandler;
29
30 class ImporterManager
31 {
32
33 /**
34 * @var Filesystem
35 */
36 private $filesystem;
37
38 /**
39 * @var TemplateManager $template_manager
40 */
41 private $template_manager;
42
43 /**
44 * @var EventHandler $event_handler
45 */
46 protected $event_handler;
47
48 public function __construct(Filesystem $filesystem, TemplateManager $template_manager, EventHandler $event_handler)
49 {
50 $this->filesystem = $filesystem;
51 $this->template_manager = $template_manager;
52 $this->event_handler = $event_handler;
53 }
54
55 /**
56 * Get Importers
57 *
58 * @return ImporterModel[]
59 */
60 public function get_importers()
61 {
62
63 $result = array();
64 $query = new \WP_Query(array(
65 'post_type' => IWP_POST_TYPE,
66 'posts_per_page' => -1,
67 ));
68
69 foreach ($query->posts as $post) {
70 $result[] = $this->get_importer($post);
71 }
72 return $result;
73 }
74
75 /**
76 * Get Importer
77 *
78 * @param int $id
79 * @return ImporterModel
80 */
81 public function get_importer($id)
82 {
83 if ($id instanceof ImporterModel) {
84 return $id;
85 }
86
87 if (IWP_POST_TYPE !== get_post_type($id)) {
88 return false;
89 }
90
91 return new ImporterModel($id, $this->is_debug());
92 }
93
94 public function is_debug()
95 {
96
97 if (defined('IWP_DEBUG') && true === IWP_DEBUG) {
98 return true;
99 }
100
101 $uninstall_enabled = get_option('iwp_settings');
102 if (isset($uninstall_enabled['debug']) && true === $uninstall_enabled['debug']) {
103 return true;
104 }
105
106 return false;
107 }
108
109 public function set_current_user($id)
110 {
111 $importer_model = $this->get_importer($id);
112 $user_id = $importer_model->getUserId();
113
114 Logger::write('set_current_user -user=' . $user_id, $importer_model->getId());
115
116 if ($user_id) {
117 return wp_set_current_user($user_id);
118 }
119
120 return false;
121 }
122
123 /**
124 * Delete Importer
125 *
126 * @param int $id
127 * @return void
128 */
129 public function delete_importer($id)
130 {
131 $importer = $this->get_importer($id);
132 $importer->delete();
133 }
134
135 public function get_file($id)
136 {
137 $importer = $this->get_importer($id);
138 $config = $this->get_config($importer);
139 $parser = $importer->getParser();
140
141 if ('xml' === $parser) {
142 return $this->get_xml_file($importer, $config);
143 } elseif ('csv' === $parser) {
144 return $this->get_csv_file($importer, $config);
145 }
146
147 return false;
148 }
149
150 public function get_csv_file($id, $config)
151 {
152 $importer = $this->get_importer($id);
153 $file = new CSVFile($importer->getFile(), $config);
154 $file->setDelimiter($importer->getFileSetting('delimiter'));
155 $file->setEnclosure($importer->getFileSetting('enclosure'));
156 return $file;
157 }
158
159 public function get_xml_file($id, $config)
160 {
161 $importer = $this->get_importer($id);
162 $file = new XMLFile($importer->getFile(), $config);
163 $file->setRecordPath($importer->getFileSetting('base_path'));
164 return $file;
165 }
166
167 public function preview_csv_file($id, $fields = [], $row = 0)
168 {
169 $importer = $this->get_importer($id);
170 $config = $this->get_config($importer, true);
171
172 $file = $this->get_csv_file($importer, $config);
173 $parser = new CSVParser($file);
174
175 $record = $parser->getRecord($row);
176
177 return $record->queryGroup(['fields' => $fields]);
178 }
179
180 public function preview_xml_file($id, $fields = [], $row = 0)
181 {
182 $importer = $this->get_importer($id);
183 $config = $this->get_config($importer, true);
184
185 $file = $this->get_xml_file($importer, $config);
186 $parser = new XMLParser($file);
187
188 $record = $parser->getRecord($row);
189 return $record->queryGroup(['fields' => $fields]);
190 }
191
192 public function process_csv_file($id, $delimiter, $enclosure, $tmp = false)
193 {
194 $importer = $this->get_importer($id);
195 $config = $this->get_config($importer->getId(), $tmp);
196
197 $file = $this->get_csv_file($importer, $config);
198 $file->setDelimiter($delimiter);
199 $file->setEnclosure($enclosure);
200 $file->processing(true);
201
202 return $file->getRecordCount();
203 }
204
205 public function process_xml_file($id, $tmp = false)
206 {
207
208 $importer = $this->get_importer($id);
209 $config = $this->get_config($importer->getId(), $tmp);
210
211 $filePath = $importer->getFile();
212 $file = new XMLFile($filePath, $config);
213 $file->processing(true);
214 $nodes = $file->get_node_list();
215 $results = [];
216
217 foreach ($nodes as $node) {
218 $config = $this->get_config($importer->getId(), $tmp);
219 $file = new XMLFile($filePath, $config);
220 $file->setRecordPath($node);
221 // TODO: Seperate record count, to when a node has been selected.
222 $results[$node] = 0; //$file->getRecordCount();
223 }
224
225 return $results;
226 }
227
228 /**
229 * Link import file to importer via post meta
230 *
231 * @param ImporterModel $id
232 * @param string $file_path
233 * @return integer Id of inserted file
234 */
235 public function link_importer_file($id, $file_path)
236 {
237 $importer = $this->get_importer($id);
238 $index = get_post_meta($importer->getId(), '_importer_files', true);
239 if (!$index) {
240 $index = 1;
241 }
242
243 $index++;
244
245 update_post_meta($importer->getId(), '_importer_files', $index);
246 update_post_meta($importer->getId(), '_importer_file_' . $index, $file_path);
247 return $index;
248 }
249
250 public function get_importer_file_prefix($importer)
251 {
252 $importer = $this->get_importer($importer);
253
254 $file_index = get_post_meta($importer->getId(), '_importer_files', true);
255 if (!$file_index) {
256 $file_index = 1;
257 }
258 $file_index++;
259
260 return $importer->getId() . '-' . intval($file_index) . '-';
261 }
262
263 public function upload_file($id, $file)
264 {
265 $importer = $this->get_importer($id);
266 Logger::setId($importer->getId());
267
268 $allowed_file_types = $this->event_handler->run('importer.allowed_file_types', [$importer->getAllowedFileTypes()]);
269 $result = $this->filesystem->upload_file($file, $allowed_file_types);
270
271 if (is_wp_error($result)) {
272 return $result;
273 }
274
275 $attachment_id = $this->insert_file_attachment($importer, $result['dest'], $result['type']);
276 if (is_wp_error($attachment_id)) {
277 return $attachment_id;
278 }
279
280 return $attachment_id;
281 }
282
283 public function remote_file($id, $source, $filetype = null)
284 {
285 $importer = $this->get_importer($id);
286 Logger::setId($importer->getId());
287
288 $allowed_file_types = $this->event_handler->run('importer.allowed_file_types', [$importer->getAllowedFileTypes()]);
289 $prefix = $this->get_importer_file_prefix($importer);
290 $result = $this->filesystem->download_file($source, $filetype, $allowed_file_types, null, $prefix);
291
292 if (is_wp_error($result)) {
293 return $result;
294 }
295
296 $attachment_id = $this->insert_file_attachment($importer, $result['dest'], $result['type']);
297 if (is_wp_error($attachment_id)) {
298 return $attachment_id;
299 }
300
301 return $attachment_id;
302 }
303
304 public function local_file($id, $source)
305 {
306 $importer = $this->get_importer($id);
307 Logger::setId($importer->getId());
308
309 $allowed_file_types = $this->event_handler->run('importer.allowed_file_types', [$importer->getAllowedFileTypes()]);
310 $prefix = $this->get_importer_file_prefix($importer);
311 $result = $this->filesystem->copy_file($source, $allowed_file_types, null, $prefix);
312
313 if (is_wp_error($result)) {
314 return $result;
315 }
316
317 $attachment_id = $this->insert_file_attachment($importer, $result['dest'], $result['type']);
318 if (is_wp_error($attachment_id)) {
319 return $attachment_id;
320 }
321
322 return $attachment_id;
323 }
324
325 /**
326 * Add uploaded file to importer
327 *
328 * Insert file record in database, set as current import file.
329 *
330 * @param integer|ImporterModel $id Importer to attach file
331 * @param string $file_path File location on server
332 * @param string $file_type Type of file
333 *
334 * @return \WP_Error|int Id of file
335 */
336 private function insert_file_attachment($id, $file_path, $file_type)
337 {
338 $importer_model = $this->get_importer($id);
339 Logger::setId($importer_model->getId());
340
341 // Allow the modification of file path
342 $file_path = wp_normalize_path($file_path);
343 $file_path = apply_filters('iwp/importer/file_uploaded/file_path', $file_path, $importer_model);
344
345 $file_id = $this->link_importer_file($id, $file_path);
346 if (!$file_id) {
347 return new \WP_Error('IWP_IM_01', 'Unable to link importer file');
348 }
349
350 if (is_null($importer_model->getParser())) {
351 $importer_model->setParser($file_type);
352 }
353
354 $importer_model->setFileId($file_id);
355 $importer_model->save();
356
357 do_action('iwp/importer/file_uploaded', $file_path, $importer_model);
358
359 return $file_id;
360 }
361
362 /**
363 * Clear config files
364 *
365 * @param int $id
366 * @return void
367 */
368 public function clear_config_files($id, $tmp = false, $all = true)
369 {
370 $config_path = $this->get_config_path($id, $tmp);
371 if (file_exists($config_path)) {
372
373 unlink($config_path);
374 if (true === $all) {
375 foreach (glob($config_path . '*') as $file) {
376
377 // don't remove status file
378 if (basename($file) === basename($config_path) . '.status') {
379 continue;
380 }
381
382 unlink($file);
383 }
384 }
385 }
386 }
387
388 public function get_config($importer, $tmp = false)
389 {
390 $config_path = $this->get_config_path($importer, $tmp);
391 $config = new Config($config_path);
392
393
394 $importer = $this->get_importer($importer);
395 $file_encoding = $importer->getFileSetting('file_encoding');
396
397 // file encoding
398 $config->set('file_encoding', apply_filters('iwp/importer/file_encoding', $file_encoding, $importer));
399
400 return $config;
401 }
402
403 public function get_session_path($id, $session, $max_depth = 4)
404 {
405 $base = $this->filesystem->get_temp_directory() . DIRECTORY_SEPARATOR;
406
407 $base .= str_pad($id, 2, STR_PAD_LEFT) . DIRECTORY_SEPARATOR;
408 if (!file_exists($base)) {
409 if (!is_writable(dirname($base)) || !mkdir($base)) {
410 throw new \Exception("Unable to create directory: " . $base);
411 }
412 }
413
414 for ($i = 0; $i < ceil(strlen($session) / 2); $i++) {
415 $base .= substr($session, $i * 2, 2) . DIRECTORY_SEPARATOR;
416 if (!file_exists($base)) {
417
418 if (!is_writable(dirname($base)) || !mkdir($base)) {
419 throw new \Exception("Unable to create directory: " . $base);
420 }
421 }
422
423 if ($i >= $max_depth - 2) {
424 break;
425 }
426 }
427 return $base;
428 }
429
430 public function get_config_path($id, $tmp = false, $session_id = null)
431 {
432 $importer = $this->get_importer($id);
433
434 $key = 'config-%d.json';
435 if ($tmp) {
436 $key = 'temp-config-%d.json';
437 }
438
439 $base = !is_null($session_id) ? $this->get_session_path($id, $session_id) : $this->filesystem->get_temp_directory() . DIRECTORY_SEPARATOR;
440
441 return $base . sprintf($key, $importer->getId());
442 }
443
444 public function import($id, $user, $session = null)
445 {
446 Logger::timer();
447
448 $importer_data = $this->get_importer($id);
449 $importer_id = $importer_data->getId();
450
451 $config_data = get_site_option('iwp_importer_config_' . $importer_id, []);
452
453 $this->event_handler->run('importer_manager.import', [$importer_data]);
454
455 $state = new ImporterState($importer_id, $user);
456
457 try {
458
459 Logger::debug('IM -init_state');
460 $state->init($session);
461
462 if ($state->has_status('init')) {
463 Logger::debug('IM -clear_config_files');
464 $this->clear_config_files($importer_id, false, true);
465 $config_data['features'] = [
466 'session_table' => true
467 ];
468 }
469
470 Logger::debug('IM -get_config');
471 $config = $this->get_config($importer_data);
472
473 // template
474 Logger::debug('IM -get_importer_template');
475 $template = $this->get_importer_template($importer_data);
476
477 Logger::debug('IM -register_hooks');
478 $template->register_hooks($importer_data);
479
480 // permission
481 Logger::debug('IM -permissions');
482 $permission = new Permission($importer_data);
483
484 // mapper
485 Logger::debug('IM -get_importer_mapper');
486 $mapper = $this->get_importer_mapper($importer_data, $template, $permission);
487
488 if ($state->has_status('init')) {
489
490 Logger::debug('IM -generate_config');
491
492 $config_data['data'] = $template->config_field_map($importer_data->getMap());
493 $config->set('data', $config_data['data']);
494
495 $config_data['id'] = $state->get_session();
496
497 // This is used for storing version on imported records
498 update_post_meta($importer_id, '_iwp_session', $config_data['id']);
499
500 // Increase Version
501 $version = get_post_meta($importer_id, '_iwp_version', true);
502 if ($version !== false) {
503 $version++;
504 } else {
505 $version = 0;
506 }
507 update_post_meta($importer_id, '_iwp_version', $version);
508 $config_data['version'] = $version;
509 }
510
511 $start = 0;
512
513 // get parser
514 if ($importer_data->getParser() === 'csv') {
515 Logger::debug('IM -get_csv_file');
516 $file = $this->get_csv_file($importer_data, $config);
517 Logger::debug('IM -load_parser');
518 $parser = new CSVParser($file);
519 if (true === $importer_data->getFileSetting('show_headings')) {
520 $start = 1;
521 }
522 } elseif ($importer_data->getParser() === 'xml') {
523 Logger::debug('IM -get_xml_file');
524 $file = $this->get_xml_file($importer_data, $config);
525 Logger::debug('IM -load_parser');
526 $parser = new XMLParser($file);
527 } else {
528 $parser = apply_filters('iwp/importer/init_parser', false, $importer_data, $config);
529 }
530
531 if ($state->has_status('init')) {
532
533 Logger::debug('IM -get_record_count');
534 $end = $parser->file()->getRecordCount();
535
536 $config_data['start'] = $this->get_start($importer_data, $start);
537 $config_data['end'] = $this->get_end($importer_data, $config_data['start'], $end);
538
539 update_site_option('iwp_importer_config_' . $importer_id, $config_data);
540
541 Logger::debug('IM -update_state');
542 $state->update(function ($state) use ($config_data) {
543
544 Logger::debug('IM -update_state=running');
545
546 $state['id'] = $config_data['id'];
547 $state['status'] = 'running';
548 $state['progress']['import']['start'] = $config_data['start'];
549 $state['progress']['import']['end'] = $config_data['end'];
550 return $state;
551 });
552
553 Logger::debug('IM -write_status_session_to_file');
554 Util::write_status_session_to_file($id, $state);
555 }
556
557 Logger::debug('IM -import');
558 $importer = new \ImportWP\Common\Importer\Importer($config);
559 $importer->parser($parser);
560 $importer->mapper($mapper);
561 $importer->from($config_data['start']);
562 $importer->to($config_data['end']);
563 $importer->filter($importer_data->getFilters());
564 $importer->import($importer_id, $user, $state);
565 Logger::debug('IM -import_complete');
566 } catch (\Exception $e) {
567
568 // TODO: Missing template errors are currently not being logged to history, possibly others?
569 Logger::error('import -error=' . $e->getMessage(), $importer_id);
570 return $state->error($e)->get_raw();
571 }
572
573 /**
574 * @var Properties $properties
575 */
576 $properties = Container::getInstance()->get('properties');
577
578 // rotate files to not fill up server
579 $importer_data->limit_importer_files($properties->file_rotation);
580 $this->prune_importer_logs($importer_data, $properties->log_rotation);
581
582 $template->unregister_hooks();
583
584 $this->event_handler->run('importer_manager.import_shutdown', [$importer_data]);
585
586 return $state->update(function ($data) {
587 $data['duration'] = floatval($data['duration']) + Logger::timer();
588 return $data;
589 })->get_raw();
590 }
591
592 public function get_start($importer_data, $start)
593 {
594 $tmp_start = $importer_data->getStartRow();
595 if (!is_null($tmp_start) && "" !== $tmp_start) {
596 $tmp_start = intval($tmp_start);
597
598 if ($tmp_start > $start) {
599 $start = $tmp_start;
600 }
601 }
602
603 return $start;
604 }
605
606 public function get_end($importer_data, $start, $end)
607 {
608 $tmp_max_row = $importer_data->getMaxRow();
609 if (!is_null($tmp_max_row) && $tmp_max_row !== '') {
610 $tmp_end = $start + intval($tmp_max_row);
611 if ($tmp_end < $end) {
612 $end = $tmp_end;
613 }
614 }
615
616 return $end;
617 }
618
619 public function get_importer_template($id)
620 {
621 $importer_model = $this->get_importer($id);
622 $templates = $this->get_templates();
623 $template_name = $importer_model->getTemplate();
624
625 if (!isset($templates[$template_name])) {
626 $exception_msg = "Unable to locate importer template: " . $template_name;
627 Logger::error('import -get_importer_template=' . $exception_msg, $importer_model->getId());
628 throw new \Exception($exception_msg);
629 }
630
631 return $this->template_manager->load_template($templates[$template_name]);
632 }
633
634 /**
635 * Get importer mapper
636 *
637 * @param int $id
638 * @param Template $template
639 * @param Permission $permission
640 * @return MapperInterface
641 */
642 public function get_importer_mapper($id, $template, $permission = null)
643 {
644 $importer = $this->get_importer($id);
645 $mapper_name = $template->get_mapper();
646
647 $mappers = $this->get_mappers();
648 return isset($mappers[$mapper_name]) ? new $mappers[$mapper_name]($importer, $template, $permission) : false;
649 }
650
651 public function get_mappers()
652 {
653 $mappers = $this->event_handler->run('mappers.register', [[]]); // apply_filters('iwp/mappers/register', []);
654 $mappers = array_merge($mappers, [
655 'post' => PostMapper::class,
656 'user' => UserMapper::class,
657 'term' => TermMapper::class
658 ]);
659 return $mappers;
660 }
661
662 public function get_mapper($key)
663 {
664 $mappers = $this->get_mappers();
665 if (!isset($mappers[$key])) {
666 return new \WP_Error('IWP_IM_1', 'Unable to locate mapper: ' . $key);
667 }
668
669 return $mappers[$key];
670 }
671
672 public function get_templates()
673 {
674 $templates = $this->event_handler->run('templates.register', [[]]);
675 $templates = array_merge($templates, [
676 'post' => PostTemplate::class,
677 'page' => PageTemplate::class,
678 'user' => UserTemplate::class,
679 'term' => TermTemplate::class,
680 'custom-post-type' => CustomPostTypeTemplate::class,
681 ]);
682 return $templates;
683 }
684
685 public function get_template($key)
686 {
687 $templates = $this->get_templates();
688 if (!isset($templates[$key])) {
689 return new \WP_Error('IWP_IM_1', 'Unable to locate template: ' . $key);
690 }
691
692 return $templates[$key];
693 }
694
695 public function get_importer_debug_log(ImporterModel $importer_data, $page = 0, $per_page = -1)
696 {
697 $file_path = Logger::getLogFile($importer_data->getId());
698
699 $line_counter = 0;
700 $lines = [];
701 $start = $end = -1;
702
703 if ($per_page > 0) {
704 $start = ($page - 1) * $per_page;
705 $end = $start + $per_page;
706 }
707
708 if (file_exists($file_path)) {
709 $fh = fopen($file_path, 'r');
710 if ($fh !== false) {
711 while (($data = fgetcsv($fh)) !== false) {
712 if ($line_counter === $end) {
713 return $lines;
714 } elseif ($per_page === -1 || $line_counter >= $start) {
715 $lines[] = $data;
716 }
717
718 $line_counter++;
719 }
720 fclose($fh);
721 }
722 }
723 return $lines;
724 }
725
726 public function prune_importer_logs($importer_model, $limit)
727 {
728 $limit = intval($limit);
729 if ($limit <= -1) {
730 return;
731 }
732
733 $file_path = Util::get_importer_status_file_path($importer_model->getId());
734 $tmp_file_path = Util::get_importer_status_file_path($importer_model->getId()) . '.tmp';
735 $lines = $this->get_importer_logs($importer_model);
736
737 if (count($lines) > $limit) {
738
739 require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php');
740 require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php');
741 $fileSystemDirect = new \WP_Filesystem_Direct(false);
742
743 $fh = fopen($tmp_file_path, 'w');
744 for ($i = 0; $i < count($lines); $i++) {
745
746 if ($i < count($lines) - $limit) {
747 $log_file_path = Util::get_importer_log_file_path($importer_model->getId(), $lines[$i]['id']);
748 if ($fileSystemDirect->exists($log_file_path)) {
749
750 $fileSystemDirect->delete($log_file_path);
751
752 $tmp = $log_file_path;
753 for ($j = 0; $j < 3; $j++) {
754 $tmp = dirname($tmp);
755 $sub_files = $fileSystemDirect->dirlist($log_file_path);
756 if (empty($sub_files)) {
757 $fileSystemDirect->rmdir($tmp);
758 }
759 }
760 }
761 } else {
762 fputs($fh, json_encode($lines[$i]) . "\n");
763 }
764 }
765 fclose($fh);
766
767 if ($fileSystemDirect->move($tmp_file_path, $file_path, true)) {
768 $fileSystemDirect->delete($tmp_file_path);
769 }
770 }
771 }
772
773 public function get_importer_logs(ImporterModel $importer_data, $page = 0, $per_page = -1)
774 {
775 $file_path = Util::get_importer_status_file_path($importer_data->getId());
776
777 $line_counter = 0;
778 $lines = [];
779
780 $start = $end = -1;
781
782 if ($per_page > 0) {
783 $start = ($page - 1) * $per_page;
784 $end = $start + $per_page;
785 }
786
787 if (file_exists($file_path)) {
788 $fh = fopen($file_path, 'r');
789 if ($fh !== false) {
790 while (($data = fgets($fh)) !== false) {
791 if ($data[strlen($data) - 1] === "\n") {
792 $data = substr($data, 0, strlen($data) - 1);
793 }
794
795 if ($line_counter === $end) {
796 return $lines;
797 } elseif ($per_page === -1 || $line_counter >= $start) {
798 $lines[] = json_decode($data, true);
799 }
800
801 $line_counter++;
802 }
803 fclose($fh);
804 }
805 }
806 return $lines;
807 }
808
809 public function get_importer_log(ImporterModel $importer_data, $session_id, $page = 0, $per_page = -1)
810 {
811 $file_path = Util::get_importer_log_file_path($importer_data->getId(), $session_id);
812
813 $line_counter = 0;
814 $lines = [];
815 $start = $end = -1;
816
817 if ($per_page > 0) {
818 $start = ($page - 1) * $per_page;
819 $end = $start + $per_page;
820 }
821
822 if (file_exists($file_path)) {
823 $fh = fopen($file_path, 'r');
824 if ($fh !== false) {
825 while (($data = fgetcsv($fh)) !== false) {
826 if ($line_counter === $end) {
827 return $lines;
828 } elseif ($per_page === -1 || $line_counter >= $start) {
829 $lines[] = $data;
830 }
831
832 $line_counter++;
833 }
834 fclose($fh);
835 }
836 }
837 return $lines;
838 }
839
840 public function get_importer_status_report(ImporterModel $immpoter_data, $session)
841 {
842 $logs = $this->get_importer_logs($immpoter_data);
843 foreach ($logs as $log) {
844 if ($log && isset($log['id']) && $log['id'] === $session) {
845 return $log;
846 }
847 }
848 return false;
849 }
850 }
851