| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Runner; |
| 4 |
|
| 5 |
use ImportWP\Common\Exporter\Mapper\MapperData; |
| 6 |
use ImportWP\Common\Util\Logger; |
| 7 |
|
| 8 |
class ExporterRunner extends Runner |
| 9 |
{ |
| 10 |
|
| 11 |
protected $file; |
| 12 |
|
| 13 |
/** |
| 14 |
* @var MapperInterface |
| 15 |
*/ |
| 16 |
protected $mapper; |
| 17 |
|
| 18 |
/** |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
protected $object_type = 'exporter'; |
| 22 |
|
| 23 |
public function __construct($properties, $mapper, $file) |
| 24 |
{ |
| 25 |
parent::__construct($properties); |
| 26 |
$this->mapper = $mapper; |
| 27 |
$this->file = $file; |
| 28 |
} |
| 29 |
|
| 30 |
public function setup($state, $state_data, $config, $user, $id) |
| 31 |
{ |
| 32 |
$state->populate($state_data); |
| 33 |
|
| 34 |
if (!$state->validate($config['id'])) { |
| 35 |
throw new \Exception("Exporter session has changed"); |
| 36 |
} |
| 37 |
|
| 38 |
$section = 'export'; |
| 39 |
|
| 40 |
if ($state->has_status('running')) { |
| 41 |
|
| 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 |
$state_data['section'] = ''; |
| 53 |
$state_data['status'] = 'end'; |
| 54 |
} |
| 55 |
|
| 56 |
// Get increase index, locking record, and saving to user importer state |
| 57 |
$state_data['progress'][$section]['current_row']++; |
| 58 |
update_site_option('iwp_exporter_state_' . $id . '_' . $user, array_merge($state_data, ['last_modified' => current_time('timestamp')])); |
| 59 |
} |
| 60 |
|
| 61 |
$state_data['duration'] = floatval($state_data['duration'] ?? 0) + Logger::timer(); |
| 62 |
|
| 63 |
return $state_data; |
| 64 |
} |
| 65 |
|
| 66 |
public function process_row($id, $user, $exporter_state, $session, $section, $progress) |
| 67 |
{ |
| 68 |
$stats = [ |
| 69 |
'rows' => 0, |
| 70 |
'skips' => 0, |
| 71 |
'errors' => 0, |
| 72 |
]; |
| 73 |
|
| 74 |
$i = $progress['start'] + $progress['current_row'] - 1; |
| 75 |
|
| 76 |
try { |
| 77 |
|
| 78 |
// TODO: Export row |
| 79 |
|
| 80 |
$mapper_data = new MapperData($this->mapper, $i); |
| 81 |
if (!$mapper_data->skip()) { |
| 82 |
$this->file->add($mapper_data); |
| 83 |
} |
| 84 |
|
| 85 |
Logger::write('export:' . $i . ' -success'); |
| 86 |
$stats['rows']++; |
| 87 |
} catch (\Exception $e) { |
| 88 |
$stats['errors']++; |
| 89 |
Logger::error('export:' . $i . ' -error=' . $e->getMessage()); |
| 90 |
} |
| 91 |
|
| 92 |
$this->update_importer_stats($exporter_state, $stats); |
| 93 |
delete_site_option('iwp_exporter_state_' . $id . '_' . $user); |
| 94 |
} |
| 95 |
|
| 96 |
function update_importer_stats($exporter_state, $stats) |
| 97 |
{ |
| 98 |
$exporter_state->update(function ($state) use ($stats) { |
| 99 |
if (!isset($state['stats'])) { |
| 100 |
$state['stats'] = [ |
| 101 |
'rows' => 0, |
| 102 |
'skips' => 0, |
| 103 |
'errors' => 0, |
| 104 |
]; |
| 105 |
} |
| 106 |
|
| 107 |
$state['stats']['rows'] += $stats['rows']; |
| 108 |
$state['stats']['skips'] += $stats['skips']; |
| 109 |
$state['stats']['errors'] += $stats['errors']; |
| 110 |
|
| 111 |
return $state; |
| 112 |
}); |
| 113 |
} |
| 114 |
} |
| 115 |
|