| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Importer; |
| 4 |
|
| 5 |
use ImportWP\Common\Importer\ConfigInterface; |
| 6 |
use ImportWP\Common\Importer\File\CSVFile; |
| 7 |
use ImportWP\Common\Importer\File\XMLFile; |
| 8 |
use ImportWP\Common\Importer\MapperInterface; |
| 9 |
use ImportWP\Common\Importer\Parser\CSVParser; |
| 10 |
use ImportWP\Common\Importer\Parser\XMLParser; |
| 11 |
use ImportWP\Common\Importer\ParserInterface; |
| 12 |
use ImportWP\Common\Importer\State\ImporterState; |
| 13 |
use ImportWP\Common\Properties\Properties; |
| 14 |
use ImportWP\Common\Runner\ImporterRunner; |
| 15 |
use ImportWP\Common\Runner\ImporterRunnerState; |
| 16 |
use ImportWP\Common\Util\Util; |
| 17 |
use ImportWP\Container; |
| 18 |
|
| 19 |
class Importer |
| 20 |
{ |
| 21 |
/** |
| 22 |
* @var ConfigInterface $config |
| 23 |
*/ |
| 24 |
public $config; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var MapperInterface $mapper |
| 28 |
*/ |
| 29 |
private $mapper; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var int $start |
| 33 |
*/ |
| 34 |
private $start; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var int end |
| 38 |
*/ |
| 39 |
private $end; |
| 40 |
|
| 41 |
/** |
| 42 |
* @var ParserInterface $parser |
| 43 |
*/ |
| 44 |
private $parser; |
| 45 |
|
| 46 |
/** |
| 47 |
* Flag used to determine type of shutdown |
| 48 |
* |
| 49 |
* @var boolean |
| 50 |
*/ |
| 51 |
private $graceful_shutdown = true; |
| 52 |
|
| 53 |
/** |
| 54 |
* List of filters that can be applied |
| 55 |
* |
| 56 |
* @var array |
| 57 |
*/ |
| 58 |
private $filter_data = []; |
| 59 |
|
| 60 |
/** |
| 61 |
* @param ConfigInterface $config |
| 62 |
*/ |
| 63 |
public function __construct($config) |
| 64 |
{ |
| 65 |
$this->config = $config; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Set Parser |
| 70 |
* |
| 71 |
* @param ParserInterface $parser |
| 72 |
* |
| 73 |
* @return $this |
| 74 |
*/ |
| 75 |
public function parser($parser) |
| 76 |
{ |
| 77 |
$this->parser = $parser; |
| 78 |
|
| 79 |
return $this; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Set Mapper |
| 84 |
* |
| 85 |
* @param MapperInterface $mapper |
| 86 |
* |
| 87 |
* @return $this |
| 88 |
*/ |
| 89 |
public function mapper(MapperInterface $mapper) |
| 90 |
{ |
| 91 |
$this->mapper = $mapper; |
| 92 |
|
| 93 |
return $this; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Load XML File |
| 98 |
* |
| 99 |
* @param string $file_path |
| 100 |
* |
| 101 |
* @return $this |
| 102 |
*/ |
| 103 |
public function xmlFile($file_path) |
| 104 |
{ |
| 105 |
$file = new XMLFile($file_path, $this->config); |
| 106 |
$this->parser = new XMLParser($file); |
| 107 |
|
| 108 |
return $this; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Load CSV File |
| 113 |
* |
| 114 |
* @param string $file_path |
| 115 |
* |
| 116 |
* @return $this |
| 117 |
*/ |
| 118 |
public function csvFile($file_path) |
| 119 |
{ |
| 120 |
$file = new CSVFile($file_path, $this->config); |
| 121 |
$this->parser = new CSVParser($file); |
| 122 |
|
| 123 |
return $this; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Set record to start importing from |
| 128 |
* |
| 129 |
* @param int $start |
| 130 |
*/ |
| 131 |
public function from($start) |
| 132 |
{ |
| 133 |
$this->start = $start; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Set record to end import at |
| 138 |
* |
| 139 |
* @param int $end |
| 140 |
*/ |
| 141 |
public function to($end) |
| 142 |
{ |
| 143 |
$this->end = $end; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Get Record Start Index |
| 148 |
* |
| 149 |
* @return int |
| 150 |
*/ |
| 151 |
private function getRecordStart() |
| 152 |
{ |
| 153 |
return isset($this->start) && $this->start >= 0 ? $this->start : 0; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Get Record End Index |
| 158 |
* |
| 159 |
* @return int |
| 160 |
*/ |
| 161 |
public function getRecordEnd() |
| 162 |
{ |
| 163 |
return isset($this->end) && $this->end >= $this->getRecordStart() ? $this->end : $this->parser->file()->getRecordCount(); |
| 164 |
} |
| 165 |
|
| 166 |
private function register_shutdown($importer_state) |
| 167 |
{ |
| 168 |
$this->graceful_shutdown = false; |
| 169 |
|
| 170 |
register_shutdown_function(function () use ($importer_state) { |
| 171 |
if ($this->is_graceful_shutdown()) { |
| 172 |
// $this->record_time(); |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
// TODO: Log errors |
| 177 |
$error = error_get_last(); |
| 178 |
if (!is_null($error)) { |
| 179 |
|
| 180 |
$importer_state->update(function ($state) use ($error) { |
| 181 |
$state['status'] = 'error'; |
| 182 |
$state['message'] = $error['message']; |
| 183 |
return $state; |
| 184 |
}); |
| 185 |
|
| 186 |
|
| 187 |
$this->mapper->teardown(); |
| 188 |
echo json_encode($importer_state->get_raw()) . "\n"; |
| 189 |
die(); |
| 190 |
} |
| 191 |
|
| 192 |
$this->mapper->teardown(); |
| 193 |
}); |
| 194 |
} |
| 195 |
|
| 196 |
private function unregister_shutdown() |
| 197 |
{ |
| 198 |
$this->graceful_shutdown = true; |
| 199 |
} |
| 200 |
|
| 201 |
private function is_graceful_shutdown() |
| 202 |
{ |
| 203 |
return $this->graceful_shutdown; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Run Import |
| 208 |
* |
| 209 |
* @param int $id Importer Id |
| 210 |
* @param string $user Unique user id |
| 211 |
* @param ImporterRunnerState $importer_state |
| 212 |
* |
| 213 |
* @throws \Exception |
| 214 |
*/ |
| 215 |
public function import($id, $user, $importer_state) |
| 216 |
{ |
| 217 |
if ($this->parser == null) { |
| 218 |
throw new \Exception("Parser Not Loaded."); |
| 219 |
} |
| 220 |
|
| 221 |
if ($this->mapper == null) { |
| 222 |
throw new \Exception("Mapper Not Loaded."); |
| 223 |
} |
| 224 |
|
| 225 |
$this->mapper->setup(); |
| 226 |
|
| 227 |
$this->register_shutdown($importer_state); |
| 228 |
|
| 229 |
/** |
| 230 |
* @var Util $util |
| 231 |
*/ |
| 232 |
$util = Container::getInstance()->get('util'); |
| 233 |
$util->set_time_limit(); |
| 234 |
|
| 235 |
/** |
| 236 |
* @var Properties $properties |
| 237 |
*/ |
| 238 |
$properties = Container::getInstance()->get('properties'); |
| 239 |
|
| 240 |
$runner = new ImporterRunner($properties, $this); |
| 241 |
$runner->process($id, $user, $importer_state); |
| 242 |
|
| 243 |
Util::write_status_session_to_file($id, $importer_state); |
| 244 |
|
| 245 |
$this->mapper->teardown(); |
| 246 |
$this->unregister_shutdown(); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Apply any importer filters to skip records |
| 251 |
* |
| 252 |
* @return boolean |
| 253 |
*/ |
| 254 |
function filterRecords() |
| 255 |
{ |
| 256 |
$result = false; |
| 257 |
|
| 258 |
if (empty($this->filter_data)) { |
| 259 |
return $result; |
| 260 |
} |
| 261 |
|
| 262 |
foreach ($this->filter_data as $group) { |
| 263 |
|
| 264 |
$result = true; |
| 265 |
|
| 266 |
if (empty($group)) { |
| 267 |
continue; |
| 268 |
} |
| 269 |
|
| 270 |
foreach ($group as $row) { |
| 271 |
|
| 272 |
$left = trim($this->parser->query_string($row['left'])); |
| 273 |
$right = $row['right']; |
| 274 |
$right_parts = array_map('trim', explode(',', $right)); |
| 275 |
|
| 276 |
switch ($row['condition']) { |
| 277 |
case 'equal': |
| 278 |
if (strcasecmp($left, $right) !== 0) { |
| 279 |
$result = false; |
| 280 |
} |
| 281 |
break; |
| 282 |
case 'contains': |
| 283 |
if (stripos($left, $right) === false) { |
| 284 |
$result = false; |
| 285 |
} |
| 286 |
break; |
| 287 |
case 'in': |
| 288 |
$found = false; |
| 289 |
foreach ($right_parts as $right_part) { |
| 290 |
if (strcasecmp($left, $right_part) === 0) { |
| 291 |
$found = true; |
| 292 |
break 1; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
if (!$found) { |
| 297 |
$result = false; |
| 298 |
} |
| 299 |
|
| 300 |
break; |
| 301 |
case 'contains-in': |
| 302 |
$found = false; |
| 303 |
foreach ($right_parts as $right_part) { |
| 304 |
if (stripos($left, $right_part) !== false) { |
| 305 |
$found = true; |
| 306 |
break 1; |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
if (!$found) { |
| 311 |
$result = false; |
| 312 |
} |
| 313 |
break; |
| 314 |
case 'not-equal': |
| 315 |
if (strcasecmp($left, $right) === 0) { |
| 316 |
$result = false; |
| 317 |
} |
| 318 |
break; |
| 319 |
case 'not-contains': |
| 320 |
if (stripos($left, $right) !== false) { |
| 321 |
$result = false; |
| 322 |
} |
| 323 |
break; |
| 324 |
case 'not-in': |
| 325 |
$found = false; |
| 326 |
foreach ($right_parts as $right_part) { |
| 327 |
if (strcasecmp($right_part, $left) === 0) { |
| 328 |
$found = true; |
| 329 |
break 1; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
if ($found) { |
| 334 |
$result = false; |
| 335 |
} |
| 336 |
|
| 337 |
break; |
| 338 |
case 'not-contains-in': |
| 339 |
$found = false; |
| 340 |
foreach ($right_parts as $right_part) { |
| 341 |
if (stripos($left, $right_part) !== false) { |
| 342 |
$found = true; |
| 343 |
break 1; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
if ($found) { |
| 348 |
$result = false; |
| 349 |
} |
| 350 |
break; |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
if ($result) { |
| 355 |
return true; |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
return $result; |
| 361 |
} |
| 362 |
|
| 363 |
function filter($filter_data = []) |
| 364 |
{ |
| 365 |
$this->filter_data = $filter_data; |
| 366 |
} |
| 367 |
|
| 368 |
public function getParser() |
| 369 |
{ |
| 370 |
return $this->parser; |
| 371 |
} |
| 372 |
|
| 373 |
public function getMapper() |
| 374 |
{ |
| 375 |
return $this->mapper; |
| 376 |
} |
| 377 |
} |
| 378 |
|