Example #1
0
<?php

define('WP_MEMORY_LIMIT', '2G');
require "do-not-event-think-about-mailing.php";
$paths = explode('wp-content', __FILE__);
require_once $paths[0] . 'wp-load.php';
// Login as Admin
$user = get_userdata('1');
wp_set_current_user('1', $user->user_login);
wp_set_auth_cookie('1');
if (!is_user_logged_in()) {
    die("Migration cancelled: Login failed!\n\r");
}
$migrations = ph_migrate_migrations();
ini_set('memory_limit', '-1');
while (0 != ob_get_level()) {
    ob_end_clean();
}
if (1 == count($argv)) {
    ?>
Usage: migrator [operation] [parameters]

Operations:
	list - lists all registered migrations.
	import - starts an import process.
	rollback - rolls back a migration.
	
import usage:
	migrator import MIGRATION [--update] [--idList=ID,ID,ID] [--limit=LIMIT] [--progress=COUNT]
	
	--update - reimports already existing items
Example #2
0
function ph_migrate_lookup()
{
    ?>
<h1>Migrate Lookup</h1>
<?php 
    $migrations = ph_migrate_migrations();
    if (isset($_POST)) {
        $input = array();
        if (isset($_POST['post_id'])) {
            $input['post_id'] = esc_html($_POST['post_id']);
        }
        if (isset($_POST['migration'])) {
            $input['migration'] = esc_html($_POST['migration']);
        }
        if (isset($_POST['source_id'])) {
            $input['source_id'] = esc_html($_POST['source_id']);
        }
        if (isset($input['post_id'])) {
            foreach ($migrations as $key => $migration) {
                $source_id = $migration->getSourceIDForDestinationID($input['post_id']);
                if ($source_id != null) {
                    ?>
<ul>
<li>Migration: <?php 
                    echo esc_html($key);
                    ?>
</li>
<li>Source ID: <?php 
                    echo esc_html($source_id);
                    ?>
</li>
<li>Post ID: <?php 
                    echo esc_html(edit_post_link($input['post_id'], "", "", $input['post_id']));
                    ?>
</li>
</ul>
<?php 
                    $migration->source->describeID($source_id);
                }
            }
        } else {
            if (isset($input['source_id'])) {
                $migration = $migrations[$input['migration']];
                $dest_id = $migration->getDestinationIDForSourceID($input['source_id'], false);
                ?>
<ul>
<li>Migration: <?php 
                echo esc_html($input['migration']);
                ?>
</li>
<li>Source ID: <?php 
                echo esc_html($input['source_id']);
                ?>
</li>
<li>Post ID: <?php 
                echo esc_html(edit_post_link($dest_id, "", "", $dest_id));
                ?>
</li>
<?php 
                $migration->source->describeID($input['source_id']);
            }
        }
    }
    ?>
<div>
	<h2>Lookup by Post ID</h2>
	<form method="POST">
	<input type="number" name="post_id"/>
	<input type="submit" value="Lookup"/>
	</form>
</div>
<div>
	<h2>Lookup by Migration and Source ID</h2>
	<form method="POST">
	<p>
		Source ID: <input type="text" name="source_id"/>
	</p>
	<p>
		Migration: <select name="migration">
<?php 
    foreach ($migrations as $key => $value) {
        ?>
	<option value="<?php 
        echo esc_html($key);
        ?>
"><?php 
        echo esc_html($key);
        ?>
</option>
<?php 
    }
    ?>
		</select>
	</p>
	<input type="submit" value="Lookup"/>
	</form>
</div>
<?php 
}
 public function map($source, $destination)
 {
     $value = null;
     if ($this->staticValue != null) {
         $value = $this->staticValue;
     } else {
         if (isset($source->{$this->sourcefield})) {
             $value = $source->{$this->sourcefield};
         }
     }
     if ($this->mappings != null) {
         if (is_array($value)) {
             $new = array();
             foreach ($value as $val) {
                 if (!is_array($val) && isset($this->mappings[$val])) {
                     $new[] = $this->mappings[$val];
                 } else {
                     $new[] = $val;
                 }
             }
             $value = $new;
         } else {
             $value = $this->mappings[$value];
         }
     }
     if ($this->mapperClass != null) {
         if (is_array($value)) {
             $new = array();
             foreach ($value as $val) {
                 $result = $this->mapperClass->map($val);
                 if ($result != null) {
                     $new[] = $result;
                 }
             }
             $value = $new;
         } else {
             $value = $this->mapperClass->map($value);
         }
     }
     if ($this->sourcemigration != null) {
         $migrations = ph_migrate_migrations();
         if (is_array($value)) {
             $new_values = array();
             foreach ($value as $src) {
                 $found = false;
                 foreach ($this->sourcemigration as $source) {
                     $sourcemigration = $migrations[$source];
                     if ($sourcemigration->source == null) {
                         echo "{$source} Migration has no source set and is used as a sourceMigration! That's not allowed.\n";
                     }
                     if ($sourcemigration->source->hasID($src)) {
                         $new_values[] = $sourcemigration->getDestinationIDForSourceID($src);
                         $found = true;
                         break;
                     }
                 }
                 if (!$found) {
                     $new_values[] = null;
                 }
             }
             $value = $new_values;
         } else {
             $found = false;
             foreach ($this->sourcemigration as $source) {
                 $sourcemigration = $migrations[$source];
                 if ($sourcemigration->source == null) {
                     echo "{$source} Migration has no source set and is used as a sourceMigration! That's not allowed.\n";
                 }
                 if ($sourcemigration->source->hasID($value)) {
                     $found = true;
                     $value = $sourcemigration->getDestinationIDForSourceID($value);
                     break;
                 }
             }
             if (!$found) {
                 $value = null;
             }
         }
     }
     if ($this->jointoken != null && is_array($value)) {
         $value = implode($this->jointoken, $value);
     }
     if ($value == null) {
         return;
     }
     if ($destination == null) {
         echo "destination is empty!\n";
         return;
     }
     $destination->{$this->destinationfield} = $value;
 }