[ Index ]

PHP Cross Reference of Textpattern 4.0.8

title

Body

[close]

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

   1  <?php
   2  //Long live zem!
   3  
   4  //Time for 45KB: 0.9648-1.3698sec.
   5  //Time for 2.612KB: 30sec.
   6  function doImportMT($file, $section, $status, $invite) {
   7  
   8      # Parse a file in the MT Import Format, as described here:
   9      # http://www.movabletype.org/docs/mtimport.html
  10      # This doesn't interpret the data at all, just parse it into
  11      # a structure.
  12  
  13      ini_set('auto_detect_line_endings', 1);
  14  
  15      $fp = fopen($file, 'r');
  16      if (!$fp)
  17          return false;
  18  
  19      //Keep some response on some part
  20      $results = array();
  21  
  22      $state = 'metadata';
  23      $item = array();
  24  
  25      while (!feof($fp)) {
  26          $line = rtrim(fgets($fp, 8192));
  27  
  28          # The states suggested by the spec are inconsisent, but
  29          # we'll do our best to fake it
  30  
  31          if ($line == '--------') {
  32              # End of an item, so we can process it
  33              $results[]=import_mt_item($item, $section, $status, $invite);
  34              $item = array();
  35              $state = 'metadata';
  36          }
  37          elseif ($line == '-----' and $state == 'metadata') {
  38              $state = 'multiline';
  39              $multiline_type = '';
  40          }
  41          elseif ($line == '-----' and $state == 'multiline') {
  42              if (!empty($multiline_type)) {
  43                  $item[$multiline_type][] = import_mt_utf8($multiline_data);
  44              }
  45              $state = 'multiline';
  46              $multiline_type = '';
  47          }
  48          elseif ($state == 'metadata') {
  49              if (preg_match('/^([A-Z ]+):\s*(.*)$/', $line, $match)) {
  50                  $item[$match[1]] = import_mt_utf8($match[2]);
  51              }
  52          }
  53          elseif ($state == 'multiline' and empty($multiline_type)) {
  54              if (preg_match('/^([A-Z ]+):\s*$/', $line, $match)) {
  55                  $multiline_type = $match[1];
  56                  $multiline_data = array();
  57              }
  58          }
  59          elseif ($state == 'multiline') {
  60              # Here's where things get hinky.  Rather than put the
  61              # multiline metadata before the field name, it goes
  62              # after, with no clear separation between metadata
  63              # and data.  And either the metadata or data might be
  64              # missing.
  65  
  66              if (empty($multiline_data['content']) and preg_match('/^([A-Z ]+):\s*(.*)$/', $line, $match)) {
  67                  # Metadata within the multiline field
  68                  $multiline_data[$match[1]] = import_mt_utf8($match[2]);
  69              }
  70              elseif (empty($multiline_data['content'])) {
  71                  $multiline_data['content'] = import_mt_utf8(($line . "\n"));
  72              }
  73              else {
  74                  $multiline_data['content'] .= import_mt_utf8(($line . "\n"));
  75              }
  76          }
  77      }
  78  
  79      # Catch the last item in the file, if it doesn't end with a separator
  80      if (!empty($item))
  81          $results[]= import_mt_item($item, $section, $status, $invite, $blogid);
  82  
  83      fclose($fp);
  84      return join('<br />', $results);
  85  }
  86  //Some \n chars on empty fields should be removed from body_extended and excerpt
  87  //What about the new title_html field?
  88  function import_mt_item($item, $section, $status, $invite) {
  89      global $prefs;
  90  
  91      # Untested import code follows
  92  
  93      if (empty($item)) return;
  94  
  95      include_once txpath.'/lib/classTextile.php';
  96      $textile = new Textile();
  97  
  98  
  99      $title = $textile->TextileThis($item['TITLE'], 1);
 100      //nice non-english permlinks
 101      $url_title = stripSpace($title,1);
 102  
 103      $body = isset($item['BODY'][0]['content']) ? $item['BODY'][0]['content'] : '';
 104      if (isset($item['EXTENDED BODY'][0]['content']))
 105          $body .= "\n <!-- more -->\n\n" . $item['EXTENDED BODY'][0]['content'];
 106  
 107      $body_html = $textile->textileThis($body);
 108  
 109      $excerpt = isset($item['EXCERPT'][0]['content']) ? $item['EXCERPT'][0]['content'] : '';
 110      $excerpt_html = $textile->textileThis($excerpt);
 111  
 112      $date = safe_strtotime($item['DATE']);
 113      $date = strftime('%Y-%m-%d %H:%M:%S', $date);
 114  
 115      if (isset($item['STATUS']))
 116          $post_status = ($item['STATUS'] == 'Draft' ? 1 : 4);
 117      else
 118          $post_status = $status;
 119  
 120      $category1 = @$item['PRIMARY CATEGORY'];
 121      if ($category1 and !safe_field("name","txp_category","name = '$category1'"))
 122              safe_insert('txp_category', "name='".doSlash($category1)."', type='article', parent='root'");
 123  
 124      $category2 = @$item['CATEGORY'];
 125      if ($category2 == $category1)
 126          $category2 = '';
 127      if ($category2 and !safe_field("name","txp_category","name = '$category2'"))
 128              safe_insert('txp_category', "name='".doSlash($category2)."', type='article', parent='root'");
 129  
 130      $keywords = isset($item['KEYWORDS'][0]['content']) ? $item['KEYWORDS'][0]['content'] : '';
 131  
 132      $annotate = !empty($item['ALLOW COMMENTS']);
 133      if (isset($item['ALLOW COMMENTS']))
 134          $annotate = intval($item['ALLOW COMMENTS']);
 135      else
 136          $annotate = (!empty($item['COMMENT']) or $prefs['comments_on_default']);
 137  
 138      $authorid = safe_field('user_id', 'txp_users', "name = '".doSlash($item['AUTHOR'])."'");
 139      if (!$authorid)
 140  //        $authorid = safe_field('user_id', 'txp_users', 'order by user_id asc limit 1');
 141          //Add new authors
 142          safe_insert('txp_users', "name='".doSlash($item['AUTHOR'])."'");
 143  
 144  
 145      if (!safe_field("ID", "textpattern", "Title = '".doSlash($title)."' AND Posted = '".doSlash($date)."'")) {
 146          $parentid = safe_insert('textpattern',
 147              "Posted='".doSlash($date)."',".
 148              "LastMod='".doSlash($date)."',".
 149              "AuthorID='".doSlash($item['AUTHOR'])."',".
 150              "LastModID='".doSlash($item['AUTHOR'])."',".
 151              "Title='".doSlash($title)."',".
 152              "Body='".doSlash($body)."',".
 153              "Body_html='".doSlash($body_html)."',".
 154              "Excerpt='".doSlash($excerpt)."',".
 155              "Excerpt_html='".doSlash($excerpt_html)."',".
 156              "Category1='".doSlash($category1)."',".
 157              "Category2='".doSlash($category2)."',".
 158              "Annotate='".doSlash($annotate)."',".
 159              "AnnotateInvite='".doSlash($invite)."',".
 160              "Status='".doSlash($post_status)."',".
 161              "Section='".doSlash($section)."',".
 162              "Keywords='".doSlash($keywords)."',".
 163              "uid='".md5(uniqid(rand(),true))."',".
 164              "feed_time='".substr($date,0,10)."',".
 165              "url_title='".doSlash($url_title)."'");
 166  
 167          if (!empty($item['COMMENT']) and is_array($item['COMMENT'])) {
 168              foreach ($item['COMMENT'] as $comment) {
 169                  $comment_date = strftime('%Y-%m-%d %H:%M:%S', safe_strtotime(@$comment['DATE']));
 170                  $comment_content = $textile->TextileThis(nl2br(@$comment['content']),1);
 171                  if (!safe_field("discussid","txp_discuss","posted = '".doSlash($comment_date)."' AND message = '".doSlash($comment_content)."'")) {
 172                      safe_insert('txp_discuss',
 173                          "parentid='".doSlash($parentid)."',".
 174                          "name='".doSlash(@$comment['AUTHOR'])."',".
 175                          "email='".doSlash(@$comment['EMAIL'])."',".
 176                          "web='".doSlash(@$comment['URL'])."',".
 177                          "ip='".doSlash(@$comment['IP'])."',".
 178                          "posted='".doSlash($comment_date)."',".
 179                          "message='".doSlash($comment_content)."',".
 180                          "visible='1'");
 181                  }
 182              }
 183              update_comments_count($parentid);
 184          }
 185          return $title;
 186      }
 187      return $title.' already imported';
 188  }
 189  
 190  function import_mt_utf8($str) {
 191      if (is_callable('mb_detect_encoding')) {
 192          $enc = mb_detect_encoding($str, 'UTF-8,ASCII,ISO-8859-1');
 193          if ($enc and $enc != 'UTF-8') {
 194              $str = mb_convert_encoding($str, 'UTF-8', $enc);
 195          }
 196      }
 197      return $str;
 198  }
 199  
 200  ?>


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