"; } //dsm($doc); } }*/ //$node->field_origine_spip['und'][0]['value'] = $actualites[$sk]['id_spip']; //$node->field_data_spip['und'][0]['value'] = serialize($actualites[$sk]); return 'FIN'; } function spip_migrate_import_librairie(){ //Librairie $idRub = 147; $docs = spip_migrate_get_articles($idRub); if(!empty($docs)){ foreach($docs as $doc){ dsm($doc); if($node = spip_migrate_getEntityBySpipId($doc['id_spip'])){ //do nothing dsm($node); } else{ $node = new stdclass(); $node->type = 'notice_doc'; $node->status = 1; $node->uid = 1; $node->title = truncate_utf8($doc['titre'], 255); $node->field_origine_spip['und'][0]['value'] = $doc['id_spip']; $node->created = strtotime($doc['date']); $node->modified = strtotime($doc['maj']); node_save($node); } $wrapper = entity_metadata_wrapper('node', $node); $wrapper->field_dc_type->set('Document'); $wrapper->title->set(truncate_utf8($doc['titre'],255)); $wrapper->field_doc_titre_complet->set($doc['titre']); if($doc['soustitre']!='') $wrapper->field_dc_creator->set(array($doc['soustitre'])); if($doc['descriptif']!='' && $doc['soustitre']=='') $wrapper->field_dc_creator->set(array($doc['descriptif'])); if(trim($doc['texte'])!='') $wrapper->field_dc_description->set( array( 'value'=>trim($doc['texte']), 'format'=>'full_html' ) ) ; if($doc['date']){ $wrapper->field_dc_created->set(strtotime($doc['date'])); } if($doc['url_site']!='') $wrapper->field_document_distant->set(array([ 'url'=>$doc['url_site'], 'title'=>$doc['nom_site']!=''?$doc['nom_site']:'En savoir plus', 'attributes' => ['target' => '_blank'] ])); $wrapper->field_data_spip->set(serialize($doc)); $wrapper->save(); } } } function spip_migrate_import_blocs($blocs){ if(!empty($blocs)){ foreach($blocs as $block){ spip_migrate_create_block($block); } cache_clear_all(); } } /** * Programmatically creates a DB level block. */ function spip_migrate_create_block($info=array()) { if(!empty($info)){ form_load_include($form_state, 'inc', 'block', 'block.admin'); $idSpip = 'bloc-'.$info['id_bloc']; //Si le block existe déjà on fait de l'annule et remplace if($block = spip_migrate_block_loadBySpipId($idSpip)){ if(isset($block->bid)){ $delta = $block->bid; db_delete('block_custom') ->condition('bid', $delta) ->execute(); db_delete('block') ->condition('module', 'block') ->condition('delta', $delta) ->execute(); db_delete('block_role') ->condition('module', 'block') ->condition('delta', $delta) ->execute(); drupal_set_message(t('The block %name has been removed.', array('%name' => $block->info))); } } //Liste des pages surlesquelles afficher le block $pages = array(); if(!empty($info['liens'])){ foreach($info['liens'] as $lien){ if($lien['id_spip']!='' && $e=spip_migrate_getEntityBySpipId($lien['id_spip'])) { $pages[]=$e['entity_type'].'/'.$e['entity_id']; } } } $delta = db_insert('block_custom') ->fields(array( 'body' => spip_migrate_formatLongText($info['texte']), 'info' => spip_migrate_supprimer_numero($info['titre']).'(SPIP :'.$idSpip.')', 'format' => 'filtered_html', 'origine_spip'=>$idSpip )) ->execute(); $query = db_insert('block')->fields(array('visibility', 'pages', 'custom', 'title', 'module', 'theme', 'status', 'weight', 'delta', 'cache', 'region')); $query->values(array( 'module' => 'block', 'delta' => $delta, 'theme' => 'sispeagoutte', 'status' => 1, 'weight' => spip_migrate_recuperer_numero($info['titre']), 'region'=>'right', 'custom' => 0, 'visibility' => 1, 'pages' => (!empty($pages)?implode("\n",$pages):''), 'title' => spip_migrate_supprimer_numero($info['titre']), 'cache' => DRUPAL_NO_CACHE )); $query->execute(); drupal_set_message(t('The block %name has been created.', array('%name' => spip_migrate_supprimer_numero($info['titre'])))); } } /* Groupes : - 8 : couleur - 9 : Position - 10 : Milieux ------> vid : 2 dpsir - 11 : Force à l'oeuvre ------> vid : 2 dpsir - 12 : Pression ------> vid : 2 dpsir - 13 : Réponse ------> vid : 2 dpsir - 14 : Thèmes ------> vid : 3 themes_sandre - 18 : document_service ------> vid : 4 document_service */ function spip_migrate_import_mots(){ //DPSIR $mots_dpsir = spip_migrate_get_mots(array(10, 11, 12, 13)); $groupe = array(); // creation des parents : $term = new stdClass(); //$term->vocabulary_machine_name='dpsir'; $term->vid = 2; $term->language='und'; $term->format = 'filtered_html'; $term->name= 'Milieux'; taxonomy_term_save($term); $groupe[10]=$term->tid; //exit( krumo($term)); $term = new stdClass(); $term->vid = 2; $term->language='und'; $term->format = 'filtered_html'; $term->name= "Forces à l'œuvre"; taxonomy_term_save($term); $groupe[11]=$term->tid; $term = new stdClass(); $term->vid = 2; $term->language='und'; $term->format = 'filtered_html'; $term->name= 'Pressions'; taxonomy_term_save($term); $groupe[12]=$term->tid; $term = new stdClass(); $term->vid = 2; $term->language='und'; $term->format = 'filtered_html'; $term->name= 'Réponses'; taxonomy_term_save($term); $groupe[13]=$term->tid; foreach($mots_dpsir as $mot){ $term = new stdClass(); $term->vid = 2; $term->language='und'; $term->format = 'filtered_html'; $term->name= $mot['titre']; $term->description = ''; $term->parent = $groupe[$mot['id_groupe']]; $term->field_origine_spip['und'][0]['value'] = 'mot-'.$mot['id_mot']; $term->field_data_spip['und'][0]['value'] = serialize($mot); taxonomy_term_save($term); } //dsm($mots_dpsir); //Thèmes Sandre $mots_themes = spip_migrate_get_mots(array(14)); //dsm($mots_themes); foreach($mots_themes as $mot){ $term = new stdClass(); $term->vid = 3; $term->language='und'; $term->format = 'filtered_html'; $term->name= $mot['titre']; $term->description = ''; $term->field_code_sandre = ''; $term->field_origine_spip['und'][0]['value'] = 'mot-'.$mot['id_mot']; $term->field_data_spip['und'][0]['value'] = serialize($mot); taxonomy_term_save($term); } //Documents service $mots_docS = spip_migrate_get_mots(array(18)); //dsm($mots_docS); foreach($mots_docS as $mot){ $term = new stdClass(); $term->vid = 4; $term->language='und'; $term->format = 'filtered_html'; $term->name= $mot['texte']; $term->description = $mot['titre']; $term->field_code_sandre = $mot['descriptif']; $term->field_origine_spip['und'][0]['value'] = 'mot-'.$mot['id_mot']; $term->field_data_spip['und'][0]['value'] = serialize($mot); taxonomy_term_save($term); } } function spip_migrate_load_nodes($content_type){ if($nodes = node_load_multiple(array(), array('type' => $content_type))){ foreach($nodes as $nid=>$node){ if(isset($node->field_data_spip)){ $nodes[$nid]->data_spip = @unserialize($node->field_data_spip['und'][0]['value']); } } } return $nodes; } function spip_migrate_import_notices($notices=array()){ if(!empty($notices)){ foreach($notices as $notice){ //On regarde si l'entitté existe déjà $entity = spip_migrate_getEntityBySpipId($notice['id_spip']); if($entity && !empty($entity) && isset($entity['entity_type']) && $entity['entity_type']=='node' && $node = node_load($entity['entity_id'] )){ //you're lucky !!! } else{ $node = new stdclass(); $node->type = 'notice_doc'; $node->uid = 1; $node->language = 'fr'; $node->translate = 0; $node->tnid = 0; $node->sticky = 0; $node->promote = 0; $node->comment = 0; } //Mapping des champs //champs techniques $node->field_origine_spip['und'][0]['value'] = $notice['id_spip']; $node->field_data_spip['und'][0]['value'] = serialize($notice); if($notice['statut']=='publie') $node->status = 1; else $node->status = 0; //===data $node->title = $notice['titre']; $node->field_dc_subject['und'][0]['value'] =$notice['sujet']; $node->field_dc_description['und'][0]['value'] =spip_migrate_formatLongText($notice['description']); $node->field_dc_description['und'][0]['format'] ='full_html'; $node->field_dc_creator['und'][0]['value'] =$notice['createur']; $node->field_dc_publisher['und'][0]['value'] =$notice['editeur']; $node->field_dc_contributor['und'][0]['value'] =$notice['contributeur']; if($notice['date']!='0000-00-00 00:00:00') $node->field_dc_created['und'][0]= array('value' => $notice['date'], 'timezone'=>'Europe/Paris', 'timezone_db'=>'Europe/Paris', 'date_type'=>'datetime'); if($notice['date_modif']!='0000-00-00 00:00:00') $node->field_dc_modified['und'][0]= array('value' => $notice['date_modif'], 'timezone'=>'Europe/Paris', 'timezone_db'=>'Europe/Paris', 'date_type'=>'datetime'); if($notice['date_valid']!='0000-00-00 00:00:00') $node->field_dc_accepted['und'][0]= array('value' => $notice['date_valid'], 'timezone'=>'Europe/Paris', 'timezone_db'=>'Europe/Paris', 'date_type'=>'datetime'); $node->field_dc_type['und'][0]['value'] =$notice['type']; $node->field_dc_format['und'][0]['value'] =$notice['format']; $node->field_dc_identifier['und'][0]['value'] =$notice['identifiant_ressources']; $node->field_dc_source['und'][0]['value'] =$notice['source']; $node->field_dc_relation['und'][0]['value'] =$notice['relation']; $node->field_dc_coverage['und'][0]['value'] =$notice['couverture']; $node->field_statut_doc_sandre['und'][0]['value'] =$notice['statut_doc']; if($notice['droits']!=''){ $node->field_dc_rigths['und'][0]['url'] =$notice['droits']; if(trim($notice['droits'])=='http://creativecommons.org/licenses/by-nc-sa/2.0/fr')$node->field_dc_rigths['und'][0]['title'] = 'Creative commons 2.0 BY NC SA - France'; } $node->field_version_doc_sandre['und'][0]['value']=$notice['version']; //$node->field_old_version_doc_sandre['und'][0]['value']=$notice['old_version']; $node->field_evolutions_doc_sandre['und'][0]['value']=spip_migrate_formatLongText($notice['evolution']); $node->field_evolutions_doc_sandre['und'][0]['format'] ='full_html'; $node->field_code_sandre['und'][0]['value']=$notice['code_sandre']; if($notice['date_sync']!='0000-00-00 00:00:00') $node->field_date_sync['und'][0] = array('value' => $notice['date_sync'], 'timezone'=>'Europe/Paris', 'timezone_db'=>'Europe/Paris', 'date_type'=>'datetime'); node_save($node); } } } function spip_migrate_notice_set_dependencies($notices){ if(!empty($notices)){ $dpsir = array_keys(taxonomy_term_load_multiple(array(), array('vid' => 2))); $themes = array_keys(taxonomy_term_load_multiple(array(), array('vid' => 3))); $ds = array_keys(taxonomy_term_load_multiple(array(), array('vid' => 4))); //dsm($dpsir); dsm($ds); foreach($notices as $notice){ //Liens de mots clés /* if(!empty($notice->data_spip['mots'])){ foreach($notice->data_spip['mots'] as $mot){ if($entity = spip_migrate_getEntityBySpipId($mot['id_spip'])){ if(in_array($entity['entity_id'], $dpsir)) { $notice->field_keywords_dpsir['und'][]=array('tid'=>$entity['entity_id']);} elseif(in_array($entity['entity_id'], $themes)) {$notice->field_themes_sandre['und'][]=array('tid'=>$entity['entity_id']);} elseif(in_array($entity['entity_id'], $ds)) {$notice->field_document_service['und'][]=array('tid'=>$entity['entity_id']);} else{ dsm('Bad key word : notice -'.$notice->nid); dsm($entity); } } else{ //dsm($mot); } } } */ //Liens avec les documents if(!empty($notice->data_spip['documents'])){ //dsm($notice); $notice->field_document_distant=array(); foreach($notice->data_spip['documents'] as $link_doc){ if($link_doc['id_document']!='' && $doc = spip_migrate_get_document($link_doc['id_document'])){ //dsm($doc); //2 cas possibles : le fichier est distant ou local if($doc['distant']=='oui'){ $notice->field_document_distant['und'][]=array( 'url'=>$doc['fichier'], 'title'=>(trim($doc['titre'])!=''?$doc['titre']:$doc['fichier']) ); } /* else{ //Créer le fichier $file = spip_migrate_add_file($doc); $notice->field_file['und'][]=(array)$file; }*/ } } } //Lien de version /* if($notice->data_spip['old_version']!=''){ if($old = spip_migrate_getEntityBySpipId('notice-'.$notice->data_spip['old_version'])){ $notice->field_old_version_doc_sandre['und'][0]['target_id'] = $old['entity_id']; } } */ node_save($notice); } } } function spip_migrate_add_file($doc){ $file = new stdClass; $uri = 'public://IMG/'.$doc['fichier']; //dsm($uri); if(file_exists($uri)){ $files = entity_load('file', array(), array('uri'=>$uri)); if(!empty($files)){ $file = reset($files); $file->display = 1; } else{ if($doc['mode']=='document'){ $file->type = 'document'; }elseif(($doc['mode']=='image')){ $file->type = 'image'; } else{ $file->type = 'undefined'; } $file->uid = 1; $file->display = 1; $file->filename = drupal_basename($uri); $file->uri = $uri; $file->title = (isset($doc['titre'])?$doc['titre']:''); $file->description = (isset($doc['descriptif'])?$doc['descriptif']:''); $file->filemime = file_get_mimetype($uri); $file->filesize = filesize($uri); $file->status = FILE_STATUS_PERMANENT; file_save($file); } } return $file; } //Fonction pour trouver un logo function spip_migrate_find_logo($id, $type='art'){ if($type=='art'){ $uri = 'public://IMG/arton'.$id; $fichier=false; if(file_exists($uri.'.jpg')){ $fichier='arton'.$id.'.jpg'; } elseif(file_exists($uri.'.png')){ $fichier='arton'.$id.'.png'; } if($fichier){ $doc=array( 'fichier'=>$fichier, 'mode'=>'image', 'titre'=>'Logo' ); return spip_migrate_add_file($doc); } //false } else{ //todo } return false; } /* type par id_rubrique */ function spip_migrate_get_articles($id_rub=null){ db_set_active(SPIP); $query= db_select('spip_articles', 'c') ->fields('c') ->condition('c.statut', 'publie','=') ->orderBy('id_article', 'ASC'); if($id_rub!='all' && $id_rub!=''){ if($id_rub=='others'){ $query->condition('c.id_rubrique', array(40, 202, 239, 238),'NOT IN'); } else{ $query->condition('c.id_rubrique', $id_rub,'='); } } $result = $query->execute(); $articles=array(); while($record = $result->fetchAssoc()) { $id = $record['id_article']; $record['id_spip'] = 'article-'.$id; $record['documents'] = spip_migrate_get_document_liens($id, 'article'); $record['mots'] = spip_migrate_get_mots_liens($id, 'article'); $articles[] = spip_migrate_clean_text($record); //Todo ajouter reference // mots clés // fichier } db_set_active(); return $articles; } // On ne garde que les rubriques qui ont au moins un article publié // jointure function spip_migrate_get_rubriques(){ db_set_active(SPIP); $query = db_select('spip_rubriques', 'c'); $query->join('spip_articles', 'a', 'a.id_rubrique = c.id_rubrique'); $query->fields('c'); $query->condition('a.statut', 'publie','='); $query->groupBy('c.id_rubrique'); $result=$query->execute(); $rubriques=array(); while($record = $result->fetchAssoc()) { $id = $record['id_rubrique']; $record['id_spip'] = 'rubrique-'.$id; $record['documents'] = spip_migrate_get_document_liens($id, 'rubrique'); $record['mots'] = spip_migrate_get_mots_liens($id, 'rubrique'); $rubriques[] = spip_migrate_clean_text($record); //Todo ajouter reference // spip_blocs_liens } db_set_active(); return $rubriques; } function spip_migrate_get_blocs($id_rub=null){ db_set_active(SPIP); $query = db_select('spip_blocs', 'c') ->fields('c'); $result=$query->execute(); $blocs=array(); while($record = $result->fetchAssoc()) { $record['liens'] = spip_migrate_get_blocs_liens($record['id_bloc']); $blocs[] = spip_migrate_clean_text($record); //Todo ajouter reference // spip_blocs_liens } db_set_active(); return $blocs; } function spip_migrate_get_document_liens($entity_id, $entity_type){ //dsm("spip_migrate_get_document_liens : ". $entity_id ." ". $entity_type); db_set_active(SPIP); $table = 'spip_documents_'.$entity_type.'s'; $id_field = 'id_'.$entity_type; $query= db_select($table, 'c'); $query->fields('c'); $query->condition($id_field, $entity_id,'='); //$query->condition('objet', $entity_type,'='); $result=$query->execute(); /* Requete pour des spip plus récents $query= db_select('spip_documents_liens', 'c') ->fields('c') ->condition('id_objet', $entity_id,'=') ->condition('objet', $entity_type,'='); $result=$query->execute(); */ $docs = array(); while($record = $result->fetchAssoc()) { $id = $record['id_document']; $record['id_spip'] = 'document-'.$id; $docs[] = spip_migrate_clean_text($record); //Todo ajouter reference // spip_blocs_liens } db_set_active(); return $docs; } function spip_migrate_get_mots_liens($entity_id, $entity_type){ db_set_active(SPIP); $query= db_select('spip_mots_'.$entity_type.'s', 'c') ->fields('c') ->condition('id_'.$entity_type, $entity_id,'='); $result=$query->execute(); $mots = array(); while($record = $result->fetchAssoc()) { $id = $record['id_mot']; $record['id_spip'] = 'mot-'.$id; $mots[] = spip_migrate_clean_text($record); //Todo ajouter reference // spip_blocs_liens } db_set_active(); return $mots; } function spip_migrate_get_blocs_liens($id_bloc){ db_set_active(SPIP); $query= db_select('spip_blocs_liens', 'c') ->fields('c') ->condition('id_bloc', $id_bloc,'='); $result=$query->execute(); $blocs = array(); while($record = $result->fetchAssoc()) { $id = $record['id_objet']; $record['id_spip'] = $record['objet'].'-'.$id; $blocs[] = spip_migrate_clean_text($record); //Todo ajouter reference // spip_blocs_liens } db_set_active(); return $blocs; } function spip_migrate_get_groupes_mots(){ db_set_active(SPIP); $query = db_select('spip_groupes_mots', 'c') ->fields('c'); $result=$query->execute(); $groupes_mots=array(); while($record = $result->fetchAssoc()) { $groupes_mots[] = spip_migrate_clean_text($record); //Todo ajouter reference // spip_blocs_liens } db_set_active(); return $groupes_mots; } function spip_migrate_get_mots($id_groupe=array()){ db_set_active(SPIP); $query = db_select('spip_mots', 'c') ->fields('c'); if(!empty($id_groupe)){ $query->condition('c.id_groupe', $id_groupe,'IN'); } $result=$query->execute(); $mots=array(); while($record = $result->fetchAssoc()) { $mots[] = spip_migrate_clean_text($record); //Todo ajouter reference // spip_blocs_liens } db_set_active(); return $mots; } function spip_migrate_get_document($id_document){ db_set_active(SPIP); $query = db_select('spip_documents', 'c'); $query->fields('c'); $query->condition('c.id_document', $id_document,'='); $result=$query->execute(); $doc = $result->fetchAssoc(); db_set_active(); return $doc; }