Make WordPress Core

Changeset 1230


Ignore:
Timestamp:
05/05/2004 08:04:12 AM (20 years ago)
Author:
saxmatt
Message:

New MT import from Carthik.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/import-mt.php

    r1108 r1230  
    11<?php
    2 define('MTEXPORT', '');
    3 
     2define('MTEXPORT', 'import.txt');// enter the relative path of the import.txt file containing the mt entries. If the file is called import.txt and it is /wp-admin, then this line
     3//should be define('MTEXPORT', 'import.txt');
    44
    55if (!file_exists('../wp-config.php')) die("There doesn't seem to be a wp-config.php file. You must install WordPress before you import any entries.");
    66require('../wp-config.php');
    7 
    8 $step = $_GET['step'];
     7$step = $HTTP_GET_VARS['step'];
    98if (!$step) $step = 0;
    109?>
     
    3635        line-height: 140%;
    3736    }
     37    #authors li     {
     38        padding:3px;
     39        border: 1px solid #ccc;
     40        width: 40%;
     41        margin-bottom:2px;
     42    }
    3843    </style>
    3944</head><body>
     
    5055<p>You have to do this manually for security reasons.</p>
    5156<p>If you've done that and you&#8217;re all ready, <a href="import-mt.php?step=1">let's go</a>! Remember that the import process may take a minute or so if you have a large number of entries and comments. Think of all the rebuilding time you'll be saving once it's done. :)</p>
    52 <p>On our test system, importing a blog of 1189 entries and about a thousand comments took 18 seconds. </p>
    5357<p>The importer is smart enough not to import duplicates, so you can run this multiple times without worry if&#8212;for whatever reason&#8212;it doesn't finish. </p>
    5458<?php
     
    5862if ('' != MTEXPORT && !file_exists(MTEXPORT)) die("The file you specified does not seem to exist. Please check the path you've given.");
    5963if ('' == MTEXPORT) die("You must edit the MTEXPORT line as described on the <a href='import-mt.php'>previous page</a> to continue.");
    60 
    61 function checkauthor($author) {
    62     global $wpdb, $tableusers;
    63     // Checks if an author exists, creates it if it doesn't, and returns author_id
    64     $user_id = $wpdb->get_var("SELECT ID FROM $tableusers WHERE user_login = '$author'");
    65     if (!$user_id) {
    66         $wpdb->query("INSERT INTO $tableusers (user_login, user_pass, user_nickname) VALUES ('$author', 'changeme', '$author')");
    67         $user_id = $wpdb->get_var("SELECT ID FROM $tableusers WHERE user_login = '$author'");
    68         echo "<strong>Created user '$author'</strong>. ";
    69     }
    70     return $user_id;
    71 }
    72 
    7364// Bring in the data
    7465set_magic_quotes_runtime(0);
     66$datalines = file(MTEXPORT); // Read the file into an array
     67$importdata = implode('', $datalines); // squish it
     68$importdata = preg_replace("/(\r\n|\n|\r)/", "\n", $importdata);
     69$authors = array();
     70$temp = array();
     71$posts = explode("--------", $importdata);
     72
     73function users_form($n) {
     74    global $wpdb, $tableusers, $testing;
     75    $users = $wpdb->get_results("SELECT * FROM $tableusers ORDER BY ID");
     76    ?><select name="userselect[<?php echo $n; ?>]">
     77    <option value="#NONE#">- Select -</option>
     78    <?php foreach($users as $user) {
     79        echo '<option value="'.$user->user_nickname.'">'.$user->user_nickname.'</option>';
     80        } ?>
     81    </select>
     82<?php }
     83
     84$i = -1;
     85foreach ($posts as $post) {
     86    if ('' != trim($post)) {
     87        ++$i;
     88        unset($post_categories);
     89        preg_match("|AUTHOR:(.*)|", $post, $thematch);
     90        $thematch = trim($thematch[1]);
     91        array_push($temp,"$thematch"); //store the extracted author names in a temporary array
     92        }
     93    }//end of foreach
     94//we need to find unique values of author names, while preserving the order, so this function emulates the unique_value(); php function, without the sorting.
     95$authors[0] = array_shift($temp);
     96$y = count($temp) + 1;
     97for ($x = 1; $x < $y; $x++) {
     98    $next = array_shift($temp);
     99    if (!(in_array($next,$authors))) array_push($authors, "$next");
     100    }
     101//by this point, we have all unique authors in the array $authors
     102?><p><?php _e('To make it easier for you to edit and save the imported posts and drafts, you may want to change the name of the author of the posts. For example, you may want to import all the entries as <code>admin</code>s entries.'); ?></p>
     103<p><?php _e('Below, you can see the names of the authors of the MovableType posts in <i>italics</i>. For each of these names, you can either pick an author in your WordPress installation from the menu, or enter a name for the author in the textbox.'); ?></p>
     104<p><?php _e('If a new user is created by WordPress, the password will be set, by default, to "changeme". Quite suggestive, eh? ;)'); ?></p>
     105    <?php
     106    echo '<ol id="authors">';
     107    echo '<form action="?step=2" method="post">';
     108    $j = -1;
     109    foreach ($authors as $author) {
     110    ++$j;
     111    echo '<li><i>'.$author.'</i><br />'.'<input type="text" value="'.$author.'" name="'.'user[]'.'" maxlength="30"';
     112    users_form($j);
     113    echo '</li>';
     114    }
     115    echo '<input type="submit" value="Submit">'.'<br/>';
     116    echo '</form>';
     117    echo '</ol>';
     118   
     119    flush();
     120
     121    break;
     122   
     123    case 2:
     124    $newauthornames = array();
     125    $formnames = array();
     126    $selectnames = array();
     127    $mtnames = array();
     128    foreach($_POST['user'] as $key => $line) {
     129    $newname = trim(stripslashes($line));
     130    if ($newname == '') $newname = 'left_blank';//passing author names from step 1 to step 2 is accomplished by using POST. left_blank denotes an empty entry in the form.
     131    array_push($formnames,"$newname");
     132    }// $formnames is the array with the form entered names
     133    foreach ($_POST['userselect'] as $user => $key) {
     134    $selected = trim(stripslashes($key));
     135    array_push($selectnames,"$selected");
     136    }
     137    $count = count($formnames);
     138    for ($i = 0; $i < $count; $i++) {
     139    if ( $selectnames[$i] != '#NONE#') {//if no name was selected from the select menu, use the name entered in the form
     140    array_push($newauthornames,"$selectnames[$i]");
     141    }
     142    else {
     143    array_push($newauthornames,"$formnames[$i]");
     144    }
     145    }
     146
     147    $j = -1;
     148    //function to check the authorname and do the mapping
     149    function checkauthor($author) {
     150    global $wpdb, $tableusers, $mtnames, $newauthornames, $j;//mtnames is an array with the names in the mt import file
     151    $md5pass = md5(changeme);
     152    if (!(in_array($author, $mtnames))) { //a new mt author name is found
     153        ++$j;
     154        $mtnames[$j] = $author; //add that new mt author name to an array
     155        $user_id = $wpdb->get_var("SELECT ID FROM $tableusers WHERE user_login = '$newauthornames[$j]'"); //check if the new author name defined by the user is a pre-existing wp user
     156        if (!$user_id) { //banging my head against the desk now.
     157            if ($newauthornames[$j] == 'left_blank') { //check if the user does not want to change the authorname
     158                $wpdb->query("INSERT INTO $tableusers (user_level, user_login, user_pass, user_nickname) VALUES ('1', '$author', '$md5pass', '$author')"); // if user does not want to change, insert the authorname $author
     159                $user_id = $wpdb->get_var("SELECT ID FROM $tableusers WHERE user_login = '$author'");
     160                $newauthornames[$j] = $author; //now we have a name, in the place of left_blank.
     161            } else {
     162            $wpdb->query("INSERT INTO $tableusers (user_level, user_login, user_pass, user_nickname) VALUES ('1', '$newauthornames[$j]', '$md5pass', '$newauthornames[$j]')"); //if not left_blank, insert the user specified name
     163            $user_id = $wpdb->get_var("SELECT ID FROM $tableusers WHERE user_login = '$newauthornames[$j]'");
     164            }
     165        } else return $user_id; // return pre-existing wp username if it exists
     166    } else {
     167    $key = array_search($author, $mtnames); //find the array key for $author in the $mtnames array
     168    $user_id = $wpdb->get_var("SELECT ID FROM $tableusers WHERE user_login = '$newauthornames[$key]'");//use that key to get the value of the author's name from $newauthornames
     169    }
     170    return $user_id;
     171}//function checkauthor ends here
     172
     173    //bring in the posts now
     174    set_magic_quotes_runtime(0);
    75175$datalines = file(MTEXPORT); // Read the file into an array
    76176$importdata = implode('', $datalines); // squish it
     
    83183    ++$i;
    84184    unset($post_categories);
    85     echo "<li>Importing post... ";
     185    echo "<li>Processing post... ";
    86186
    87187    // Take the pings out first
     
    124224                break;
    125225            case 'AUTHOR':
    126                 $post_author = checkauthor($value);
     226                $post_author = $value;
    127227                break;
    128228            case 'TITLE':
     
    175275        echo "Post already imported.";
    176276    } else {
     277        $post_author = checkauthor($post_author);//just so that if a post already exists, new users are not created by checkauthor
    177278        $wpdb->query("INSERT INTO $tableposts (
    178279            post_author, post_date, post_content, post_title, post_excerpt,  post_status, comment_status, ping_status, post_name)
     
    206307            if (!$exists) $wpdb->query("INSERT INTO $tablepost2cat (post_id, category_id) VALUES ($post_id, 1) ");
    207308        }
     309        echo " Post imported successfully...";
    208310        // Now for comments
    209311        $comments = explode("-----\nCOMMENT:", $comments[0]);
     
    293395    echo "</li>";
    294396    flush();
    295     // n
     397
    296398} }
    297399
Note: See TracChangeset for help on using the changeset viewer.