home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 108 / MacAddict108.iso / Software / Internet & Communication / WordPress 1.5.1.dmg / wordpress / wp-admin / import-livejournal.php < prev    next >
Encoding:
PHP Script  |  2005-04-19  |  4.2 KB  |  124 lines

  1. <?php
  2. define('XMLFILE', '');
  3. // Example:
  4. // define('XMLFILE', '/home/example/public_html/rss.xml');
  5. // or if it's in the same directory as import-rss.php
  6. // define('XMLFILE', 'rss.xml');
  7.  
  8. $post_author = 1; // Author to import posts as author ID
  9. $timezone_offset = 0; // GMT offset of posts your importing
  10.  
  11.  
  12. $add_hours = intval($timezone_offset);
  13. $add_minutes = intval(60 * ($timezone_offset - $add_hours));
  14.  
  15. if (!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.");
  16. require('../wp-config.php');
  17.  
  18. $step = $_GET['step'];
  19. if (!$step) $step = 0;
  20. header( 'Content-Type: text/html; charset=utf-8' );
  21. ?>
  22. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  23. <html xmlns="http://www.w3.org/1999/xhtml">
  24. <title>WordPress › Import from RSS</title>
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  26. <style media="screen" type="text/css">
  27.     body {
  28.         font-family: Georgia, "Times New Roman", Times, serif;
  29.         margin-left: 20%;
  30.         margin-right: 20%;
  31.     }
  32.     #logo {
  33.         margin: 0;
  34.         padding: 0;
  35.         background-image: url(http://wordpress.org/images/logo.png);
  36.         background-repeat: no-repeat;
  37.         height: 60px;
  38.         border-bottom: 4px solid #333;
  39.     }
  40.     #logo a {
  41.         display: block;
  42.         text-decoration: none;
  43.         text-indent: -100em;
  44.         height: 60px;
  45.     }
  46.     p {
  47.         line-height: 140%;
  48.     }
  49.     </style>
  50. </head><body> 
  51. <h1 id="logo"><a href="http://wordpress.org/">WordPress</a></h1> 
  52. <?php
  53. switch($step) {
  54.  
  55.     case 0:
  56. ?> 
  57. <p>Howdy! This importer allows you to extract posts from a LiveJournal XML export file. To get started you must edit the following line in this file (<code>import-livejournal.php</code>) </p>
  58. <p><code>define('XMLFILE', '');</code></p>
  59. <p>You want to define where the XML file we'll be working with is, for example: </p>
  60. <p><code>define('XMLFILE', '2002-04.xml');</code></p>
  61. <p>You have to do this manually for security reasons.</p>
  62. <p>If you've done that and you’re all ready, <a href="import-livejournal.php?step=1">let's go</a>!</p>
  63. <?php
  64.     break;
  65.     
  66.     case 1:
  67. if ('' != XMLFILE && !file_exists(XMLFILE)) die("The file you specified does not seem to exist. Please check the path you've given.");
  68. if ('' == XMLFILE) die("You must edit the XMLFILE line as described on the <a href='import-rss.php'>previous page</a> to continue.");
  69.  
  70. // Bring in the data
  71. set_magic_quotes_runtime(0);
  72. $datalines = file(XMLFILE); // Read the file into an array
  73. $importdata = implode('', $datalines); // squish it
  74. $importdata = str_replace(array("\r\n", "\r"), "\n", $importdata);
  75.  
  76. preg_match_all('|<entry>(.*?)</entry>|is', $importdata, $posts);
  77. $posts = $posts[1];
  78.  
  79. echo '<ol>';
  80. foreach ($posts as $post) :
  81. $title = $date = $categories = $content = $post_id =  '';
  82. echo "<li>Importing post... ";
  83.  
  84. preg_match('|<subject>(.*?)</subject>|is', $post, $title);
  85. $title = addslashes( trim($title[1]) );
  86. $post_name = sanitize_title($title);
  87.  
  88. preg_match('|<eventtime>(.*?)</eventtime>|is', $post, $date);
  89. $date = strtotime($date[1]);
  90.  
  91. $post_date = date('Y-m-d H:i:s', $date);
  92.  
  93.  
  94. preg_match('|<event>(.*?)</event>|is', $post, $content);
  95. $content = str_replace( array('<![CDATA[', ']]>'), '', addslashes( trim($content[1]) ) );
  96.  
  97. // Now lets put it in the DB
  98. if ($wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' AND post_date = '$post_date'")) :
  99.     echo 'Post already imported';
  100. else : 
  101.     
  102.     $wpdb->query("INSERT INTO $wpdb->posts 
  103.         (post_author, post_date, post_date_gmt, post_content, post_title,post_status, comment_status, ping_status, post_name)
  104.         VALUES 
  105.         ('$post_author', '$post_date', DATE_ADD('$post_date', INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE), '$content', '$title', 'publish', '$comment_status', '$ping_status', '$post_name')");
  106.     $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' AND post_date = '$post_date'");
  107.     if (!$post_id) die("couldn't get post ID");
  108.     $exists = $wpdb->get_row("SELECT * FROM $wpdb->post2cat WHERE post_id = $post_id AND category_id = 1");
  109.     if (!$exists) $wpdb->query("INSERT INTO $wpdb->post2cat (post_id, category_id) VALUES ($post_id, 1) ");
  110.     echo 'Done!</li>';
  111. endif;
  112.  
  113.  
  114. endforeach;
  115. ?>
  116. </ol>
  117.  
  118. <h3>All done. <a href="../">Have fun!</a></h3>
  119. <?php
  120.     break;
  121. }
  122. ?> 
  123. </body>
  124. </html>