[ Index ]

PHP Cross Reference of Textpattern 4.0.8

title

Body

[close]

/textpattern/include/import/ -> import_mtdb.php (source)

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


Generated: Thu May 21 23:03:01 2009 Cross-referenced by PHPXref 0.7