[ Index ]
 

Code source de Serendipity 1.2

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/include/admin/importers/ -> movabletype.inc.php (source)

   1  <?php # $Id: movabletype.inc.php 1816 2007-08-06 10:18:39Z garvinhicking $
   2  # Copyright (c) 2003-2005, Jannis Hermanns (on behalf the Serendipity Developer Team)
   3  # All rights reserved.  See LICENSE file for licensing details
   4  
   5  /*****************************************************************
   6   *                MovableType Importer, by Evan Nemerson         *
   7   *****************************************************************/
   8  
   9  switch ($serendipity['lang']) {
  10      case 'de':
  11          @define('IMPORTER_MT_WARN_PLUGIN',     'Bitte installieren Sie das Plugin "%s"');
  12          @define('IMPORTER_MT_NOTE', 'Falls Sie weiter machen, ohne die Plugins zu installieren, werden möglicherweise Zeilenumbrüche falsch importiert (verdoppelt oder entfernt)');
  13          break;
  14  
  15      case 'en':
  16      default:
  17          @define('IMPORTER_MT_WARN_PLUGIN',     'Please install the plugin "%s"');
  18          @define('IMPORTER_MT_NOTE', 'If you continue without installing those plugins, line breaks may be incorrectly imported (doubled or removed)');
  19          break;
  20  }
  21  
  22  class Serendipity_Import_MovableType extends Serendipity_Import {
  23      var $info        = array('software' => 'MovableType');
  24      var $data        = array();
  25      var $inputFields = array();
  26  
  27      function Serendipity_Import_MovableType($data) {
  28          $this->data = $data;
  29          $this->inputFields = array(array('text'    => MT_DATA_FILE,
  30                                           'type'    => 'file',
  31                                           'name'    => 'mt_dat'),
  32  
  33                                     array('text'    => FORCE,
  34                                           'type'    => 'bool',
  35                                           'name'    => 'mt_force',
  36                                           'default' => 'true'),
  37  
  38                                     array('text'    => CHARSET,
  39                                           'type'    => 'list',
  40                                           'name'    => 'charset',
  41                                           'value'   => 'UTF-8',
  42                                           'default' => $this->getCharsets(true)),
  43  
  44                                     array('text'    => ACTIVATE_AUTODISCOVERY,
  45                                           'type'    => 'bool',
  46                                           'name'    => 'autodiscovery',
  47                                           'default' => 'false'),
  48  
  49                                     array('text'    => 'Debugging',
  50                                           'type'    => 'bool',
  51                                           'name'    => 'debug',
  52                                           'default' => 'false')
  53                              );
  54      }
  55  
  56      function debug($string) {
  57          static $debug = null;
  58          static $c = 0;
  59  
  60          if ($debug === null) {
  61              if ($this->data['debug'] == 'true') {
  62                  $debug = true;
  63              } else {
  64                  $debug = false;
  65              }
  66          }
  67  
  68          if ($debug) {
  69              $c++;
  70              echo '#' . $c . ' [' . date('d.m.Y H:i.s') . '] ' . $string . "<br />\n";
  71          }
  72      }
  73  
  74      function getImportNotes(){
  75          $notes = array();
  76          if (!class_exists('serendipity_event_nl2br')){
  77              $notes[] = sprintf(IMPORTER_MT_WARN_PLUGIN, 'serendipity_event_nl2br');
  78          }
  79          if (!class_exists('serendipity_event_entryproperties')){
  80              $notes[] = sprintf(IMPORTER_MT_WARN_PLUGIN, 'serendipity_event_entryproperties');
  81          }
  82          if (count($notes) > 0){
  83              return '<ul><li>'.implode('</li><li>', $notes).'</li></ul>'.IMPORTER_MT_NOTE;
  84          }
  85      }
  86      function validateData() {
  87          return sizeof($this->data);
  88      }
  89  
  90      function getInputFields() {
  91          return $this->inputFields;
  92      }
  93  
  94      function toTime($string) {
  95          $ts = strtotime($string);
  96          $this->debug('Calling strtotime(' . $string . ') = ' . $ts);
  97          if ($ts <= 1) {
  98              // Match strings like: "11/16/2005 00:14:53 PM"
  99              if (preg_match('@([01][0-9])/([0-3][0-9])/([0-9]{4}) ([0-2][0-9]):([0-5][0-9]):([0-5][0-9]) (P|A|F|E)M@i', $string, $match)) {
 100                  if ($match[7] == 'P' || $match[7] == 'E') {
 101                      // Post mediam, add 12 hours.
 102                      $match[4] = $match[4] + 12;
 103                  }
 104  
 105                  $ts = mktime($match[4], $match[5], $match[6], $match[1], $match[2], $match[3]);
 106                  $this->debug('Matched string date format: ' . $ts);
 107              }
 108          }
 109  
 110          if ($ts <= 1) {
 111              $ts = time();
 112          }
 113  
 114          return $ts;
 115      }
 116  
 117      function doEntryWork(&$mt_entry, &$tasks){
 118          global $serendipity;
 119  
 120          $authors = array();
 121          $entry = array();
 122          $entry['categories'] = array();
 123          $entryprops = array();
 124  
 125          $this->debug("doEntryWork: " . print_r($mt_entry, true));
 126  
 127          foreach($mt_entry as $name => $data) {
 128              $name = trim($name);
 129              if (is_string($data)) {
 130                  $data = trim($data);
 131              }
 132              $this->debug($name . ': "' . print_r($data, true) . '"');
 133              switch($name) {
 134                  case 's9y_comments':
 135                      $entry['s9y_comments'] = $data;
 136                      break;
 137                  case 'AUTHOR':
 138                      if ( !isset($authors[$data]) ) {
 139                          $au_inf = serendipity_fetchAuthor($data);
 140                          if ( !is_array($au_inf) ) {
 141                              $tasks[] = sprintf(CREATE_AUTHOR, htmlspecialchars($data));
 142                              $tasks[] = 'Input array is: ' . print_r($data, true) . '<br />Return is: ' . print_r($au_inf, true) . '<br />';
 143                              $au_inf = serendipity_fetchAuthor($serendipity['authorid']);
 144                          }
 145                          $authors[$data] = $au_inf[0];
 146                      }
 147                      $entry['authorid'] = $authors[$data]['authorid'];
 148                      $entry['author'] = $authors[$data]['username'];
 149                      break;
 150                  case 'TITLE':
 151                      $entry['title'] = $data;
 152                      break;
 153                  case 'STATUS':
 154                      $entry['isdraft'] = ($data == 'Publish') ? 'false' : 'true';
 155                      break;
 156                  case 'ALLOW COMMENTS':
 157                      $entry['allow_comments'] = ($data == '1') ? 'true' : 'false';
 158                      break;
 159                  case 'DATE':
 160                      $entry['timestamp'] = $this->totime($data);
 161                      break;
 162  
 163                  case 's9y_body':
 164                  case 'BODY':
 165                      $entry['body'] = $data;
 166                      break;
 167  
 168                  case 's9y_extended':
 169                  case 'EXTENDED BODY':
 170                      $entry['extended'] = $data;
 171                      break;
 172  
 173                  case 'CONVERT BREAKS':
 174                      $entryprops['nl2br'] = ($data == '1') ? true : false;
 175                      break;
 176  
 177                  case 'PRIMARY CATEGORY':
 178                  case 'CATEGORY':
 179                      $categories = explode("\0", $data);
 180                      #echo '<pre>' . print_r($this->categories, true) . '</pre>';
 181                      foreach($categories AS $data) {
 182                          $data = trim($data);
 183                          $cat_found  = false;
 184                          if (is_array($this->categories)) {
 185                              for ( $y=0 ; $y<sizeof($this->categories) ; $y++ ) {
 186                                  echo '"' . $this->categories[$y]['category_name'] . '" == "' . $data . '"<br />';
 187                                  if ( $this->categories[$y]['category_name'] == $data ) {
 188                                      $cat_found = true;
 189                                      break;
 190                                  }
 191                              }
 192                              if ($cat_found) {
 193                                  if (!in_array($this->categories[$y]['categoryid'], $entry['categories']) ) {
 194                                      //$entries[$n]['categories'][] = $categories[$y]['categoryid'];
 195                                      $entry['categories'][] = $this->categories[$y]['categoryid'];
 196                                  }
 197                              } else {
 198                                  $tasks[] = sprintf(CREATE_CATEGORY, htmlspecialchars($data));
 199                              }
 200                          }
 201                      }
 202                      break;
 203              }
 204          }
 205          $entry['props'] = $entryprops;
 206          return $entry;
 207      }
 208  
 209      function doCommentWork(&$mt_entry, &$tasks, $type = 'NORMAL'){
 210          $comment = array(
 211              'parent_id'  => 0,
 212              'status'     => 'approved',
 213              'subscribed' => 'false',
 214              'type'       => $type,
 215              'body'       => ''
 216          );
 217  
 218          $this->debug("MT_ENTRY: " . print_r($mt_entry, true));
 219          $parsed_entry   = array();
 220          $unparsed_entry = explode("\n", $mt_entry[$type == 'NORMAL' ? 'COMMENT' : 'PING']);
 221          foreach($unparsed_entry AS $line) {
 222              if (preg_match('/^([A-Z\s]+):\s+(.*)$/', $line, $match)) {
 223                  $parsed_entry[$match[1]] = $match[2];
 224              } else {
 225                  $parsed_entry['s9y_body'] .= $line . "\n";
 226              }
 227          }
 228  
 229          foreach($parsed_entry as $name => $data){
 230              $data = trim($data);
 231              $name = trim($name);
 232  
 233              switch($name) {
 234                  case 'EMAIL':
 235                      $comment['email'] = $data;
 236                      break;
 237  
 238                  case 'URL':
 239                      $comment['url'] = $data;
 240                      break;
 241  
 242                  case 'IP':
 243                      $comment['ip'] = $data;
 244                      break;
 245  
 246                  case 'AUTHOR':
 247                  case 'BLOG NAME':
 248                      $comment['author'] = $data;
 249                      break;
 250  
 251                  case 'DATE':
 252                      $comment['timestamp'] = $this->toTime($data);
 253                      break;
 254  
 255                  case 'REPLY':
 256                  case 'TITLE':
 257                      break;
 258  
 259                  default:
 260                      $comment['body'] .= $data;
 261              }
 262          }
 263  
 264          $this->debug("S9Y_ENTRY: " . print_r($comment, true));
 265          return $comment;
 266      }
 267  
 268      function import() {
 269          global $serendipity;
 270  
 271          $force    = ($this->data['mt_force'] == 'true');
 272  
 273          if ($this->data['autodiscovery'] == 'false') {
 274              $serendipity['noautodiscovery'] = 1;
 275          }
 276  
 277          // Rewritten to parse the file line by line. Can save quite some
 278          // memory on large blogs
 279          //$contents   = file_get_contents($_FILES['serendipity']['tmp_name']['import']['mt_dat']);
 280  
 281          $this->categories = serendipity_fetchCategories();
 282          $tasks      = array();
 283  
 284          $entries = array();
 285  
 286          if (empty($_FILES['serendipity']['tmp_name']['import']['mt_dat'])) {
 287              $fh = fopen('/tmp/mt.dat', 'r');
 288          } else {
 289              $fh = fopen($_FILES['serendipity']['tmp_name']['import']['mt_dat'], 'r');
 290          }
 291  
 292          $entry        = array();
 293          $el           = "";
 294          $c_el         = "";
 295          $skip         = false;
 296          $is_comment   = false;
 297          $is_trackback = false;
 298          $nofetch      = false;
 299          while (!feof($fh)) {
 300              if ($nofetch === false) {
 301                  $this->debug('Next line');
 302                  $line = $this->decode(fgets($fh, 8192));
 303              } else {
 304                  $this->debug('NO Next line');
 305                  // Keep line from previous run.
 306                  $nofetch = false;
 307              }
 308  
 309              if ($is_comment || $is_trackback) {
 310                  $this->debug("COMMENT/TRACKBACK mode is active.");
 311                  if (preg_match('/^--------/', $line)) {
 312                      $this->debug("Next full section requested.");
 313                      $is_comment = $is_trackback = false;
 314                  } elseif (preg_match('/^-----/', $line)) {
 315                      $this->debug("Next partial section requested.");
 316                      if ($is_trackback) {
 317                          $this->debug("Parsing trackback.");
 318                          $entry['s9y_comments'][] = $this->doCommentWork($comment, $tasks, 'TRACKBACK');
 319                      } elseif ($is_comment) {
 320                          $this->debug("Parsing comment.");
 321                          $entry['s9y_comments'][] = $this->doCommentWork($comment, $tasks, 'NORMAL');
 322                      }
 323                      $el = $c_el = "";
 324                  }
 325              }
 326  
 327              if ($skip && (!preg_match('/^--------/', $line))) {
 328                  $this->debug("No next section match, and skip is activated. Skipping '$line'");
 329                  continue;
 330              }
 331  
 332              if (preg_match('/^--------/', $line)) {
 333                  // We found the end marker of the current entry. Add to entries-Array
 334                  $this->debug("End marker found. Parsing full entry.");
 335                  $entries[]    = $this->doEntryWork($entry, $tasks);
 336                  $entry        = array();
 337                  $el           = "";
 338                  $c_el         = "";
 339                  $skip         = false;
 340                  $is_comment   = false;
 341                  $is_trackback = false;
 342              } elseif (preg_match('/^-----/', $line)) {
 343                  $this->debug("New section match. Current EL: $el");
 344                  unset($el); # DEBUG!
 345                  if (empty($el)) {
 346                      $line = $this->decode(fgets($fh, 8192));
 347                      $this->debug("Inspecting next line: $line");
 348                      $tline = trim($line);
 349                      while (($is_comment || $is_trackback) && empty($tline)) {
 350                          $line = $this->decode(fgets($fh, 8192));
 351                          $tline = trim($line);
 352                          $this->debug("Continuing inspecting next line: $line");
 353                      }
 354                      if (preg_match('/^--------/', $line)) {
 355                          $this->debug('Next line is new element. End marker found. Parsing full entry.');
 356                          $entries[]    = $this->doEntryWork($entry, $tasks);
 357                          $entry        = array();
 358                          $el           = "";
 359                          $c_el         = "";
 360                          $skip         = false;
 361                          $is_comment   = false;
 362                          $is_trackback = false;
 363                      } elseif (preg_match('/^([A-Z\s]+):/', $line, $matches)) {
 364                          $this->debug("Match result: $matches[1]");
 365                          if ($matches[1] == 'COMMENT') {
 366                              $this->debug("Marking COMMENT.");
 367                              $is_comment   = true;
 368                              $is_trackback = false;
 369                              $comment      = array();
 370                              $skip         = false;
 371                          } elseif ($matches[1] == 'PING') {
 372                              $this->debug("Marking TRACKBACK");
 373                              $is_comment   = false;
 374                              $is_trackback = true;
 375                              $comment      = array();
 376                              $skip         = false;
 377                          }
 378  
 379                          $this->debug("Setting EL to {$matches[1]}");
 380                          $el   = $matches[1];
 381                          $c_el = "";
 382                      } else {
 383                          $this->debug("Could not parse next line. Keeping it for next cycle.");
 384                          $nofetch = true;
 385                      }
 386                  } else {
 387                      $this->debug("Resetting EL to an empty string");
 388                      $el = $c_el = "";
 389                  }
 390              } elseif (empty($el)) {
 391                  $this->debug("EL is empty. Line is '$line'");
 392                  $content = "";
 393                  if (preg_match('/^([A-Z\s]+):\s+(.*)$/s', $line, $matches)) {
 394                      $this->debug("Section match {$matches[1]} found, input: {$matches[2]}");
 395                      $c_el    = $matches[1];
 396                      $content = $matches[2];
 397                  } elseif (!empty($c_el)) {
 398                      $this->debug("Still in subsection of previous run: $c_el.");
 399                      $content = trim($line);
 400                  }
 401  
 402                  if (!empty($content)) {
 403                      if ($is_comment || $is_trackback) {
 404                          $this->debug("Appending to comments: $line");
 405                          $comment[$c_el] = $content;
 406                      } else {
 407                          $this->debug("Appending to entry: $line");
 408                          if (isset($entry[$c_el])) {
 409                              $entry[$c_el] .= "\0" . $content;
 410                          } else {
 411                              $entry[$c_el]   = $content;
 412                          }
 413                      }
 414                  }
 415              } elseif ($is_comment || $is_trackback) {
 416                  $this->debug("Appending Line in current Element $el to comments: $line");
 417                  $comment[$el] .= $line;
 418              } else {
 419                  $this->debug("Appending Line in current Element $el to entries: $line");
 420                  $entry[$el] .= $line;
 421              }
 422          }
 423          fclose($fh);
 424  
 425          if ( !sizeof($tasks) || $force == true ) {
 426              serendipity_db_begin_transaction();
 427              foreach ($entries as $entry) {
 428                  #echo '<pre>' . printR_($entry, true) . '</pre><br />';
 429                  #continue;
 430                  if (empty($entry['authorid'])) {
 431                      $entry['authorid'] = $serendipity['authorid'];
 432                      $entry['author']   = $serendipity['realname'];
 433                  }
 434  
 435                  if (!isset($entry['isdraft'])) {
 436                      $entry['isdraft'] = 'false';
 437                  }
 438  
 439                  if (!isset($entry['allow_comments'])) {
 440                      $entry['allow_comments'] = 'true';
 441                  }
 442  
 443                  $comments   = $entry['s9y_comments'];
 444                  $entryprops = $entry['props'];
 445                  unset($entry['props']);
 446                  unset($entry['s9y_comments']);
 447  
 448                  if ( !is_int($r = serendipity_updertEntry($entry)) ) {
 449                      echo '<div class="serendipityAdminMsgError"><img style="width: 22px; height: 22px; border: 0px; padding-right: 4px; vertical-align: middle" src="' . serendipity_getTemplateFile('admin/img/admin_msg_error.png') . '" alt="" />' . $r . '</div>';
 450                  } else {
 451                      $this->debug('Saved entry ' . $r . ' (' . $entry['title'] . ')');
 452                      $entry['id'] = $r;
 453                      foreach((array)$comments AS $comment) {
 454                          $comment['entry_id'] = $r;
 455                          if ($rc = serendipity_db_insert('comments', $comment)) {
 456                              $cid = serendipity_db_insert_id('comments', 'id');
 457                              serendipity_approveComment($cid, $entry['id'], true);
 458                          } else {
 459                              echo '<div class="serendipityAdminMsgError"><img style="width: 22px; height: 22px; border: 0px; padding-right: 4px; vertical-align: middle" src="' . serendipity_getTemplateFile('admin/img/admin_msg_error.png') . '" alt="" />' . $rc . '</div>';
 460                          }
 461                      }
 462                      // Let the plugins do some additional stuff. Here it's used with
 463                      // event_entryproperties in mind to setup the nl2br-stuff
 464                      serendipity_plugin_api::hook_event('backend_import_entry', $entry, $entryprops);
 465                  }
 466              }
 467              serendipity_db_end_transaction(true);
 468              return true;
 469          } else {
 470              return '<ul><li>'.implode('</li><li>', array_unique($tasks)).'</li></ul>';
 471          }
 472      }
 473  }
 474  return 'Serendipity_Import_MovableType';
 475  ?>


Généré le : Sat Nov 24 09:00:37 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics