Citations. Comme les proverbes, j'en invente et personne jusqu'ici ne semble s'en être aperçu. Hervé Bazin
Howto création de sujet et de réponse dans le forum de Drupal par code.
Ayant lancé un sujet sur drupalfr pour connaitre la meilleur méthode de finir la migration de mon site vers Drupal 7.22, Vincent59 m'a aiguillé vers la bonne voie, Je le remercie encore.
Seulement cela ne s'est pas fait sans problème.
Voici comment j'ai procédé.
Le but de l'excercice était de transférer tout les sujets contenu dans le forum de mon ancien site, postnuke+pnforum, vers Drupal 7.22.
Il y avait les bbcode dans les sujets.
J'avais déjà créé toute l'architecture des forums dans le nouveau site.
La migration c'est faite forum par forum.
J'ai d'abord eu du mal a connaitre la bonne manière de créer de nouveau topic pour le forum de Drupal.
Après quelques recherche et beaucoup de testes voici ma méthode:
function createTopic($title, $body, $postDate, $forumTitle)
{
$node = new stdClass();
// Set the values for the node
$node->title = utf8_encode($title);
$node->body[LANGUAGE_NONE][0]['value'] = utf8_encode(parseubb($body));
$node->body[LANGUAGE_NONE][0]['format'] = 'full_html';
$node->type = 'forum'; // Your specified content type
$node->created = convertDate($postDate);
$node->changed = $node->created;
$node->status = 1; // To have published, else use 0
$node->promote = 0; // If you want promoted to front page, else use 0
$node->comment = 2;
$node->sticky = 0;
$node->format = 1; // Filtered HTML
$node->uid = 1; // UID of content owner
$node->language = LANGUAGE_NONE;
$node->taxonomy_forums['und'][0]['tid'] = getForumID($forumTitle);
node_save($node);
return $node->nid;
}
Les réponses au topic sont enfaite des commentaires dans Drupal.
Voici la méthode utilisée:
function createTopicResponse($nid, $subject, $text, $postDate)
{
$subject = (strlen($subject) > 60) ? substr($subject,0,60).'...' : $subject;
$comment = (object) array(
'nid' => $nid,
'cid' => 0,
'pid' => 0,
'uid' => 1,
'mail' => '',
'created' => convertDate($postDate),
'changed' => convertDate($postDate),
'is_anonymous' => 0,
'homepage' => '',
'status' => COMMENT_PUBLISHED,
'subject' => utf8_encode($subject),
'language' => LANGUAGE_NONE,
'comment_body' => array(
LANGUAGE_NONE => array(
0 => array (
'value' => utf8_encode(parseubb($text)),
'format' => 'full_html'
)
)
),
);
comment_submit($comment);
comment_save($comment);
return $comment->cid;
}
La première ligne de code on peut constater que je coupe le sujet.
Ca fait partie des galères de migration ça et de la non connaissance du nouveau système.
Dans drupal les sujets des commentaires sont limité à 64 caractères, mais pas le sujet des topics.
Dès que ces deux méthodes étaient fonctionnel, le reste c'est un jeu d'enfants.
Lecture des post dans l'ancienne DB, un boucle et une rupture et l'affaire était réglé.
Voici la quasi totalité du script php utilisé pour la migration:
<?php
define('DRUPAL_ROOT','.');
require_once 'includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
require_once 'modules/forum/forum.admin.inc';
/**
* Get the id for a forum based on his name.
*
* Search in Taxonomy of Drupal the forum.
*
* @param $forumName
* The name of the forum to search for.
*
* @return
* The ID of the founded forum otherwise false;
*/
function getForumID($forumName){
$forumTerm = taxonomy_get_term_by_name($forumName);
if (count($forumTerm) !== 0){
foreach ($forumTerm as $key => $value)
{
$returnValue = $value->tid;
}
return $returnValue;
}
else {
return false;
}
}
/**
* Replace bbcode by html syntaxe.
*
* Replace bbcode in a text bloc. All bbcode are replace except smilies.
* This came from www.unibia.com, thanks to him.
*
* @param $text
* Text to treat.
*
* @return
* The modified text.
*/
function parseubb($text) {
$text = preg_replace('#\[b\](.*?)\[/b\]#si', '<b>\1</b>', $text);
$text = preg_replace('#\[i\](.*?)\[/i\]#si', '<i>\1</i>', $text);
$text = preg_replace('#\[u\](.*?)\[/u\]#si', '<u>\1</u>', $text);
$text = preg_replace('#\[center\](.*?)\[/center\]#si', '<center>\1</center>', $text);
$text = preg_replace('#\[url\]([\r\n]*)(http://|ftp://|https://|ftps://)([^\s\'\";\+]*?)([\r\n]*)\[/url\]#si', '<a href=\'\2\3\' target=\'_blank\'>\2\3</a>', $text);
$text = preg_replace('#\[url\]([\r\n]*)([^\s\'\";\+]*?)([\r\n]*)\[/url\]#si', '<a href=\'http://\2\' target=\'_blank\'>\2</a>', $text);
$text = preg_replace('#\[url=([\r\n]*)(http://|ftp://|https://|ftps://)([^\s\'\";\+]*?)\](.*?)([\r\n]*)\[/url\]#si', '<a href=\'\2\3\' target=\'_blank\'>\4</a>', $text);
$text = preg_replace('#\[url=([\r\n]*)([^\s\'\";\+]*?)\](.*?)([\r\n]*)\[/url\]#si', '<a href=\'http://\2\' target=\'_blank\'>\3</a>', $text);
$text = preg_replace('#\[mail\]([\r\n]*)([^\s\'\";:\+]*?)([\r\n]*)\[/mail\]#si', '<a href=\'mailto:\2\'>\2</a>', $text);
$text = preg_replace('#\[mail=([\r\n]*)([^\s\'\";:\+]*?)\](.*?)([\r\n]*)\[/mail\]#si', '<a href=\'mailto:\2\'>\2</a>', $text);
$text = preg_replace('#\[small\](.*?)\[/small\]#si', '<span class=\'small\'>\1</span>', $text);
$text = preg_replace('#\[color=(black|blue|brown|cyan|gray|green|lime|maroon|navy|olive|orange|purple|red|silver|violet|white|yellow)\](.*?)\[/color\]#si', '<span style=\'color:\1\'>\2</span>', $text);
$text = preg_replace('#\[flash width=([0-9]*?) height=([0-9]*?)\]([^\s\'\";:\+]*?)(\.swf)\[/flash\]#si', '<object classid=\'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\' codebase=\'http://active.macromedia.com/flash6/cabs/swflash.cab#version=6,0,0,0\' id=\'\3\4\' width=\'\1\' height=\'\2\'><param name=movie value=\'\3\4\'><param name=\'quality\' value=\'high\'><param name=\'bgcolor\' value=\'#ffffff\'><embed src=\'\3\4\' quality=\'high\' bgcolor=\'#ffffff\' width=\'\1\' height=\'\2\' type=\'application/x-shockwave-flash\' pluginspage=\'http://www.macromedia.com/go/getflashplayer\'></embed></object>', $text);
$text = preg_replace("#\[img\]((http|ftp|https|ftps)://)(.*?)(\.(jpg|jpeg|gif|png|JPG|JPEG|GIF|PNG))\[/img\]#sie","'<img src=\'\\1'.str_replace(array('.php','?','&','='),'','\\3').'\\4\' style=\'border:0px\'>'",$text);
$qcount = substr_count($text, "[quote]"); $ccount = substr_count($text, "[code]");
for ($i=0;$i < $qcount;$i++) $text = preg_replace('#\[quote\](.*?)\[/quote\]#si', '<div class=\'quote\'>\1</div>', $text);
for ($i=0;$i < $ccount;$i++) $text = preg_replace('#\[code\](.*?)\[/code\]#si', '<div class=\'quote\' style=\'width:400px;white-space:nowrap;overflow:auto\'><code style=\'white-space:nowrap\'>\1<br><br><br></code></div>', $text);
return $text;
}
/**
* Convert date to fit in Drupal.
*
* Convert date in text format 'Y-m-d H:i' to timestamp.
*
* @param $dateInText
* The date in text.
*
* @return
* The converted timestamp.
*/
function convertDate($dateInText)
{
$e = new DateTime($dateInText);
return date_timestamp_get($e);
}
/**
* Create a topic into a forum.
*
* Create a new topic in a given forum.
*
* @param $title
* The date in text.
* @param $body
* The body of the forum post.
* @param $postDate
* A text that represent a date, date format should be 'Y-m-d H:i'
* @param $forumTitle
* The destination forum name.
*
* @return
* The id of the newly create topic. This is usefull to link replies.
*/
function createTopic($title, $body, $postDate, $forumTitle)
{
$node = new stdClass();
// Set the values for the node
$node->title = utf8_encode($title);
$node->body[LANGUAGE_NONE][0]['value'] = utf8_encode(parseubb($body));
//$node->body[LANGUAGE_NONE][0]['summary'] = "test";
$node->body[LANGUAGE_NONE][0]['format'] = 'full_html';
$node->type = 'forum'; // Your specified content type
$node->created = convertDate($postDate);
$node->changed = $node->created;
$node->status = 1; // To have published, else use 0
$node->promote = 0; // If you want promoted to front page, else use 0
$node->comment = 2;
$node->sticky = 0;
$node->format = 1; // Filtered HTML
$node->uid = 1; // UID of content owner
$node->language = LANGUAGE_NONE;
$node->taxonomy_forums['und'][0]['tid'] = getForumID($forumTitle);
node_save($node);
return $node->nid;
}
/**
* Create a reply to a topic.
*
* @param $nid
* ID of the topic to link to.
* @param $subject
* Subject of the reply.
* @param $text
* The body of the reply.
* @param $postDate
* A text that represent a date, date format should be 'Y-m-d H:i'
*
* @return
* The id of the newly create topic. This is usefull to link replies.
*/
function createTopicResponse($nid, $subject, $text, $postDate)
{
$subject = (strlen($subject) > 60) ? substr($subject,0,60).'...' : $subject;
$comment = (object) array(
'nid' => $nid,
'cid' => 0,
'pid' => 0,
'uid' => 1,
'mail' => '',
'created' => convertDate($postDate),
'changed' => convertDate($postDate),
'is_anonymous' => 0,
'homepage' => '',
'status' => COMMENT_PUBLISHED,
'subject' => utf8_encode($subject),
'language' => LANGUAGE_NONE,
'comment_body' => array(
LANGUAGE_NONE => array(
0 => array (
'value' => utf8_encode(parseubb($text)),
'format' => 'full_html'
)
)
),
);
comment_submit($comment);
comment_save($comment);
return $comment->cid;
}
?>
Remerciement à:
Vincent59, membre de Drupalfr
StackOverFlow
Unibia