Advertisement
Guest User

Exporter de Dotclear vers WordPress

a guest
Feb 25th, 2013
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.79 KB | None | 0 0
  1. <?php
  2. /*
  3.  * Script pour exporter des données de Dotclear vers le format de Wordpress
  4.  * Avertissement : j'ai codé comme un porc
  5.  */
  6.  
  7. // Nécessaire pour mettre à jour les URL
  8. include('../wordpress/wp-includes/load.php');
  9. include('../wordpress/wp-includes/plugin.php');
  10. include('../wordpress/wp-includes/default-filters.php');
  11. include('../wordpress/wp-includes/formatting.php');
  12.  
  13. function lien($matches)
  14. {
  15.     $bdd = new PDO('mysql:host=localhost;dbname=dotclear_export', 'root', '***');
  16.     $res = $bdd->query('SELECT post_dt, post_title FROM dc_post WHERE post_url = "' . urldecode($matches[1]) . '"');
  17.     $article = $res->fetch();
  18.    
  19.     $date_pub = new DateTime($article['post_dt']);
  20.     return 'href="/blog/' . $date_pub->format('Y/m/') . sanitize_title($article['post_title']) . '"';
  21. }
  22.  
  23. // Création du XML
  24. $xml = new SimpleXMLElement('<rss version="2.0"
  25.     xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/"
  26.     xmlns:content="http://purl.org/rss/1.0/modules/content/"
  27.     xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  28.     xmlns:dc="http://purl.org/dc/elements/1.1/"
  29.     xmlns:wp="http://wordpress.org/export/1.2/"
  30. />');
  31.  
  32. $channel = $xml->addChild('channel');
  33. $channel->addChild('title', 'Le blog de Mut');
  34. $channel->addChild('link', 'http://mut.angenoir.me/blog');
  35. $channel->addChild('description', 'À compléter');
  36. $channel->addChild('pubDate', date('r'));
  37. $channel->addChild('wp__wxr_version', 1.2);
  38. $channel->addChild('wp__base_site_url', 'http://mut.angenoir.me/blog');
  39. $channel->addChild('wp__base_blog_url', 'http://mut.angenoir.me/blog');
  40.  
  41. $author = $channel->addChild('wp__author');
  42. $author->addChild('wp__author_id', '1');
  43. $author->addChild('wp__author_login', 'mut');
  44. $author->addChild('wp__author_email', '***@gmail.com');
  45. $author->addChild('wp__author_display_name', '<![CDATA[Mut]]>');
  46. $author->addChild('wp__author_first_name', '<![CDATA[]]>');
  47. $author->addChild('wp__author_last_name', '<![CDATA[]]>');
  48.  
  49. // Connexion à la base de données
  50. $bdd = new PDO('mysql:host=localhost;dbname=dotclear_export', 'root', 'rien');
  51. $articles = $bdd->query('SELECT * FROM dc_post LEFT JOIN dc_category USING(cat_id)');
  52.  
  53. while ($article = $articles->fetch()) // Boucle des articles
  54. {
  55.     $item = $channel->addChild('item');
  56.     $item->addChild('title', $article['post_title']);
  57.     $item->addChild('link', $article['post_url']); // Pas important ?
  58.    
  59.     $date_pub = new DateTime($article['post_dt'], new DateTimeZone($article['post_tz']));
  60.     $item->addChild('pubDate', $date_pub->format('r'));
  61.    
  62.     $item->addChild('dc__creator', 'mut');
  63.    
  64.     $guid = $item->addChild('guid', $article['post_url']); // Pas important ?  
  65.     $guid->addAttribute('isPermaLink', 'false');
  66.    
  67.     if (empty($article['post_excerpt_xhtml'])) // Contenu de l'article
  68.         $contenu = $article['post_content_xhtml'];
  69.     else
  70.         $contenu = $article['post_excerpt_xhtml'] . '<!--more-->' . $article['post_content_xhtml'];
  71.    
  72.     $contenu = str_replace('&', '&amp;', $contenu);
  73.     $contenu = preg_replace("!([^>])\r\n!", "$1 ", $contenu); // Sinon Wordpress ajoute des retours à la ligne n'importe où
  74.    
  75.     // Nouvelle adresse des articles
  76.     $contenu = preg_replace_callback('!href="/dotclear/\?post/([^"]+)"!', 'lien', $contenu);
  77.     //$contenu = preg_replace('!href="/dotclear/\?post/([0-9]+)/([0-9]+)/([0-9]+)/([^"]+)"!', 'href="/blog/$1/$2/' . sanitize_title($article['post_title']) . '"', $contenu);
  78.     // Nouvelle adresse des images
  79.     $contenu = str_replace('="/dotclear/public/', '="/blog/wp-content/uploads/dotclear/', $contenu);
  80.    
  81.     $item->addChild('content__encoded', '<![CDATA[' . $contenu . ']]>');
  82.  
  83.     $item->addChild('excerpt__encoded', '<![CDATA[]]>');
  84.     $item->addChild('wp__post_id', $article['post_id']);
  85.     $item->addChild('wp__post_date', $date_pub->format('Y-m-d H:i:s'));
  86.     $item->addChild('wp__post_date_gmt', $date_pub->setTimezone(new DateTimeZone('GMT'))->format('Y-m-d H:i:s'));
  87.     $item->addChild('wp__comment_status', ($article['post_open_comment'] == '1' ? 'open' : 'closed'));
  88.     $item->addChild('wp__ping_status', ($article['post_open_tb'] == '1' ? 'open' : 'closed'));
  89.     $item->addChild('wp__post_name', preg_replace('!^([0-9]+/){3}!', '', $article['post_url']));
  90.     $item->addChild('wp__status', 'publish');
  91.     $item->addChild('wp__post_parent', '0');
  92.     $item->addChild('wp__menu_order', '0');
  93.     $item->addChild('wp__post_type', 'post');
  94.     $item->addChild('wp__post_password', '');
  95.     $item->addChild('wp__is_sticky', '0');
  96.    
  97.     // Aucune idée de l'utilité, mais ça semble obligatoire
  98.     $postmeta = $item->addChild('wp__postmeta');
  99.     $postmeta->addChild('wp__meta_key', '_edit_last');
  100.     $postmeta->addChild('wp__meta_value', '<![CDATA[1]]>');
  101.    
  102.     // Catégorie de l'article
  103.     $cat = $item->addChild('category', '<![CDATA[' . $article['cat_title'] . ']]>');
  104.     $cat->addAttribute('domain', 'category');
  105.     $cat->addAttribute('nicename', $article['cat_url']);
  106.    
  107.     // Étiquettes
  108.     $tags = $bdd->query('SELECT meta_id FROM dc_meta WHERE meta_type = \'tag\' AND post_id = ' . $article['post_id']);
  109.     while ($tag = $tags->fetch())
  110.     {
  111.         $t = $item->addChild('category', '<![CDATA[' . $tag['meta_id'] . ']]>');
  112.         $t->addAttribute('domain', 'post_tag');
  113.         $t->addAttribute('nicename', $tag['meta_id']);
  114.     }
  115.    
  116.     // Commentaires
  117.     if ($article['nb_comment'] != '0' || $article['nb_trackback'] != '0')
  118.     {
  119.         $commentaires = $bdd->query('SELECT * FROM dc_comment WHERE post_id = ' . $article['post_id'] . ' AND comment_status = 1');
  120.         while ($commentaire = $commentaires->fetch())
  121.         {
  122.             $com = $item->addChild('wp__comment');
  123.             $com->addChild('wp__comment_id', $commentaire['comment_id']);
  124.             $com->addChild('wp__comment_author', '<![CDATA[' . $commentaire['comment_author'] . ']]>');
  125.             $com->addChild('wp__comment_author_email', $commentaire['comment_email']);
  126.             $com->addChild('wp__comment_author_url', $commentaire['comment_site']);
  127.             $com->addChild('wp__comment_author_IP', $commentaire['comment_ip']);
  128.            
  129.             $date_com = new DateTime($commentaire['comment_dt'], new DateTimeZone($commentaire['comment_tz']));
  130.             $com->addChild('wp__comment_date', $date_com->format('Y-m-d H:i:s'));
  131.             $com->addChild('wp__comment_date_gmt', $date_com->setTimezone(new DateTimeZone('GMT'))->format('Y-m-d H:i:s'));
  132.            
  133.             $com->addChild('wp__comment_content', '<![CDATA[' . $commentaire['comment_content'] . ']]>');
  134.             $com->addChild('wp__comment_approved', '1');
  135.             if ($commentaire['comment_trackback'] == 1)
  136.                 $com->addChild('wp__comment_type', 'pingback');
  137.             else
  138.                 $com->addChild('wp__comment_type');
  139.             $com->addChild('wp__comment_parent', '0');
  140.             if ($commentaire['comment_email'] == '***@gmail.com') // Si l'auteur c'est moi
  141.                 $com->addChild('wp__comment_user_id', '1');
  142.             else
  143.                 $com->addChild('wp__comment_user_id', '0');
  144.         }
  145.     }
  146. }
  147.  
  148. $articles->closeCursor();
  149.  
  150. // Affichage du XML
  151. header('Content-type: text/xml');
  152. echo html_entity_decode(str_replace('__', ':', $xml->asXML()));
  153. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement