Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Script pour exporter des données de Dotclear vers le format de Wordpress
- * Avertissement : j'ai codé comme un porc
- */
- // Nécessaire pour mettre à jour les URL
- include('../wordpress/wp-includes/load.php');
- include('../wordpress/wp-includes/plugin.php');
- include('../wordpress/wp-includes/default-filters.php');
- include('../wordpress/wp-includes/formatting.php');
- function lien($matches)
- {
- $bdd = new PDO('mysql:host=localhost;dbname=dotclear_export', 'root', '***');
- $res = $bdd->query('SELECT post_dt, post_title FROM dc_post WHERE post_url = "' . urldecode($matches[1]) . '"');
- $article = $res->fetch();
- $date_pub = new DateTime($article['post_dt']);
- return 'href="/blog/' . $date_pub->format('Y/m/') . sanitize_title($article['post_title']) . '"';
- }
- // Création du XML
- $xml = new SimpleXMLElement('<rss version="2.0"
- xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/"
- xmlns:content="http://purl.org/rss/1.0/modules/content/"
- xmlns:wfw="http://wellformedweb.org/CommentAPI/"
- xmlns:dc="http://purl.org/dc/elements/1.1/"
- xmlns:wp="http://wordpress.org/export/1.2/"
- />');
- $channel = $xml->addChild('channel');
- $channel->addChild('title', 'Le blog de Mut');
- $channel->addChild('link', 'http://mut.angenoir.me/blog');
- $channel->addChild('description', 'À compléter');
- $channel->addChild('pubDate', date('r'));
- $channel->addChild('wp__wxr_version', 1.2);
- $channel->addChild('wp__base_site_url', 'http://mut.angenoir.me/blog');
- $channel->addChild('wp__base_blog_url', 'http://mut.angenoir.me/blog');
- $author = $channel->addChild('wp__author');
- $author->addChild('wp__author_id', '1');
- $author->addChild('wp__author_login', 'mut');
- $author->addChild('wp__author_email', '***@gmail.com');
- $author->addChild('wp__author_display_name', '<![CDATA[Mut]]>');
- $author->addChild('wp__author_first_name', '<![CDATA[]]>');
- $author->addChild('wp__author_last_name', '<![CDATA[]]>');
- // Connexion à la base de données
- $bdd = new PDO('mysql:host=localhost;dbname=dotclear_export', 'root', 'rien');
- $articles = $bdd->query('SELECT * FROM dc_post LEFT JOIN dc_category USING(cat_id)');
- while ($article = $articles->fetch()) // Boucle des articles
- {
- $item = $channel->addChild('item');
- $item->addChild('title', $article['post_title']);
- $item->addChild('link', $article['post_url']); // Pas important ?
- $date_pub = new DateTime($article['post_dt'], new DateTimeZone($article['post_tz']));
- $item->addChild('pubDate', $date_pub->format('r'));
- $item->addChild('dc__creator', 'mut');
- $guid = $item->addChild('guid', $article['post_url']); // Pas important ?
- $guid->addAttribute('isPermaLink', 'false');
- if (empty($article['post_excerpt_xhtml'])) // Contenu de l'article
- $contenu = $article['post_content_xhtml'];
- else
- $contenu = $article['post_excerpt_xhtml'] . '<!--more-->' . $article['post_content_xhtml'];
- $contenu = str_replace('&', '&', $contenu);
- $contenu = preg_replace("!([^>])\r\n!", "$1 ", $contenu); // Sinon Wordpress ajoute des retours à la ligne n'importe où
- // Nouvelle adresse des articles
- $contenu = preg_replace_callback('!href="/dotclear/\?post/([^"]+)"!', 'lien', $contenu);
- //$contenu = preg_replace('!href="/dotclear/\?post/([0-9]+)/([0-9]+)/([0-9]+)/([^"]+)"!', 'href="/blog/$1/$2/' . sanitize_title($article['post_title']) . '"', $contenu);
- // Nouvelle adresse des images
- $contenu = str_replace('="/dotclear/public/', '="/blog/wp-content/uploads/dotclear/', $contenu);
- $item->addChild('content__encoded', '<![CDATA[' . $contenu . ']]>');
- $item->addChild('excerpt__encoded', '<![CDATA[]]>');
- $item->addChild('wp__post_id', $article['post_id']);
- $item->addChild('wp__post_date', $date_pub->format('Y-m-d H:i:s'));
- $item->addChild('wp__post_date_gmt', $date_pub->setTimezone(new DateTimeZone('GMT'))->format('Y-m-d H:i:s'));
- $item->addChild('wp__comment_status', ($article['post_open_comment'] == '1' ? 'open' : 'closed'));
- $item->addChild('wp__ping_status', ($article['post_open_tb'] == '1' ? 'open' : 'closed'));
- $item->addChild('wp__post_name', preg_replace('!^([0-9]+/){3}!', '', $article['post_url']));
- $item->addChild('wp__status', 'publish');
- $item->addChild('wp__post_parent', '0');
- $item->addChild('wp__menu_order', '0');
- $item->addChild('wp__post_type', 'post');
- $item->addChild('wp__post_password', '');
- $item->addChild('wp__is_sticky', '0');
- // Aucune idée de l'utilité, mais ça semble obligatoire
- $postmeta = $item->addChild('wp__postmeta');
- $postmeta->addChild('wp__meta_key', '_edit_last');
- $postmeta->addChild('wp__meta_value', '<![CDATA[1]]>');
- // Catégorie de l'article
- $cat = $item->addChild('category', '<![CDATA[' . $article['cat_title'] . ']]>');
- $cat->addAttribute('domain', 'category');
- $cat->addAttribute('nicename', $article['cat_url']);
- // Étiquettes
- $tags = $bdd->query('SELECT meta_id FROM dc_meta WHERE meta_type = \'tag\' AND post_id = ' . $article['post_id']);
- while ($tag = $tags->fetch())
- {
- $t = $item->addChild('category', '<![CDATA[' . $tag['meta_id'] . ']]>');
- $t->addAttribute('domain', 'post_tag');
- $t->addAttribute('nicename', $tag['meta_id']);
- }
- // Commentaires
- if ($article['nb_comment'] != '0' || $article['nb_trackback'] != '0')
- {
- $commentaires = $bdd->query('SELECT * FROM dc_comment WHERE post_id = ' . $article['post_id'] . ' AND comment_status = 1');
- while ($commentaire = $commentaires->fetch())
- {
- $com = $item->addChild('wp__comment');
- $com->addChild('wp__comment_id', $commentaire['comment_id']);
- $com->addChild('wp__comment_author', '<![CDATA[' . $commentaire['comment_author'] . ']]>');
- $com->addChild('wp__comment_author_email', $commentaire['comment_email']);
- $com->addChild('wp__comment_author_url', $commentaire['comment_site']);
- $com->addChild('wp__comment_author_IP', $commentaire['comment_ip']);
- $date_com = new DateTime($commentaire['comment_dt'], new DateTimeZone($commentaire['comment_tz']));
- $com->addChild('wp__comment_date', $date_com->format('Y-m-d H:i:s'));
- $com->addChild('wp__comment_date_gmt', $date_com->setTimezone(new DateTimeZone('GMT'))->format('Y-m-d H:i:s'));
- $com->addChild('wp__comment_content', '<![CDATA[' . $commentaire['comment_content'] . ']]>');
- $com->addChild('wp__comment_approved', '1');
- if ($commentaire['comment_trackback'] == 1)
- $com->addChild('wp__comment_type', 'pingback');
- else
- $com->addChild('wp__comment_type');
- $com->addChild('wp__comment_parent', '0');
- if ($commentaire['comment_email'] == '***@gmail.com') // Si l'auteur c'est moi
- $com->addChild('wp__comment_user_id', '1');
- else
- $com->addChild('wp__comment_user_id', '0');
- }
- }
- }
- $articles->closeCursor();
- // Affichage du XML
- header('Content-type: text/xml');
- echo html_entity_decode(str_replace('__', ':', $xml->asXML()));
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement