Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/include/import/import_mtdb.php - 224 lines - 6341 bytes - Summary - Text - Print

   1  <?php
   2  /*
   3  $HeadURL: https://textpattern.googlecode.com/svn/releases/4.5.4/source/textpattern/include/import/import_mtdb.php $
   4  $LastChangedRevision: 3997 $
   5  */
   6  
   7  
   8  // ----------------------------------------------------------------
   9  	function doImportMTDB($mt_dblogin, $mt_db, $mt_dbpass, $mt_dbhost, $blog_id, $insert_into_section, $insert_with_status, $default_comment_invite)
  10      {
  11          //Keep some response on some part
  12          $results = array();
  13  
  14          //Avoid left joins
  15          $authors_map = array();
  16          $categories_map = array();
  17  
  18          // let's go - Dean says ;-).
  19          $mtlink = mysql_connect($mt_dbhost,$mt_dblogin,$mt_dbpass,true);
  20          if(!$mtlink){
  21                  return 'mt database values don&#8217;t work. Please replace them and try again';
  22          }
  23          mysql_select_db($mt_db,$mtlink);
  24          $results[]= 'connected to mt database. Importing Data';
  25  
  26          sleep(2);
  27  
  28          $a = mysql_query("
  29              select
  30              author_id as user_id,
  31              author_nickname as name,
  32              author_name as RealName,
  33              author_email as email,
  34              author_password as pass
  35              from mt_author
  36          ",$mtlink);
  37  
  38          while($b = mysql_fetch_assoc($a)){
  39              $authors[] = $b;
  40          }
  41  
  42          $a = mysql_query("
  43              select
  44              mt_entry.entry_id as ID,
  45              mt_entry.entry_text as Body,
  46              mt_entry.entry_text_more as Body2,
  47              mt_entry.entry_title as Title,
  48              mt_entry.entry_excerpt as Excerpt,
  49              mt_entry.entry_keywords as Keywords,
  50              mt_entry.entry_created_on as Posted,
  51              mt_entry.entry_modified_on as LastMod,
  52              mt_entry.entry_author_id as AuthorID
  53              from mt_entry
  54              where entry_blog_id = '$blog_id'
  55          ",$mtlink);
  56  
  57          $results[]= mysql_error();
  58  
  59          while($b = mysql_fetch_assoc($a)){
  60              $cat = mysql_query("select placement_category_id as category_id from mt_placement where placement_entry_id='{$b['ID']}'");
  61              while ($cat_id = mysql_fetch_row($cat)){
  62                  $categories[] = $cat_id[0];
  63              }
  64  
  65              if (!empty($categories[0])) $b['Category1'] = $categories[0];
  66              if (!empty($categories[1])) $b['Category2'] = $categories[1];
  67  
  68              unset($categories);
  69  
  70              //Trap comments for each article
  71              $comments = array();
  72  
  73              $q = "
  74                  select
  75                  mt_comment.comment_id as discussid,
  76                  mt_comment.comment_ip as ip,
  77                  mt_comment.comment_author as name,
  78                  mt_comment.comment_email as email,
  79                  mt_comment.comment_url as web,
  80                  mt_comment.comment_text as message,
  81                  mt_comment.comment_created_on as posted
  82                  from mt_comment where comment_blog_id = '$blog_id' AND comment_entry_id='{$b['ID']}'
  83              ";
  84  
  85              $c = mysql_query($q, $mtlink);
  86  
  87              while($d=mysql_fetch_assoc($c)){
  88                  $comments[] = $d;
  89              }
  90              //Attach comments to article
  91              $b['comments'] = $comments;
  92              unset($comments);
  93  
  94              //Article finished
  95              $articles[] = $b;
  96          }
  97  
  98  
  99          $a = mysql_query("
 100              select category_id,category_label from mt_category where category_blog_id='{$blog_id}'
 101          ",$mtlink);
 102  
 103          while($b=mysql_fetch_assoc($a)){
 104              $categories_map[$b['category_id']] = $b['category_label'];
 105          }
 106  
 107          mysql_close($mtlink);
 108  
 109          //Yes, we have to make a new connection
 110          //otherwise doArray complains
 111          $DB = new DB;
 112  
 113          include txpath.'/lib/classTextile.php';
 114  
 115          $textile = new Textile;
 116  
 117          if (!empty($authors)) {
 118              foreach($authors as $author) {
 119                  extract($author);
 120                  $name = (empty($name)) ? $RealName : $name;
 121  
 122                  $authors_map[$user_id] = $name;
 123  
 124                  $authorid = safe_field('user_id', 'txp_users', "name = '".doSlash($name)."'");
 125                  if (!$authorid){
 126                      //Add new authors
 127                      $q = safe_insert("txp_users","
 128                          name     = '".doSlash($RealName)."',
 129                          email    = '".doSlash($email)."',
 130                          pass     = '".doSlash(txp_hash_password($pass))."',
 131                          RealName = '".doSlash($RealName)."',
 132                          privs='1'"
 133                      );
 134  
 135                      if($q) {
 136                          $results[]= 'inserted '.$RealName.' into txp_users';
 137                      } else $results[]=mysql_error();
 138                  }
 139              }
 140          }
 141  
 142          if (!empty($categories_map)) {
 143  
 144              foreach ($categories_map as $category) {
 145                  $category = doSlash($category);
 146                  $rs = safe_row('id', 'txp_category', "name='$category' and type='article'");
 147                  if (!$rs){
 148                      $q = safe_insert("txp_category","name='$category',type='article',parent='root'");
 149  
 150                      if($q) {
 151                          $results[]= 'inserted '.stripslashes($category).' into txp_category';
 152                      } else $results[]=mysql_error();
 153                  }
 154  
 155              }
 156          }
 157  
 158          if (!empty($articles)) {
 159              foreach ($articles as $article) {
 160                  extract($article);
 161                  $Body .= (trim($Body2)) ? "\n\n".$Body2 : '';
 162  
 163                  $Body_html = $textile->textileThis($Body);
 164                  $Excerpt_html = $textile->textileThis($Excerpt);
 165                  $Title = $textile->textileThis($Title,1);
 166  
 167                  $Category1 = (!empty($Category1)) ? doSlash($Category1) : '';
 168  
 169                  $AuthorID = (!empty($authors_map[$AuthorID])) ? doSlash($authors_map[$AuthorID]) : '';
 170  
 171                  $insertID = safe_insert("textpattern","
 172                      ID             = '$ID',
 173                      Posted         = '$Posted',
 174                      LastMod        = '$LastMod',
 175                      Title          = '".doSlash($Title)."',
 176                      Body           = '".doSlash($Body)."',
 177                      Excerpt        = '".doSlash($Excerpt)."',
 178                      Excerpt_html   = '".doSlash($Excerpt_html)."',
 179                      Keywords       = '".doSlash($Keywords)."',
 180                      Body_html      = '".doSlash($Body_html)."',
 181                      AuthorID       = '$AuthorID',
 182                      Category1      = '$Category1',
 183                      AnnotateInvite = '".doSlash($default_comment_invite)."',
 184                      Section        = '".doSlash($insert_into_section)."',
 185                      uid            = '".md5(uniqid(rand(),true))."',
 186                      feed_time      = '".substr($Posted,0,10)."',
 187                      Status         = '$insert_with_status'
 188                  ");
 189  
 190                  if($insertID) {
 191                      $results[] = 'inserted MT entry '.strong($Title).
 192                          ' into Textpattern as article '.strong($insertID).'';
 193  
 194                      //Do coment for article
 195                      if (!empty($comments) && is_array($comments)) {
 196                          foreach ($comments as $comment) {
 197                              extract($comment);
 198                              $message = nl2br($message);
 199  
 200                              $commentID = safe_insert("txp_discuss","
 201                                  discussid = $discussid,
 202                                  parentid  = $insertID,
 203                                  name      = '".doSlash($name)."',
 204                                  email     = '".doSlash($email)."',
 205                                  web       = '".doSlash($web)."',
 206                                  message   = '".doSlash($message)."',
 207                                  ip        = '$ip',
 208                                  posted    = '$posted',
 209                                  visible   = 1"
 210                              );
 211  
 212                              if($commentID) {
 213                                  $results[] = 'inserted MT comment '.$commentID.
 214                                      ' for article '.$insertID.' into txp_discuss';
 215                              } else $results[]=mysql_error();
 216                          }
 217                      }
 218                  } else $results[] = mysql_error();
 219              }
 220          }
 221          return join('<br />', $results);
 222      }
 223  
 224  ?>

title

Description

title

Description

title

Description

title

title

Body