function ph_migrate_import($migration, $update, $idlist, $skip, $limit, $progress) { ph_migrate_statistics_init(); ini_set('memory_limit', '-1'); do_action('ph_migrate_register_field_handlers'); $migrations = ph_migrate_migrations(); $migration = $migrations[$migration]; //step one: fetch the mapping table as a backing store for all migration decisions global $wpdb; $mapping = $wpdb->get_results('select source_id,dest_id,needs_import from ' . $wpdb->prefix . 'ph_migrate_map_' . $migration->name); //step two: fetch all source ids $ids = $migration->source->getIDs(); //step three: apply the idlist as a filter to all source ids if (is_array($idlist) && count($idlist) > 0) { $new_ids = array(); foreach ($ids as $id) { if (in_array($id, $idlist)) { $new_ids[] = $id; } } $ids = $new_ids; } //step four: apply the skip and limit as a filter to all source ids if (0 != $skip) { echo "First " . $skip . " items of " . count($ids) . " were skipped!\n"; $ids = array_slice($ids, $skip); } if (0 != $limit) { $ids = array_splice($ids, 0, $limit); } ph_migrate_statistics_increment("Sources to be migrated", count($ids)); //step five: for each source id check wether migration is neccessary: // * using update // * checking wether it's already migrated // * calling prepareRow (returning NULL cancels this item) // if migration should be done: // * load existing entity if available // * map fields over // * save entity $nImported = 0; $nIgnored = 0; foreach ($ids as $source_id) { ph_migrate_statistics_increment("Sources analyzed", 1); echo "ID: " . $source_id . "\n"; ini_set('memory_limit', '-1'); $entry = null; foreach ($mapping as $map_entry) { if ($map_entry->source_id == $source_id) { $entry = $map_entry; break; } } if ($entry == null || true == $entry->needs_import || $entry->dest_id == null || $entry->dest_id != null && $update) { if ($entry == null) { $entry = new Stdclass(); $entry->source_id = $source_id; $entry->dest_id = null; $entry->needs_import = true; $entry->is_new = true; $mapping[] = $entry; } $data = $migration->source->getItemByID($source_id); $data = $migration->prepareRow($data); if ($data != null) { $destination = null; if ($entry->dest_id != null) { echo "fetching destination..." . $entry->dest_id . "\n"; $destination = $migration->destination->getItemByID($entry->dest_id); } else { echo "creating destination...\n"; $destination = $migration->destination->createItem(); } if ($destination == null) { echo "unable to get destination!\n"; if ($migration->destination == null) { echo "migration destination is null!\n"; } } else { $migration->map($data, $destination); $dest_id = $migration->destination->save($destination); // if $dest_id wp_error -> abfangen $entry->dest_id = $dest_id; $entry->needs_import = false; if (isset($entry->is_new)) { $wpdb->insert($wpdb->prefix . 'ph_migrate_map_' . $migration->name, array('source_id' => $entry->source_id, 'dest_id' => $entry->dest_id, 'needs_import' => $entry->needs_import)); } else { $wpdb->update($wpdb->prefix . 'ph_migrate_map_' . $migration->name, array('source_id' => $entry->source_id, 'dest_id' => $entry->dest_id, 'needs_import' => $entry->needs_import), array('source_id' => $entry->source_id)); } $nImported++; ph_migrate_statistics_increment("Sources imported or updated", 1); } } else { $nIgnored++; ph_migrate_statistics_increment("Sources ignored", 1); } } else { $nIgnored++; ph_migrate_statistics_increment("Sources ignored", 1); } if (0 == ($nImported + $nIgnored) % $progress && $progress != -1) { echo esc_html("Migration progress: {$nImported} imported, {$nIgnored} skipped, " . (count($ids) - ($nImported + $nIgnored)) . " remaining.\n"); } } //step six: PROFIT echo esc_html("Migration result: {$nImported} imported, {$nIgnored} skipped.\n"); }
} if (0 === strpos($argv[$i], '--skip=')) { $skip = intval(substr($argv[$i], strlen('--skip='))); } if (0 === strpos($argv[$i], '--progress=')) { $progress = intval(substr($argv[$i], strlen('--progress='))); } } ph_migrate_import($migration, $update, $idlist, $skip, $limit, $progress); } if ('rollback' == $argv[1] && count($argv) >= 3) { $migration = $argv[2]; $progress = -1; $idlist = array(); for ($i = 3; $i < count($argv); $i++) { if (0 === strpos($argv[$i], '--progress=')) { $progress = intval(substr($argv[$i], strlen('--progress='))); } if (0 === strpos($argv[$i], '--idList=')) { $tmp = substr($argv[$i], strlen('--idList=')); $idlist = explode(',', $tmp); } } ph_migrate_rollback($migration, $progress, $idlist); } if ('test' == $argv[1]) { ph_migrate_statistics_init(); ph_migrate_statistics_increment('Test', 1); ph_migrate_statistics_increment('Test', 2); ph_migrate_statistics_increment("Test with apostroph: '", 1); }