public function save($item) { global $post; $post = array(); $metas = array(); global $ph_migrate_field_handlers; $field_handlers = array(); $class = get_class($this); while (FALSE != $class) { if (isset($ph_migrate_field_handlers[$class])) { $field_handlers = array_merge($field_handlers, $ph_migrate_field_handlers[$class]); } $class = get_parent_class($class); } $postprocess = array(); foreach ($item as $property => $value) { $handled = false; foreach ($field_handlers as $key => $callback) { if (0 === strpos($property, $key)) { $handled = true; if (!isset($postprocess[$key])) { $postprocess[$key] = array('callback' => $callback, 'fields' => array()); } $postprocess[$key]['fields'][$property] = $value; } } if (!$handled) { $post[$property] = $value; } } if (isset($post['ID'])) { wp_update_post($post); if (isset($post['post_format'])) { set_post_format($post['ID'], $post['post_format']); } $id = $post['ID']; ph_migrate_statistics_increment("Posts (Type:" . $post['post_type'] . ") updated", 1); } else { $id = wp_insert_post($post); $post['ID'] = $id; if (isset($post['post_format'])) { set_post_format($post['ID'], $post['post_format']); } ph_migrate_statistics_increment("Posts (Type:" . $post['post_type'] . ") created", 1); } foreach ($postprocess as $key => $dataset) { $callback = $dataset['callback']; $callback($post, $dataset['fields']); } return $id; }
public function save($item) { global $ph_migrate_field_handlers; $field_handlers = array(); $class = get_class($this); while (FALSE != $class) { if (isset($ph_migrate_field_handlers[$class])) { $field_handlers = array_merge($field_handlers, $ph_migrate_field_handlers[$class]); } $class = get_parent_class($class); } $userprocess = array(); foreach ($item as $property => $value) { $handled = false; foreach ($field_handlers as $key => $callback) { if (0 === strpos($property, $key)) { $handled = true; if (!isset($userprocess[$key])) { $userprocess[$key] = array('callback' => $callback, 'fields' => array()); } $userprocess[$key]['fields'][$property] = $value; } } if (!$handled) { $post[$property] = $value; } } if (!isset($item->ID)) { $userdata = array(); foreach ($this->user_fields as $valid) { if (!empty($item->{$valid})) { $userdata[$valid] = $item->{$valid}; } } $id = wp_insert_user($userdata); $item->ID = $id; ph_migrate_statistics_increment("Users created", 1); } else { ph_migrate_statistics_increment("Users updated", 1); } wp_update_user($item); $user = get_userdata($item->ID); foreach ($userprocess as $key => $dataset) { $callback = $dataset['callback']; $callback($user, $dataset['fields']); } return $item->ID; }
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"); }
public function save($item) { $post = array(); $metas = array(); global $ph_migrate_field_handlers; $field_handlers = array(); if (isset($ph_migrate_field_handlers[get_class($this)])) { $field_handlers = $ph_migrate_field_handlers[get_class($this)]; } $postprocess = array(); foreach ($item as $property => $value) { $handled = false; foreach ($field_handlers as $key => $callback) { if (0 === strpos($property, $key)) { $handled = true; if (!isset($postprocess[$key])) { $postprocess[$key] = array('callback' => $callback, 'fields' => array()); } $postprocess[$key]['fields'][$property] = $value; } } if (!$handled) { $post[$property] = $value; } } if (isset($post['ID'])) { $path = get_attached_file($post['ID']); copy($post['path'], $path); $attach_data = wp_generate_attachment_metadata($id, $path); wp_update_attachment_metadata($id, $attach_data); $id = $post['ID']; ph_migrate_statistics_increment("Attachments updated", 1); } else { try { $magic = new Imagick($post['path']); $extension = pathinfo($post['path'], PATHINFO_EXTENSION); } catch (Exception $e) { echo 'Exception: ' . $e->getMessage(); $extension = "jpg"; } $filename = basename($post['path']); if ($extension == "" || $extension == NULL) { $info = $magic->identifyImage(); $compression = $info['compression']; $info = pathinfo($post['path']); $extension = ""; if ($compression == "JPEG") { $extension = "jpg"; } else { if ($compression == "PNG") { $extension = "png"; } else { if ($compression == "GIF") { $extension = "gif"; } } } $filename = $info['filename'] . "." . $extension; } $tmp = tempnam('/tmp', 'ph_migrate'); $data = file_get_contents($post['path']); file_put_contents($tmp, $data); $file_array = array('name' => $filename, 'size' => filesize($tmp), 'tmp_name' => $tmp); $id = media_handle_sideload($file_array, $post['parent']); $path = get_attached_file($id); $attach_data = wp_generate_attachment_metadata($id, $path); wp_update_attachment_metadata($id, $attach_data); $new_post = (array) get_post($id); $post = array_merge($new_post, $post); ph_migrate_statistics_increment("Attachments created", 1); } $mapping = array('title' => 'post_title', 'caption' => 'post_excerpt', 'description' => 'post_content'); foreach ($mapping as $old => $new) { if (isset($post[$old])) { $post[$new] = $post[$old]; } } wp_update_post($post); if (isset($post['alt'])) { add_post_meta($post['ID'], '_wp_attachment_image_alt', $post['alt'], true); } foreach ($postprocess as $key => $dataset) { $callback = $dataset['callback']; $callback($post, $dataset['fields']); } return $id; }
} 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); }